158
|
1 using NUnit.Framework;
|
|
2 using System;
|
182
|
3 using Implab.Formats.JSON;
|
183
|
4 using Implab.Automaton;
|
158
|
5
|
|
6 namespace Implab.Format.Test {
|
182
|
7 [TestFixture]
|
158
|
8 public class JsonTests {
|
182
|
9 [Test]
|
|
10 public void TestScannerValidTokens() {
|
183
|
11 using (var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) {
|
|
12
|
|
13 Tuple<JsonTokenType,object>[] expexted = new [] {
|
|
14 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d),
|
|
15 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
|
|
16 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d),
|
|
17 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
|
|
18 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d),
|
|
19 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
|
|
20 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d),
|
|
21 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
|
|
22 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d),
|
|
23 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
|
|
24 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d),
|
|
25 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
|
|
26 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d),
|
|
27 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
|
|
28 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"),
|
|
29 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
|
|
30 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"),
|
|
31 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, " ["),
|
|
32 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, "]"),
|
|
33 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, "{"),
|
|
34 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, "}"),
|
|
35 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, ":")
|
|
36 };
|
182
|
37
|
183
|
38 object value;
|
|
39 JsonTokenType tokenType;
|
|
40 for (var i = 0; i < expexted.Length; i++) {
|
|
41
|
|
42 Assert.IsTrue(scanner.ReadToken(out value, out tokenType));
|
|
43 Assert.AreEqual(expexted[i].Item1, tokenType);
|
|
44 Assert.AreEqual(expexted[i].Item2, value);
|
|
45 }
|
|
46
|
|
47 Assert.IsFalse(scanner.ReadToken(out value, out tokenType));
|
|
48 }
|
|
49 }
|
|
50
|
|
51 [Test]
|
|
52 public void TestScannerBadTokens() {
|
|
53 var bad = new [] {
|
|
54 " 1",
|
|
55 " literal",
|
|
56 " \"",
|
|
57 "\"unclosed string",
|
|
58 "1.bad",
|
|
59 "001", // should be read as three numbers
|
|
60 "--10",
|
|
61 "+10",
|
|
62 "1.0.0",
|
|
63 "1e1.0",
|
|
64 "l1teral0",
|
|
65 ".123",
|
|
66 "-.123"
|
182
|
67 };
|
|
68
|
183
|
69 foreach (var json in bad)
|
|
70 using (var scanner = new JSONScanner(json)) {
|
|
71 try {
|
|
72 object value;
|
|
73 JsonTokenType token;
|
|
74 scanner.ReadToken(out value, out token);
|
|
75 if (!Object.Equals(value,json)) {
|
|
76 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value );
|
|
77 continue;
|
|
78 }
|
|
79 Assert.Fail("Token '{0}' shouldn't pass", json);
|
|
80 } catch (ParserException e) {
|
|
81 Console.WriteLine(e.Message);
|
|
82 }
|
|
83 }
|
|
84
|
158
|
85 }
|
|
86 }
|
|
87 }
|
|
88
|