-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Oom
authored
May 6, 2019
1 parent
574835c
commit 015c0ed
Showing
14 changed files
with
1,041 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Spinit.Expressions.UnitTests/Infrastructure/XunitSerializable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Newtonsoft.Json; | ||
using Xunit.Abstractions; | ||
|
||
namespace Spinit.Expressions.UnitTests.Infrastructure | ||
{ | ||
public class XunitSerializable : IXunitSerializable | ||
{ | ||
private static readonly JsonSerializerSettings settings = new JsonSerializerSettings | ||
{ | ||
TypeNameHandling = TypeNameHandling.All | ||
}; | ||
|
||
public void Deserialize(IXunitSerializationInfo info) | ||
{ | ||
JsonConvert.PopulateObject(info.GetValue<string>("json"), this, settings); | ||
} | ||
|
||
public void Serialize(IXunitSerializationInfo info) | ||
{ | ||
info.AddValue("json", JsonConvert.SerializeObject(this, settings)); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Spinit.Expressions.UnitTests/Internals/EnumerableExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using Spinit.Expressions.UnitTests.Infrastructure; | ||
using Xunit; | ||
|
||
namespace Spinit.Expressions.UnitTests.Internals | ||
{ | ||
public class EnumerableExtensionsTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(GetScenarios))] | ||
public void IsEmptyShouldReturnExpectedValue(Scenario scenario) | ||
{ | ||
var result = scenario.Enumerable.IsEmpty(); | ||
Assert.Equal(scenario.ExpectedResult, result); | ||
} | ||
|
||
public static TheoryData<Scenario> GetScenarios() | ||
{ | ||
return new TheoryData<Scenario> | ||
{ | ||
new Scenario | ||
{ | ||
Enumerable = Array.Empty<bool>(), | ||
ExpectedResult = true | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = Array.Empty<string>(), | ||
ExpectedResult = true | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = new Collection<decimal>(), | ||
ExpectedResult = true | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = new List<int>(), | ||
ExpectedResult = true | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = new HashSet<int>(), | ||
ExpectedResult = true | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = new bool[] { false }, | ||
ExpectedResult = false | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = new string[] { "" }, | ||
ExpectedResult = false | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = new Collection<decimal>{ 1M }, | ||
ExpectedResult = false | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = new List<int>{ 0, 1, 2 }, | ||
ExpectedResult = false | ||
}, | ||
new Scenario | ||
{ | ||
Enumerable = new HashSet<int>{ 123 }, | ||
ExpectedResult = false | ||
} | ||
}; | ||
} | ||
|
||
public class Scenario : XunitSerializable | ||
{ | ||
public IEnumerable Enumerable { get; set; } | ||
public bool ExpectedResult { get; set; } | ||
} | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
src/Spinit.Expressions.UnitTests/Internals/TypeExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace Spinit.Expressions.UnitTests | ||
{ | ||
public class TypeExtensionsTests | ||
{ | ||
public class IsNullable | ||
{ | ||
[Fact] | ||
public void StringIsNotNullable() | ||
{ | ||
Assert.False(typeof(string).IsNullable()); | ||
} | ||
|
||
[Fact] | ||
public void IntIsNotNullable() | ||
{ | ||
Assert.False(typeof(int).IsNullable()); | ||
} | ||
|
||
[Fact] | ||
public void NullabeIntIsNullable() | ||
{ | ||
Assert.True(typeof(int?).IsNullable()); | ||
Assert.True(typeof(Nullable<int>).IsNullable()); | ||
} | ||
|
||
[Fact] | ||
public void ClassDoesNotImplementNullable() | ||
{ | ||
Assert.False(typeof(TestClass).IsNullable()); | ||
} | ||
|
||
#if false | ||
// only for C#8, nullable string | ||
[Fact] | ||
public void NullabestringIsNullable() | ||
{ | ||
Assert.True(typeof(string?).IsNullable()); | ||
Assert.True(typeof(Nullable<string>).IsNullable()); | ||
} | ||
#endif | ||
public class TestClass | ||
{ | ||
public int IntProperty1 { get; set; } | ||
} | ||
} | ||
|
||
public class IsNullableOfT | ||
{ | ||
[Fact] | ||
public void NullableIntIsNotNullableOfBool() | ||
{ | ||
Assert.False(typeof(int?).IsNullableOf<bool>()); | ||
} | ||
|
||
[Fact] | ||
public void NullableIntIsNullableOfInt() | ||
{ | ||
Assert.True(typeof(int?).IsNullableOf<int>()); | ||
} | ||
} | ||
|
||
public class IsNullableOf | ||
{ | ||
[Fact] | ||
public void NullableIntIsNullableOfPrimitiveType() | ||
{ | ||
Assert.True(typeof(int?).IsNullableOf(x => x.IsPrimitive)); | ||
} | ||
|
||
[Fact] | ||
public void NullableIntIsNullableOfInt() | ||
{ | ||
Assert.True(typeof(int?).IsNullableOf(x => x == typeof(int))); | ||
} | ||
} | ||
|
||
public class IsEnumerable | ||
{ | ||
[Fact] | ||
public void StringIsAnEnumerable() | ||
{ | ||
Assert.True(typeof(string).IsEnumerable()); | ||
} | ||
|
||
[Fact] | ||
public void ICollectionOfStringIsAnEnumerable() | ||
{ | ||
Assert.True(typeof(ICollection<string>).IsEnumerable()); | ||
} | ||
|
||
[Fact] | ||
public void ListOfStringIsAnEnumerable() | ||
{ | ||
Assert.True(typeof(List<string>).IsEnumerable()); | ||
} | ||
|
||
[Fact] | ||
public void BoolArrayIsAnEnumerable() | ||
{ | ||
Assert.True(typeof(bool[]).IsEnumerable()); | ||
} | ||
} | ||
|
||
public class IsEnumerableOfT | ||
{ | ||
[Fact] | ||
public void StringIsAnEnumerableOfChar() | ||
{ | ||
Assert.True(typeof(string).IsEnumerableOf<char>()); | ||
} | ||
|
||
[Fact] | ||
public void ICollectionOfStringIsAnEnumerableOfString() | ||
{ | ||
Assert.True(typeof(ICollection<string>).IsEnumerableOf<string>()); | ||
} | ||
|
||
[Fact] | ||
public void ListOfStringIsAnEnumerableOfString() | ||
{ | ||
Assert.True(typeof(List<string>).IsEnumerableOf<string>()); | ||
} | ||
} | ||
|
||
public class IsEnumerableOf | ||
{ | ||
[Fact] | ||
public void IntArrayIsEnumerableOfPrimitiveType() | ||
{ | ||
Assert.True(typeof(int[]).IsEnumerableOf(x => x.IsPrimitive)); | ||
} | ||
|
||
[Fact] | ||
public void StringListIsEnumerableOfString() | ||
{ | ||
Assert.True(typeof(List<string>).IsEnumerableOf(x => x == typeof(string))); | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/Spinit.Expressions.UnitTests/PredicateGenerator/PredicateGeneratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using Xunit; | ||
|
||
namespace Spinit.Expressions.UnitTests | ||
{ | ||
public class PredicateGeneratorTests | ||
{ | ||
[Fact] | ||
public void TestIdPredicate() | ||
{ | ||
var filter = new TodoFilter { Id = 123 }; | ||
var predicateGenerator = new PredicateGenerator<TodoFilter, Todo>(); | ||
var predicate = predicateGenerator.Generate(filter); | ||
//var expectedPredicate = ExpressionExtensions.Expression(x => x.Id == 123); | ||
Expression<Func<Todo, bool>> expectedPredicate = x => x.Id == 123; | ||
Assert.Equal(expectedPredicate.ToString(), predicate.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void TestTypePredicate() | ||
{ | ||
var filter = new TodoFilter { Type = new[] { "a", "b" } }; | ||
var predicateGenerator = new PredicateGenerator<TodoFilter, Todo>(); | ||
var predicate = predicateGenerator.Generate(filter); | ||
|
||
var items = new List<Todo> | ||
{ | ||
new Todo { Type = "a" }, | ||
new Todo { Type = "b" }, | ||
new Todo { Type = "c" }, | ||
}; | ||
var filteredItems = items.AsQueryable().Where(predicate).ToList(); | ||
Assert.DoesNotContain(filteredItems, x => x.Type == "c"); | ||
} | ||
|
||
//[Fact] | ||
//public void TestTagsPredicate() | ||
//{ | ||
// var filter = new TodoFilter { Tags = new[] { "a", "b" } }; | ||
// var predicateGenerator = new PredicateGenerator<TodoFilter, Todo>(); | ||
// var predicate = predicateGenerator.Generate(filter); | ||
|
||
// var items = new List<Todo> | ||
// { | ||
// new Todo {Tags = new [] { "a" } }, | ||
// new Todo {Tags = new [] { "b" } }, | ||
// new Todo {Tags = new [] { "c" } }, | ||
// }; | ||
// var filteredItems = items.AsQueryable().Where(predicate).ToList(); | ||
// Assert.DoesNotContain(filteredItems, x => x.Tags.Contains("c")); | ||
//} | ||
|
||
public class TodoFilter | ||
{ | ||
public int? Id { get; set; } | ||
|
||
public IEnumerable<string> Type { get; set; } | ||
|
||
// TODO: add OperatorAttribute (AND | OR) + implement IEnumerable support on target side | ||
//public IEnumerable<string> Tags { get; set; } | ||
} | ||
|
||
public class Todo | ||
{ | ||
public int Id { get; set; } | ||
public string Type { get; set; } | ||
//public IEnumerable<string> Tags { get; set; } | ||
} | ||
} | ||
} |
Oops, something went wrong.