comparison Implab.Format.Test/JsonTests.cs @ 228:6fa235c5a760 v2

Rewritten JsonScanner, JsonParser, fixed naming style
author cin
date Tue, 12 Sep 2017 01:19:12 +0300
parents 8d5de4eb9c2c
children 5f7a3e1d32b9
comparison
equal deleted inserted replaced
227:8d5de4eb9c2c 228:6fa235c5a760
1 using NUnit.Framework; 1 using NUnit.Framework;
2 using System; 2 using System;
3 using Implab.Formats.JSON;
4 using Implab.Automaton; 3 using Implab.Automaton;
5 using Implab.Xml; 4 using Implab.Xml;
6 using System.Xml; 5 using System.Xml;
7 using System.Text; 6 using Implab.Formats;
7 using Implab.Formats.Json;
8 8
9 namespace Implab.Format.Test { 9 namespace Implab.Format.Test {
10 [TestFixture] 10 [TestFixture]
11 public class JsonTests { 11 public class JsonTests {
12
12 [Test] 13 [Test]
13 public void TestScannerValidTokens() { 14 public void TestScannerValidTokens() {
14 using (var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { 15 using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) {
15 16
16 Tuple<JsonTokenType, object>[] expexted = { 17 Tuple<JsonTokenType, object>[] expexted = {
17 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d), 18 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d),
18 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), 19 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
19 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d), 20 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d),
20 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), 21 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
21 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d), 22 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d),
22 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), 23 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
23 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d), 24 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d),
24 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), 25 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
25 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d), 26 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d),
26 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), 27 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
27 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d), 28 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d),
28 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), 29 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
29 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d), 30 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d),
30 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), 31 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
31 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"), 32 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"),
32 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "), 33 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
33 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"), 34 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"),
34 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, " ["), 35 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, null),
35 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, "]"), 36 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, null),
36 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, "{"), 37 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, null),
37 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, "}"), 38 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, null),
38 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, ":") 39 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, null)
39 }; 40 };
40 41
41 object value; 42 object value;
42 JsonTokenType tokenType; 43 JsonTokenType tokenType;
43 for (var i = 0; i < expexted.Length; i++) { 44 for (var i = 0; i < expexted.Length; i++) {
68 ".123", 69 ".123",
69 "-.123" 70 "-.123"
70 }; 71 };
71 72
72 foreach (var json in bad) { 73 foreach (var json in bad) {
73 using (var scanner = new JSONScanner(json)) { 74 using (var scanner = JsonStringScanner.Create(json)) {
74 try { 75 try {
75 object value; 76 object value;
76 JsonTokenType token; 77 JsonTokenType token;
77 scanner.ReadToken(out value, out token); 78 scanner.ReadToken(out value, out token);
78 if (!Object.Equals(value, json)) { 79 if (!Object.Equals(value, json)) {
120 } 121 }
121 122
122 void DumpJsonParse(string json) { 123 void DumpJsonParse(string json) {
123 Console.WriteLine($"JSON: {json}"); 124 Console.WriteLine($"JSON: {json}");
124 Console.WriteLine("XML"); 125 Console.WriteLine("XML");
125 using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { 126 using (var xmlReader = new JsonXmlReader(new JsonParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) {
126 while (xmlReader.Read()) 127 while (xmlReader.Read())
127 Console.WriteLine($"{new string(' ', xmlReader.Depth * 2)}{xmlReader}"); 128 Console.WriteLine($"{new string(' ', xmlReader.Depth * 2)}{xmlReader}");
128 } 129 }
129 } 130 }
130 131
134 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { 135 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings {
135 Indent = true, 136 Indent = true,
136 CloseOutput = false, 137 CloseOutput = false,
137 ConformanceLevel = ConformanceLevel.Document 138 ConformanceLevel = ConformanceLevel.Document
138 })) 139 }))
139 using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { 140 using (var xmlReader = new JsonXmlReader(new JsonParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) {
140 xmlWriter.WriteNode(xmlReader, false); 141 xmlWriter.WriteNode(xmlReader, false);
141 } 142 }
142 } 143 }
143 } 144 }
144 } 145 }