コード例 #1
0
        public static void EnumDataTypeAttribute_creation_DataType_and_CustomDataType()
        {
            var attribute = new EnumDataTypeAttribute(null);

            Assert.Equal(DataType.Custom, attribute.DataType);
            Assert.Equal("Enumeration", attribute.CustomDataType);
        }
コード例 #2
0
		public void IsValid ()
		{
			var attr = new EnumDataTypeAttribute (typeof (string));
			
			try {
				attr.IsValid (null);
				Assert.Fail ("#A1-1");
			} catch (InvalidOperationException) {
				// success
			}

			try {
				attr.IsValid ("stuff");
				Assert.Fail ("#A1-2");
			} catch (InvalidOperationException) {
				// success
			}

			attr = new EnumDataTypeAttribute (typeof (TestEnum));
			Assert.IsTrue (attr.IsValid (null), "#A2-1");
			Assert.IsTrue (attr.IsValid (1), "#A2-2");
			Assert.IsFalse (attr.IsValid (0), "#A2-3");
			Assert.IsTrue (attr.IsValid (TestEnum.Two), "#A2-4");
			Assert.IsFalse (attr.IsValid (AnotherEnum.Five), "#A2-5");
			Assert.IsFalse (attr.IsValid ("stuff"), "#A2-5");

			AnotherEnum val = AnotherEnum.Six;
			Assert.IsFalse (attr.IsValid (val), "#A2-6");

			Assert.IsTrue (attr.IsValid (String.Empty), "#A2-7");
			Assert.IsTrue (attr.IsValid ("Three"), "#A2-8");
			Assert.IsFalse (attr.IsValid ("Four"), "#A2-9");
			Assert.IsFalse (attr.IsValid (true), "#A2-10");
			Assert.IsFalse (attr.IsValid (' '), "#A2-11");
			Assert.IsFalse (attr.IsValid (0.12F), "#A2-12");
			Assert.IsTrue (attr.IsValid ((short) 1), "#A2-13");
			Assert.IsFalse (attr.IsValid (12.3M), "#A2-14");
			Assert.IsFalse (attr.IsValid (12.3D), "#A2-15");
			Assert.IsTrue (attr.IsValid ((long) 1), "#A2-16");

			attr = new EnumDataTypeAttribute (typeof (AnotherEnum));
			Assert.IsTrue (attr.IsValid (null), "#A3-1");
			Assert.IsTrue (attr.IsValid (4), "#A3-2");
			Assert.IsFalse (attr.IsValid (0), "#A3-3");
			Assert.IsTrue (attr.IsValid (AnotherEnum.Five), "#A3-4");
			Assert.IsFalse (attr.IsValid (TestEnum.One), "#A3-5");
			Assert.IsFalse (attr.IsValid ("stuff"), "#A3-5");

			val = AnotherEnum.Four;
			Assert.IsTrue (attr.IsValid (val), "#A3-6");

			Assert.IsTrue (attr.IsValid (String.Empty), "#A3-7");
			Assert.IsTrue (attr.IsValid ("Four"), "#A3-8");
			Assert.IsFalse (attr.IsValid ("Three"), "#A3-9");
			Assert.IsTrue (attr.IsValid (12), "#A3-10");
			Assert.IsTrue (attr.IsValid ("Five, Six"), "#A3-11");
			Assert.IsFalse (attr.IsValid (true), "#A3-12");
			Assert.IsFalse (attr.IsValid (' '), "#A3-13");
			Assert.IsFalse (attr.IsValid (0.12), "#A3-14");
		}
コード例 #3
0
 public static void Validate_throws_InvalidOperationException_for_null_EnumType()
 {
     var attribute = new EnumDataTypeAttribute(null);
     Assert.Null(attribute.EnumType);
     Assert.Throws<InvalidOperationException>(
         () => attribute.Validate("Value does not matter - EnumType is null", s_testValidationContext));
 }
コード例 #4
0
 public static void Validate_throws_InvalidOperationException_for_non_enum_EnumType()
 {
     var attribute = new EnumDataTypeAttribute(typeof(string));
     Assert.Equal(typeof(string), attribute.EnumType);
     Assert.Throws<InvalidOperationException>(
         () => attribute.Validate("Value does not matter - EnumType is not an enum", s_testValidationContext));
 }
コード例 #5
0
        public static void Validate_throws_for_matching_flags_enums_and_non_matching_values()
        {
            var attribute = new EnumDataTypeAttribute(typeof(FlagsEnumType));

            Assert.Throws <ValidationException>(() => attribute.Validate(0, s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate(8, s_testValidationContext));
        }
コード例 #6
0
        public static void Validate_successful_for_null_or_empty_value()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(string.Empty, s_testValidationContext));
        }
コード例 #7
0
        public static void Validate_throws_InvalidOperationException_for_non_enum_EnumType()
        {
            var attribute = new EnumDataTypeAttribute(typeof(string));

            Assert.Equal(typeof(string), attribute.EnumType);
            Assert.Throws <InvalidOperationException>(
                () => attribute.Validate("Value does not matter - EnumType is not an enum", s_testValidationContext));
        }
コード例 #8
0
        public static void Validate_throws_InvalidOperationException_for_null_EnumType()
        {
            var attribute = new EnumDataTypeAttribute(null);

            Assert.Null(attribute.EnumType);
            Assert.Throws <InvalidOperationException>(
                () => attribute.Validate("Value does not matter - EnumType is null", s_testValidationContext));
        }
コード例 #9
0
        public static void Ctor(Type enumType)
        {
            var attribute = new EnumDataTypeAttribute(enumType);
            Assert.Equal(DataType.Custom, attribute.DataType);
            Assert.Equal("Enumeration", attribute.CustomDataType);

            Assert.Equal(enumType, attribute.EnumType);
        }
コード例 #10
0
        public static void Validate_successful_for_matching_non_flags_enums_and_matching_values()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            AssertEx.DoesNotThrow(() => attribute.Validate(NonFlagsEnumType.A, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(10, s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate(100, s_testValidationContext));
        }
コード例 #11
0
        public static void Can_get_EnumType()
        {
            var attribute = new EnumDataTypeAttribute(null);
            Assert.Null(attribute.EnumType);

            attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));
            Assert.Equal(typeof(NonFlagsEnumType), attribute.EnumType);
        }
コード例 #12
0
        public static void Can_get_EnumType()
        {
            var attribute = new EnumDataTypeAttribute(null);

            Assert.Null(attribute.EnumType);

            attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));
            Assert.Equal(typeof(NonFlagsEnumType), attribute.EnumType);
        }
コード例 #13
0
        public static void Ctor(Type enumType)
        {
            var attribute = new EnumDataTypeAttribute(enumType);

            Assert.Equal(DataType.Custom, attribute.DataType);
            Assert.Equal("Enumeration", attribute.CustomDataType);

            Assert.Equal(enumType, attribute.EnumType);
        }
コード例 #14
0
        public static void Validate_throws_for_non_integral_values()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            Assert.Throws <ValidationException>(() => attribute.Validate(true, s_testValidationContext));     // bool
            Assert.Throws <ValidationException>(() => attribute.Validate(1.1f, s_testValidationContext));     // float
            Assert.Throws <ValidationException>(() => attribute.Validate(123.456d, s_testValidationContext)); // double
            Assert.Throws <ValidationException>(() => attribute.Validate(123.456m, s_testValidationContext)); // decimal
            Assert.Throws <ValidationException>(() => attribute.Validate('0', s_testValidationContext));      // char
        }
コード例 #15
0
        public static void Validate_throws_InvalidOperationException_for_Nullable_EnumType()
        {
            var attribute = new EnumDataTypeAttribute(typeof(Nullable<NonFlagsEnumType>));
            Assert.Throws<InvalidOperationException>(
                () => attribute.Validate("Value does not matter - EnumType is Nullable", s_testValidationContext));

            attribute = new EnumDataTypeAttribute(typeof(Nullable<FlagsEnumType>));
            Assert.Throws<InvalidOperationException>(
                () => attribute.Validate("Value does not matter - EnumType is Nullable", s_testValidationContext));
        }
コード例 #16
0
        public static void Validate_throws_InvalidOperationException_for_Nullable_EnumType()
        {
            var attribute = new EnumDataTypeAttribute(typeof(Nullable <NonFlagsEnumType>));

            Assert.Throws <InvalidOperationException>(
                () => attribute.Validate("Value does not matter - EnumType is Nullable", s_testValidationContext));

            attribute = new EnumDataTypeAttribute(typeof(Nullable <FlagsEnumType>));
            Assert.Throws <InvalidOperationException>(
                () => attribute.Validate("Value does not matter - EnumType is Nullable", s_testValidationContext));
        }
コード例 #17
0
        public static void Validate_throws_for_string_values_which_cannot_be_converted_to_enum_values()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            Assert.Throws <ValidationException>(() => attribute.Validate("NonExist", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("42", s_testValidationContext));

            attribute = new EnumDataTypeAttribute(typeof(FlagsEnumType));
            Assert.Throws <ValidationException>(() => attribute.Validate("NonExist", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("0", s_testValidationContext));
            Assert.Throws <ValidationException>(() => attribute.Validate("8", s_testValidationContext));
        }
コード例 #18
0
		public void Constructor ()
		{
			var attr = new EnumDataTypeAttribute (typeof (string));

			Assert.AreEqual (DataType.Custom, attr.DataType, "#A1-1");
			Assert.AreEqual (typeof (string), attr.EnumType, "#A1-2");

			attr = new EnumDataTypeAttribute (typeof (TestEnum));
			Assert.AreEqual (DataType.Custom, attr.DataType, "#B1-1");
			Assert.AreEqual (typeof (TestEnum), attr.EnumType, "#B1-2");

			attr = new EnumDataTypeAttribute (null);
			Assert.AreEqual (DataType.Custom, attr.DataType, "#C1-1");
			Assert.AreEqual (null, attr.EnumType, "#C1-2");
		}
コード例 #19
0
        private static bool AddEnumFacet(EnumDataTypeAttribute attribute, IFacetHolder holder, Type typeOfEnum) {
            if (attribute != null) {
                return FacetUtils.AddFacet(Create(attribute, holder));
            }

            Type typeOrNulledType = TypeUtils.GetNulledType(typeOfEnum);
            if (TypeUtils.IsEnum(typeOrNulledType)) {
                return FacetUtils.AddFacet(new EnumFacet(holder, typeOrNulledType));
            }
            if (CollectionUtils.IsGenericOfEnum(typeOfEnum)) {
                Type enumInstanceType = typeOfEnum.GetGenericArguments().First();
                return FacetUtils.AddFacet(new EnumFacet(holder, enumInstanceType));
            }

            return false;
        }
コード例 #20
0
        public static void Validate_successful_for_string_values_which_can_be_converted_to_enum_values()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            AssertEx.DoesNotThrow(() => attribute.Validate("A", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("B", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("C", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("0", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("10", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("100", s_testValidationContext));

            attribute = new EnumDataTypeAttribute(typeof(FlagsEnumType));
            AssertEx.DoesNotThrow(() => attribute.Validate("X", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("X, Y", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("X, Y, Z", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("1", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("5", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("7", s_testValidationContext));
        }
コード例 #21
0
 public static void Validate_Invalid_ThrowsValidationException(Type enumType, object value)
 {
     var attribute = new EnumDataTypeAttribute(enumType);
     Assert.Throws<ValidationException>(() => attribute.Validate(value, s_testValidationContext));
 }
コード例 #22
0
        public static void Validate_Valid_DoesNotThrow(Type enumType, object value)
        {
            var attribute = new EnumDataTypeAttribute(enumType);

            attribute.Validate(value, s_testValidationContext);
        }
コード例 #23
0
        public static void Validate_Invalid_ThrowsValidationException(Type enumType, object value)
        {
            var attribute = new EnumDataTypeAttribute(enumType);

            Assert.Throws <ValidationException>(() => attribute.Validate(value, s_testValidationContext));
        }
コード例 #24
0
        public static void Validate_successful_for_string_values_which_can_be_converted_to_enum_values()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));
            AssertEx.DoesNotThrow(() => attribute.Validate("A", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("B", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("C", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("0", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("10", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("100", s_testValidationContext));

            attribute = new EnumDataTypeAttribute(typeof(FlagsEnumType));
            AssertEx.DoesNotThrow(() => attribute.Validate("X", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("X, Y", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("X, Y, Z", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("1", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("5", s_testValidationContext));
            AssertEx.DoesNotThrow(() => attribute.Validate("7", s_testValidationContext));
        }
コード例 #25
0
        public static void Validate_throws_for_string_values_which_cannot_be_converted_to_enum_values()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));
            Assert.Throws<ValidationException>(() => attribute.Validate("NonExist", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("42", s_testValidationContext));

            attribute = new EnumDataTypeAttribute(typeof(FlagsEnumType));
            Assert.Throws<ValidationException>(() => attribute.Validate("NonExist", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("0", s_testValidationContext));
            Assert.Throws<ValidationException>(() => attribute.Validate("8", s_testValidationContext));
        }
コード例 #26
0
 public static void EnumDataTypeAttribute_creation_DataType_and_CustomDataType()
 {
     var attribute = new EnumDataTypeAttribute(null);
     Assert.Equal(DataType.Custom, attribute.DataType);
     Assert.Equal("Enumeration", attribute.CustomDataType);
 }
コード例 #27
0
 public static void Validate_throws_for_matching_flags_enums_and_non_matching_values()
 {
     var attribute = new EnumDataTypeAttribute(typeof(FlagsEnumType));
     Assert.Throws<ValidationException>(() => attribute.Validate(0, s_testValidationContext));
     Assert.Throws<ValidationException>(() => attribute.Validate(8, s_testValidationContext));
 }
コード例 #28
0
        public static void Validate_throws_for_non_ValueType_value()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            Assert.Throws <ValidationException>(() => attribute.Validate(new object(), s_testValidationContext));
        }
コード例 #29
0
 public static void Validate_successful_for_matching_flags_enums_and_matching_values()
 {
     var attribute = new EnumDataTypeAttribute(typeof(FlagsEnumType));
     AssertEx.DoesNotThrow(() => attribute.Validate(FlagsEnumType.X, s_testValidationContext));
     AssertEx.DoesNotThrow(() => attribute.Validate(FlagsEnumType.X | FlagsEnumType.Y, s_testValidationContext));
     AssertEx.DoesNotThrow(() => attribute.Validate(5, s_testValidationContext));
     AssertEx.DoesNotThrow(() => attribute.Validate(7, s_testValidationContext));
 }
コード例 #30
0
 public static void Validate_throws_for_non_ValueType_value()
 {
     var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));
     Assert.Throws<ValidationException>(() => attribute.Validate(new object(), s_testValidationContext));
 }
コード例 #31
0
 public static void Validate_successful_for_null_or_empty_value()
 {
     var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));
     AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext));
     AssertEx.DoesNotThrow(() => attribute.Validate(string.Empty, s_testValidationContext));
 }
コード例 #32
0
 public static void Validate_InvalidEnumType_ThrowsInvalidOperationException(Type enumType)
 {
     var attribute = new EnumDataTypeAttribute(enumType);
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("AnyValue", s_testValidationContext));
 }
コード例 #33
0
 public static void Validate_Valid_DoesNotThrow(Type enumType, object value)
 {
     var attribute = new EnumDataTypeAttribute(enumType);
     attribute.Validate(value, s_testValidationContext);
 }
コード例 #34
0
        public static void Validate_InvalidEnumType_ThrowsInvalidOperationException(Type enumType)
        {
            var attribute = new EnumDataTypeAttribute(enumType);

            Assert.Throws <InvalidOperationException>(() => attribute.Validate("AnyValue", s_testValidationContext));
        }
コード例 #35
0
 public static void Validate_throws_for_non_matching_EnumType()
 {
     var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));
     Assert.Throws<ValidationException>(() => attribute.Validate(FlagsEnumType.X, s_testValidationContext));
 }
コード例 #36
0
 private static IEnumFacet Create(EnumDataTypeAttribute attribute, IFacetHolder holder) {
     return attribute == null ? null : new EnumFacet(holder, attribute.EnumType);
 }
コード例 #37
0
 public static void Validate_throws_for_non_integral_values()
 {
     var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));
     Assert.Throws<ValidationException>(() => attribute.Validate(true, s_testValidationContext)); // bool
     Assert.Throws<ValidationException>(() => attribute.Validate(1.1f, s_testValidationContext)); // float
     Assert.Throws<ValidationException>(() => attribute.Validate(123.456d, s_testValidationContext)); // double
     Assert.Throws<ValidationException>(() => attribute.Validate(123.456m, s_testValidationContext)); // decimal
     Assert.Throws<ValidationException>(() => attribute.Validate('0', s_testValidationContext)); // char
 }
コード例 #38
0
        public static void Validate_throws_for_non_matching_EnumType()
        {
            var attribute = new EnumDataTypeAttribute(typeof(NonFlagsEnumType));

            Assert.Throws <ValidationException>(() => attribute.Validate(FlagsEnumType.X, s_testValidationContext));
        }