Esempio n. 1
0
        public void CastTests()
        {
            int       value  = 10;
            JsonValue target = new JsonPrimitive(value);

            int v1 = JsonValue.CastValue <int>(target);

            Assert.AreEqual <int>(value, v1);
            v1 = (int)target;
            Assert.AreEqual <int>(value, v1);

            long v2 = JsonValue.CastValue <long>(target);

            Assert.AreEqual <long>(value, v2);
            v2 = (long)target;
            Assert.AreEqual <long>(value, v2);

            string s = JsonValue.CastValue <string>(target);

            Assert.AreEqual <string>(value.ToString(), s);
            s = (string)target;
            Assert.AreEqual <string>(value.ToString(), s);

            object obj = JsonValue.CastValue <object>(target);

            Assert.AreEqual(target, obj);
            obj = (object)target;
            Assert.AreEqual(target, obj);

            object nill = JsonValue.CastValue <object>(null);

            Assert.IsNull(nill);

            dynamic   dyn       = target;
            JsonValue defaultJv = dyn.IamDefault;

            nill = JsonValue.CastValue <string>(defaultJv);
            Assert.IsNull(nill);
            nill = (string)defaultJv;
            Assert.IsNull(nill);

            obj = JsonValue.CastValue <object>(defaultJv);
            Assert.AreSame(defaultJv, obj);
            obj = (object)defaultJv;
            Assert.AreSame(defaultJv, obj);

            JsonValue jv = JsonValue.CastValue <JsonValue>(target);

            Assert.AreEqual <JsonValue>(target, jv);

            jv = JsonValue.CastValue <JsonValue>(defaultJv);
            Assert.AreEqual <JsonValue>(defaultJv, jv);

            jv = JsonValue.CastValue <JsonPrimitive>(target);
            Assert.AreEqual <JsonValue>(target, jv);

            ExceptionTestHelper.ExpectException <InvalidCastException>(delegate { int i = JsonValue.CastValue <int>(null); });
            ExceptionTestHelper.ExpectException <InvalidCastException>(delegate { int i = JsonValue.CastValue <int>(defaultJv); });
            ExceptionTestHelper.ExpectException <InvalidCastException>(delegate { int i = JsonValue.CastValue <char>(target); });
        }
Esempio n. 2
0
        public void JsonArrayConstructorParamsTest()
        {
            JsonArray target;

            target = new JsonArray();
            Assert.AreEqual(0, target.Count);

            target = new JsonArray(null);
            Assert.AreEqual(0, target.Count);

            List <JsonValue> items = new List <JsonValue> {
                AnyInstance.AnyJsonValue1, AnyInstance.AnyJsonValue2
            };

            target = new JsonArray(items.ToArray());
            ValidateJsonArrayItems(target, items);

            target = new JsonArray(items[0], items[1]);
            ValidateJsonArrayItems(target, items);

            // Invalide tests
            items.Add(AnyInstance.DefaultJsonValue);
            ExceptionTestHelper.ExpectException <ArgumentException>(() => new JsonArray(items.ToArray()));
            ExceptionTestHelper.ExpectException <ArgumentException>(() => new JsonArray(items[0], items[1], items[2]));
        }
Esempio n. 3
0
        public void InvalidBooleanOperatorsTest()
        {
            dynamic dyn = (JsonValue)true;

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn &= false; }, string.Format(OperationNotDefinedMsgFormat, "AndAssign", "Boolean"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn |= false; }, string.Format(OperationNotDefinedMsgFormat, "OrAssign", "Boolean"));
        }
Esempio n. 4
0
        public void ValidationAPITest()
        {
            JsonObject jo = new JsonObject();

            jo.Add("date", new DateTime(2000, 1, 1, 0, 0, 0));
            jo.Add("int", 1);
            jo.Add("double", 1.1);
            jo.Add("string", "12CharString");
            jo.Add("enum", "Number");

            jo.ValidatePresence("date")
            .ValidateEnum("enum", typeof(JsonType))
            .ValidateRange("double", 0.1, 1.2)
            .ValidateRange("int", 0, 2)
            .ValidateRange <DateTime>("date", DateTime.MinValue, DateTime.MaxValue)
            .ValidateRegularExpression("int", "^.+$")
            .ValidateStringLength("string", 15)
            .ValidateStringLength("string", 0, 15)
            .ValidateTypeOf <double>("int")
            .ValidateCustomValidator("string", typeof(MyCustomValidationClass), "IsStringContainsCharSimple")
            .ValidateCustomValidator("string", typeof(MyCustomValidationClass), "IsStringContainsCharComplex");

            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidatePresence("invalidkey"); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateEnum("date", typeof(JsonType)); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateRange("double", 2.2, 3.2); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateRange("int", 2, 3); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateRange <DateTime>("date", DateTime.MaxValue, DateTime.MaxValue); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateRegularExpression("string", "doesnotmatch"); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateStringLength("string", 10); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateStringLength("string", 15, 25); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateTypeOf <double>("date"); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateCustomValidator("enum", typeof(MyCustomValidationClass), "IsStringContainsCharSimple"); });
            ExceptionTestHelper.ExpectException <ValidationException>(delegate { jo.ValidateCustomValidator("enum", typeof(MyCustomValidationClass), "IsStringContainsCharComplex"); });
        }
        public void LegacyArraysTest()
        {
            ValidateFormsEncodingParsing("a=1&a=hello&a=333", "{\"a\":[\"1\",\"hello\",\"333\"]}");

            // Only valid in shallow serialization
            ExceptionTestHelper.ExpectException <ArgumentException>(() => FormUrlEncodedExtensions.ParseFormUrlEncoded("a[z]=2&a[z]=3"));
        }
Esempio n. 6
0
        public void JsonObjectConstructorParmsTest()
        {
            JsonObject target = new JsonObject();

            Assert.AreEqual(0, target.Count);

            string    key1   = AnyInstance.AnyString;
            string    key2   = AnyInstance.AnyString2;
            JsonValue value1 = AnyInstance.AnyJsonValue1;
            JsonValue value2 = AnyInstance.AnyJsonValue2;

            List <KeyValuePair <string, JsonValue> > items = new List <KeyValuePair <string, JsonValue> >()
            {
                new KeyValuePair <string, JsonValue>(key1, value1),
                new KeyValuePair <string, JsonValue>(key2, value2),
            };

            target = new JsonObject(items[0], items[1]);
            Assert.AreEqual(2, target.Count);
            ValidateJsonObjectItems(target, key1, value1, key2, value2);

            target = new JsonObject(items.ToArray());
            Assert.AreEqual(2, target.Count);
            ValidateJsonObjectItems(target, key1, value1, key2, value2);

            // Invalid tests
            items.Add(new KeyValuePair <string, JsonValue>(key1, AnyInstance.DefaultJsonValue));
            ExceptionTestHelper.ExpectException <ArgumentException>(delegate { new JsonObject(items[0], items[1], items[2]); });
            ExceptionTestHelper.ExpectException <ArgumentException>(delegate { new JsonObject(items.ToArray()); });
        }
Esempio n. 7
0
        public void AddTest()
        {
            string    key1   = AnyInstance.AnyString;
            string    key2   = AnyInstance.AnyString2;
            JsonValue value1 = AnyInstance.AnyJsonValue1;
            JsonValue value2 = AnyInstance.AnyJsonValue2;

            JsonObject target;

            target = new JsonObject();
            target.Add(new KeyValuePair <string, JsonValue>(key1, value1));
            Assert.AreEqual(1, target.Count);
            Assert.IsTrue(target.ContainsKey(key1));
            Assert.AreEqual(value1, target[key1]);

            target.Add(key2, value2);
            Assert.AreEqual(2, target.Count);
            Assert.IsTrue(target.ContainsKey(key2));
            Assert.AreEqual(value2, target[key2]);

            ExceptionTestHelper.ExpectException <ArgumentNullException>(delegate { new JsonObject().Add(null, value1); });
            ExceptionTestHelper.ExpectException <ArgumentNullException>(delegate { new JsonObject().Add(new KeyValuePair <string, JsonValue>(null, value1)); });

            ExceptionTestHelper.ExpectException <ArgumentException>(delegate { new JsonObject().Add(key1, AnyInstance.DefaultJsonValue); });
            ExceptionTestHelper.ExpectException <ArgumentException>(delegate { new JsonArray().Add(AnyInstance.DefaultJsonValue); });
        }
Esempio n. 8
0
        public void AddRangeEnumTest()
        {
            string    key1   = AnyInstance.AnyString;
            string    key2   = AnyInstance.AnyString2;
            JsonValue value1 = AnyInstance.AnyJsonValue1;
            JsonValue value2 = AnyInstance.AnyJsonValue2;

            List <KeyValuePair <string, JsonValue> > items = new List <KeyValuePair <string, JsonValue> >()
            {
                new KeyValuePair <string, JsonValue>(key1, value1),
                new KeyValuePair <string, JsonValue>(key2, value2),
            };

            JsonObject target;

            target = new JsonObject();
            target.AddRange(items);
            Assert.AreEqual(2, target.Count);
            ValidateJsonObjectItems(target, key1, value1, key2, value2);

            ExceptionTestHelper.ExpectException <ArgumentNullException>(delegate { new JsonObject().AddRange(null); });

            items[1] = new KeyValuePair <string, JsonValue>(key2, AnyInstance.DefaultJsonValue);
            ExceptionTestHelper.ExpectException <ArgumentException>(delegate { new JsonObject().AddRange(items); });
        }
Esempio n. 9
0
        public void CopyToTest()
        {
            string    key1   = AnyInstance.AnyString;
            string    key2   = AnyInstance.AnyString2;
            JsonValue value1 = AnyInstance.AnyJsonValue1;
            JsonValue value2 = AnyInstance.AnyJsonValue2;

            JsonObject target = new JsonObject {
                { key1, value1 }, { key2, value2 }
            };

            KeyValuePair <string, JsonValue>[] array = new KeyValuePair <string, JsonValue> [target.Count + 1];

            target.CopyTo(array, 1);
            int index1 = key1 == array[1].Key ? 1 : 2;
            int index2 = index1 == 1 ? 2 : 1;

            Assert.AreEqual(key1, array[index1].Key);
            Assert.AreEqual(value1, array[index1].Value);
            Assert.AreEqual(key2, array[index2].Key);
            Assert.AreEqual(value2, array[index2].Value);

            ExceptionTestHelper.ExpectException <ArgumentNullException>(() => target.CopyTo(null, 0));
            ExceptionTestHelper.ExpectException <ArgumentOutOfRangeException>(() => target.CopyTo(array, -1));
            ExceptionTestHelper.ExpectException <ArgumentException>(() => target.CopyTo(array, array.Length - target.Count + 1));
        }
Esempio n. 10
0
        public void InvalidBitwiseOperatorsTest()
        {
            dynamic dyn = (JsonValue)0x0010;

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn |= 0x0001; }, string.Format(OperationNotDefinedMsgFormat, "OrAssign", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn &= 0x0001; }, string.Format(OperationNotDefinedMsgFormat, "AndAssign", "Number"));
        }
Esempio n. 11
0
        public void InvalidShiftOperatorsTests()
        {
            dynamic dyn = (JsonValue)0x0010;

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn <<= 2; }, string.Format(OperationNotDefinedMsgFormat, "LeftShiftAssign", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn >>= 2; }, string.Format(OperationNotDefinedMsgFormat, "RightShiftAssign", "Number"));
        }
Esempio n. 12
0
        public void JsonCollectionsInvalidOperationsTest()
        {
            dynamic[] values = { AnyInstance.AnyJsonArray, AnyInstance.AnyJsonObject };

            for (int i = 0; i < values.Length; i++)
            {
                dynamic jv = values[i];

                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv++; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv--; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv > 0; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv >= 0; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv < 0; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv <= 0; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv + 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv * 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv / 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv << 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv || 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv && 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv ^ 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv & 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv | 1; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = ~jv; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = !jv; });

                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv == "Hello"; });
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = jv != 0; });

                ExceptionTestHelper.ExpectException <RuntimeBinderException>(delegate { var v = 0 != jv; });
                ExceptionTestHelper.ExpectException <RuntimeBinderException>(delegate { var v = 0 == jv; });
            }
        }
            public static void TestBindParams(DynamicMetaObject target, DynamicMetaObject arg)
            {
                BinaryOperationBinder binder = new TestBinaryOperationBinder(ExpressionType.Equal);

                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindBinaryOperation(null, arg); });
                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindBinaryOperation(binder, null); });
            }
            public static void TestBindParams(DynamicMetaObject target, DynamicMetaObject value)
            {
                SetMemberBinder binder = new TestSetMemberBinder("AnyProperty");

                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindSetMember(null, value); });
                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindSetMember(binder, null); });
            }
Esempio n. 15
0
        public void InsertTest()
        {
            JsonValue item1  = AnyInstance.AnyJsonValue1;
            JsonValue item2  = AnyInstance.AnyJsonValue2;
            JsonValue item3  = AnyInstance.AnyJsonValue3;
            JsonArray target = new JsonArray(item1);

            Assert.AreEqual(1, target.Count);
            target.Insert(0, item2);
            Assert.AreEqual(2, target.Count);
            Assert.AreEqual(item2, target[0]);
            Assert.AreEqual(item1, target[1]);

            target.Insert(1, item3);
            Assert.AreEqual(3, target.Count);
            Assert.AreEqual(item2, target[0]);
            Assert.AreEqual(item3, target[1]);
            Assert.AreEqual(item1, target[2]);

            target.Insert(target.Count, item2);
            Assert.AreEqual(4, target.Count);
            Assert.AreEqual(item2, target[0]);
            Assert.AreEqual(item3, target[1]);
            Assert.AreEqual(item1, target[2]);
            Assert.AreEqual(item2, target[3]);

            ExceptionTestHelper.ExpectException <ArgumentOutOfRangeException>(() => target.Insert(-1, item3));
            ExceptionTestHelper.ExpectException <ArgumentOutOfRangeException>(() => target.Insert(target.Count + 1, item1));
            ExceptionTestHelper.ExpectException <ArgumentException>(() => target.Insert(0, AnyInstance.DefaultJsonValue));
        }
            public static void TestBindParams(DynamicMetaObject target)
            {
                GetIndexBinder binder         = new TestGetIndexBinder();
                Expression     typeExpression = Expression.Parameter(typeof(int));

                DynamicMetaObject[] indexes =
                {
                    new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, 0),
                    new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, 1),
                    new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, 2)
                };

                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindGetIndex(null, indexes); });
                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindGetIndex(binder, null); });

                DynamicMetaObject[][] invalidIndexesParam =
                {
                    indexes,
                    new DynamicMetaObject[] { new DynamicMetaObject(typeExpression,BindingRestrictions.Empty,                                                         null) },
                    new DynamicMetaObject[] { null },
                    new DynamicMetaObject[] { }
                };

                foreach (DynamicMetaObject[] indexesParam in invalidIndexesParam)
                {
                    DynamicMetaObject metaObj = target.BindGetIndex(binder, indexesParam);

                    Expression <Action> expression = Expression.Lambda <Action>(Expression.Block(metaObj.Expression), new ParameterExpression[] { });
                    ExceptionTestHelper.ExpectException <ArgumentException>(() => { expression.Compile().Invoke(); }, NonSingleNonNullIndexNotSupported);
                }
            }
Esempio n. 17
0
        public void LoadTest()
        {
            string json = "{\"a\":123,\"b\":[false,null,12.34]}";

            foreach (bool useLoadTextReader in new bool[] { false, true })
            {
                JsonValue jv;
                if (useLoadTextReader)
                {
                    using (StringReader sr = new StringReader(json))
                    {
                        jv = JsonValue.Load(sr);
                    }
                }
                else
                {
                    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                    {
                        jv = JsonValue.Load(ms);
                    }
                }

                Assert.AreEqual(json, jv.ToString());
            }

            ExceptionTestHelper.ExpectException <ArgumentNullException>(() => JsonValue.Load((Stream)null));
            ExceptionTestHelper.ExpectException <ArgumentNullException>(() => JsonValue.Load((TextReader)null));
        }
            public static void TestMetaObject(DynamicMetaObject target, Type type, bool isValid = true)
            {
                ConvertBinder     binder = new TestConvertBinder(type);
                DynamicMetaObject result = target.BindConvert(binder);

                Assert.IsNotNull(result);

                // Convert expression
                UnaryExpression expression = result.Expression as UnaryExpression;

                Assert.IsNotNull(expression);
                Assert.AreEqual <Type>(binder.Type, expression.Type);

                if (isValid)
                {
                    MethodCallExpression methodCallExp = expression.Operand as MethodCallExpression;

                    if (methodCallExp != null)
                    {
                        Assert.IsTrue(methodCallExp.Method.ToString().Contains("CastValue"));
                    }
                    else
                    {
                        ParameterExpression paramExpression = expression.Operand as ParameterExpression;
                        Assert.IsNotNull(paramExpression);
                    }
                }
                else
                {
                    Expression <Action> throwExp = Expression.Lambda <Action>(Expression.Block(expression), new ParameterExpression[] { });
                    ExceptionTestHelper.ExpectException <InvalidCastException>(() => throwExp.Compile().Invoke());
                }
            }
Esempio n. 19
0
        public void ItemTests()
        {
            JsonValue target = AnyInstance.DefaultJsonValue;

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = target["MissingProperty"]; }, string.Format(IndexerNotSupportedMsgFormat, typeof(string).FullName));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { target["NewProperty"] = AnyInstance.AnyJsonValue1; }, string.Format(IndexerNotSupportedMsgFormat, typeof(string).FullName));
        }
Esempio n. 20
0
        public void InvalidConfigTest()
        {
            string configXml = CreateConfig(null, WebMessageBodyStyle.Bare, null, null, null).Replace("'Bare'", "'notAStyle'");

            ExceptionTestHelper.ExpectException <ConfigurationException>(delegate { var b = TestjQueryConfigurationSection.GetWebHttpBehavior3Section(configXml).WebHttpElement3.DefaultBodyStyle; });

            configXml = CreateConfig(null, null, WebMessageFormat.Xml, null, null).Replace("'Xml'", "'notAFormat'");
            ExceptionTestHelper.ExpectException <ConfigurationException>(delegate { var f = TestjQueryConfigurationSection.GetWebHttpBehavior3Section(configXml).WebHttpElement3.DefaultOutgoingResponseFormat; });
        }
Esempio n. 21
0
        public void SaveTest()
        {
            JsonValue target = AnyInstance.DefaultJsonValue;

            using (MemoryStream ms = new MemoryStream())
            {
                ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { target.Save(ms); });
            }
        }
Esempio n. 22
0
        public void JsonValueInvalidOperatorsTest()
        {
            dynamic ja = AnyInstance.AnyJsonArray;
            dynamic jo = AnyInstance.AnyJsonObject;
            dynamic jp = (JsonValue)AnyInstance.AnyString;
            dynamic jd = AnyInstance.DefaultJsonValue;

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja * jd; }, string.Format(OperationNotDefinedMsgFormat, "Multiply", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jd / jo; }, string.Format(OperationNotDefinedMsgFormat, "Divide", "Default"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jo % jp; }, string.Format(OperationNotDefinedMsgFormat, "Modulo", "Object"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja - ja; }, string.Format(OperationNotDefinedMsgFormat, "Subtract", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja + ja; }, string.Format(OperationNotDefinedMsgFormat, "Add", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja > jd; }, string.Format(OperationNotDefinedMsgFormat, "GreaterThan", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jd < jo; }, string.Format(OperationNotDefinedMsgFormat, "LessThan", "Default"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jo >= jp; }, string.Format(OperationNotDefinedMsgFormat, "GreaterThanOrEqual", "Object"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jp <= ja; }, string.Format(OperationNotDefinedMsgFormat, "LessThanOrEqual", "String"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja & ja; }, string.Format(OperationNotDefinedMsgFormat, "And", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jd | jd; }, string.Format(OperationNotDefinedMsgFormat, "Or", "Default"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jo ^ jo; }, string.Format(OperationNotDefinedMsgFormat, "ExclusiveOr", "Object"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jp << ja; }, string.Format(OperationNotDefinedMsgFormat, "LeftShift", "String"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja >> jo; }, string.Format(OperationNotDefinedMsgFormat, "RightShift", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jd && true; }, string.Format(OperationNotDefinedMsgFormat, "IsFalse", "Default"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jo || false; }, string.Format(OperationNotDefinedMsgFormat, "IsTrue", "Object"));

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja * 10; }, string.Format(OperationNotDefinedMsgFormat, "Multiply", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jd / 10; }, string.Format(OperationNotDefinedMsgFormat, "Divide", "Default"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jo % 10; }, string.Format(OperationNotDefinedMsgFormat, "Modulo", "Object"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja + 10; }, string.Format(OperationNotDefinedMsgFormat, "Add", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja - 10; }, string.Format(OperationNotDefinedMsgFormat, "Subtract", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja > 10; }, string.Format(OperationNotDefinedMsgFormat, "GreaterThan", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jd >= 10; }, string.Format(OperationNotDefinedMsgFormat, "GreaterThanOrEqual", "Default"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jo <= 10.5; }, string.Format(OperationNotDefinedMsgFormat, "LessThanOrEqual", "Object"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja & 0x001; }, string.Format(OperationNotDefinedMsgFormat, "And", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jd | 0x001; }, string.Format(OperationNotDefinedMsgFormat, "Or", "Default"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jo ^ 0x001; }, string.Format(OperationNotDefinedMsgFormat, "ExclusiveOr", "Object"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja << 10; }, string.Format(OperationNotDefinedMsgFormat, "LeftShift", "Array"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja >> 10; }, string.Format(OperationNotDefinedMsgFormat, "RightShift", "Array"));

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jp + "Hello"; }, string.Format(OperatorNotDefinedMsgFormat, "Add", typeof(string).FullName, typeof(string).FullName));

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jp << 10; }, string.Format(OperatorNotDefinedMsgFormat, "LeftShift", "System.String", "System.Int32"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jp <= 10.5; }, string.Format(OperatorNotDefinedMsgFormat, "LessThanOrEqual", "System.String", "System.Double"));

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jd < null; }, string.Format(OperatorNotAllowedOnOperands, "LessThan", typeof(JsonValue), "<null>"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jp > null; }, string.Format(OperatorNotAllowedOnOperands, "GreaterThan", typeof(JsonPrimitive), "<null>"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = ja + null; }, string.Format(OperatorNotAllowedOnOperands, "Add", typeof(JsonArray), "<null>"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var val = jo & null; }, string.Format(OperatorNotAllowedOnOperands, "And", typeof(JsonObject), "<null>"));


            dynamic dyn        = (JsonValue)20;
            string  jpTypeName = typeof(JsonPrimitive).FullName;

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = ++dyn; }, string.Format(OperationNotDefinedMsgFormat, "Increment", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = --dyn; }, string.Format(OperationNotDefinedMsgFormat, "Decrement", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn++; }, string.Format(OperationNotDefinedMsgFormat, "Increment", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn--; }, string.Format(OperationNotDefinedMsgFormat, "Decrement", "Number"));
        }
Esempio n. 23
0
        public void ParseTest()
        {
            string    json = "{\"a\":123,\"b\":[false,null,12.34],\"with space\":\"hello\",\"\":\"empty key\",\"withTypeHint\":{\"__type\":\"typeHint\"}}";
            JsonValue jv   = JsonValue.Parse(json);

            Assert.AreEqual(json, jv.ToString());

            ExceptionTestHelper.ExpectException <ArgumentNullException>(() => JsonValue.Parse(null));
            ExceptionTestHelper.ExpectException <ArgumentException>(() => JsonValue.Parse(""));
        }
Esempio n. 24
0
        public void InvalidArithmeticOperatorsTest()
        {
            dynamic dyn = (JsonValue)10;

            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn += 2;; }, string.Format(OperationNotDefinedMsgFormat, "AddAssign", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn -= 1; }, string.Format(OperationNotDefinedMsgFormat, "SubtractAssign", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn *= 1; }, string.Format(OperationNotDefinedMsgFormat, "MultiplyAssign", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn /= 1; }, string.Format(OperationNotDefinedMsgFormat, "DivideAssign", "Number"));
            ExceptionTestHelper.ExpectException <InvalidOperationException>(delegate { var v = dyn %= 1; }, string.Format(OperationNotDefinedMsgFormat, "ModuloAssign", "Number"));
        }
Esempio n. 25
0
        public void AsDynamicTest()
        {
            JsonValue jv2 = 3;

            Assert.IsTrue(jv2.AsDynamic() == 3);
            Assert.IsTrue(3 == (int)jv2.AsDynamic());
            Assert.IsTrue((JsonValue)3 == jv2.AsDynamic());

            ExceptionTestHelper.ExpectException <Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(delegate { bool b = 3 == jv2.AsDynamic(); });
        }
Esempio n. 26
0
        public void CastingDefaultValueTest()
        {
            JsonValue jv = AnyInstance.DefaultJsonValue;
            dynamic   d  = jv;

            ExceptionTestHelper.ExpectException <InvalidCastException>(delegate { float p = (float)d; });
            ExceptionTestHelper.ExpectException <InvalidCastException>(delegate { byte p = (byte)d; });
            ExceptionTestHelper.ExpectException <InvalidCastException>(delegate { int p = (int)d; });

            Assert.IsNull((string)d);
        }
Esempio n. 27
0
        public void InvalidAssignmentValueTest()
        {
            JsonValue target;
            JsonValue value = AnyInstance.DefaultJsonValue;

            target = AnyInstance.AnyJsonArray;
            ExceptionTestHelper.ExpectException <ArgumentException>(delegate { target[0] = value; }, OperationNotAllowedOnDefaultMsgFormat);

            target = AnyInstance.AnyJsonObject;
            ExceptionTestHelper.ExpectException <ArgumentException>(delegate { target["key"] = value; }, OperationNotAllowedOnDefaultMsgFormat);
        }
            public static void TestBindParams(DynamicMetaObject target)
            {
                string methodName = "ToString";

                object[] arguments = new object[] { };

                InvokeMemberBinder binder = new TestInvokeMemberBinder(methodName, arguments.Length);

                DynamicMetaObject[] args = new DynamicMetaObject[arguments.Length];

                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindInvokeMember(null, args); });
                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindInvokeMember(binder, null); });
            }
Esempio n. 29
0
        public void AddTest()
        {
            JsonArray target = new JsonArray();
            JsonValue item   = AnyInstance.AnyJsonValue1;

            Assert.IsFalse(target.Contains(item));
            target.Add(item);
            Assert.AreEqual(1, target.Count);
            Assert.AreEqual(item, target[0]);
            Assert.IsTrue(target.Contains(item));

            ExceptionTestHelper.ExpectException <ArgumentException>(() => target.Add(AnyInstance.DefaultJsonValue));
        }
Esempio n. 30
0
        public void ParseNumbersTest()
        {
            string    json         = "{\"long\":12345678901234,\"zero\":0.0,\"double\":1.23e+200}";
            string    expectedJson = "{\"long\":12345678901234,\"zero\":0,\"double\":1.23E+200}";
            JsonValue jv           = JsonValue.Parse(json);

            Assert.AreEqual(expectedJson, jv.ToString());
            Assert.AreEqual(12345678901234L, (long)jv["long"]);
            Assert.AreEqual <double>(0, jv["zero"].ReadAs <double>());
            Assert.AreEqual <double>(1.23e200, jv["double"].ReadAs <double>());

            ExceptionTestHelper.ExpectException <ArgumentException>(() => JsonValue.Parse("[1.2e+400]"));
        }