Mercurial > pub > ImplabNet
annotate 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 |
rev | line source |
---|---|
158 | 1 using NUnit.Framework; |
2 using System; | |
183 | 3 using Implab.Automaton; |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
4 using Implab.Xml; |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
5 using System.Xml; |
228 | 6 using Implab.Formats; |
7 using Implab.Formats.Json; | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
8 |
158 | 9 namespace Implab.Format.Test { |
182 | 10 [TestFixture] |
158 | 11 public class JsonTests { |
228 | 12 |
182 | 13 [Test] |
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 = { |
183 | 18 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d), |
228 | 19 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 20 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d), |
228 | 21 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 22 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d), |
228 | 23 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 24 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d), |
228 | 25 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 26 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d), |
228 | 27 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 28 new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d), |
228 | 29 new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, null), |
183 | 30 new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d), |
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 |
183 | 42 object value; |
43 JsonTokenType tokenType; | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
44 for (var i = 0; i < expexted.Length; i++) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
45 |
183 | 46 Assert.IsTrue(scanner.ReadToken(out value, out tokenType)); |
47 Assert.AreEqual(expexted[i].Item1, tokenType); | |
48 Assert.AreEqual(expexted[i].Item2, value); | |
49 } | |
50 | |
51 Assert.IsFalse(scanner.ReadToken(out value, out tokenType)); | |
52 } | |
53 } | |
54 | |
55 [Test] | |
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 | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
73 foreach (var json in bad) { |
228 | 74 using (var scanner = JsonStringScanner.Create(json)) { |
183 | 75 try { |
76 object value; | |
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 } | |
83 Assert.Fail("Token '{0}' shouldn't pass", json); | |
84 } catch (ParserException e) { | |
85 Console.WriteLine(e.Message); | |
86 } | |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
87 } |
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 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
91 [Test] |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
92 public void JsonXmlReaderSimpleTest() { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
93 var json = "\"some text\""; |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
94 //Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
95 //Console.WriteLine("XML"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
96 /*using (var xmlReader = new JsonXmlReader(new JSONParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "string", NodesPrefix = "json" })) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
97 Assert.AreEqual(xmlReader.ReadState, System.Xml.ReadState.Initial); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
98 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
99 AssertRead(xmlReader, XmlNodeType.XmlDeclaration); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
100 AssertRead(xmlReader, XmlNodeType.Element); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
101 AssertRead(xmlReader, XmlNodeType.Text); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
102 AssertRead(xmlReader, XmlNodeType.EndElement); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
103 Assert.IsFalse(xmlReader.Read()); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
104 }*/ |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
105 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
106 //DumpJsonParse("\"text value\""); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
107 //DumpJsonParse("null"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
108 //DumpJsonParse("true"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
109 //DumpJsonParse("{}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
110 //DumpJsonParse("[]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
111 DumpJsonParse("{\"one\":1, \"two\":2}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
112 DumpJsonParse("[1,2,3]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
113 DumpJsonParse("[{\"info\": [7,8,9]}]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
114 DumpJsonFlatParse("[1,2,[3,4],{\"info\": [5,6]},{\"num\": [7,8,null]}, null,[null]]"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
115 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
116 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
117 void AssertRead(XmlReader reader, XmlNodeType expected) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
118 Assert.IsTrue(reader.Read()); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
119 Console.WriteLine($"{new string(' ', reader.Depth*2)}{reader}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
120 Assert.AreEqual(expected, reader.NodeType); |
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 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
123 void DumpJsonParse(string json) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
124 Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
125 Console.WriteLine("XML"); |
228 | 126 using (var xmlReader = new JsonXmlReader(new JsonParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
127 while (xmlReader.Read()) |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
128 Console.WriteLine($"{new string(' ', xmlReader.Depth * 2)}{xmlReader}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
129 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
130 } |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
131 |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
132 void DumpJsonFlatParse(string json) { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
133 Console.WriteLine($"JSON: {json}"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
134 Console.WriteLine("XML"); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
135 using (var xmlWriter = XmlWriter.Create(Console.Out, new XmlWriterSettings { |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
136 Indent = true, |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
137 CloseOutput = false, |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
138 ConformanceLevel = ConformanceLevel.Document |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
139 })) |
228 | 140 using (var xmlReader = new JsonXmlReader(new JsonParser(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "", FlattenArrays = true })) { |
227
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
141 xmlWriter.WriteNode(xmlReader, false); |
8d5de4eb9c2c
Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents:
184
diff
changeset
|
142 } |
158 | 143 } |
144 } | |
145 } | |
146 |