Mercurial > pub > ImplabNet
annotate 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 |
rev | line source |
---|---|
262 | 1 using Xunit; |
158 | 2 using System; |
183 | 3 using Implab.Automaton; |
262 | 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] | |
182 | 14 public void TestScannerValidTokens() { |
228 | 15 using (var scanner = JsonStringScanner.Create(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) { |
183 | 16 |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
17 Tuple<JsonTokenType, object>[] expexted = { |
229 | 18 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "9123"), |
228 | 19 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 20 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-123"), |
228 | 21 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 22 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0"), |
228 | 23 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 24 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "0.1"), |
228 | 25 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 26 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.2"), |
228 | 27 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 28 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "-0.1e3"), |
228 | 29 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
229 | 30 new Tuple<JsonTokenType,object>(JsonTokenType.Number, "1.3E-3"), |
228 | 31 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 32 new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n text"), |
228 | 33 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 34 new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"), |
228 | 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) | |
183 | 40 }; |
182 | 41 |
229 | 42 string value; |
183 | 43 JsonTokenType tokenType; |
262 | 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); | |
183 | 49 } |
50 | |
262 | 51 Assert.False(scanner.ReadToken(out value, out tokenType)); |
183 | 52 } |
53 } | |
54 | |
262 | 55 [Fact] |
183 | 56 public void TestScannerBadTokens() { |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
57 var bad = new[] { |
183 | 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" | |
182 | 71 }; |
72 | |
262 | 73 foreach (var json in bad) { |
228 | 74 using (var scanner = JsonStringScanner.Create(json)) { |
183 | 75 try { |
229 | 76 string value; |
183 | 77 JsonTokenType token; |
78 scanner.ReadToken(out value, out token); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
79 if (!Object.Equals(value, json)) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
80 Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value); |
183 | 81 continue; |
82 } | |
262 | 83 Assert.True(false, $"Token '{json}' shouldn't pass"); |
183 | 84 } catch (ParserException e) { |
85 Console.WriteLine(e.Message); | |
86 } | |
262 | 87 } |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
88 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
89 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
90 |
262 | 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]]"); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
115 } |
236 | 116 |
262 | 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); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
121 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
122 |
262 | 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(); | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
135 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
136 |
262 | 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(); | |
158 | 149 } |
150 } | |
151 } | |
152 |