annotate Implab.Test/Implab.Format.Test/JsonTests.cs @ 226:9428ea36838e v2

fixed JSON parser error when parsing escaped double quote symbol
author cin
date Fri, 25 Aug 2017 02:16:35 +0300
parents d6a8cba73acc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
158
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
1 using NUnit.Framework;
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
2 using System;
182
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 158
diff changeset
3 using Implab.Formats.JSON;
183
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
4 using Implab.Automaton;
158
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
5
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
6 namespace Implab.Format.Test {
182
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 158
diff changeset
7 [TestFixture]
158
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
8 public class JsonTests {
182
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 158
diff changeset
9 [Test]
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 158
diff changeset
10 public void TestScannerValidTokens() {
183
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
11 using (var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) {
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
12
184
d6a8cba73acc working on runnable component
cin
parents: 183
diff changeset
13 Tuple<JsonTokenType,object>[] expexted = {
183
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
14 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
15 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
16 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
17 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
18 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
19 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
20 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
21 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
22 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
23 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
24 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
25 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
26 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
27 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
28 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
29 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
30 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
31 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, " ["),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
32 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, "]"),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
33 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, "{"),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
34 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, "}"),
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
35 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, ":")
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
36 };
182
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 158
diff changeset
37
183
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
38 object value;
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
39 JsonTokenType tokenType;
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
40 for (var i = 0; i < expexted.Length; i++) {
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
41
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
42 Assert.IsTrue(scanner.ReadToken(out value, out tokenType));
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
43 Assert.AreEqual(expexted[i].Item1, tokenType);
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
44 Assert.AreEqual(expexted[i].Item2, value);
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
45 }
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
46
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
47 Assert.IsFalse(scanner.ReadToken(out value, out tokenType));
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
48 }
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
49 }
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
50
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
51 [Test]
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
52 public void TestScannerBadTokens() {
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
53 var bad = new [] {
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
54 " 1",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
55 " literal",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
56 " \"",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
57 "\"unclosed string",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
58 "1.bad",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
59 "001", // should be read as three numbers
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
60 "--10",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
61 "+10",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
62 "1.0.0",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
63 "1e1.0",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
64 "l1teral0",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
65 ".123",
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
66 "-.123"
182
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 158
diff changeset
67 };
76e8f2ba12b8 pretty print DFA, the minimization is still buggy
cin
parents: 158
diff changeset
68
183
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
69 foreach (var json in bad)
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
70 using (var scanner = new JSONScanner(json)) {
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
71 try {
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
72 object value;
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
73 JsonTokenType token;
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
74 scanner.ReadToken(out value, out token);
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
75 if (!Object.Equals(value,json)) {
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
76 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value );
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
77 continue;
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
78 }
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
79 Assert.Fail("Token '{0}' shouldn't pass", json);
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
80 } catch (ParserException e) {
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
81 Console.WriteLine(e.Message);
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
82 }
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
83 }
4f82e0f161c3 fixed DFA optimization, JSON is fully functional
cin
parents: 182
diff changeset
84
158
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
85 }
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
86 }
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
87 }
130781364799 refactoring, code cleanup
cin
parents:
diff changeset
88