コード例 #1
0
        public void EnumProperty <T, TResult>(
            T instance,
            Expression <Func <T, TResult> > propertyGetter,
            TResult expectedDefaultValue,
            TResult illegalValue,
            TResult roundTripTestValue
            ) where TResult : struct
        {
            PropertyInfo        property = GetPropertyInfo(propertyGetter);
            Func <T, TResult>   getFunc  = (obj) => (TResult)property.GetValue(obj, index: null);
            Action <T, TResult> setFunc  = (obj, value) => property.SetValue(obj, value, index: null);

            Assert.Equal(expectedDefaultValue, getFunc(instance));

            Assert.ThrowsInvalidEnumArgument(
                () =>
            {
                setFunc(instance, illegalValue);
            },
                "value",
                Convert.ToInt32(illegalValue),
                typeof(TResult)
                );

            TestPropertyValue(instance, getFunc, setFunc, roundTripTestValue);
        }
コード例 #2
0
 /// <summary>
 /// Override this if InvalidEnumArgument is not supported in the targetted platform
 /// </summary>
 /// <param name="testCode">A delegate to the code to be tested</param>
 /// <param name="paramName">The name of the parameter that should throw the exception</param>
 /// <param name="invalidValue">The expected invalid value that should appear in the message</param>
 /// <param name="enumType">The type of the enumeration</param>
 /// <param name="allowDerivedExceptions">Pass true to allow exceptions which derive from TException; pass false, otherwise</param>
 protected virtual void AssertForUndefinedValue(Action testCode, string parameterName, int invalidValue, Type enumType, bool allowDerivedExceptions = false)
 {
     Assert.ThrowsInvalidEnumArgument(
         testCode,
         parameterName,
         invalidValue,
         enumType,
         allowDerivedExceptions);
 }
コード例 #3
0
 public void Validate_ThrowsForUndefinedValues()
 {
     Assert.ThrowsInvalidEnumArgument(
         () => _validate(_undefined, "parameter"),
         "parameter",
         (int)Convert.ChangeType(_undefined, typeof(int)),
         typeof(TEnum),
         allowDerivedExceptions: false);
 }