comparison Implab.Test/Implab.Format.Test/JsonTests.cs @ 183:4f82e0f161c3 ref20160224

fixed DFA optimization, JSON is fully functional
author cin
date Fri, 25 Mar 2016 02:49:02 +0300
parents 76e8f2ba12b8
children d6a8cba73acc
comparison
equal deleted inserted replaced
182:76e8f2ba12b8 183:4f82e0f161c3
1 using NUnit.Framework; 1 using NUnit.Framework;
2 using System; 2 using System;
3 using Implab.Formats.JSON; 3 using Implab.Formats.JSON;
4 using Implab.Automaton;
4 5
5 namespace Implab.Format.Test { 6 namespace Implab.Format.Test {
6 [TestFixture] 7 [TestFixture]
7 public class JsonTests { 8 public class JsonTests {
8 [Test] 9 [Test]
9 public void TestScannerValidTokens() { 10 public void TestScannerValidTokens() {
10 var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:"); 11 using (var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) {
11 12
12 Tuple<JsonTokenType,object>[] expexted = new [] { 13 Tuple<JsonTokenType,object>[] expexted = new [] {
13 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d), 14 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d),
14 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ), 15 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
15 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d ), 16 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d),
16 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ), 17 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
17 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d ), 18 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d),
18 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ), 19 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
19 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d ), 20 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d),
20 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ), 21 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
21 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d ), 22 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d),
22 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ), 23 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
23 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d ), 24 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d),
24 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ), 25 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
25 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d ), 26 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d),
26 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ), 27 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
27 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text" ), 28 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"),
28 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ), 29 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
29 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal" ), 30 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"),
30 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, " [" ), 31 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, " ["),
31 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, "]" ), 32 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, "]"),
32 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, "{" ), 33 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, "{"),
33 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, "}" ), 34 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, "}"),
34 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, ":" ) 35 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, ":")
36 };
37
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"
35 }; 67 };
36 68
37 object value; 69 foreach (var json in bad)
38 JsonTokenType tokenType; 70 using (var scanner = new JSONScanner(json)) {
39 for (var i = 0; i < expexted.Length; i++) { 71 try {
40 72 object value;
41 Assert.IsTrue(scanner.ReadToken(out value, out tokenType)); 73 JsonTokenType token;
42 Assert.AreEqual(expexted[i].Item1, tokenType); 74 scanner.ReadToken(out value, out token);
43 Assert.AreEqual(expexted[i].Item2, value); 75 if (!Object.Equals(value,json)) {
44 } 76 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value );
45 77 continue;
46 Assert.IsFalse(scanner.ReadToken(out value, out tokenType)); 78 }
79 Assert.Fail("Token '{0}' shouldn't pass", json);
80 } catch (ParserException e) {
81 Console.WriteLine(e.Message);
82 }
83 }
84
47 } 85 }
48 } 86 }
49 } 87 }
50 88