diff 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
line wrap: on
line diff
--- a/Implab.Test/Implab.Format.Test/JsonTests.cs	Thu Mar 24 18:52:10 2016 +0300
+++ b/Implab.Test/Implab.Format.Test/JsonTests.cs	Fri Mar 25 02:49:02 2016 +0300
@@ -1,49 +1,87 @@
 using NUnit.Framework;
 using System;
 using Implab.Formats.JSON;
+using Implab.Automaton;
 
 namespace Implab.Format.Test {
     [TestFixture]
     public class JsonTests {
         [Test]
         public void TestScannerValidTokens() {
-            var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:");
+            using (var scanner = new JSONScanner(@"9123, -123, 0, 0.1, -0.2, -0.1e3, 1.3E-3, ""some \t\n\u0020 text"", literal []{}:")) {
+
+                Tuple<JsonTokenType,object>[] expexted = new [] {
+                    new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n  text"),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", "),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal"),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, " ["),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, "]"),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, "{"),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, "}"),
+                    new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, ":")
+                };
 
-            Tuple<JsonTokenType,object>[] expexted =  new [] {
-                new Tuple<JsonTokenType,object>(JsonTokenType.Number, 9123d),
-                new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.Number, -123d ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0d ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.Number, 0.1d ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.2d ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.Number, -0.1e3d ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.Number, 1.3E-3d ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.String, "some \t\n  text" ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.ValueSeparator, ", " ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.Literal, "literal" ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.BeginArray, " [" ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.EndArray, "]" ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.BeginObject, "{" ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.EndObject, "}" ),
-                new Tuple<JsonTokenType,object>(JsonTokenType.NameSeparator, ":" )
+                object value;
+                JsonTokenType tokenType;
+                for (var i = 0; i < expexted.Length; i++) {
+                
+                    Assert.IsTrue(scanner.ReadToken(out value, out tokenType));
+                    Assert.AreEqual(expexted[i].Item1, tokenType);
+                    Assert.AreEqual(expexted[i].Item2, value);
+                }
+
+                Assert.IsFalse(scanner.ReadToken(out value, out tokenType));
+            }
+        }
+
+        [Test]
+        public void TestScannerBadTokens() {
+            var bad = new [] {
+                " 1",
+                " literal",
+                " \"",
+                "\"unclosed string",
+                "1.bad",
+                "001", // should be read as three numbers
+                "--10",
+                "+10",
+                "1.0.0",
+                "1e1.0",
+                "l1teral0",
+                ".123",
+                "-.123"
             };
 
-            object value;
-            JsonTokenType tokenType;
-            for (var i = 0; i < expexted.Length; i++) {
-                
-                Assert.IsTrue(scanner.ReadToken(out value, out tokenType));
-                Assert.AreEqual(expexted[i].Item1, tokenType);
-                Assert.AreEqual(expexted[i].Item2, value);
-            }
-
-            Assert.IsFalse(scanner.ReadToken(out value, out tokenType));
+            foreach (var json in bad)
+                using (var scanner = new JSONScanner(json)) {
+                    try {
+                        object value;
+                        JsonTokenType token;
+                        scanner.ReadToken(out value, out token);
+                        if (!Object.Equals(value,json)) {
+                            Console.WriteLine("'{0}' is read as {1}", json, value is String ? String.Format("'{0}'", value) : value );
+                            continue;
+                        }
+                        Assert.Fail("Token '{0}' shouldn't pass", json);
+                    } catch (ParserException e) {
+                        Console.WriteLine(e.Message);
+                    }
+                }
+            
         }
     }
 }