File size: 10,933 Bytes
7b715bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | /*
Copyright 2010 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Google.Apis.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Globalization;
using System.Threading;
using Xunit;
namespace Google.Apis.Tests.Apis.Util
{
/// <summary>Tests for <seealso cref="Google.Apis.Util.Utilities"/> class.</summary>
public class UtilitiesTest
{
/// <summary>Tests the "ThrowIfNull" method.</summary>
[Fact]
public void ThrowIfNullTest()
{
string str = null;
Assert.Throws<ArgumentNullException>(() => str.ThrowIfNull("str"));
str = "123";
str.ThrowIfNull("Not throwen");
}
private enum MockEnum
{
[StringValue("Test")]
EntryWithStringValue,
[StringValue("3.14159265358979323846")]
EntryWithSecondStringValue,
EntryWithoutStringValue
}
/// <summary>Tests the "GetStringValue" extension method of Enums.</summary>
[Fact]
public void StringValueTest()
{
Assert.Equal("Test", Utilities.GetEnumStringValue(MockEnum.EntryWithStringValue));
Assert.Equal("3.14159265358979323846", Utilities.GetEnumStringValue(MockEnum.EntryWithSecondStringValue));
Assert.Throws<ArgumentException>(() => Utilities.GetEnumStringValue(MockEnum.EntryWithoutStringValue));
Assert.Throws<ArgumentNullException>(() => Utilities.GetEnumStringValue((MockEnum)123456));
}
/// <summary>Tests the "ConvertToString" method.</summary>
[Fact]
public void ConvertToStringTest()
{
Assert.Equal("FooBar", Google.Apis.Util.Utilities.ConvertToString("FooBar"));
Assert.Equal("123", Google.Apis.Util.Utilities.ConvertToString(123));
Assert.Equal("false", Google.Apis.Util.Utilities.ConvertToString(false));
Assert.Equal("true", Google.Apis.Util.Utilities.ConvertToString(true));
// Check Enums work with and without StringValueAttribute.
Assert.Equal("Test", Google.Apis.Util.Utilities.ConvertToString(MockEnum.EntryWithStringValue));
Assert.Equal("3.14159265358979323846",
Google.Apis.Util.Utilities.ConvertToString(MockEnum.EntryWithSecondStringValue));
Assert.Equal("EntryWithoutStringValue",
Google.Apis.Util.Utilities.ConvertToString(MockEnum.EntryWithoutStringValue));
Assert.Null(Google.Apis.Util.Utilities.ConvertToString(null));
// Test nullable types.
int? nullable = 123;
Assert.Equal("123", Google.Apis.Util.Utilities.ConvertToString(nullable));
nullable = null;
Assert.Null(Google.Apis.Util.Utilities.ConvertToString(nullable));
MockEnum? nullEnum = null;
Assert.Null(Google.Apis.Util.Utilities.ConvertToString(nullEnum));
}
[Fact]
public void GetDateTimeOffsetFromString_Null() =>
Assert.Null(Utilities.GetDateTimeOffsetFromString(null));
[Theory]
[InlineData("broken")]
[InlineData("")]
[InlineData("2023-13-01T00:00:00Z")]
[InlineData("2023-13-01T00:00:00+00")] // We require the Z
[InlineData("2023-13-01T00:00:00+01")] // We require the Z
[InlineData("2023-12-01T00:00:00.000000Z")] // We only support millisecond precision
public void GetDateTimeOffsetFromString_Invalid(string input) =>
Assert.Throws<FormatException>(() => Utilities.GetDateTimeOffsetFromString(input));
[Theory]
[InlineData("2023-12-01T00:00:00Z", 2023, 12, 1, 0, 0, 0, 0)]
[InlineData("0001-01-01T00:00:00Z", 1, 1, 1, 0, 0, 0, 0)]
[InlineData("9999-12-31T23:59:59.999Z", 9999, 12, 31, 23, 59, 59, 999)]
[InlineData("2023-06-14T12:23:45.5Z", 2023, 6, 14, 12,23, 45, 500)] // This is slightly unfortunate, but not actively harmful.
[InlineData("2023-06-14T12:23:45.123Z", 2023, 6, 14, 12, 23, 45, 123)]
[InlineData("2023-06-14T12:23:45.000Z", 2023, 6, 14, 12, 23, 45, 0)] // .000 is redundant but valid
[InlineData("2023-06-14T12:23:45Z", 2023, 6, 14, 12, 23, 45, 0)]
public void GetDateTimeOffsetFromString_Valid(string input, int year, int month, int day, int hour, int minute, int second, int millisecond)
{
var actual = Utilities.GetDateTimeOffsetFromString(input).Value;
var expected = new DateTimeOffset(year, month, day, hour, minute, second, millisecond, TimeSpan.Zero);
Assert.Equal(expected, actual);
// Be explicit about this, as DateTimeOffset.Equals only compares instants in time, not offsets.
Assert.Equal(TimeSpan.Zero, actual.Offset);
}
[Theory]
[InlineData(5000000, "2023-06-13T15:54:13.500Z")]
[InlineData(1234567, "2023-06-13T15:54:13.123Z")]
[InlineData(1239999, "2023-06-13T15:54:13.123Z")]
[InlineData(9999999, "2023-06-13T15:54:13.999Z")]
[InlineData(10000, "2023-06-13T15:54:13.001Z")]
[InlineData(100000, "2023-06-13T15:54:13.010Z")]
[InlineData(0, "2023-06-13T15:54:13Z")]
public void GetStringFromDateTimeOffset_MillisecondHandling(int tickOfSecond, string expectedResult)
{
var value = new DateTimeOffset(2023, 6, 13, 15, 54, 13, TimeSpan.Zero).AddTicks(tickOfSecond);
Assert.Equal(expectedResult, Utilities.GetStringFromDateTimeOffset(value));
}
[Fact]
public void DateTimeOffsetConversionsAreInvariant()
{
using var _ = new CultureSwitcher("da-DK");
var dto = new DateTimeOffset(2023, 6, 13, 15, 54, 13, 500, TimeSpan.Zero);
string text = "2023-06-13T15:54:13.500Z";
Assert.Equal(text, Utilities.GetStringFromDateTimeOffset(dto));
Assert.Equal(dto, Utilities.GetDateTimeOffsetFromString(text));
}
// Local time of 2023-06-13T15:54:13, with variable UTC offset.
[Theory]
[InlineData(60, "2023-06-13T14:54:13Z")]
[InlineData(30, "2023-06-13T15:24:13Z")]
[InlineData(0, "2023-06-13T15:54:13Z")]
[InlineData(-30, "2023-06-13T16:24:13Z")]
[InlineData(-60, "2023-06-13T16:54:13Z")]
public void GetStringFromDateTimeOffset_ConvertsToUtc(int offsetMinutes, string expectedResult)
{
var value = new DateTimeOffset(2023, 6, 13, 15, 54, 13, TimeSpan.FromMinutes(offsetMinutes));
Assert.Equal(expectedResult, Utilities.GetStringFromDateTimeOffset(value));
}
[Fact]
public void GetStringFromDateTimeOffset_Null() =>
Assert.Null(Utilities.GetStringFromDateTimeOffset(null));
[Fact]
public void GetStringFromDateTimeOffset_NonGregorianCulture()
{
var culture = CultureInfo.GetCultureInfo("fa-AF");
var original = CultureInfo.CurrentCulture;
try
{
Thread.CurrentThread.CurrentCulture = culture;
// Demonstrate that there's a problem for DateTime, albeit with an invalid raw value...
// (DateTime.Parse seems to use the invariant culture if a full ISO-8601 timestamp is provided...)
var dtResult = Utilities.GetDateTimeFromString("2023-06-14");
// The difference in calendar means the year is actually 2644.
Assert.NotEqual(2023, dtResult.Value.Year);
// We don't have the possibility of this problem for DateTimeOffset, because
// we use the invariant culture. We can't check this against the "just date" value,
// because we require the exact value for DateTimeOffset.
var dtoResult = Utilities.GetDateTimeFromString("2023-06-14T00:00:00Z");
var expected = new DateTimeOffset(2023, 6, 14, 0, 0, 0, TimeSpan.Zero);
Assert.Equal(expected, dtoResult.Value);
}
finally
{
Thread.CurrentThread.CurrentCulture = original;
}
}
[Fact]
public void SerializeForGoogleFormat_Null() =>
Assert.Null(Utilities.SerializeForGoogleFormat(null));
[Theory]
[InlineData("simple")]
[InlineData("with \"quotes\"")]
[InlineData("with emoji \U0001F604")]
[InlineData("with backslash \\")]
[InlineData("2023-06-15T10:52:00+00:00")] // DeserializeForGoogleFormat would return a DateTime
public void SerializeForGoogleFormat_String(string input) =>
Assert.Equal(input, Utilities.SerializeForGoogleFormat(input));
[Fact]
public void SerializeForGoogleFormat_DateTime() =>
Assert.Equal("2023-06-15T10:52:00.000Z", Utilities.SerializeForGoogleFormat(new DateTime(2023, 6, 15, 10, 52, 0, DateTimeKind.Utc)));
[Fact]
public void SerializeForGoogleFormat_DateTimeOffset_Utc() =>
Assert.Equal("2023-06-15T10:52:00+00:00", Utilities.SerializeForGoogleFormat(new DateTimeOffset(2023, 6, 15, 10, 52, 0, TimeSpan.Zero)));
[Fact]
public void SerializeForGoogleFormat_DateTimeOffset_NonUtc() =>
Assert.Equal("2023-06-15T10:52:00+01:00", Utilities.SerializeForGoogleFormat(new DateTimeOffset(2023, 6, 15, 10, 52, 0, TimeSpan.FromHours(1))));
[Fact]
public void DeserializeForGoogleFormat_Null() =>
Assert.Null(Utilities.DeserializeForGoogleFormat(null));
[Theory]
[InlineData("simple")]
[InlineData("with \"quotes\"")]
[InlineData("with emoji \U0001F604")]
[InlineData("with backslash \\")]
public void DeserializeForGoogleFormat_String(string input) =>
Assert.Equal(input, Utilities.DeserializeForGoogleFormat(input));
[Theory]
[InlineData("2023-06-15T10:52:00.000Z")]
[InlineData("2023-06-15T10:52:00Z")]
public void DeserializeForGoogleFormat_DateTime(string text)
{
var result = Utilities.DeserializeForGoogleFormat(text);
var dt = Assert.IsAssignableFrom<DateTime>(result);
Assert.Equal(new DateTime(2023, 6, 15, 10, 52, 0, DateTimeKind.Utc), dt);
// DateTime.Equals doesn't take the kind into account.
Assert.Equal(DateTimeKind.Utc, dt.Kind);
}
}
} |