[Test] public void TestImplicitExpr() { Assert.AreEqual(LongV.Of(10), (Expr)10); Assert.AreEqual(LongV.Of(10), (Expr)10L); Assert.AreEqual(BooleanV.True, (Expr)true); Assert.AreEqual(BooleanV.False, (Expr)false); Assert.AreEqual(DoubleV.Of(3.14), (Expr)3.14); Assert.AreEqual(StringV.Of("a string"), (Expr)"a string"); Assert.AreEqual(StringV.Of("create"), (Expr)ActionType.Create); Assert.AreEqual(StringV.Of("delete"), (Expr)ActionType.Delete); Assert.AreEqual(StringV.Of("second"), (Expr)TimeUnit.Second); Assert.AreEqual(StringV.Of("millisecond"), (Expr)TimeUnit.Millisecond); Assert.AreEqual(StringV.Of("microsecond"), (Expr)TimeUnit.Microsecond); Assert.AreEqual(StringV.Of("nanosecond"), (Expr)TimeUnit.Nanosecond); Assert.AreEqual(DateV.Of("2000-01-01"), (Expr) new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc)); Assert.AreEqual(DateV.Of("2000-01-01"), (Expr) new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero)); Assert.AreEqual(TimeV.Of("2000-01-01T01:01:01.123Z"), (Expr) new DateTime(2000, 1, 1, 1, 1, 1, 123, DateTimeKind.Utc)); Assert.AreEqual(TimeV.Of("2000-01-01T01:01:01.123Z"), (Expr) new DateTimeOffset(2000, 1, 1, 1, 1, 1, 123, TimeSpan.Zero)); Assert.AreEqual(NullV.Instance, (Expr)(string)null); Assert.AreEqual( ObjectV.With("name", "foo", "count", 42), (Expr) new Dictionary <string, Expr>() { { "name", "foo" }, { "count", 42 } }); }
[Test] public void TestArray() { AssertJsonEqual(ArrayV.Of(LongV.Of(1), StringV.Of("a string"), DoubleV.Of(3.14), BooleanV.True, NullV.Instance), "[1, \"a string\", 3.14, true, null]"); AssertJsonEqual(ArrayV.Empty, "[]"); }
public void TestPrimitive() { Assert.AreEqual(StringV.Of("a string"), Encode("a string")); Assert.AreEqual(BooleanV.True, Encode(true)); Assert.AreEqual(BooleanV.False, Encode(false)); Assert.AreEqual(NullV.Instance, Encode((object)null)); Assert.AreEqual(new BytesV(1, 2, 3, 4), Encode(new byte[] { 1, 2, 3, 4 })); Assert.AreEqual(new DateV("2001-01-01"), Encode(new DateTime(2001, 1, 1))); Assert.AreEqual(new TimeV("2000-01-01T01:10:30.123Z"), Encode(new DateTime(2000, 1, 1, 1, 10, 30, 123))); Assert.AreEqual(new DateV("2001-01-01"), Encode(new DateTimeOffset(2001, 1, 1, 0, 0, 0, TimeSpan.Zero))); Assert.AreEqual(new TimeV("2000-01-01T01:10:30.123Z"), Encode(new DateTimeOffset(2000, 1, 1, 1, 10, 30, 123, TimeSpan.Zero))); //float point Assert.AreEqual(DoubleV.Of((float)3.14), Encode((float)3.14)); Assert.AreEqual(DoubleV.Of(3.14), Encode((double)3.14)); Assert.AreEqual(DoubleV.Of(3.14), Encode((decimal)3.14)); //signed values Assert.AreEqual(LongV.Of(10), Encode((sbyte)10)); Assert.AreEqual(LongV.Of(10), Encode((short)10)); Assert.AreEqual(LongV.Of(10), Encode((int)10)); Assert.AreEqual(LongV.Of(10), Encode((long)10)); //unsigned values Assert.AreEqual(LongV.Of(10), Encode((char)10)); Assert.AreEqual(LongV.Of(10), Encode((byte)10)); Assert.AreEqual(LongV.Of(10), Encode((ushort)10)); Assert.AreEqual(LongV.Of(10), Encode((uint)10)); Assert.AreEqual(LongV.Of(10), Encode((ulong)10)); }
[Test] public void TestExplicit() { Assert.AreEqual(10, (int)LongV.Of(10)); Assert.AreEqual(10L, (long)LongV.Of(10)); Assert.AreEqual(true, (bool)BooleanV.True); Assert.AreEqual(false, (bool)BooleanV.False); Assert.AreEqual(3.14, (double)DoubleV.Of(3.14)); Assert.AreEqual("a string", (string)StringV.Of("a string")); Assert.AreEqual(ActionType.Create, (ActionType)StringV.Of("create")); Assert.AreEqual(ActionType.Delete, (ActionType)StringV.Of("delete")); Assert.AreEqual(TimeUnit.Second, (TimeUnit)StringV.Of("second")); Assert.AreEqual(TimeUnit.Millisecond, (TimeUnit)StringV.Of("millisecond")); Assert.AreEqual(TimeUnit.Microsecond, (TimeUnit)StringV.Of("microsecond")); Assert.AreEqual(TimeUnit.Nanosecond, (TimeUnit)StringV.Of("nanosecond")); Assert.AreEqual(null, (string)(Expr)NullV.Instance); }
[Test] public void TestImplicit() { Assert.AreEqual(LongV.Of(10), (Expr)10); Assert.AreEqual(LongV.Of(10), (Expr)10L); Assert.AreEqual(BooleanV.True, (Expr)true); Assert.AreEqual(BooleanV.False, (Expr)false); Assert.AreEqual(DoubleV.Of(3.14), (Expr)3.14); Assert.AreEqual(StringV.Of("a string"), (Expr)"a string"); Assert.AreEqual(StringV.Of("create"), (Expr)ActionType.Create); Assert.AreEqual(StringV.Of("delete"), (Expr)ActionType.Delete); Assert.AreEqual(StringV.Of("second"), (Expr)TimeUnit.Second); Assert.AreEqual(StringV.Of("millisecond"), (Expr)TimeUnit.Millisecond); Assert.AreEqual(StringV.Of("microsecond"), (Expr)TimeUnit.Microsecond); Assert.AreEqual(StringV.Of("nanosecond"), (Expr)TimeUnit.Nanosecond); Assert.AreEqual(NullV.Instance, (Expr)(string)null); }
[Test] public void TestNestedArray() { var nested = ArrayV.Of(10, ArrayV.Of(1234, ArrayV.Of(4321))); Assert.AreEqual(LongV.Of(10), nested.Get(Field.At(0))); Assert.AreEqual(LongV.Of(1234), nested.Get(Field.At(1, 0))); Assert.AreEqual(LongV.Of(4321), nested.Get(Field.At(1, 1, 0))); Assert.Throws(typeof(InvalidOperationException), () => nested.Get(Field.At(1, 1, 1)), "Cannot find path \"1/1/1\". Array index \"1\" not found"); }
[Test] public void TestArrayIndex() { var array = ArrayV.Of("a string", 10); Assert.AreEqual(StringV.Of("a string"), array.Get(Field.At(0))); Assert.AreEqual(LongV.Of(10), array.Get(Field.At(1))); Assert.AreEqual(None(), array.GetOption(Field.At(1234))); Assert.Throws(typeof(InvalidOperationException), () => array.Get(Field.At(1234)), "Cannot find path \"1234\". Array index \"1234\" not found"); }
[Test] public void TestExplicitValue() { Assert.AreEqual(10, (int)LongV.Of(10)); Assert.AreEqual(10L, (long)LongV.Of(10)); Assert.AreEqual(true, (bool)BooleanV.True); Assert.AreEqual(false, (bool)BooleanV.False); Assert.AreEqual(3.14, (double)DoubleV.Of(3.14)); Assert.AreEqual("a string", (string)StringV.Of("a string")); Assert.AreEqual(ActionType.Create, (ActionType)StringV.Of("create")); Assert.AreEqual(ActionType.Delete, (ActionType)StringV.Of("delete")); Assert.AreEqual(TimeUnit.Second, (TimeUnit)StringV.Of("second")); Assert.AreEqual(TimeUnit.Millisecond, (TimeUnit)StringV.Of("millisecond")); Assert.AreEqual(TimeUnit.Microsecond, (TimeUnit)StringV.Of("microsecond")); Assert.AreEqual(TimeUnit.Nanosecond, (TimeUnit)StringV.Of("nanosecond")); Assert.AreEqual(new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc), (DateTime)DateV.Of("2000-01-01")); Assert.AreEqual(new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero), (DateTimeOffset)DateV.Of("2000-01-01")); Assert.AreEqual(new DateTime(2000, 1, 1, 1, 1, 1, 123, DateTimeKind.Utc), (DateTime)TimeV.Of("2000-01-01T01:01:01.123Z")); Assert.AreEqual(new DateTimeOffset(2000, 1, 1, 1, 1, 1, 123, TimeSpan.Zero), (DateTimeOffset)TimeV.Of("2000-01-01T01:01:01.123Z")); }
public void TestImplicitValue() { Assert.AreEqual(LongV.Of(10), (Value)10); Assert.AreEqual(LongV.Of(10), (Value)10L); Assert.AreEqual(BooleanV.True, (Value)true); Assert.AreEqual(BooleanV.False, (Value)false); Assert.AreEqual(DoubleV.Of(3.14), (Value)3.14); Assert.AreEqual(StringV.Of("a string"), (Value)"a string"); Assert.AreEqual(StringV.Of("create"), (Value)ActionType.Create); Assert.AreEqual(StringV.Of("delete"), (Value)ActionType.Delete); Assert.AreEqual(StringV.Of("second"), (Value)TimeUnit.Second); Assert.AreEqual(StringV.Of("millisecond"), (Value)TimeUnit.Millisecond); Assert.AreEqual(StringV.Of("microsecond"), (Value)TimeUnit.Microsecond); Assert.AreEqual(StringV.Of("nanosecond"), (Value)TimeUnit.Nanosecond); Assert.AreEqual(DateV.Of("2000-01-01"), (Value) new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc)); Assert.AreEqual(DateV.Of("2000-01-01"), (Value) new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero)); Assert.AreEqual(TimeV.Of("2000-01-01T01:01:01.123Z"), (Value) new DateTime(2000, 1, 1, 1, 1, 1, 123, DateTimeKind.Utc)); Assert.AreEqual(TimeV.Of("2000-01-01T01:01:01.123Z"), (Value) new DateTimeOffset(2000, 1, 1, 1, 1, 1, 123, TimeSpan.Zero)); Assert.AreEqual(NullV.Instance, (Value)(string)null); }
[Test] public void TestLong() { AssertJsonEqual(LongV.Of(long.MaxValue), long.MaxValue.ToString()); AssertJsonEqual(LongV.Of(long.MinValue), long.MinValue.ToString()); AssertJsonEqual(LongV.Of(10), "10"); }
public void TestNullableFields() { var structWithNullableFields = Decode <StructWithNullableFields>(ObjectV.With( "anInteger", LongV.Of(10), "aDouble", DoubleV.Of(3.14), "aStruct", ObjectV.With("aByte", LongV.Of(10)) )); Assert.AreEqual(10, structWithNullableFields.anInteger); Assert.AreEqual(3.14, structWithNullableFields.aDouble); Assert.IsNotNull(structWithNullableFields.aStruct); Assert.AreEqual(10, structWithNullableFields.aStruct?.aByte); Assert.IsNull(structWithNullableFields.aStruct?.aShort); }