comparison Implab.Test/JsonTests.cs @ 262:f1696cdc3d7a v3 v3.0.8

Added IInitializable.Initialize() overload Added IRunnable.Start(), IRunnable.Start() overloads Fixed cancellation of the current operation when Stop() is called More tests
author cin
date Mon, 16 Apr 2018 02:12:39 +0300
parents Implab.Format.Test/JsonTests.cs@302ca905c19e
children 3a6e18c432be
comparison
equal deleted inserted replaced
261:05a87f575512 262:f1696cdc3d7a
1 using Xunit;
2 using System;
3 using Implab.Automaton;
4 using Implab.Xml;
5 using System.Xml;
6 using Implab.Formats;
7 using Implab.Formats.Json;
8 using System.IO;
9
10 namespace Implab.Test {
11 public class JsonTests {
12
13 [Fact]
14 public void TestScannerValidTokens() {
15 using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) {
16
17 Tuple<JsonTokenType, object>[] expexted = {
18 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "9123"),
19 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
20 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-123"),
21 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
22 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0"),
23 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
24 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0.1"),
25 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
26 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.2"),
27 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
28 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.1e3"),
29 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
30 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "1.3E-3"),
31 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
32 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"),
33 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null),
34 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"),
35 new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, null),
36 new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, null),
37 new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, null),
38 new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, null),
39 new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, null)
40 };
41
42 string value;
43 JsonTokenType tokenType;
44 for (var i = 0; i < expexted.Length; i++) {
45
46 Assert.True(scanner.ReadToken(out value, out tokenType));
47 Assert.Equal(expexted[i].Item1, tokenType);
48 Assert.Equal(expexted[i].Item2, value);
49 }
50
51 Assert.False(scanner.ReadToken(out value, out tokenType));
52 }
53 }
54
55 [Fact]
56 public void TestScannerBadTokens() {
57 var bad = new[] {
58 " 1",
59 " literal",
60 " \"",
61 "\"unclosed string",
62 "1.bad",
63 "001", // should be read as three numbers
64 "--10",
65 "+10",
66 "1.0.0",
67 "1e1.0",
68 "l1teral0",
69 ".123",
70 "-.123"
71 };
72
73 foreach (var json in bad) {
74 using (var scanner = JsonStringScanner.Create(json)) {
75 try {
76 string value;
77 JsonTokenType token;
78 scanner.ReadToken(out value, out token);
79 if (!Object.Equals(value, json)) {
80 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value);
81 continue;
82 }
83 Assert.True(false, $"Token '{json}' shouldn't pass");
84 } catch (ParserException e) {
85 Console.WriteLine(e.Message);
86 }
87 }
88 }
89 }
90
91 [Fact]
92 public void JsonXmlReaderSimpleTest() {
93 var json = "\"some text\"";
94 //Console.WriteLine($"JSON: {json}");
95 //Console.WriteLine("XML");
96 /*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) {
97 Assert.AreEqual(xmlReader.ReadState, System.Xml.ReadState.Initial);
98
99 AssertRead(xmlReader, XmlNodeType.XmlDeclaration);
100 AssertRead(xmlReader, XmlNodeType.Element);
101 AssertRead(xmlReader, XmlNodeType.Text);
102 AssertRead(xmlReader, XmlNodeType.EndElement);
103 Assert.IsFalse(xmlReader.Read());
104 }*/
105
106 DumpJsonParse("\"text value\"");
107 DumpJsonParse("null");
108 DumpJsonParse("true");
109 DumpJsonParse("{}");
110 DumpJsonParse("[]");
111 DumpJsonParse("{\"one\":1, \"two\":2}");
112 DumpJsonParse("[1,\"\",2,3]");
113 DumpJsonParse("[{\"info\": [7,8,9]}]");
114 DumpJsonFlatParse("[1,2,\"\",[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]");
115 }
116
117 void AssertRead(XmlReader reader, XmlNodeType expected) {
118 Assert.True(reader.Read());
119 Console.WriteLine($"{new string(' ', reader.Depth * 2)}{reader}");
120 Assert.Equal(expected, reader.NodeType);
121 }
122
123 void DumpJsonParse(string json) {
124 Console.WriteLine($"JSON: {json}");
125 Console.WriteLine("XML");
126 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings {
127 Indent = true,
128 CloseOutput = false,
129 ConformanceLevel = ConformanceLevel.Document
130 }))
131 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) {
132 xmlWriter.WriteNode(xmlReader, false);
133 }
134 Console.WriteLine();
135 }
136
137 void DumpJsonFlatParse(string json) {
138 Console.WriteLine($"JSON: {json}");
139 Console.WriteLine("XML");
140 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings {
141 Indent = true,
142 CloseOutput = false,
143 ConformanceLevel = ConformanceLevel.Document
144 }))
145 using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) {
146 xmlWriter.WriteNode(xmlReader, false);
147 }
148 Console.WriteLine();
149 }
150 }
151 }
152