Skip to content

Commit e8d1ea2

Browse files
committed
Add tests for JsonReader
* Remove unreachable code in ReadBoolean * Fix several error positions
1 parent 0a0269b commit e8d1ea2

2 files changed

Lines changed: 83 additions & 15 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/LightJson/Serialization/JsonReaderTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace StyleCop.Analyzers.Test.LightJson.Serialization
55
{
6+
using System;
7+
using System.IO;
68
using global::LightJson;
79
using global::LightJson.Serialization;
810
using Xunit;
@@ -24,5 +26,70 @@ public void TestDuplicateKeys()
2426
{
2527
Assert.ThrowsAny<JsonParseException>(() => JsonValue.Parse("{ \"x\": \"value\", \"x\": \"value\" }"));
2628
}
29+
30+
[Fact]
31+
public void TestParse()
32+
{
33+
Assert.Equal("true", JsonReader.Parse("true").AsString);
34+
35+
using (var reader = new StringReader("true"))
36+
{
37+
Assert.Equal("true", JsonReader.Parse(reader).AsString);
38+
}
39+
40+
Assert.Throws<ArgumentNullException>("source", () => JsonReader.Parse(default(string)));
41+
Assert.Throws<ArgumentNullException>("reader", () => JsonReader.Parse(default(TextReader)));
42+
}
43+
44+
[Fact]
45+
public void TestNumbers()
46+
{
47+
Assert.Equal(0, JsonReader.Parse("0").AsInteger);
48+
Assert.Equal(0, JsonReader.Parse("-0").AsInteger);
49+
Assert.Equal(-1, JsonReader.Parse("-1").AsInteger);
50+
Assert.Equal(-1.0, JsonReader.Parse("-1.0").AsNumber);
51+
Assert.Equal(-1e1, JsonReader.Parse("-1e1").AsNumber);
52+
Assert.Equal(-1E1, JsonReader.Parse("-1E1").AsNumber);
53+
Assert.Equal(-1E+1, JsonReader.Parse("-1E+1").AsNumber);
54+
Assert.Equal(-10E-1, JsonReader.Parse("-10E-1").AsNumber);
55+
}
56+
57+
[Fact]
58+
public void TestEscapeSequences()
59+
{
60+
Assert.Equal("\"", JsonReader.Parse("\"\\\"\"").AsString);
61+
Assert.Equal("\\", JsonReader.Parse("\"\\\\\"").AsString);
62+
Assert.Equal("/", JsonReader.Parse("\"\\/\"").AsString);
63+
Assert.Equal("\b", JsonReader.Parse("\"\\b\"").AsString);
64+
Assert.Equal("\f", JsonReader.Parse("\"\\f\"").AsString);
65+
Assert.Equal("\n", JsonReader.Parse("\"\\n\"").AsString);
66+
Assert.Equal("\r", JsonReader.Parse("\"\\r\"").AsString);
67+
Assert.Equal("\t", JsonReader.Parse("\"\\t\"").AsString);
68+
Assert.Equal("\u0123\u4567\u89AB\uCDEF", JsonReader.Parse("\"\\u0123\\u4567\\u89AB\\uCDEF\"").AsString);
69+
70+
var ex = Assert.Throws<JsonParseException>(() => JsonReader.Parse("\"\\x\""));
71+
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
72+
Assert.Equal(0, ex.Position.Line);
73+
Assert.Equal(2, ex.Position.Column);
74+
75+
ex = Assert.Throws<JsonParseException>(() => JsonReader.Parse("\"\\u11GA\""));
76+
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
77+
Assert.Equal(0, ex.Position.Line);
78+
Assert.Equal(5, ex.Position.Column);
79+
80+
ex = Assert.Throws<JsonParseException>(() => JsonReader.Parse("\"\r\""));
81+
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
82+
Assert.Equal(0, ex.Position.Line);
83+
Assert.Equal(1, ex.Position.Column);
84+
}
85+
86+
[Fact]
87+
public void TestArrayMissingComma()
88+
{
89+
var ex = Assert.ThrowsAny<JsonParseException>(() => JsonReader.Parse("[ 1 2 ]"));
90+
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
91+
Assert.Equal(0, ex.Position.Line);
92+
Assert.Equal(4, ex.Position.Column);
93+
}
2794
}
2895
}

StyleCop.Analyzers/StyleCop.Analyzers/LightJson/Serialization/JsonReader.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static JsonValue Parse(TextReader reader)
3030
{
3131
if (reader == null)
3232
{
33-
throw new ArgumentNullException("reader");
33+
throw new ArgumentNullException(nameof(reader));
3434
}
3535

3636
return new JsonReader(reader).Parse();
@@ -45,12 +45,12 @@ public static JsonValue Parse(string source)
4545
{
4646
if (source == null)
4747
{
48-
throw new ArgumentNullException("source");
48+
throw new ArgumentNullException(nameof(source));
4949
}
5050

5151
using (var reader = new StringReader(source))
5252
{
53-
return new JsonReader(reader).Parse();
53+
return Parse(reader);
5454
}
5555
}
5656

@@ -112,14 +112,9 @@ private JsonValue ReadBoolean()
112112
this.scanner.Assert("true");
113113
return true;
114114

115-
case 'f':
115+
default:
116116
this.scanner.Assert("false");
117117
return false;
118-
119-
default:
120-
throw new JsonParseException(
121-
ErrorType.InvalidOrUnexpectedCharacter,
122-
this.scanner.Position);
123118
}
124119
}
125120

@@ -191,10 +186,12 @@ private string ReadString()
191186

192187
while (true)
193188
{
189+
var errorPosition = this.scanner.Position;
194190
var c = this.scanner.Read();
195191

196192
if (c == '\\')
197193
{
194+
errorPosition = this.scanner.Position;
198195
c = this.scanner.Read();
199196

200197
switch (char.ToLower(c))
@@ -225,7 +222,7 @@ private string ReadString()
225222
default:
226223
throw new JsonParseException(
227224
ErrorType.InvalidOrUnexpectedCharacter,
228-
this.scanner.Position);
225+
errorPosition);
229226
}
230227
}
231228
else if (c == '"')
@@ -238,7 +235,7 @@ private string ReadString()
238235
{
239236
throw new JsonParseException(
240237
ErrorType.InvalidOrUnexpectedCharacter,
241-
this.scanner.Position);
238+
errorPosition);
242239
}
243240
else
244241
{
@@ -252,6 +249,7 @@ private string ReadString()
252249

253250
private int ReadHexDigit()
254251
{
252+
var errorPosition = this.scanner.Position;
255253
switch (char.ToUpper(this.scanner.Read()))
256254
{
257255
case '0':
@@ -305,7 +303,7 @@ private int ReadHexDigit()
305303
default:
306304
throw new JsonParseException(
307305
ErrorType.InvalidOrUnexpectedCharacter,
308-
this.scanner.Position);
306+
errorPosition);
309307
}
310308
}
311309

@@ -342,13 +340,14 @@ private JsonObject ReadObject(JsonObject jsonObject)
342340
{
343341
this.scanner.SkipWhitespace();
344342

343+
var errorPosition = this.scanner.Position;
345344
var key = this.ReadJsonKey();
346345

347346
if (jsonObject.ContainsKey(key))
348347
{
349348
throw new JsonParseException(
350349
ErrorType.DuplicateObjectKeys,
351-
this.scanner.Position);
350+
errorPosition);
352351
}
353352

354353
this.scanner.SkipWhitespace();
@@ -363,6 +362,7 @@ private JsonObject ReadObject(JsonObject jsonObject)
363362

364363
this.scanner.SkipWhitespace();
365364

365+
errorPosition = this.scanner.Position;
366366
var next = this.scanner.Read();
367367
if (next == ',')
368368
{
@@ -386,7 +386,7 @@ private JsonObject ReadObject(JsonObject jsonObject)
386386
{
387387
throw new JsonParseException(
388388
ErrorType.InvalidOrUnexpectedCharacter,
389-
this.scanner.Position);
389+
errorPosition);
390390
}
391391
}
392392
}
@@ -421,6 +421,7 @@ private JsonArray ReadArray(JsonArray jsonArray)
421421

422422
this.scanner.SkipWhitespace();
423423

424+
var errorPosition = this.scanner.Position;
424425
var next = this.scanner.Read();
425426
if (next == ',')
426427
{
@@ -444,7 +445,7 @@ private JsonArray ReadArray(JsonArray jsonArray)
444445
{
445446
throw new JsonParseException(
446447
ErrorType.InvalidOrUnexpectedCharacter,
447-
this.scanner.Position);
448+
errorPosition);
448449
}
449450
}
450451
}

0 commit comments

Comments
 (0)