-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathTestClasses.SimpleTestStruct.cs
220 lines (208 loc) · 10.4 KB
/
TestClasses.SimpleTestStruct.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace System.Text.Json.Serialization.Tests
{
public struct SimpleTestStruct : ITestClass
{
public short MyInt16 { get; set; }
public int MyInt32 { get; set; }
public long MyInt64 { get; set; }
public ushort MyUInt16 { get; set; }
public uint MyUInt32 { get; set; }
public ulong MyUInt64 { get; set; }
public byte MyByte { get; set; }
public sbyte MySByte { get; set; }
public char MyChar { get; set; }
public string MyString { get; set; }
public decimal MyDecimal { get; set; }
public bool MyBooleanTrue { get; set; }
public bool MyBooleanFalse { get; set; }
public float MySingle { get; set; }
public double MyDouble { get; set; }
public DateTime MyDateTime { get; set; }
public DateTimeOffset MyDateTimeOffset { get; set; }
public SampleEnum MyEnum { get; set; }
public SampleEnumInt64 MyInt64Enum { get; set; }
public SampleEnumUInt64 MyUInt64Enum { get; set; }
public SimpleStruct MySimpleStruct { get; set; }
public SimpleTestClass MySimpleTestClass { get; set; }
public short[] MyInt16Array { get; set; }
public int[] MyInt32Array { get; set; }
public long[] MyInt64Array { get; set; }
public ushort[] MyUInt16Array { get; set; }
public uint[] MyUInt32Array { get; set; }
public ulong[] MyUInt64Array { get; set; }
public byte[] MyByteArray { get; set; }
public sbyte[] MySByteArray { get; set; }
public char[] MyCharArray { get; set; }
public string[] MyStringArray { get; set; }
public decimal[] MyDecimalArray { get; set; }
public bool[] MyBooleanTrueArray { get; set; }
public bool[] MyBooleanFalseArray { get; set; }
public float[] MySingleArray { get; set; }
public double[] MyDoubleArray { get; set; }
public DateTime[] MyDateTimeArray { get; set; }
public DateTimeOffset[] MyDateTimeOffsetArray { get; set; }
public SampleEnum[] MyEnumArray { get; set; }
public List<string> MyStringList { get; set; }
public IEnumerable<string> MyStringIEnumerableT { get; set; }
public IList<string> MyStringIListT { get; set; }
public ICollection<string> MyStringICollectionT { get; set; }
public IReadOnlyCollection<string> MyStringIReadOnlyCollectionT { get; set; }
public IReadOnlyList<string> MyStringIReadOnlyListT { get; set; }
public ISet<string> MyStringISetT { get; set; }
public static readonly string s_json = $"{{{s_partialJsonProperties},{s_partialJsonArrays}}}";
public static readonly string s_json_flipped = $"{{{s_partialJsonArrays},{s_partialJsonProperties}}}";
private const string s_partialJsonProperties =
@"""MyInt16"" : 1," +
@"""MyInt32"" : 2," +
@"""MyInt64"" : 3," +
@"""MyUInt16"" : 4," +
@"""MyUInt32"" : 5," +
@"""MyUInt64"" : 6," +
@"""MyByte"" : 7," +
@"""MySByte"" : 8," +
@"""MyChar"" : ""a""," +
@"""MyString"" : ""Hello""," +
@"""MyBooleanTrue"" : true," +
@"""MyBooleanFalse"" : false," +
@"""MySingle"" : 1.1," +
@"""MyDouble"" : 2.2," +
@"""MyDecimal"" : 3.3," +
@"""MyDateTime"" : ""2019-01-30T12:01:02.0000000Z""," +
@"""MyDateTimeOffset"" : ""2019-01-30T12:01:02.0000000+01:00""," +
@"""MyEnum"" : 2," + // int by default
@"""MyInt64Enum"" : -9223372036854775808," +
@"""MyUInt64Enum"" : 18446744073709551615," +
@"""MySimpleStruct"" : {""One"" : 11, ""Two"" : 1.9999, ""Three"" : 33}";
private const string s_partialJsonArrays =
@"""MyInt16Array"" : [1]," +
@"""MyInt32Array"" : [2]," +
@"""MyInt64Array"" : [3]," +
@"""MyUInt16Array"" : [4]," +
@"""MyUInt32Array"" : [5]," +
@"""MyUInt64Array"" : [6]," +
@"""MyByteArray"" : ""Bw==""," + // Base64 encoded value of 7
@"""MySByteArray"" : [8]," +
@"""MyCharArray"" : [""a""]," +
@"""MyStringArray"" : [""Hello""]," +
@"""MyBooleanTrueArray"" : [true]," +
@"""MyBooleanFalseArray"" : [false]," +
@"""MySingleArray"" : [1.1]," +
@"""MyDoubleArray"" : [2.2]," +
@"""MyDecimalArray"" : [3.3]," +
@"""MyDateTimeArray"" : [""2019-01-30T12:01:02.0000000Z""]," +
@"""MyDateTimeOffsetArray"" : [""2019-01-30T12:01:02.0000000+01:00""]," +
@"""MyEnumArray"" : [2]," + // int by default
@"""MyStringList"" : [""Hello""]," +
@"""MyStringIEnumerableT"" : [""Hello""]," +
@"""MyStringIListT"" : [""Hello""]," +
@"""MyStringICollectionT"" : [""Hello""]," +
@"""MyStringIReadOnlyCollectionT"" : [""Hello""]," +
@"""MyStringIReadOnlyListT"" : [""Hello""]," +
@"""MyStringISetT"" : [""Hello""]";
public static readonly byte[] s_data = Encoding.UTF8.GetBytes(s_json);
public void Initialize()
{
MyInt16 = 1;
MyInt32 = 2;
MyInt64 = 3;
MyUInt16 = 4;
MyUInt32 = 5;
MyUInt64 = 6;
MyByte = 7;
MySByte = 8;
MyChar = 'a';
MyString = "Hello";
MyBooleanTrue = true;
MyBooleanFalse = false;
MySingle = 1.1f;
MyDouble = 2.2d;
MyDecimal = 3.3m;
MyDateTime = new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc);
MyDateTimeOffset = new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0));
MyEnum = SampleEnum.Two;
MyInt64Enum = SampleEnumInt64.MinNegative;
MyUInt64Enum = SampleEnumUInt64.Max;
MySimpleStruct = new SimpleStruct { One = 11, Two = 1.9999 };
MyInt16Array = new short[] { 1 };
MyInt32Array = new int[] { 2 };
MyInt64Array = new long[] { 3 };
MyUInt16Array = new ushort[] { 4 };
MyUInt32Array = new uint[] { 5 };
MyUInt64Array = new ulong[] { 6 };
MyByteArray = new byte[] { 7 };
MySByteArray = new sbyte[] { 8 };
MyCharArray = new char[] { 'a' };
MyStringArray = new string[] { "Hello" };
MyBooleanTrueArray = new bool[] { true };
MyBooleanFalseArray = new bool[] { false };
MySingleArray = new float[] { 1.1f };
MyDoubleArray = new double[] { 2.2d };
MyDecimalArray = new decimal[] { 3.3m };
MyDateTimeArray = new DateTime[] { new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc) };
MyDateTimeOffsetArray = new DateTimeOffset[] { new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0)) };
MyEnumArray = new SampleEnum[] { SampleEnum.Two };
MyStringList = new List<string>() { "Hello" };
MyStringIEnumerableT = new string[] { "Hello" };
MyStringIListT = new string[] { "Hello" };
MyStringICollectionT = new string[] { "Hello" };
MyStringIReadOnlyCollectionT = new string[] { "Hello" };
MyStringIReadOnlyListT = new string[] { "Hello" };
MyStringISetT = new HashSet<string> { "Hello" };
}
public void Verify()
{
Assert.Equal((short)1, MyInt16);
Assert.Equal((int)2, MyInt32);
Assert.Equal((long)3, MyInt64);
Assert.Equal((ushort)4, MyUInt16);
Assert.Equal((uint)5, MyUInt32);
Assert.Equal((ulong)6, MyUInt64);
Assert.Equal((byte)7, MyByte);
Assert.Equal((sbyte)8, MySByte);
Assert.Equal('a', MyChar);
Assert.Equal("Hello", MyString);
Assert.Equal(3.3m, MyDecimal);
Assert.False(MyBooleanFalse);
Assert.True(MyBooleanTrue);
Assert.Equal(1.1f, MySingle);
Assert.Equal(2.2d, MyDouble);
Assert.Equal(new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc), MyDateTime);
Assert.Equal(new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0)), MyDateTimeOffset);
Assert.Equal(SampleEnum.Two, MyEnum);
Assert.Equal(SampleEnumInt64.MinNegative, MyInt64Enum);
Assert.Equal(SampleEnumUInt64.Max, MyUInt64Enum);
Assert.Equal(11, MySimpleStruct.One);
Assert.Equal(1.9999, MySimpleStruct.Two);
Assert.Equal((short)1, MyInt16Array[0]);
Assert.Equal((int)2, MyInt32Array[0]);
Assert.Equal((long)3, MyInt64Array[0]);
Assert.Equal((ushort)4, MyUInt16Array[0]);
Assert.Equal((uint)5, MyUInt32Array[0]);
Assert.Equal((ulong)6, MyUInt64Array[0]);
Assert.Equal((byte)7, MyByteArray[0]);
Assert.Equal((sbyte)8, MySByteArray[0]);
Assert.Equal('a', MyCharArray[0]);
Assert.Equal("Hello", MyStringArray[0]);
Assert.Equal(3.3m, MyDecimalArray[0]);
Assert.False(MyBooleanFalseArray[0]);
Assert.True(MyBooleanTrueArray[0]);
Assert.Equal(1.1f, MySingleArray[0]);
Assert.Equal(2.2d, MyDoubleArray[0]);
Assert.Equal(new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc), MyDateTimeArray[0]);
Assert.Equal(new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0)), MyDateTimeOffsetArray[0]);
Assert.Equal(SampleEnum.Two, MyEnumArray[0]);
Assert.Equal("Hello", MyStringList[0]);
Assert.Equal("Hello", MyStringIEnumerableT.First());
Assert.Equal("Hello", MyStringIListT[0]);
Assert.Equal("Hello", MyStringICollectionT.First());
Assert.Equal("Hello", MyStringIReadOnlyCollectionT.First());
Assert.Equal("Hello", MyStringIReadOnlyListT[0]);
Assert.Equal("Hello", MyStringISetT.First());
}
}
}