-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJsonReaderTests.cs
More file actions
97 lines (84 loc) · 4 KB
/
Copy pathJsonReaderTests.cs
File metadata and controls
97 lines (84 loc) · 4 KB
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
// Copyright (c) Contributors to the New StyleCop Analyzers project.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable disable
namespace StyleCop.Analyzers.Test.LightJson.Serialization
{
using System;
using System.IO;
using global::LightJson;
using global::LightJson.Serialization;
using Xunit;
public class JsonReaderTests
{
[Fact]
public void TestKeyMatchesPreviousValue()
{
var jsonObject = JsonValue.Parse("{ \"x\": \"value\", \"value\": \"value\" }");
Assert.NotEqual(jsonObject, JsonValue.Null);
Assert.Equal("value", jsonObject["x"].AsString);
Assert.Equal("value", jsonObject["value"].AsString);
Assert.Equal(jsonObject["x"], jsonObject["value"]);
}
[Fact]
public void TestDuplicateKeys()
{
Assert.ThrowsAny<JsonParseException>(() => JsonValue.Parse("{ \"x\": \"value\", \"x\": \"value\" }"));
}
[Fact]
public void TestParse()
{
Assert.Equal("true", JsonReader.Parse("true").AsString);
using (var reader = new StringReader("true"))
{
Assert.Equal("true", JsonReader.Parse(reader).AsString);
}
Assert.Throws<ArgumentNullException>("source", () => JsonReader.Parse(default(string)));
Assert.Throws<ArgumentNullException>("reader", () => JsonReader.Parse(default(TextReader)));
}
[Fact]
public void TestNumbers()
{
Assert.Equal(0, JsonReader.Parse("0").AsInteger);
Assert.Equal(0, JsonReader.Parse("-0").AsInteger);
Assert.Equal(-1, JsonReader.Parse("-1").AsInteger);
Assert.Equal(-1.0, JsonReader.Parse("-1.0").AsNumber);
Assert.Equal(-1e1, JsonReader.Parse("-1e1").AsNumber);
Assert.Equal(-1E1, JsonReader.Parse("-1E1").AsNumber);
Assert.Equal(-1E+1, JsonReader.Parse("-1E+1").AsNumber);
Assert.Equal(-10E-1, JsonReader.Parse("-10E-1").AsNumber);
}
[Fact]
public void TestEscapeSequences()
{
Assert.Equal("\"", JsonReader.Parse("\"\\\"\"").AsString);
Assert.Equal("\\", JsonReader.Parse("\"\\\\\"").AsString);
Assert.Equal("/", JsonReader.Parse("\"\\/\"").AsString);
Assert.Equal("\b", JsonReader.Parse("\"\\b\"").AsString);
Assert.Equal("\f", JsonReader.Parse("\"\\f\"").AsString);
Assert.Equal("\n", JsonReader.Parse("\"\\n\"").AsString);
Assert.Equal("\r", JsonReader.Parse("\"\\r\"").AsString);
Assert.Equal("\t", JsonReader.Parse("\"\\t\"").AsString);
Assert.Equal("\u0123\u4567\u89AB\uCDEF", JsonReader.Parse("\"\\u0123\\u4567\\u89AB\\uCDEF\"").AsString);
var ex = Assert.Throws<JsonParseException>(() => JsonReader.Parse("\"\\x\""));
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
Assert.Equal(0, ex.Position.Line);
Assert.Equal(2, ex.Position.Column);
ex = Assert.Throws<JsonParseException>(() => JsonReader.Parse("\"\\u11GA\""));
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
Assert.Equal(0, ex.Position.Line);
Assert.Equal(5, ex.Position.Column);
ex = Assert.Throws<JsonParseException>(() => JsonReader.Parse("\"\r\""));
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
Assert.Equal(0, ex.Position.Line);
Assert.Equal(1, ex.Position.Column);
}
[Fact]
public void TestArrayMissingComma()
{
var ex = Assert.ThrowsAny<JsonParseException>(() => JsonReader.Parse("[ 1 2 ]"));
Assert.Equal(JsonParseException.ErrorType.InvalidOrUnexpectedCharacter, ex.Type);
Assert.Equal(0, ex.Position.Line);
Assert.Equal(4, ex.Position.Column);
}
}
}