From 2f2703bdc9c32ad514aa6b9f804044b0bff802be Mon Sep 17 00:00:00 2001 From: Ogoun Date: Tue, 9 Apr 2019 15:54:17 +0300 Subject: [PATCH] Append unit tests --- ZeroLevel.UnitTests/CollectionsTests.cs | 62 +++ ZeroLevel.UnitTests/MappingTest.cs | 47 ++ ZeroLevel.UnitTests/Models/PocoFields.cs | 11 + ZeroLevel.UnitTests/Models/PocoProperties.cs | 11 + .../Specifications/IsFlagSpecification.cs | 21 + .../Specifications/LongNumberSpecification.cs | 21 + .../Specifications/NumberSpecification.cs | 21 + .../Specifications/RealSpecification.cs | 21 + .../Specifications/SummarySpecification.cs | 21 + .../Specifications/TitleSpecification.cs | 21 + ZeroLevel.UnitTests/Models/TestDTO.cs | 12 + ZeroLevel.UnitTests/PredicateBuilderTests.cs | 114 +++++ ZeroLevel.UnitTests/QueriesTests.cs | 190 ++++++++ .../SpecificationPatternTest.cs | 419 ++++++++++++++++++ .../ZeroLevel.UnitTests.csproj | 13 + 15 files changed, 1005 insertions(+) create mode 100644 ZeroLevel.UnitTests/CollectionsTests.cs create mode 100644 ZeroLevel.UnitTests/Models/PocoFields.cs create mode 100644 ZeroLevel.UnitTests/Models/PocoProperties.cs create mode 100644 ZeroLevel.UnitTests/Models/Specifications/IsFlagSpecification.cs create mode 100644 ZeroLevel.UnitTests/Models/Specifications/LongNumberSpecification.cs create mode 100644 ZeroLevel.UnitTests/Models/Specifications/NumberSpecification.cs create mode 100644 ZeroLevel.UnitTests/Models/Specifications/RealSpecification.cs create mode 100644 ZeroLevel.UnitTests/Models/Specifications/SummarySpecification.cs create mode 100644 ZeroLevel.UnitTests/Models/Specifications/TitleSpecification.cs create mode 100644 ZeroLevel.UnitTests/Models/TestDTO.cs create mode 100644 ZeroLevel.UnitTests/PredicateBuilderTests.cs create mode 100644 ZeroLevel.UnitTests/QueriesTests.cs create mode 100644 ZeroLevel.UnitTests/SpecificationPatternTest.cs diff --git a/ZeroLevel.UnitTests/CollectionsTests.cs b/ZeroLevel.UnitTests/CollectionsTests.cs new file mode 100644 index 0000000..5af0b42 --- /dev/null +++ b/ZeroLevel.UnitTests/CollectionsTests.cs @@ -0,0 +1,62 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using ZeroLevel.Services.Collections; + +namespace ZeroLevel.UnitTests +{ + [TestClass] + public class CollectionsTests + { + [TestMethod] + public void RoundRobinCollectionTest() + { + // Arrange + var collection = new RoundRobinCollection(); + var iter1 = new int[] { 1, 2, 3 }; + var iter2 = new int[] { 2, 3, 1 }; + var iter3 = new int[] { 3, 1, 2 }; + + var iter11 = new int[] { 1, 3 }; + var iter12 = new int[] { 3, 1 }; + // Act + collection.Add(1); + collection.Add(2); + collection.Add(3); + // Assert + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter1)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter1)); + Assert.IsTrue(collection.MoveNext()); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter2)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter2)); + Assert.IsTrue(collection.MoveNext()); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter3)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter3)); + + collection.Remove(2); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter11)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter11)); + Assert.IsTrue(collection.MoveNext()); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter12)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter12)); + } + + [TestMethod] + public void RoundRobinOverCollectionTest() + { + var arr = new int[] { 1, 2, 3 }; + // Arrange + var collection = new RoundRobinOverCollection(arr); + var iter1 = new int[] { 1, 2, 3 }; + var iter2 = new int[] { 2, 3, 1 }; + var iter3 = new int[] { 3, 1, 2 }; + // Act + // Assert + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter1)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter2)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter3)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter1)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter2)); + Assert.IsTrue(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter3)); + } + } +} diff --git a/ZeroLevel.UnitTests/MappingTest.cs b/ZeroLevel.UnitTests/MappingTest.cs index 02fe775..4bd142b 100644 --- a/ZeroLevel.UnitTests/MappingTest.cs +++ b/ZeroLevel.UnitTests/MappingTest.cs @@ -3,6 +3,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using ZeroLevel.Services.ObjectMapping; using ZeroMappingTest.Models; using System.Collections.Generic; +using BusinessApp.DatabaseTest.Model; namespace ZeroMappingTest { @@ -203,5 +204,51 @@ namespace ZeroMappingTest Assert.AreNotSame(mapper1, mapper3); Assert.AreNotSame(mapper3, mapper2); } + + [TestMethod] + public void PocoFieldMapper() + { + // Arrange + var date = new DateTime(2005, 09, 27); + var mapper = new TypeMapper(typeof(PocoFields)); + var obj = new PocoFields { Id = 1000, Date = date, Title = "Caption" }; + + // Assert + Assert.AreEqual(mapper.EntityType, typeof(PocoFields)); + + Assert.IsTrue(mapper.Exists("Id")); + Assert.IsTrue(mapper.Exists("Date")); + Assert.IsTrue(mapper.Exists("Title")); + + Assert.AreEqual(mapper.Get(obj, "Id"), (long)1000); + Assert.AreEqual(mapper.Get(obj, "Date"), date); + Assert.AreEqual(mapper.Get(obj, "Title"), "Caption"); + + mapper.Set(obj, "Id", 1001); + Assert.AreEqual(mapper.Get(obj, "Id"), (long)1001); + } + + [TestMethod] + public void PocoPropertiesMapper() + { + // Arrange + var date = new DateTime(2005, 09, 27); + var mapper = new TypeMapper(typeof(PocoProperties)); + var obj = new PocoProperties { Id = 1000, Date = date, Title = "Caption" }; + + // Assert + Assert.AreEqual(mapper.EntityType, typeof(PocoProperties)); + + Assert.IsTrue(mapper.Exists("Id")); + Assert.IsTrue(mapper.Exists("Date")); + Assert.IsTrue(mapper.Exists("Title")); + + Assert.AreEqual(mapper.Get(obj, "Id"), (long)1000); + Assert.AreEqual(mapper.Get(obj, "Date"), date); + Assert.AreEqual(mapper.Get(obj, "Title"), "Caption"); + + mapper.Set(obj, "Id", 1001); + Assert.AreEqual(mapper.Get(obj, "Id"), (long)1001); + } } } diff --git a/ZeroLevel.UnitTests/Models/PocoFields.cs b/ZeroLevel.UnitTests/Models/PocoFields.cs new file mode 100644 index 0000000..cd52ada --- /dev/null +++ b/ZeroLevel.UnitTests/Models/PocoFields.cs @@ -0,0 +1,11 @@ +using System; + +namespace BusinessApp.DatabaseTest.Model +{ + public class PocoFields + { + public long Id; + public string Title; + public DateTime Date; + } +} diff --git a/ZeroLevel.UnitTests/Models/PocoProperties.cs b/ZeroLevel.UnitTests/Models/PocoProperties.cs new file mode 100644 index 0000000..f7d6d66 --- /dev/null +++ b/ZeroLevel.UnitTests/Models/PocoProperties.cs @@ -0,0 +1,11 @@ +using System; + +namespace BusinessApp.DatabaseTest.Model +{ + public class PocoProperties + { + public long Id { get; set; } + public string Title { get; set; } + public DateTime Date { get; set; } + } +} diff --git a/ZeroLevel.UnitTests/Models/Specifications/IsFlagSpecification.cs b/ZeroLevel.UnitTests/Models/Specifications/IsFlagSpecification.cs new file mode 100644 index 0000000..b33faa5 --- /dev/null +++ b/ZeroLevel.UnitTests/Models/Specifications/IsFlagSpecification.cs @@ -0,0 +1,21 @@ +using ZeroLevel.Specification; +using ZeroSpecificationPatternsTest.Models; + +namespace ZeroSpecificationPatternsTest.Specifications +{ + public class IsFlagSpecification : + BaseSpecification + { + private readonly bool _expected; + + public IsFlagSpecification(bool expected) + { + _expected = expected; + } + + public override bool IsSatisfiedBy(TestDTO o) + { + return o.IsFlag == _expected; + } + } +} diff --git a/ZeroLevel.UnitTests/Models/Specifications/LongNumberSpecification.cs b/ZeroLevel.UnitTests/Models/Specifications/LongNumberSpecification.cs new file mode 100644 index 0000000..6b19ec4 --- /dev/null +++ b/ZeroLevel.UnitTests/Models/Specifications/LongNumberSpecification.cs @@ -0,0 +1,21 @@ +using ZeroLevel.Specification; +using ZeroSpecificationPatternsTest.Models; + +namespace ZeroSpecificationPatternsTest.Specifications +{ + public class LongNumberSpecification : + BaseSpecification + { + private readonly long _expected; + + public LongNumberSpecification(long expected) + { + _expected = expected; + } + + public override bool IsSatisfiedBy(TestDTO o) + { + return o.LongNumber == _expected; + } + } +} diff --git a/ZeroLevel.UnitTests/Models/Specifications/NumberSpecification.cs b/ZeroLevel.UnitTests/Models/Specifications/NumberSpecification.cs new file mode 100644 index 0000000..60bb3c9 --- /dev/null +++ b/ZeroLevel.UnitTests/Models/Specifications/NumberSpecification.cs @@ -0,0 +1,21 @@ +using ZeroLevel.Specification; +using ZeroSpecificationPatternsTest.Models; + +namespace ZeroSpecificationPatternsTest.Specifications +{ + public class NumberSpecification : + BaseSpecification + { + private readonly int _expected; + + public NumberSpecification(int expected) + { + _expected = expected; + } + + public override bool IsSatisfiedBy(TestDTO o) + { + return o.Number == _expected; + } + } +} diff --git a/ZeroLevel.UnitTests/Models/Specifications/RealSpecification.cs b/ZeroLevel.UnitTests/Models/Specifications/RealSpecification.cs new file mode 100644 index 0000000..36f3b7a --- /dev/null +++ b/ZeroLevel.UnitTests/Models/Specifications/RealSpecification.cs @@ -0,0 +1,21 @@ +using ZeroLevel.Specification; +using ZeroSpecificationPatternsTest.Models; + +namespace ZeroSpecificationPatternsTest.Specifications +{ + public class RealSpecification : + BaseSpecification + { + private readonly double _expected; + + public RealSpecification(double expected) + { + _expected = expected; + } + + public override bool IsSatisfiedBy(TestDTO o) + { + return o.Real == _expected; + } + } +} diff --git a/ZeroLevel.UnitTests/Models/Specifications/SummarySpecification.cs b/ZeroLevel.UnitTests/Models/Specifications/SummarySpecification.cs new file mode 100644 index 0000000..a885d20 --- /dev/null +++ b/ZeroLevel.UnitTests/Models/Specifications/SummarySpecification.cs @@ -0,0 +1,21 @@ +using ZeroLevel.Specification; +using ZeroSpecificationPatternsTest.Models; + +namespace ZeroSpecificationPatternsTest.Specifications +{ + public class SummarySpecification : + BaseSpecification + { + private readonly string _expected; + + public SummarySpecification(string expected) + { + _expected = expected; + } + + public override bool IsSatisfiedBy(TestDTO o) + { + return o.Summary == _expected; + } + } +} diff --git a/ZeroLevel.UnitTests/Models/Specifications/TitleSpecification.cs b/ZeroLevel.UnitTests/Models/Specifications/TitleSpecification.cs new file mode 100644 index 0000000..8dd05bc --- /dev/null +++ b/ZeroLevel.UnitTests/Models/Specifications/TitleSpecification.cs @@ -0,0 +1,21 @@ +using ZeroLevel.Specification; +using ZeroSpecificationPatternsTest.Models; + +namespace ZeroSpecificationPatternsTest.Specifications +{ + public class TitleSpecification : + BaseSpecification + { + private readonly string _expected; + + public TitleSpecification(string expected) + { + _expected = expected; + } + + public override bool IsSatisfiedBy(TestDTO o) + { + return o.Title == _expected; + } + } +} diff --git a/ZeroLevel.UnitTests/Models/TestDTO.cs b/ZeroLevel.UnitTests/Models/TestDTO.cs new file mode 100644 index 0000000..1c845ae --- /dev/null +++ b/ZeroLevel.UnitTests/Models/TestDTO.cs @@ -0,0 +1,12 @@ +namespace ZeroSpecificationPatternsTest.Models +{ + public class TestDTO + { + public bool IsFlag { get; set; } + public int Number; + public long LongNumber; + public string Title { get; set; } + public string Summary; + public double Real; + } +} diff --git a/ZeroLevel.UnitTests/PredicateBuilderTests.cs b/ZeroLevel.UnitTests/PredicateBuilderTests.cs new file mode 100644 index 0000000..642c7d0 --- /dev/null +++ b/ZeroLevel.UnitTests/PredicateBuilderTests.cs @@ -0,0 +1,114 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using ZeroLevel.Specification; + +namespace ZeroSpecificationPatternsTest +{ + [TestClass] + public class PredicateBuilderTests + { + [TestMethod] + public void PredicateBuilder_FromExpression_AND() + { + // Arrange + var expression = PredicateBuilder.True(); + // Act + var invoker = expression. + And(str => str.Length < 255). + And(str => char.IsLetter(str[0])). + And(str => char.IsUpper(str[0])).Compile(); + // Assert + Assert.IsFalse(invoker("test")); + Assert.IsTrue(invoker("Test")); + Assert.IsFalse(invoker("1test")); + Assert.IsFalse(invoker("Test" + new string('_', 260))); + } + + [TestMethod] + public void PredicateBuilder_FromFunc_AND() + { + // Arrange + var func = new Func(str => str.Length < 255); + // Act + var invoker = func. + And(str => str.Length < 255). + And(str => Char.IsLetter(str[0])). + And(str => Char.IsUpper(str[0])).Compile(); + // Assert + Assert.IsFalse(invoker("test")); + Assert.IsTrue(invoker("Test")); + Assert.IsFalse(invoker("1test")); + Assert.IsFalse(invoker("Test" + new string('_', 260))); + } + + [TestMethod] + public void PredicateBuilder_FromExpression_OR() + { + // Arrange + var expression = PredicateBuilder. + Create(str => str.Equals("hello", StringComparison.OrdinalIgnoreCase)); + // Act + var invoker = expression. + Or(str => str.Equals("world", StringComparison.OrdinalIgnoreCase)). + Or(str => str.Equals("test", StringComparison.OrdinalIgnoreCase)). + Or(str => str.Equals("wow", StringComparison.OrdinalIgnoreCase)). + Compile(); + // Assert + Assert.IsTrue(invoker("hello")); + Assert.IsTrue(invoker("world")); + Assert.IsTrue(invoker("test")); + Assert.IsTrue(invoker("wow")); + Assert.IsFalse(invoker("Tests")); + } + + [TestMethod] + public void PredicateBuilder_FromFunc_OR() + { + // Arrange + var func = new Func(str => str.Equals("hello", StringComparison.OrdinalIgnoreCase)); + // Act + var invoker = func. + Or(str => str.Equals("world", StringComparison.OrdinalIgnoreCase)). + Or(str => str.Equals("test", StringComparison.OrdinalIgnoreCase)). + Or(str => str.Equals("wow", StringComparison.OrdinalIgnoreCase)). + Compile(); + // Assert + Assert.IsTrue(invoker("hello")); + Assert.IsTrue(invoker("world")); + Assert.IsTrue(invoker("test")); + Assert.IsTrue(invoker("wow")); + Assert.IsFalse(invoker("Tests")); + } + + [TestMethod] + public void PredicateBuilder_FromExpression_NOT() + { + // Arrange + var expression = PredicateBuilder. + Create(i => i < 100 && i > 0); + // Act + var invoker = expression.Not(). + Compile(); + // Assert + Assert.IsFalse(invoker(1)); + Assert.IsFalse(invoker(50)); + Assert.IsTrue(invoker(100)); + Assert.IsTrue(invoker(0)); + } + + [TestMethod] + public void PredicateBuilder_FromFunc_NOT() + { + // Arrange + var expression = new Func(i => i < 100 && i > 0); + // Act + var invoker = expression.Not(). + Compile(); + // Assert + Assert.IsFalse(invoker(1)); + Assert.IsFalse(invoker(50)); + Assert.IsTrue(invoker(100)); + Assert.IsTrue(invoker(0)); + } + } +} diff --git a/ZeroLevel.UnitTests/QueriesTests.cs b/ZeroLevel.UnitTests/QueriesTests.cs new file mode 100644 index 0000000..4d3c86f --- /dev/null +++ b/ZeroLevel.UnitTests/QueriesTests.cs @@ -0,0 +1,190 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using ZeroLevel.Patterns.Queries; +using ZeroSpecificationPatternsTest.Models; + +namespace ZeroSpecificationPatternsTest +{ + /// + /// Summary description for UnitTest1 + /// + [TestClass] + public class QueriesTests + { + private static bool TestDTOEqual(TestDTO left, TestDTO right) + { + if (left == null && right == null) return true; + if (left == null || right == null) return false; + if (left.IsFlag != right.IsFlag) return false; + if (left.LongNumber != right.LongNumber) return false; + if (left.Number != right.Number) return false; + if (left.Real != right.Real) return false; + if (string.Compare(left.Summary, right.Summary, StringComparison.Ordinal) != 0) return false; + if (string.Compare(left.Title, right.Title, StringComparison.Ordinal) != 0) return false; + return true; + } + + [TestMethod] + public void MemoryStoragePostTest() + { + var storage = new MemoryStorage(); + var a1 = storage.Post(new TestDTO + { + IsFlag = false, + LongNumber = 100, + Number = 1000, + Real = 1000.0001, + Summary = "Summary #1", + Title = "Title #1" + }); + var a2 = storage.Post(new TestDTO + { + IsFlag = true, + LongNumber = 0, + Number = -500, + Real = 500.005, + Summary = "Summary #2", + Title = "Title #2" + }); + var a3 = storage.Post(new TestDTO + { + IsFlag = false, + LongNumber = -1, + Number = 500, + Real = -0.0001, + Summary = "Summary #3", + Title = "Title #3" + }); + Assert.IsTrue(a1.Success); + Assert.AreEqual(a1.Count, 1); + Assert.IsTrue(a2.Success); + Assert.AreEqual(a2.Count, 1); + Assert.IsTrue(a3.Success); + Assert.AreEqual(a3.Count, 1); + } + + [TestMethod] + public void MemoryStorageGetAllTest() + { + var set = new List(); + set.Add(new TestDTO + { + IsFlag = false, + LongNumber = 100, + Number = 1000, + Real = 1000.0001, + Summary = "Summary #1", + Title = "Title #1" + }); + set.Add(new TestDTO + { + IsFlag = true, + LongNumber = 0, + Number = -500, + Real = 500.005, + Summary = "Summary #2", + Title = "Title #2" + }); + set.Add(new TestDTO + { + IsFlag = false, + LongNumber = -1, + Number = 500, + Real = -0.0001, + Summary = "Summary #3", + Title = "Title #3" + }); + var storage = new MemoryStorage(); + foreach (var i in set) + { + var ar = storage.Post(i); + Assert.IsTrue(ar.Success); + Assert.AreEqual(ar.Count, 1); + } + // Test equals set and storage data + foreach (var i in storage.Get()) + { + Assert.IsTrue(set.Exists(dto => TestDTOEqual(i, dto))); + } + // Modify originals + foreach (var i in set) + { + i.Title = "###"; + } + // Test independences storage from original + foreach (var i in storage.Get()) + { + Assert.IsFalse(set.Exists(dto => TestDTOEqual(i, dto))); + } + } + + + [TestMethod] + public void MemoryStorageGetByTest() + { + var set = new List(); + set.Add(new TestDTO + { + IsFlag = false, + LongNumber = 100, + Number = 1000, + Real = 1000.0001, + Summary = "Summary #1", + Title = "Title #1" + }); + set.Add(new TestDTO + { + IsFlag = true, + LongNumber = 0, + Number = -500, + Real = 500.005, + Summary = "Summary #2", + Title = "Title #2" + }); + set.Add(new TestDTO + { + IsFlag = false, + LongNumber = -1, + Number = 500, + Real = -0.0001, + Summary = "Summary #3", + Title = "Title #3" + }); + var storage = new MemoryStorage(); + foreach (var i in set) + { + var ar = storage.Post(i); + Assert.IsTrue(ar.Success); + Assert.AreEqual(ar.Count, 1); + } + // Test equals set and storage data + foreach (var i in storage.Get()) + { + Assert.IsTrue(set.Exists(dto => TestDTOEqual(i, dto))); + } + foreach (var i in storage.Get(Query.ALL())) + { + Assert.IsTrue(set.Exists(dto => TestDTOEqual(i, dto))); + } + var result_eq = storage.Get(Query.EQ("Title", "Title #1")); + Assert.AreEqual(result_eq.Count(), 1); + Assert.IsTrue(TestDTOEqual(set[0], result_eq.First())); + + var result_neq = storage.Get(Query.NEQ("Title", "Title #1")); + Assert.AreEqual(result_neq.Count(), 2); + Assert.IsTrue(TestDTOEqual(set[1], result_neq.First())); + Assert.IsTrue(TestDTOEqual(set[2], result_neq.Skip(1).First())); + + var result_gt = storage.Get(Query.GT("Number", 1)); + Assert.AreEqual(result_gt.Count(), 2); + Assert.IsTrue(TestDTOEqual(set[0], result_gt.First())); + Assert.IsTrue(TestDTOEqual(set[2], result_gt.Skip(1).First())); + + var result_lt = storage.Get(Query.LT("Number", 1)); + Assert.AreEqual(result_lt.Count(), 1); + Assert.IsTrue(TestDTOEqual(set[1], result_lt.First())); + } + } +} diff --git a/ZeroLevel.UnitTests/SpecificationPatternTest.cs b/ZeroLevel.UnitTests/SpecificationPatternTest.cs new file mode 100644 index 0000000..17f84a9 --- /dev/null +++ b/ZeroLevel.UnitTests/SpecificationPatternTest.cs @@ -0,0 +1,419 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ZeroLevel.Specification; +using ZeroSpecificationPatternsTest.Models; +using ZeroSpecificationPatternsTest.Specifications; + +namespace ZeroSpecificationPatternsTest +{ + /// + /// Summary description for SpecificationPatternTest + /// + [TestClass] + public class SpecificationPatternTest + { + [TestMethod] + public void SimpleSpecificationTest() + { + // Assert + var dto_one = new TestDTO + { + IsFlag = true, + LongNumber = 100, + Number = 1000, + Real = 3.141, + Summary = "Summary", + Title = "Title" + }; + var dto_two = new TestDTO + { + IsFlag = false, + LongNumber = 0, + Number = 0, + Real = 0, + Summary = string.Empty, + Title = string.Empty + }; + var flag_spec_true = new IsFlagSpecification(true); + var flag_spec_false = new IsFlagSpecification(false); + + var long_specification_full = new LongNumberSpecification(100); + var long_specification_empty = new LongNumberSpecification(0); + + var number_specification_full = new NumberSpecification(1000); + var number_specification_empty = new NumberSpecification(0); + + var real_specification_full = new RealSpecification(3.141); + var real_specification_empty = new RealSpecification(0); + + var summary_specification_full = new SummarySpecification("Summary"); + var summary_specification_empty = new SummarySpecification(string.Empty); + + var title_specification_full = new TitleSpecification("Title"); + var title_specification_empty = new TitleSpecification(string.Empty); + + // Assert + Assert.IsTrue(flag_spec_true.IsSatisfiedBy(dto_one)); + Assert.IsFalse(flag_spec_false.IsSatisfiedBy(dto_one)); + Assert.IsTrue(flag_spec_false.IsSatisfiedBy(dto_two)); + Assert.IsFalse(flag_spec_true.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(long_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(long_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(long_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(long_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(number_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(number_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(number_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(number_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(real_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(real_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(real_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(real_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(summary_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(summary_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(summary_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(summary_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(title_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(title_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(title_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(title_specification_full.IsSatisfiedBy(dto_two)); + } + + [TestMethod] + public void NotSpecificationTest() + { + // Assert + var dto_one = new TestDTO + { + IsFlag = true, + LongNumber = 100, + Number = 1000, + Real = 3.141, + Summary = "Summary", + Title = "Title" + }; + var dto_two = new TestDTO + { + IsFlag = false, + LongNumber = 0, + Number = 0, + Real = 0, + Summary = string.Empty, + Title = string.Empty + }; + var flag_spec_true = new IsFlagSpecification(true).Not(); + var flag_spec_false = new IsFlagSpecification(false).Not(); + + var long_specification_full = new LongNumberSpecification(100).Not(); + var long_specification_empty = new LongNumberSpecification(0).Not(); + + var number_specification_full = new NumberSpecification(1000).Not(); + var number_specification_empty = new NumberSpecification(0).Not(); + + var real_specification_full = new RealSpecification(3.141).Not(); + var real_specification_empty = new RealSpecification(0).Not(); + + var summary_specification_full = new SummarySpecification("Summary").Not(); + var summary_specification_empty = new SummarySpecification(string.Empty).Not(); + + var title_specification_full = new TitleSpecification("Title").Not(); + var title_specification_empty = new TitleSpecification(string.Empty).Not(); + + // Assert + Assert.IsFalse(flag_spec_true.IsSatisfiedBy(dto_one)); + Assert.IsTrue(flag_spec_false.IsSatisfiedBy(dto_one)); + Assert.IsFalse(flag_spec_false.IsSatisfiedBy(dto_two)); + Assert.IsTrue(flag_spec_true.IsSatisfiedBy(dto_two)); + + Assert.IsFalse(long_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsTrue(long_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsFalse(long_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsTrue(long_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsFalse(number_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsTrue(number_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsFalse(number_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsTrue(number_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsFalse(real_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsTrue(real_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsFalse(real_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsTrue(real_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsFalse(summary_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsTrue(summary_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsFalse(summary_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsTrue(summary_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsFalse(title_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsTrue(title_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsFalse(title_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsTrue(title_specification_full.IsSatisfiedBy(dto_two)); + } + + [TestMethod] + public void ComposedAndSpecificationTest() + { + // Assert + var dto_one = new TestDTO + { + IsFlag = true, + LongNumber = 100, + Number = 1000, + Real = 3.141, + Summary = "Summary", + Title = "Title" + }; + var dto_two = new TestDTO + { + IsFlag = false, + LongNumber = 0, + Number = 0, + Real = 0, + Summary = string.Empty, + Title = string.Empty + }; + var flag_spec_true = new IsFlagSpecification(true); + var flag_spec_false = new IsFlagSpecification(false); + + var long_specification_full = new LongNumberSpecification(100); + var long_specification_empty = new LongNumberSpecification(0); + + var number_specification_full = new NumberSpecification(1000); + var number_specification_empty = new NumberSpecification(0); + + var real_specification_full = new RealSpecification(3.141); + var real_specification_empty = new RealSpecification(0); + + var summary_specification_full = new SummarySpecification("Summary"); + var summary_specification_empty = new SummarySpecification(string.Empty); + + var title_specification_full = new TitleSpecification("Title"); + var title_specification_empty = new TitleSpecification(string.Empty); + + // Act + + var composed_full = flag_spec_true. + And(long_specification_full). + And(number_specification_full). + And(real_specification_full). + And(summary_specification_full). + And(title_specification_full); + + var composed_empty = flag_spec_false. + And(long_specification_empty). + And(number_specification_empty). + And(real_specification_empty). + And(summary_specification_empty). + And(title_specification_empty); + + // Assert + Assert.IsTrue(composed_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(composed_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(composed_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(composed_full.IsSatisfiedBy(dto_two)); + } + + [TestMethod] + public void ComposedOrSpecificationTest() + { + // Assert + var dto_one = new TestDTO + { + IsFlag = true, + LongNumber = 100, + Number = 1000, + Real = 3.141, + Summary = "Summary", + Title = "Title" + }; + var dto_two = new TestDTO + { + IsFlag = false, + LongNumber = 0, + Number = 0, + Real = 0, + Summary = string.Empty, + Title = string.Empty + }; + var flag_spec_true = new IsFlagSpecification(true); + var flag_spec_false = new IsFlagSpecification(false); + + var long_specification_full = new LongNumberSpecification(100); + var long_specification_empty = new LongNumberSpecification(0); + + var number_specification_full = new NumberSpecification(1000); + var number_specification_empty = new NumberSpecification(0); + + var real_specification_full = new RealSpecification(3.141); + var real_specification_empty = new RealSpecification(0); + + var summary_specification_full = new SummarySpecification("Summary"); + var summary_specification_empty = new SummarySpecification(string.Empty); + + var title_specification_full = new TitleSpecification("Title"); + var title_specification_empty = new TitleSpecification(string.Empty); + + // Act + + var composed_full = flag_spec_false. + Or(long_specification_full). + Or(number_specification_full). + Or(real_specification_full). + Or(summary_specification_full). + Or(title_specification_full); + + var composed_empty = flag_spec_true. + Or(long_specification_empty). + Or(number_specification_empty). + Or(real_specification_empty). + Or(summary_specification_empty). + Or(title_specification_empty); + + // Assert + Assert.IsTrue(composed_full.IsSatisfiedBy(dto_one)); + Assert.IsTrue(composed_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(composed_empty.IsSatisfiedBy(dto_two)); + Assert.IsTrue(composed_full.IsSatisfiedBy(dto_two)); + } + + [TestMethod] + public void ExpressionSpecificationTest() + { + // Assert + var dto_one = new TestDTO + { + IsFlag = true, + LongNumber = 100, + Number = 1000, + Real = 3.141, + Summary = "Summary", + Title = "Title" + }; + var dto_two = new TestDTO + { + IsFlag = false, + LongNumber = 0, + Number = 0, + Real = 0, + Summary = string.Empty, + Title = string.Empty + }; + var flag_spec_true = new ExpressionSpecification(o=>o.IsFlag == true); + var flag_spec_false = new ExpressionSpecification(o => o.IsFlag == false); + + var long_specification_full = new ExpressionSpecification(o => o.LongNumber == 100); + var long_specification_empty = new ExpressionSpecification(o => o.LongNumber == 0); + + var number_specification_full = new ExpressionSpecification(o => o.Number == 1000); + var number_specification_empty = new ExpressionSpecification(o => o.Number == 0); + + var real_specification_full = new ExpressionSpecification(o => o.Real == 3.141); + var real_specification_empty = new ExpressionSpecification(o => o.Real == 0); + + var summary_specification_full = new ExpressionSpecification(o => o.Summary == "Summary"); + var summary_specification_empty = new ExpressionSpecification(o => o.Summary == string.Empty); + + var title_specification_full = new ExpressionSpecification(o => o.Title == "Title"); + var title_specification_empty = new ExpressionSpecification(o => o.Title == string.Empty); + + // Assert + Assert.IsTrue(flag_spec_true.IsSatisfiedBy(dto_one)); + Assert.IsFalse(flag_spec_false.IsSatisfiedBy(dto_one)); + Assert.IsTrue(flag_spec_false.IsSatisfiedBy(dto_two)); + Assert.IsFalse(flag_spec_true.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(long_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(long_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(long_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(long_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(number_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(number_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(number_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(number_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(real_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(real_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(real_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(real_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(summary_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(summary_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(summary_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(summary_specification_full.IsSatisfiedBy(dto_two)); + + Assert.IsTrue(title_specification_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(title_specification_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(title_specification_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(title_specification_full.IsSatisfiedBy(dto_two)); + } + + [TestMethod] + public void ComposedExpressionSpecificationTest() + { + // Assert + var dto_one = new TestDTO + { + IsFlag = true, + LongNumber = 100, + Number = 1000, + Real = 3.141, + Summary = "Summary", + Title = "Title" + }; + var dto_two = new TestDTO + { + IsFlag = false, + LongNumber = 0, + Number = 0, + Real = 0, + Summary = string.Empty, + Title = string.Empty + }; + var flag_spec_true = new ExpressionSpecification(o => o.IsFlag == true); + var flag_spec_false = new ExpressionSpecification(o => o.IsFlag == false); + + var long_specification_full = new ExpressionSpecification(o => o.LongNumber == 100); + var long_specification_empty = new ExpressionSpecification(o => o.LongNumber == 0); + + var number_specification_full = new ExpressionSpecification(o => o.Number == 1000); + var number_specification_empty = new ExpressionSpecification(o => o.Number == 0); + + var real_specification_full = new ExpressionSpecification(o => o.Real == 3.141); + var real_specification_empty = new ExpressionSpecification(o => o.Real == 0); + + var summary_specification_full = new ExpressionSpecification(o => o.Summary == "Summary"); + var summary_specification_empty = new ExpressionSpecification(o => o.Summary == string.Empty); + + var title_specification_full = new ExpressionSpecification(o => o.Title == "Title"); + var title_specification_empty = new ExpressionSpecification(o => o.Title == string.Empty); + + // Act + + var composed_full = flag_spec_true. + And(long_specification_full). + And(number_specification_full). + And(real_specification_full). + And(summary_specification_full). + And(title_specification_full); + + var composed_empty = flag_spec_false. + And(long_specification_empty). + And(number_specification_empty). + And(real_specification_empty). + And(summary_specification_empty). + And(title_specification_empty); + + // Assert + Assert.IsTrue(composed_full.IsSatisfiedBy(dto_one)); + Assert.IsFalse(composed_empty.IsSatisfiedBy(dto_one)); + Assert.IsTrue(composed_empty.IsSatisfiedBy(dto_two)); + Assert.IsFalse(composed_full.IsSatisfiedBy(dto_two)); + } + } +} diff --git a/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj b/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj index 12a3835..c4739c4 100644 --- a/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj +++ b/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj @@ -50,15 +50,28 @@ + + + + + + + + + + + + +