public void ReadAsTests() { JsonValue target = AnyInstance.DefaultJsonValue; string typeName = target.GetType().FullName; string errorMsgFormat = "Cannot read '{0}' as '{1}' type."; ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs(typeof(bool)); }, String.Format(errorMsgFormat, typeName, typeof(bool))); ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs(typeof(string)); }, String.Format(errorMsgFormat, typeName, typeof(string))); ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs(typeof(JsonObject)); }, String.Format(errorMsgFormat, typeName, typeof(JsonObject))); ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs <bool>(); }, String.Format(errorMsgFormat, typeName, typeof(bool))); ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs <string>(); }, String.Format(errorMsgFormat, typeName, typeof(string))); ExceptionHelper.Throws <NotSupportedException>(delegate { target.ReadAs <JsonObject>(); }, String.Format(errorMsgFormat, typeName, typeof(JsonObject))); bool boolValue; string stringValue; JsonObject objValue; object value; Assert.False(target.TryReadAs(typeof(bool), out value), "TryReadAs expected to return false"); Assert.Null(value); Assert.False(target.TryReadAs(typeof(string), out value), "TryReadAs expected to return false"); Assert.Null(value); Assert.False(target.TryReadAs(typeof(JsonObject), out value), "TryReadAs expected to return false"); Assert.Null(value); Assert.False(target.TryReadAs <bool>(out boolValue), "TryReadAs expected to return false"); Assert.False(boolValue); Assert.False(target.TryReadAs <string>(out stringValue), "TryReadAs expected to return false"); Assert.Null(stringValue); Assert.False(target.TryReadAs <JsonObject>(out objValue), "TryReadAs expected to return false"); Assert.Null(objValue); }
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 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.NotNull(result); MethodCallExpression expression = result.Expression as MethodCallExpression; Assert.NotNull(expression); Assert.Equal <string>(expectedMethodSignature, expression.Method.ToString()); }
/// <summary> /// Returns an expression representing a 'throw' instruction based on the specified <see cref="OperationSupport"/> value. /// </summary> /// <param name="supportValue">The <see cref="OperationSupport"/> value.</param> /// <param name="operation">The operation type.</param> /// <param name="thisValue">The operation left operand.</param> /// <param name="operand">The operation right operand.</param> /// <returns>A <see cref="Expression"/> representing a 'throw' instruction.</returns> private static Expression GetOperationErrorExpression(OperationSupport supportValue, ExpressionType operation, JsonValue thisValue, object operand) { string exceptionMessage; string operandTypeName = operand != null?operand.GetType().FullName : "<null>"; switch (supportValue) { default: case OperationSupport.NotSupported: case OperationSupport.NotSupportedOnJsonType: case OperationSupport.NotSupportedOnValueType: exceptionMessage = SG.GetString(SR.OperatorNotDefinedForJsonType, operation, thisValue.JsonType); break; case OperationSupport.NotSupportedOnOperand: exceptionMessage = SG.GetString(SR.OperatorNotAllowedOnOperands, operation, thisValue.GetType().FullName, operandTypeName); break; } return(Expression.Throw(Expression.Constant(new InvalidOperationException(exceptionMessage)), typeof(object))); }