Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions SpecExpress/src/SpecExpress.Test/Entities/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpecExpress.Test.Entities
{
public class Person
{
public string Name { get; set; }

public Address2 MailingAddress { get; set; }

public Address2 ShippingAddress { get; set; }
}

public class Address2
{
public const string MailingType = "MAIL";

public const string ShippingType = "SHIP";

public string Type { get; set; }
public string AddressLine { get; set; }
public string City { get; set; }
public string State { get; set; }
}

public class PersonSpecification : Validates<Person>
{
public PersonSpecification()
{
Check(_ => _.Name).Required();

Check(_ => _.MailingAddress).Optional()
.Specification(v => v.Check(x => x.Type == Address2.MailingType))
.And
.Specification<Address2Specification>();

Check(_ => _.ShippingAddress).Optional()
.Specification(v => v.Check(x => x.Type == Address2.ShippingType))
.And
.Specification<Address2Specification>();
}
}

public class Address2Specification : Validates<Address2>
{
public Address2Specification()
{
Check(_ => _.AddressLine).Required();
Check(_ => _.City).Required();
Check(_ => _.State).Required();
}
}
}
97 changes: 97 additions & 0 deletions SpecExpress/src/SpecExpress.Test/PersonSpecificationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SpecExpress.Test.Entities;

namespace SpecExpress.Test
{
[TestFixture]
public class PersonSpecificationTests
{
[TestFixtureSetUp]
public void FixtureSetup()
{
ValidationCatalog.Reset();
ValidationCatalog.Scan(x => x.AddAssembly(typeof(Person).Assembly));
ValidationCatalog.AssertConfigurationIsValid();
}

[TestFixtureTearDown]
public void FixtureTeardown()
{
ValidationCatalog.Reset();
}

[Test]
public void Valid_Person()
{
var person = new Person()
{
Name = "Someone",
MailingAddress =
new Address2()
{
AddressLine = "Dallas Pkwy",
City = "Dallas",
State = "TX",
Type = Address2.MailingType
},
ShippingAddress =
new Address2()
{
AddressLine = "Dallas Pkwy",
City = "Dallas",
State = "TX",
Type = Address2.ShippingType
},
};

var results = ValidationCatalog.Validate(person);

Assert.That(results.IsValid, Is.True);

CollectionAssert.IsEmpty(results.All());
}

[Test]

public void Invalid_Person()
{
var person = new Person()
{
Name = "Someone",
MailingAddress =
new Address2()
{
AddressLine = "Dallas Pkwy",
City = "Dallas",
State = "TX",
},
ShippingAddress =
new Address2()
{
//AddressLine = "Dallas Pkwy",
//City = "Dallas",
//State = "TX",
Type = Address2.ShippingType
},
};

var results = ValidationCatalog.Validate(person);

foreach (var result in results.All())
{
Console.WriteLine(result.Message);
}

Assert.That(results.IsValid, Is.False);

CollectionAssert.IsNotEmpty(results.All());



}
}
}
2 changes: 2 additions & 0 deletions SpecExpress/src/SpecExpress.Test/SpecExpress.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@
<Compile Include="Container\ComplexTypesTests.cs" />
<Compile Include="ContactSpecification.cs" />
<Compile Include="CustomerAddressSpecification.cs" />
<Compile Include="Entities\Person.cs" />
<Compile Include="ExceptionsTest.cs" />
<Compile Include="InlineSpecificationTests.cs" />
<Compile Include="PersonSpecificationTests.cs" />
<Compile Include="PolymorphismTests.cs" />
<Compile Include="RequiredEnumsTests.cs" />
<Compile Include="RuleTreeTests\RuleExpressionFactoryTest.cs" />
Expand Down
12 changes: 12 additions & 0 deletions SpecExpress/src/SpecExpress/Util/RuleValidatorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ private static PropertyInfo GetValidationProperty(PropertyValidator validator, T
_propertyBag.Add(instanceType, new Dictionary<string, PropertyInfo>());
}
var propertyBag = _propertyBag[instanceType];

if (string.IsNullOrEmpty(validator.PropertyName))
{
return null;
}

if (!propertyBag.ContainsKey(validator.PropertyName))
{
propertyBag.Add(validator.PropertyName, instanceType.GetProperty(validator.PropertyName));
Expand Down Expand Up @@ -176,6 +182,12 @@ private static PropertyInfo GetValidationProperty(PropertyValidator validator, T
_propertyBag.Add(instanceType, new Dictionary<string, PropertyInfo>());
}
var propertyBag = _propertyBag[instanceType];

if (string.IsNullOrEmpty(validator.PropertyName))
{
return null;
}

if (!propertyBag.ContainsKey(validator.PropertyName))
{
propertyBag.Add(validator.PropertyName, instanceType.GetProperty(validator.PropertyName));
Expand Down