|
| 1 | +// Copyright (c) Ubiquity.NET Contributors. All rights reserved. |
| 2 | +// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.ComponentModel; |
| 6 | + |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | + |
| 9 | +namespace Ubiquity.NET.Extensions.UT |
| 10 | +{ |
| 11 | + [TestClass] |
| 12 | + public sealed class FluentValidationExtensionsTests |
| 13 | + { |
| 14 | + [TestMethod] |
| 15 | + public void ThrowIfNull_throws_expected_exception_when_null( ) |
| 16 | + { |
| 17 | + var ex = Assert.ThrowsExactly<ArgumentNullException>(()=> |
| 18 | + { |
| 19 | + FluentValidationExtensions.ThrowIfNull<string>( null ); |
| 20 | + } ); |
| 21 | + Assert.AreEqual("null", ex.ParamName, "parameter name should match input expression"); |
| 22 | + } |
| 23 | + |
| 24 | + [TestMethod] |
| 25 | + public void ThrowIfNull_does_not_throw_on_non_null_input() |
| 26 | + { |
| 27 | + const string input = "This is a test"; |
| 28 | + |
| 29 | + Assert.AreSame(input, FluentValidationExtensions.ThrowIfNull(input), "Fluent API should return input value on success" ); |
| 30 | + } |
| 31 | + |
| 32 | + [TestMethod] |
| 33 | + public void ThrowIfNull_reports_exception_whith_provided_expression( ) |
| 34 | + { |
| 35 | + const string exp = "My-Expression"; |
| 36 | + var ex = Assert.ThrowsExactly<ArgumentNullException>(()=> |
| 37 | + { |
| 38 | + FluentValidationExtensions.ThrowIfNull<string>( null, exp ); |
| 39 | + } ); |
| 40 | + Assert.AreEqual( exp, ex.ParamName, "parameter name should match input expression" ); |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + public void ThrowIfNotDefined_does_not_throw_for_defined_value() |
| 45 | + { |
| 46 | + Assert.AreEqual(TestEnum.Max, FluentValidationExtensions.ThrowIfNotDefined(TestEnum.Max), "Fluent API should return input value on success" ); |
| 47 | + } |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public void ThrowIfOutOfRange_does_not_throw_for_inrange_values( ) |
| 51 | + { |
| 52 | + double value = 1.0; |
| 53 | + double min = 0.0; |
| 54 | + double max = 2.0; |
| 55 | + |
| 56 | + Assert.AreEqual(value, FluentValidationExtensions.ThrowIfOutOfRange(value, min, max), "Fluent API should return input value on success"); |
| 57 | + } |
| 58 | + |
| 59 | + [TestMethod] |
| 60 | + public void ThrowIfOutOfRange_throws_for_out_of_range_values( ) |
| 61 | + { |
| 62 | + double value = 2.0; |
| 63 | + double min = 1.0; |
| 64 | + double max = 1.5; |
| 65 | + |
| 66 | + var ex = Assert.ThrowsExactly<ArgumentOutOfRangeException>(()=> |
| 67 | + { |
| 68 | + _ = FluentValidationExtensions.ThrowIfOutOfRange( value, min, max ); |
| 69 | + } ); |
| 70 | + Assert.AreEqual(value, ex.ActualValue); |
| 71 | + Assert.AreEqual(nameof(value), ex.ParamName); |
| 72 | + } |
| 73 | + |
| 74 | + [TestMethod] |
| 75 | + public void ThrowIfOutOfRange_throws_with_custom_expression_for_out_of_range_values( ) |
| 76 | + { |
| 77 | + double value = 2.0; |
| 78 | + double min = 1.0; |
| 79 | + double max = 1.5; |
| 80 | + |
| 81 | + const string exp = "My Expression"; |
| 82 | + var ex = Assert.ThrowsExactly<ArgumentOutOfRangeException>(()=> |
| 83 | + { |
| 84 | + _ = FluentValidationExtensions.ThrowIfOutOfRange( value, min, max, exp ); |
| 85 | + } ); |
| 86 | + Assert.AreEqual( value, ex.ActualValue ); |
| 87 | + Assert.AreEqual( exp, ex.ParamName ); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + public void ThrowIfNotDefined_throws_for_undefined_values( ) |
| 92 | + { |
| 93 | + var temp = (TestEnum)4; |
| 94 | + var ex = Assert.ThrowsExactly<InvalidEnumArgumentException>( ( ) => |
| 95 | + { |
| 96 | + FluentValidationExtensions.ThrowIfNotDefined(temp); |
| 97 | + } ); |
| 98 | + Assert.AreEqual(nameof(temp), ex.ParamName, "parameter name should match input expression" ); |
| 99 | + |
| 100 | + var temp2 = (TestByteEnum)4; |
| 101 | + var ex2 = Assert.ThrowsExactly<InvalidEnumArgumentException>( ( ) => |
| 102 | + { |
| 103 | + FluentValidationExtensions.ThrowIfNotDefined(temp2); |
| 104 | + } ); |
| 105 | + Assert.AreEqual( nameof( temp2 ), ex2.ParamName, "parameter name should match input expression" ); |
| 106 | + |
| 107 | + // This still fits an int so the normal constructor that sets paramName is available |
| 108 | + var temp3 = (TestU64Enum)int.MaxValue; |
| 109 | + var ex3 = Assert.ThrowsExactly<InvalidEnumArgumentException>( ( ) => |
| 110 | + { |
| 111 | + FluentValidationExtensions.ThrowIfNotDefined(temp3); |
| 112 | + } ); |
| 113 | + Assert.AreEqual( nameof( temp3 ), ex3.ParamName, "parameter name should match input expression" ); |
| 114 | + |
| 115 | + // This can't fit into an int so, the exception constructor that does not provide paramName is |
| 116 | + // the only option :( [But at least this scenario is VERY rare in the real world] |
| 117 | + var temp4 = (TestU64Enum)(UInt64.MaxValue - 1); |
| 118 | + var ex4 = Assert.ThrowsExactly<InvalidEnumArgumentException>( ( ) => |
| 119 | + { |
| 120 | + FluentValidationExtensions.ThrowIfNotDefined(temp4); |
| 121 | + } ); |
| 122 | + Assert.IsNull( ex4.ParamName, "parameter name not available for non-int formattable enums" ); |
| 123 | + } |
| 124 | + |
| 125 | + private enum TestEnum // default underling type is Int32 |
| 126 | + { |
| 127 | + Zero, |
| 128 | + One, |
| 129 | + Two, |
| 130 | + Max = int.MaxValue |
| 131 | + } |
| 132 | + |
| 133 | + private enum TestByteEnum |
| 134 | + : byte |
| 135 | + { |
| 136 | + Zero, |
| 137 | + One, |
| 138 | + Two, |
| 139 | + Max = byte.MaxValue |
| 140 | + } |
| 141 | + |
| 142 | + private enum TestU64Enum |
| 143 | + : UInt64 |
| 144 | + { |
| 145 | + Zero, |
| 146 | + One, |
| 147 | + Two, |
| 148 | + Max = UInt64.MaxValue |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments