Skip to content

Commit 745abed

Browse files
committed
Added check.CheckTable
Useful for larger collections of mostly flat objects (like tokens from a parser)
1 parent 3b946f1 commit 745abed

7 files changed

Lines changed: 516 additions & 17 deletions

File tree

exampleTest/SomeTest.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,83 @@ public void NewtonsoftJsonCompatibility()
100100
check.CheckJsonObject(
101101
JObject.FromObject(new { number = 3, str = "jaja", list = new List<object> { 1, "2313", new SomeTestObject { Prop = "hmm" } } })
102102
);
103+
}
104+
105+
[Fact]
106+
public void ShortTable()
107+
{
108+
check.CheckTable(
109+
someSmallTable
110+
);
111+
}
103112

113+
[Fact]
114+
public void ShortTableWithManualProperties()
115+
{
116+
check.CheckTable(
117+
someSmallTable,
118+
properties: new [] { "Title", "ID", "Age", "Something.Prop", "PolymorphicSomething.trrrr" }
119+
);
120+
}
121+
122+
[Fact]
123+
public void BiggerTable()
124+
{
125+
check.CheckTable(
126+
bitBiggerTable
127+
);
104128
}
105129

106130
class SomeTestObject
107131
{
108132
public string Prop { get; set; }
109133
}
134+
135+
class BitMoreComplexObject
136+
{
137+
public int ID { get; set; }
138+
public int Age { get; set; }
139+
public SomeTestObject Something { get; set; }
140+
public object PolymorphicSomething { get; set; }
141+
public string Title { get; set; }
142+
public string SomeText { get; set; }
143+
}
144+
145+
static BitMoreComplexObject[] someSmallTable = new []{
146+
new BitMoreComplexObject {
147+
ID = 1,
148+
Age = 10,
149+
Something = new SomeTestObject { Prop = "hmm" },
150+
Title = "Yes",
151+
SomeText = "Lorem ipsum..."
152+
},
153+
new BitMoreComplexObject {
154+
ID = 2,
155+
Age = 20,
156+
PolymorphicSomething = new { trrrr = 1 },
157+
Title = "please no",
158+
SomeText = "You are telling the enemy exactly what you are going to do. No wonder you have been fighting Lorem Ipsum your entire adult life. Look at these words. Are they small words? And he referred to my words - if they are small, something else must be small. I guarantee you there's no problem, I guarantee. Trump Ipsum is calling for a total and complete shutdown of Muslim text entering your website. Lorem Ipsum is unattractive, both inside and out. I fully understand why its former users left it for something else. They made a good decision."
159+
},
160+
new BitMoreComplexObject {
161+
ID = 3,
162+
Age = 20,
163+
Title = "I don't know",
164+
},
165+
new BitMoreComplexObject {
166+
ID = 4,
167+
Age = 20,
168+
Title = "...",
169+
},
170+
};
171+
172+
static BitMoreComplexObject[] bitBiggerTable =
173+
Enumerable.Range(0, 200).Select(id =>
174+
new BitMoreComplexObject {
175+
ID = id,
176+
Age = id * 31 % 60,
177+
Title = "X" + id,
178+
PolymorphicSomething = id % 17 > 0 ? null : new {x = "jjjjj"}
179+
}
180+
).ToArray();
110181
}
111182
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
ID Age Title SomeText Something PolymorphicSomething
2+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3+
0 0 X0 {"x":"jjjjj"}
4+
1 31 X1
5+
2 2 X2
6+
3 33 X3
7+
4 4 X4
8+
5 35 X5
9+
6 6 X6
10+
7 37 X7
11+
8 8 X8
12+
9 39 X9
13+
10 10 X10
14+
11 41 X11
15+
12 12 X12
16+
13 43 X13
17+
14 14 X14
18+
15 45 X15
19+
16 16 X16
20+
17 47 X17 {"x":"jjjjj"}
21+
18 18 X18
22+
19 49 X19
23+
20 20 X20
24+
21 51 X21
25+
22 22 X22
26+
23 53 X23
27+
24 24 X24
28+
25 55 X25
29+
26 26 X26
30+
27 57 X27
31+
28 28 X28
32+
29 59 X29
33+
30 30 X30
34+
31 1 X31
35+
32 32 X32
36+
33 3 X33
37+
34 34 X34 {"x":"jjjjj"}
38+
35 5 X35
39+
36 36 X36
40+
37 7 X37
41+
38 38 X38
42+
39 9 X39
43+
40 40 X40
44+
41 11 X41
45+
42 42 X42
46+
43 13 X43
47+
44 44 X44
48+
45 15 X45
49+
46 46 X46
50+
47 17 X47
51+
48 48 X48
52+
49 19 X49
53+
50 50 X50
54+
51 21 X51 {"x":"jjjjj"}
55+
52 52 X52
56+
53 23 X53
57+
54 54 X54
58+
55 25 X55
59+
56 56 X56
60+
57 27 X57
61+
58 58 X58
62+
59 29 X59
63+
60 0 X60
64+
61 31 X61
65+
62 2 X62
66+
63 33 X63
67+
64 4 X64
68+
65 35 X65
69+
66 6 X66
70+
67 37 X67
71+
68 8 X68 {"x":"jjjjj"}
72+
69 39 X69
73+
70 10 X70
74+
71 41 X71
75+
72 12 X72
76+
73 43 X73
77+
74 14 X74
78+
75 45 X75
79+
76 16 X76
80+
77 47 X77
81+
78 18 X78
82+
79 49 X79
83+
80 20 X80
84+
81 51 X81
85+
82 22 X82
86+
83 53 X83
87+
84 24 X84
88+
85 55 X85 {"x":"jjjjj"}
89+
86 26 X86
90+
87 57 X87
91+
88 28 X88
92+
89 59 X89
93+
90 30 X90
94+
91 1 X91
95+
92 32 X92
96+
93 3 X93
97+
94 34 X94
98+
95 5 X95
99+
96 36 X96
100+
97 7 X97
101+
98 38 X98
102+
99 9 X99
103+
100 40 X100
104+
101 11 X101
105+
102 42 X102 {"x":"jjjjj"}
106+
103 13 X103
107+
104 44 X104
108+
105 15 X105
109+
106 46 X106
110+
107 17 X107
111+
108 48 X108
112+
109 19 X109
113+
110 50 X110
114+
111 21 X111
115+
112 52 X112
116+
113 23 X113
117+
114 54 X114
118+
115 25 X115
119+
116 56 X116
120+
117 27 X117
121+
118 58 X118
122+
119 29 X119 {"x":"jjjjj"}
123+
120 0 X120
124+
121 31 X121
125+
122 2 X122
126+
123 33 X123
127+
124 4 X124
128+
125 35 X125
129+
126 6 X126
130+
127 37 X127
131+
128 8 X128
132+
129 39 X129
133+
130 10 X130
134+
131 41 X131
135+
132 12 X132
136+
133 43 X133
137+
134 14 X134
138+
135 45 X135
139+
136 16 X136 {"x":"jjjjj"}
140+
137 47 X137
141+
138 18 X138
142+
139 49 X139
143+
140 20 X140
144+
141 51 X141
145+
142 22 X142
146+
143 53 X143
147+
144 24 X144
148+
145 55 X145
149+
146 26 X146
150+
147 57 X147
151+
148 28 X148
152+
149 59 X149
153+
150 30 X150
154+
151 1 X151
155+
152 32 X152
156+
153 3 X153 {"x":"jjjjj"}
157+
154 34 X154
158+
155 5 X155
159+
156 36 X156
160+
157 7 X157
161+
158 38 X158
162+
159 9 X159
163+
160 40 X160
164+
161 11 X161
165+
162 42 X162
166+
163 13 X163
167+
164 44 X164
168+
165 15 X165
169+
166 46 X166
170+
167 17 X167
171+
168 48 X168
172+
169 19 X169
173+
170 50 X170 {"x":"jjjjj"}
174+
171 21 X171
175+
172 52 X172
176+
173 23 X173
177+
174 54 X174
178+
175 25 X175
179+
176 56 X176
180+
177 27 X177
181+
178 58 X178
182+
179 29 X179
183+
180 0 X180
184+
181 31 X181
185+
182 2 X182
186+
183 33 X183
187+
184 4 X184
188+
185 35 X185
189+
186 6 X186
190+
187 37 X187 {"x":"jjjjj"}
191+
188 8 X188
192+
189 39 X189
193+
190 10 X190
194+
191 41 X191
195+
192 12 X192
196+
193 43 X193
197+
194 14 X194
198+
195 45 X195
199+
196 16 X196
200+
197 47 X197
201+
198 18 X198
202+
199 49 X199
203+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ID Age Something Title PolymorphicSomething
2+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3+
1 10 {"Prop":"hmm"} Yes
4+
SomeText: "Lorem ipsum..."
5+
6+
2 20 "please no" {"trrrr":1}
7+
SomeText: "You are telling the enemy exactly what you are going to do. No wonder you have been fighting Lorem Ipsum your entire adult life. Look at these words. Are they small words? And he referred to my words - if they are small, something else must be small. I guarantee you there's no problem, I guarantee. Trump Ipsum is calling for a total and complete shutdown of Muslim text entering your website. Lorem Ipsum is unattractive, both inside and out. I fully understand why its former users left it for something else. They made a good decision."
8+
9+
3 20 "I don't know"
10+
4 20 ...
11+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Title ID Age Something.Prop PolymorphicSomething.trrrr
2+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3+
Yes 1 10 hmm
4+
"please no" 2 20 1
5+
"I don't know" 3 20
6+
... 4 20
7+

src/CheckTestOutput.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.1;net6.0</TargetFrameworks>
5+
<LangVersion>9</LangVersion>
56

67
<Description>Simple helper which checks that output of a test matches a file. If not matching, just git staging the new file will accept the new version.</Description>
78
<PackageId>CheckTestOutput</PackageId>
@@ -23,6 +24,7 @@
2324
<IncludeSymbols>false</IncludeSymbols>
2425
<DebugType>embedded</DebugType>
2526
<EmbedAllSources>true</EmbedAllSources>
27+
2628
</PropertyGroup>
2729

2830
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">

src/JsonChecks.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,8 @@ public static void CheckJsonObject(
2323
[System.Runtime.CompilerServices.CallerMemberName] string memberName = null,
2424
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = null)
2525
{
26-
jsonOptions ??= new JsonSerializerOptions() { WriteIndented = true };
27-
var strOutput = SerializeJson(output, jsonOptions);
28-
29-
if (normalizePropertyOrder)
30-
{
31-
// this awesome, just give me back newtonsoft...
32-
var jsonDocument = JsonDocument.Parse(strOutput);
33-
var outputStream = new MemoryStream();
34-
var writer = new Utf8JsonWriter(outputStream, new JsonWriterOptions { Indented = true });
35-
NormalizePropertyOrder(jsonDocument.RootElement, writer);
36-
writer.Flush();
37-
strOutput = System.Text.Encoding.UTF8.GetString(outputStream.ToArray());
38-
}
26+
jsonOptions ??= new JsonSerializerOptions() { WriteIndented = true, Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
27+
var strOutput = SerializeJson(output, jsonOptions, normalizePropertyOrder);
3928

4029
// indent using tabs for back compatibility
4130
strOutput = Regex.Replace(strOutput, "^( )+", m => new string('\t', m.Value.Length / 2), RegexOptions.Multiline);
@@ -47,7 +36,7 @@ public static void CheckJsonObject(
4736
);
4837
}
4938

50-
static string SerializeJson(object obj, JsonSerializerOptions options)
39+
internal static string SerializeJson(object obj, JsonSerializerOptions options, bool normalizePropertyOrder)
5140
{
5241
if (obj is null) return "null";
5342

@@ -62,11 +51,23 @@ static string SerializeJson(object obj, JsonSerializerOptions options)
6251
}
6352
type = type.BaseType;
6453
}
65-
if (isToStringableObject)
66-
return obj.ToString();
6754

68-
return JsonSerializer.Serialize(obj, options);
55+
var output =
56+
isToStringableObject ? obj.ToString() :
57+
JsonSerializer.Serialize(obj, options);
6958

59+
return normalizePropertyOrder ? NormalizePropertyOrder(output) : output;
60+
}
61+
62+
static string NormalizePropertyOrder(string json)
63+
{
64+
// this awesome, just give me back newtonsoft...
65+
var jsonDocument = JsonDocument.Parse(json);
66+
var outputStream = new MemoryStream();
67+
var writer = new Utf8JsonWriter(outputStream, new JsonWriterOptions { Indented = true });
68+
NormalizePropertyOrder(jsonDocument.RootElement, writer);
69+
writer.Flush();
70+
return System.Text.Encoding.UTF8.GetString(outputStream.ToArray());
7071
}
7172

7273
static void NormalizePropertyOrder(JsonElement e, Utf8JsonWriter output)

0 commit comments

Comments
 (0)