Skip to content

Commit

Permalink
Merge pull request #18 from /issues/10
Browse files Browse the repository at this point in the history
Update unit test
  • Loading branch information
mr5z authored Dec 27, 2023
2 parents 0cf194e + 7a9feee commit 6063233
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("PropertyValidator.Test")]
16 changes: 9 additions & 7 deletions src/Helpers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ namespace PropertyValidator.Helpers;

internal static class ReflectionHelper
{
public static FieldInfo? GetField(Type fromType, string fieldName)
public static FieldInfo? GetField(Type? fromType, string fieldName)
{
var flags = BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;
FieldInfo field;
const BindingFlags flags = BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly;
FieldInfo? field;
// TODO find a way to break
while ((field = fromType.GetField(fieldName, flags)) == null && (fromType = fromType.BaseType) != null)
;
while ((field = fromType?.GetField(fieldName, flags)) == null && (fromType = fromType?.BaseType) != null)
{
// intentionally empty body
}
return field;
}
}
3 changes: 1 addition & 2 deletions src/Models/ValidationResultArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ public class ValidationResultArgs(IDictionary<string, IEnumerable<string?>> erro

public bool HasError => ErrorMessages?.Any() == true;

public string? FirstError => ErrorMessages?
.FirstOrDefault();
public string? FirstError => ErrorMessages?.FirstOrDefault();
}
13 changes: 8 additions & 5 deletions src/PropertyValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
<Version>1.1.1.0</Version>
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright (c) 2023 Mark Laureta. All rights reserved.</Copyright>
<PackageLicenseUrl>https://github.com/mr5z/PropertyValidator/blob/main/LICENSE</PackageLicenseUrl>
</PropertyGroup>

<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Remove="tests\**" />
</ItemGroup>

Expand Down
52 changes: 52 additions & 0 deletions test/PropertyValidator.Test/Exceptions/PropertyException.test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using PropertyValidator.Models;

namespace PropertyValidator.Test.Exceptions;

public class PropertyException
{
[Test, Description("Must be able to get the empty ValidationResultArgs.")]
public void MustGetEmptyResultArgs()
{
var exception =
new PropertyValidator.Exceptions.PropertyException(
new ValidationResultArgs(
new Dictionary<string, IEnumerable<string?>>()
)
);
Assert.Multiple(() =>
{
Assert.That(exception.ValidationResultArgs.HasError, Is.EqualTo(false));
Assert.That(exception.ValidationResultArgs.FirstError, Is.Null);
Assert.That(exception.ValidationResultArgs.ErrorMessages, Is.Null);
Assert.That(exception.ValidationResultArgs.ErrorDictionary, Is.Empty);
});
}

[Test, Description("Must be able to get the non-empty ValidationResultArgs.")]
[TestCase("A", new[] { "1st", "2nd" })]
[TestCase("B", new[] { "1st", "2nd", "3rd" })]
[TestCase("C", new[] { "1st", "2nd", "3rd", "4th" })]
public void MustGetNonEmptyResultArgs(string propertyName, IEnumerable<string> errorMessages)
{
var exception =
new PropertyValidator.Exceptions.PropertyException(
new ValidationResultArgs(
new Dictionary<string, IEnumerable<string?>>
{
{ propertyName, errorMessages }
}
)
);

Assert.Multiple(() =>
{
Assert.That(exception.ValidationResultArgs.HasError, Is.EqualTo(true));
Assert.That(exception.ValidationResultArgs.FirstError, Is.Not.Null);
Assert.That(exception.ValidationResultArgs.ErrorMessages, Is.Not.Null);
Assert.That(exception.ValidationResultArgs.ErrorDictionary, Is.Not.Empty);
Assert.That(exception.ValidationResultArgs.FirstError, Is.EqualTo("1st"));
Assert.That(exception.ValidationResultArgs.ErrorMessages, Contains.Item("2nd"));
Assert.That(exception.ValidationResultArgs.ErrorDictionary.Values.SelectMany(x => x), Contains.Item("2nd"));
});
}
}
36 changes: 36 additions & 0 deletions test/PropertyValidator.Test/Helpers/ReflectionHelper.test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace PropertyValidator.Test.Helpers;

public class ReflectionHelper
{
private class Dummy
{
private readonly int x = 1;
private readonly string y = "2";

public int GetX() => x;
public string GetY() => y;
}

[Test, Description("Must retrieve the fields and set their values.")]
public void GetFieldTest()
{
var propX = PropertyValidator.Helpers.ReflectionHelper.GetField(typeof(Dummy), "x");
var propY = PropertyValidator.Helpers.ReflectionHelper.GetField(typeof(Dummy), "y");

Assert.Multiple(() =>
{
Assert.That(propX, Is.Not.Null);
Assert.That(propY, Is.Not.Null);
});

Dummy dummy = new();
propX?.SetValue(dummy, 2);
propY?.SetValue(dummy, "1");

Assert.Multiple(() =>
{
Assert.That(dummy.GetX(), Is.EqualTo(2));
Assert.That(dummy.GetY(), Is.EqualTo("1"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void MustThrowForMultipleCalls()
[Test, Description("Validate() must not throw Exception if setup properly.")]
public void MustNotThrowWhenConfigured()
{
var ex1 = new Exception("This shouldn't be thrown.");
var ex1 = new Exception("This should be thrown.");
try
{
validationService.For(new DummyViewModel());
Expand Down Expand Up @@ -115,7 +115,7 @@ public void MustValidateStringRequiredRule(DummyViewModel vm)
Assert.That(result, Is.EqualTo(true));
}

[Test, Description("Validate() must validate for RangeLengthRule() rule.")]
[Test, Description("Validate() must return true with RangeLengthRule() rule.")]
[TestCaseSource(nameof(DummyViewModelFixturesRangeLengthRule))]
public void MustValidateRangeLengthRule(DummyViewModel vm, RangeLengthRule rule)
{
Expand All @@ -125,7 +125,7 @@ public void MustValidateRangeLengthRule(DummyViewModel vm, RangeLengthRule rule)

var result = validationService.Validate();

Assert.That(result, Is.EqualTo(true), $"Didn't succeed because vb.Value: '{vm.Value}', length: {vm.Value?.Length}");
Assert.That(result, Is.EqualTo(true), $"Didn't succeed because vm.Value: '{vm.Value}', length: {vm.Value?.Length}");
}

[Test, Description("PropertyInvalid must be invoked upon violation of property rules.")]
Expand Down

0 comments on commit 6063233

Please sign in to comment.