Skip to content

Commit

Permalink
Porting FluentValidation on .Net 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Apr 22, 2020
1 parent 9cba444 commit 4e28b50
Show file tree
Hide file tree
Showing 42 changed files with 1,431 additions and 87 deletions.
10 changes: 10 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<Project>
<PropertyGroup>
<NoWarn>NU1701</NoWarn>
</PropertyGroup>
<PropertyGroup>
<FrameworkPathOverride Condition="'$(TargetFramework)|$(OS)' == 'net35|Windows_NT'">$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
<FrameworkPathOverride Condition="'$(TargetFramework)|$(OS)' == 'net35|Unix'">$(MONO_DIR)/../lib/mono/2.0-api</FrameworkPathOverride>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "2.2.100"
"version": "2.2.204"
}
}
7 changes: 5 additions & 2 deletions src/FluentValidation.Tests/AbstractValidatorTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace FluentValidation.Tests {
using Xunit;
using Results;

#if NET35
using Task = System.Threading.Tasks.TaskEx;
#endif

public class AbstractValidatorTester {
TestValidator validator;
Expand Down Expand Up @@ -272,7 +275,7 @@ public void WhenPreValidationReturnsFalse_ResultReturnToUserImmediatly_Validate(

[Theory]
[MemberData(nameof(PreValidationReturnValueTheoryData))]
public async Task WhenPreValidationReturnsFalse_ResultReturnToUserImmediatly_ValidateAsync(ValidationResult preValidationResult) {
public async System.Threading.Tasks.Task WhenPreValidationReturnsFalse_ResultReturnToUserImmediatly_ValidateAsync(ValidationResult preValidationResult) {
testValidatorWithPreValidate.PreValidateMethod = (context, validationResult) => {
foreach (ValidationFailure validationFailure in preValidationResult.Errors) {
validationResult.Errors.Add(validationFailure);
Expand Down Expand Up @@ -316,7 +319,7 @@ public void WhenPreValidationReturnsTrue_ValidatorsGetHit_Validate() {
}

[Fact]
public async Task WhenPreValidationReturnsTrue_ValidatorsGetHit_ValidateAsync() {
public async System.Threading.Tasks.Task WhenPreValidationReturnsTrue_ValidatorsGetHit_ValidateAsync() {
const string testProperty = "TestProperty";
const string testMessage = "Test Message";
testValidatorWithPreValidate.PreValidateMethod = (context, validationResult) => {
Expand Down
10 changes: 9 additions & 1 deletion src/FluentValidation.Tests/AccessorCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@
using Xunit;
using Xunit.Abstractions;

public class AccessorCacheTests {
public class AccessorCacheTests
{
private readonly ITestOutputHelper output;
#if NET35
public AccessorCacheTests() :
this(new DebugWindowOutputHelper())
{

}
#endif

public AccessorCacheTests(ITestOutputHelper output) {
this.output = output;
Expand Down
16 changes: 12 additions & 4 deletions src/FluentValidation.Tests/CollectionValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ namespace FluentValidation.Tests {
using System.Threading.Tasks;
using Xunit;


#if NET35
using Task = System.Threading.Tasks.TaskEx;
#endif

public class CollectionValidatorTests {
private Person person;
private object _lock = new object();
Expand Down Expand Up @@ -178,7 +181,7 @@ public void Validates_child_validator_synchronously() {
}

[Fact]
public async Task Validates_child_validator_asynchronously() {
public async System.Threading.Tasks.Task Validates_child_validator_asynchronously() {
var validator = new ComplexValidationTester.TracksAsyncCallValidator<Person>();
var childValidator = new ComplexValidationTester.TracksAsyncCallValidator<Person>();
childValidator.RuleFor(x => x.Forename).NotNull();
Expand All @@ -189,7 +192,7 @@ public async Task Validates_child_validator_asynchronously() {
}

[Fact]
public async Task Collection_async_RunsTasksSynchronously() {
public async System.Threading.Tasks.Task Collection_async_RunsTasksSynchronously() {
var result = new List<bool>();
var validator = new InlineValidator<Person>();
var orderValidator = new InlineValidator<Order>();
Expand All @@ -205,7 +208,12 @@ public async Task Collection_async_RunsTasksSynchronously() {
await validator.ValidateAsync(person);

Assert.NotEmpty(result);
Assert.All(result, Assert.True);
#if NET35
AssertEx
#else
Assert
#endif
.All(result, Assert.True);
}

public class OrderValidator : AbstractValidator<Order> {
Expand Down
17 changes: 17 additions & 0 deletions src/FluentValidation.Tests/Compatibility/DataAnnotations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#if NET35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.ComponentModel.DataAnnotations
{
[System.AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
sealed class DisplayAttribute : Attribute
{
public string Name { get; set; }
public Type ResourceType { get; set; }
}
}
#endif
70 changes: 70 additions & 0 deletions src/FluentValidation.Tests/Compatibility/Genric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#if NET35
using System.Reflection;

namespace System.Collections.Generic
{
//
// Summary:
// Represents a strongly-typed, read-only collection of elements.
//
// Type parameters:
// T:
// The type of the elements.This type parameter is covariant. That is, you can use
// either the type you specified or any type that is more derived. For more information
// about covariance and contravariance, see Covariance and Contravariance in Generics.
//[TypeDependencyAttribute("System.SZArrayHelper")]
public interface IReadOnlyCollection<T> : IEnumerable<T>, IEnumerable
{
//
// Summary:
// Gets the number of elements in the collection.
//
// Returns:
// The number of elements in the collection.
int Count { get; }
}

//
// Summary:
// Represents a read-only collection of elements that can be accessed by index.
//
// Type parameters:
// T:
// The type of elements in the read-only list. This type parameter is covariant.
// That is, you can use either the type you specified or any type that is more derived.
// For more information about covariance and contravariance, see Covariance and
// Contravariance in Generics.
//[DefaultMember("Item")]
//[TypeDependencyAttribute("System.SZArrayHelper")]
public interface IReadOnlyList<T> : IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
//
// Summary:
// Gets the element at the specified index in the read-only list.
//
// Parameters:
// index:
// The zero-based index of the element to get.
//
// Returns:
// The element at the specified index in the read-only list.
T this[int index] { get; }
}

static class RealdOnlyExtension
{
class RL<T>: ObjectModel.ReadOnlyCollection<T>, IReadOnlyList<T>
{
public RL(IList<T> source):base(source)
{

}
}

public static IReadOnlyList<T> AsReadOnlyEx<T>(this IList<T> source)
{
return new RL<T>(source);
}
}
}
#endif
13 changes: 13 additions & 0 deletions src/FluentValidation.Tests/Compatibility/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace System
{
static class StringExtensions
{
public static bool IsNullOrWhiteSpace(this string source) =>
#if NET35
string.IsNullOrEmpty(source) || source.Trim().Length == 0;
#else
string.IsNullOrWhiteSpace(source);
#endif

}
}
17 changes: 17 additions & 0 deletions src/FluentValidation.Tests/Compatibility/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if NET35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
static class TypeExtensions
{
public static Type GetTypeInfo(this Type source)
{
return source;
}
}
}
# endif
Loading

0 comments on commit 4e28b50

Please sign in to comment.