Skip to content

Commit 6721348

Browse files
authored
Merge pull request #254 from yollosun/feature/#236
[#236][add] rangeAttribute and rangeAttributeTests
2 parents 7cac239 + f97d8b1 commit 6721348

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using NUnit.Framework;
3+
using RangeAttribute = Simplify.Web.Model.Validation.Attributes.RangeAttribute;
4+
5+
namespace Simplify.Web.Tests.Model.Validation.Attributes;
6+
7+
[TestFixture]
8+
public class RangeAttributeTests : AttributesTestBase
9+
{
10+
public const int MinValue = 2;
11+
public const int MaxValue = 12;
12+
13+
[OneTimeSetUp]
14+
public void SetupAttribute() => Attr = new RangeAttribute(MinValue, MaxValue);
15+
16+
[Test]
17+
public void Validate_ValueInRange_Ok()
18+
{
19+
// Act & Assert
20+
TestAttributeForValidValue(10);
21+
}
22+
23+
[Test]
24+
public void Validate_BelowMinValue_ExceptionThrown()
25+
{
26+
// Assign
27+
28+
var value = 1;
29+
var defaultMessage = $"The value is out of range. The range constraint - {MinValue} - {MaxValue}, actual value: {value}";
30+
31+
// Act & Assert
32+
TestAttribute(value, defaultMessage);
33+
}
34+
35+
[Test]
36+
public void Validate_AboveMaxValue_ExceptionThrown()
37+
{
38+
// Assign
39+
40+
var value = 13;
41+
var defaultMessage = $"The value is out of range. The range constraint - {MinValue} - {MaxValue}, actual value: {value}";
42+
43+
// Act & Assert
44+
TestAttribute(value, defaultMessage);
45+
}
46+
47+
[Test]
48+
public void Validate_MaxValueEqualsValue_Ok()
49+
{
50+
// Act & Assert
51+
TestAttributeForValidValue(12);
52+
}
53+
54+
[Test]
55+
public void Validate_MinValueEqualsValue_Ok()
56+
{
57+
// Act & Assert
58+
TestAttributeForValidValue(2);
59+
}
60+
61+
[Test]
62+
public void Validate_NullValue_NoExceptions()
63+
{
64+
// Act & Assert
65+
TestAttributeForValidValue(null);
66+
}
67+
68+
[Test]
69+
public void Validate_DifferentTypes_ExceptionThrown()
70+
{
71+
// Act & Assert
72+
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(15.2));
73+
74+
}
75+
76+
[Test]
77+
public void Validate_ValueTypeNotInheritIComparable_ExceptionThrown()
78+
{
79+
// Act & Assert
80+
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(new object()));
81+
}
82+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Reflection;
3+
using Simplify.DI;
4+
5+
namespace Simplify.Web.Model.Validation.Attributes;
6+
7+
/// <summary>
8+
/// Sets a range constraint
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Property)]
11+
public class RangeAttribute : ValidationAttribute
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="RangeAttribute"/> class.
15+
/// </summary>
16+
/// <param name="minValue">The minimum value, inclusive.</param>
17+
/// <param name="maxValue">The maximum value, inclusive.</param>
18+
/// <param name="errorMessage">The error message.</param>
19+
/// <param name="isMessageFromStringTable">if set to <c>true</c> [is message from string table].</param>
20+
public RangeAttribute(IComparable minValue, IComparable maxValue, string? errorMessage = null, bool isMessageFromStringTable = true) : base(errorMessage, isMessageFromStringTable)
21+
{
22+
MinValue = minValue;
23+
MaxValue = maxValue;
24+
}
25+
26+
/// <summary>
27+
/// Gets or sets the minimum value for the range.
28+
/// </summary>
29+
/// <value>
30+
/// The minimum value for the range.
31+
/// </value>
32+
public IComparable MinValue { get; }
33+
34+
/// <summary>
35+
/// Gets or sets the maximum value for the range.
36+
/// </summary>
37+
/// <value>
38+
/// The maximum value for the range.
39+
/// </value>
40+
public IComparable MaxValue { get; }
41+
42+
/// <summary>
43+
/// Validates the specified property value.
44+
/// </summary>
45+
/// <param name="value">The object value.</param>
46+
/// <param name="propertyInfo">Information about the property containing this attribute.</param>
47+
/// <param name="resolver">The objects resolver, useful if you need to retrieve some dependencies to perform validation.</param>
48+
public override void Validate(object? value, PropertyInfo propertyInfo, IDIResolver resolver)
49+
{
50+
if (value == null)
51+
return;
52+
53+
if (value is not IComparable comparableValue)
54+
throw new ArgumentException($"The type of specified property value must be inherited from {typeof(IComparable)}");
55+
56+
ValidateTypesMatching(comparableValue);
57+
58+
TryThrowCustomOrStringTableException(resolver);
59+
60+
if (comparableValue.CompareTo(MinValue) < 0 || comparableValue.CompareTo(MaxValue) > 0)
61+
throw new ModelValidationException(
62+
$"The value is out of range. The range constraint - {MinValue} - {MaxValue}, actual value: {value}");
63+
}
64+
65+
private void ValidateTypesMatching(IComparable comparableValue)
66+
{
67+
if (comparableValue.GetType() != MinValue.GetType())
68+
throw new ArgumentException("Type mismatch. The minimum value and property value should be of the same type.");
69+
70+
if (comparableValue.GetType() != MaxValue.GetType())
71+
throw new ArgumentException("Type mismatch. The maximum value and property value should be of the same type.");
72+
}
73+
}

0 commit comments

Comments
 (0)