public void BindSetIndexTest()
        {
            JsonValue jsonValue = AnyInstance.AnyJsonArray;

            DynamicMetaObject target = GetJsonValueDynamicMetaObject(jsonValue);

            TestSetIndexBinder.TestBindParams(target);

            int value = 0;

            foreach (KeyValuePair <string, JsonValue> pair in jsonValue)
            {
                TestSetIndexBinder.TestMetaObject(target, int.Parse(pair.Key), value++);
            }
        }
            public static void TestMetaObject(DynamicMetaObject target, int index, JsonValue jsonValue, bool isValid = true)
            {
                string expectedMethodSignature = "System.Json.JsonValue SetValue(Int32, System.Object)";

                SetIndexBinder binder = new TestSetIndexBinder();

                DynamicMetaObject[] indexes = { new DynamicMetaObject(Expression.Parameter(typeof(int)), BindingRestrictions.Empty, index) };
                DynamicMetaObject   value   = new DynamicMetaObject(Expression.Parameter(jsonValue.GetType()), BindingRestrictions.Empty, jsonValue);
                DynamicMetaObject   result  = target.BindSetIndex(binder, indexes, value);

                Assert.IsNotNull(result);

                MethodCallExpression expression = result.Expression as MethodCallExpression;

                Assert.IsNotNull(expression);
                Assert.AreEqual <string>(expectedMethodSignature, expression.Method.ToString());
            }
            public static void TestBindParams(DynamicMetaObject target)
            {
                SetIndexBinder binder         = new TestSetIndexBinder();
                Expression     typeExpression = Expression.Parameter(typeof(int));

                DynamicMetaObject[] indexes = new DynamicMetaObject[] { new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, 0) };
                DynamicMetaObject   value   = new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, (JsonValue)10);

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

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

                    new DynamicMetaObject[]
                    {
                        new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, null)
                    },

                    new DynamicMetaObject[]
                    {
                    }
                };

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

                    Expression <Action> expression = Expression.Lambda <Action>(Expression.Block(metaObj.Expression), new ParameterExpression[] { });
                    ExceptionTestHelper.ExpectException <ArgumentException>(() => { expression.Compile().Invoke(); }, NonSingleNonNullIndexNotSupported);
                }
            }
            public static void TestMetaObject(DynamicMetaObject target, int index, JsonValue jsonValue, bool isValid = true)
            {
                string expectedMethodSignature = "System.Json.JsonValue SetValue(Int32, System.Object)";

                SetIndexBinder binder = new TestSetIndexBinder();
                DynamicMetaObject[] indexes = { new DynamicMetaObject(Expression.Parameter(typeof(int)), BindingRestrictions.Empty, index) };
                DynamicMetaObject value = new DynamicMetaObject(Expression.Parameter(jsonValue.GetType()), BindingRestrictions.Empty, jsonValue);
                DynamicMetaObject result = target.BindSetIndex(binder, indexes, value);
                Assert.IsNotNull(result);

                MethodCallExpression expression = result.Expression as MethodCallExpression;
                Assert.IsNotNull(expression);
                Assert.AreEqual<string>(expectedMethodSignature, expression.Method.ToString());
            }
            public static void TestBindParams(DynamicMetaObject target)
            {
                SetIndexBinder binder = new TestSetIndexBinder();
                Expression typeExpression = Expression.Parameter(typeof(int));
                DynamicMetaObject[] indexes = new DynamicMetaObject[] { new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, 0) };
                DynamicMetaObject value = new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, (JsonValue)10);

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

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

                    new DynamicMetaObject[]
                    {
                        new DynamicMetaObject(typeExpression, BindingRestrictions.Empty, null)
                    },

                    new DynamicMetaObject[]
                    {
                    }
                };

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

                    Expression<Action> expression = Expression.Lambda<Action>(Expression.Block(metaObj.Expression), new ParameterExpression[] { });
                    ExceptionTestHelper.ExpectException<ArgumentException>(() => { expression.Compile().Invoke(); }, NonSingleNonNullIndexNotSupported);
                }
            }