Browse Source

More test cases + bug fixes

Brad Robinson 11 years ago
parent
commit
73aa54a938
3 changed files with 185 additions and 15 deletions
  1. 32 15
      PetaJson.cs
  2. 1 0
      TestCases/TestCases.csproj
  3. 152 0
      TestCases/TestPrimitiveTypes.cs

+ 32 - 15
PetaJson.cs

@@ -488,21 +488,35 @@ namespace PetaJson
                     return reader.ReadLiteral(literal => Convert.ChangeType(literal, type, CultureInfo.InvariantCulture));
                 };
 
+                Func<IJsonReader, Type, object> numberConverter = (reader, type) =>
+                {
+                    switch (reader.GetLiteralKind())
+                    {
+                        case LiteralKind.SignedInteger:
+                        case LiteralKind.UnsignedInteger:
+                        case LiteralKind.FloatingPoint:
+                            object val = Convert.ChangeType(reader.GetLiteralString(), type, CultureInfo.InvariantCulture);
+                            reader.NextToken();
+                            return val;
+                    }
+                    throw new InvalidDataException("expected a numeric literal");
+                };
+
                 // Default type handlers
                 _parsers.Set(typeof(string), simpleConverter);
                 _parsers.Set(typeof(char), simpleConverter);
                 _parsers.Set(typeof(bool), simpleConverter);
-                _parsers.Set(typeof(byte), simpleConverter);
-                _parsers.Set(typeof(sbyte), simpleConverter);
-                _parsers.Set(typeof(short), simpleConverter);
-                _parsers.Set(typeof(ushort), simpleConverter);
-                _parsers.Set(typeof(int), simpleConverter);
-                _parsers.Set(typeof(uint), simpleConverter);
-                _parsers.Set(typeof(long), simpleConverter);
-                _parsers.Set(typeof(ulong), simpleConverter);
-                _parsers.Set(typeof(decimal), simpleConverter);
-                _parsers.Set(typeof(float), simpleConverter);
-                _parsers.Set(typeof(double), simpleConverter);
+                _parsers.Set(typeof(byte), numberConverter);
+                _parsers.Set(typeof(sbyte), numberConverter);
+                _parsers.Set(typeof(short), numberConverter);
+                _parsers.Set(typeof(ushort), numberConverter);
+                _parsers.Set(typeof(int), numberConverter);
+                _parsers.Set(typeof(uint), numberConverter);
+                _parsers.Set(typeof(long), numberConverter);
+                _parsers.Set(typeof(ulong), numberConverter);
+                _parsers.Set(typeof(decimal), numberConverter);
+                _parsers.Set(typeof(float), numberConverter);
+                _parsers.Set(typeof(double), numberConverter);
                 _parsers.Set(typeof(DateTime), (reader, type) =>
                 {
                     return reader.ReadLiteral(literal => Utils.FromUnixMilliseconds((long)Convert.ChangeType(literal, typeof(long), CultureInfo.InvariantCulture)));
@@ -2142,7 +2156,7 @@ namespace PetaJson
 
                 // Theses types we also generate for
                 var otherSupportedTypes = new Type[] {
-                    typeof(double), typeof(float), typeof(string), typeof(char), typeof(bool)
+                    typeof(double), typeof(float), typeof(string), typeof(char)
                 };
 
                 // Call IJsonWriting if implemented
@@ -2778,9 +2792,12 @@ namespace PetaJson
             // Helper to fetch a literal string from an IJsonReader
             public static string GetLiteralString(IJsonReader r)
             {
-                if (r.GetLiteralKind() != LiteralKind.String)
-                    throw new InvalidDataException("expected a string literal");
-                return r.GetLiteralString();
+                switch (r.GetLiteralKind())
+                {
+                    case LiteralKind.Null: return null;
+                    case LiteralKind.String: return r.GetLiteralString();
+                }
+                throw new InvalidDataException("expected a string literal");
             }
 
             // Helper to fetch a literal number from an IJsonReader (returns the raw string)

+ 1 - 0
TestCases/TestCases.csproj

@@ -53,6 +53,7 @@
     <Compile Include="TestAbstractTypes.cs" />
     <Compile Include="TestCustomFormat.cs" />
     <Compile Include="TestOptions.cs" />
+    <Compile Include="TestPrimitiveTypes.cs" />
     <Compile Include="TestsEvents.cs" />
     <Compile Include="TestsGeneral.cs" />
     <Compile Include="TestsReflection.cs" />

+ 152 - 0
TestCases/TestPrimitiveTypes.cs

@@ -0,0 +1,152 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using PetaTest;
+using PetaJson;
+
+namespace TestCases
+{
+    [Json]
+    class AllTypes
+    {
+        public string String;
+        public char Char;
+        public bool Bool;
+        public byte Byte;
+        public sbyte SByte;
+        public short Short;
+        public ushort UShort;
+        public int Int;
+        public uint UInt;
+        public long Long;
+        public ulong ULong;
+        public decimal Decimal;
+        public float Float;
+        public double Double;
+        public DateTime DateTime;
+        public byte[] Blob;
+
+        public void Init()
+        {
+            String = "PetaJson!";
+            Char = 'J';
+            Bool = false;
+            Byte = 1;
+            SByte = 2;
+            Short = 3;
+            UShort = 4;
+            Int = 5;
+            UInt = 6;
+            Long = 7;
+            ULong = 8;
+            Decimal = 9.1M;
+            Float = 10.2f;
+            Double = 11.3;
+            DateTime = new DateTime(2014, 1, 1, 13, 23, 24);
+            Blob = new byte[] { 12, 13, 14, 15 };
+        }
+    }
+
+    [TestFixture]
+    public class TestPrimitiveTypes
+    {
+        void Compare(AllTypes all, AllTypes all2)
+        {
+            Assert.AreEqual(all.String, all2.String);
+            Assert.AreEqual(all.Char, all2.Char);
+            Assert.AreEqual(all.Bool, all2.Bool);
+            Assert.AreEqual(all.Byte, all2.Byte);
+            Assert.AreEqual(all.SByte, all2.SByte);
+            Assert.AreEqual(all.Short, all2.Short);
+            Assert.AreEqual(all.UShort, all2.UShort);
+            Assert.AreEqual(all.Int, all2.Int);
+            Assert.AreEqual(all.UInt, all2.UInt);
+            Assert.AreEqual(all.Long, all2.Long);
+            Assert.AreEqual(all.ULong, all2.ULong);
+            Assert.AreEqual(all.Decimal, all2.Decimal);
+            Assert.AreEqual(all.Float, all2.Float);
+            Assert.AreEqual(all.Double, all2.Double);
+            Assert.AreEqual(all.DateTime, all2.DateTime);
+            Assert.IsTrue((all.Blob==null && all2.Blob==null) || Convert.ToBase64String(all.Blob)==Convert.ToBase64String(all2.Blob));
+        }
+
+        [Test]
+        public void TestBasics()
+        {
+            var all = new AllTypes();
+            all.Init();
+            var json = Json.Format(all);
+            var all2 = Json.Parse<AllTypes>(json);
+
+            Compare(all, all2);
+        }
+
+        [Test]
+        public void TestNegatives()
+        {
+            var all = new AllTypes();
+            all.Init();
+            all.SByte = -1;
+            all.Short = -2;
+            all.Int = -3;
+            all.Long = -4;
+            all.Decimal = -5.1M;
+            all.Float = -6.2f;
+            all.Double = -7.3;
+
+            var json = Json.Format(all);
+            var all2 = Json.Parse<AllTypes>(json);
+            Compare(all, all2);
+        }
+
+        [Test]
+        public void TestMaxValue()
+        {
+            var all = new AllTypes();
+            all.Init();
+            all.Bool = true;
+            all.Byte = Byte.MaxValue;
+            all.SByte = SByte.MaxValue;
+            all.Short = short.MaxValue;
+            all.UShort = ushort.MaxValue;
+            all.Int = int.MaxValue;
+            all.UInt = uint.MaxValue;
+            all.Long = long.MaxValue;
+            all.ULong = ulong.MaxValue;
+            all.Decimal = decimal.MaxValue;
+            all.Float = float.MaxValue;
+            all.Double = double.MaxValue;
+
+            var json = Json.Format(all);
+            Console.WriteLine(json);
+            var all2 = Json.Parse<AllTypes>(json);
+            Compare(all, all2);
+        }
+
+        [Test]
+        public void TestMinValue()
+        {
+            var all = new AllTypes();
+            all.String = null;
+            all.Bool = false;
+            all.Byte = Byte.MinValue;
+            all.SByte = SByte.MinValue;
+            all.Short = short.MinValue;
+            all.UShort = ushort.MinValue;
+            all.Int = int.MinValue;
+            all.UInt = uint.MinValue;
+            all.Long = long.MinValue;
+            all.ULong = ulong.MinValue;
+            all.Decimal = decimal.MinValue;
+            all.Float = float.MinValue;
+            all.Double = double.MinValue;
+            all.Blob = null;
+
+            var json = Json.Format(all);
+            Console.WriteLine(json);
+            var all2 = Json.Parse<AllTypes>(json);
+            Compare(all, all2);
+        }
+    }
+}