예제 #1
0
        public void IsSatisfiedByExample()
        {
            var goldCustomer = new Customer {
                Name = "Customer1", Type = CustomerType.Gold
            };
            var silverCustomer = new Customer {
                Name = "Customer2", Type = CustomerType.Silver
            };
            var bronzeCustomer = new Customer {
                Name = "Customer3", Type = CustomerType.Bronze
            };

            var goldSpecification             = new PredicateSpecification <Customer>(x => x.Type == CustomerType.Gold);
            var specificCustomerSpecification = new PredicateSpecification <Customer>(x => x.Name == "Customer3");

            // Gold customer
            Assert.IsTrue(goldSpecification.IsSatisfiedBy(goldCustomer));

            // Silver customer
            Assert.IsFalse(goldSpecification.IsSatisfiedBy(silverCustomer));

            // Customer 3
            Assert.IsTrue(specificCustomerSpecification.IsSatisfiedBy(bronzeCustomer));

            // Customer 2
            Assert.IsFalse(specificCustomerSpecification.IsSatisfiedBy(silverCustomer));
        }
예제 #2
0
        public void Can_bench_specification_extension_method_performance()
        {
            var stopwatch = new Stopwatch();
            const int trials = 1000;

            ISpecification<int> isGreaterThanThree = new PredicateSpecification<int>(v => v > 3);
            ISpecification<int> isLessThanFive = new PredicateSpecification<int>(v => v < 5);
            var isFour = isGreaterThanThree.And(isLessThanFive);

            stopwatch.Start();
            
            for (var i = 0; i < trials; i++)
            {
                isFour.IsSatisfiedBy(2 + 2);
            }
            stopwatch.Stop();
            Trace.WriteLine(stopwatch.Elapsed);

            stopwatch.Start();
            for (var i = 0; i < trials; i++)
            {
                (2 + 2).Satisfies(isFour);
            }
            stopwatch.Stop();
            Trace.WriteLine(stopwatch.Elapsed);
        }
예제 #3
0
        public void ISpecification_LengthBetween5And10()
        {
            ISpecification <string> subject = new PredicateSpecification <string>(s => s.Length >= 5 && s.Length <= 10);

            Assert.That(subject, Must.Be.SatisfiedBy("123456"));
            Assert.That(subject, Must.Not.Be.SatisfiedBy("1234").Or("1234567890123"));
        }
예제 #4
0
        public void Specification_Should_Be_Saved()
        {
            ISpecification <int> spec = new PredicateSpecification <int>(x => x == 5);
            var notSpec = spec.Not();

            Assert.IsInstanceOf <NotSpecification <int> >(notSpec);
            Assert.AreEqual(((NotSpecification <int>)notSpec).Specification, spec);
        }
예제 #5
0
        public void Implicit_To_Fucntion()
        {
            var lessThan5      = new PredicateSpecification <int>(i => i < 5);
            var a              = new[] { 2, 4, 6, 8, 10 };
            Func <int, bool> p = lessThan5;

            Assert.That(a.Where(p), Must.Have.Count(Is.EqualTo(2)));
        }
예제 #6
0
        public void Implicit_To_Predicate()
        {
            var             lessThan5 = new PredicateSpecification <int>(i => i < 5);
            var             l         = new List <int>(new[] { 2, 4, 6, 8, 10 });
            Predicate <int> p         = lessThan5;

            Assert.That(l.FindAll(p), Has.Count.EqualTo(2));
        }
예제 #7
0
 public void Can_chain_specifications()
 {
     ISpecification<int> isGreaterThanThree = new PredicateSpecification<int>(v => v > 3);
     ISpecification<int> isLessThanFive = new PredicateSpecification<int>(v => v < 5);
     var isFour = isGreaterThanThree.And(isLessThanFive);
     Assert.AreEqual(true, isFour.IsSatisfiedBy(2 + 2));
     Assert.AreEqual(true, (2 + 2).Satisfies(isFour));
 }
예제 #8
0
        public void Specification_Should_Be_Saved()
        {
            ISpecification<int> spec = new PredicateSpecification<int>(x => x == 5);
            var notSpec = spec.Not();

            Assert.IsInstanceOf<NotSpecification<int>>(notSpec);
            Assert.AreEqual(((NotSpecification<int>)notSpec).Specification, spec);
        }
예제 #9
0
        public void MoreThan10_OrOp_LessThan5()
        {
            PredicateSpecification <int> moreThan10 = PredicateSpecification.For <int>(i => i > 10);
            var lessThan5 = new PredicateSpecification <int>(i => i < 5);
            PredicateSpecification <int> subject = lessThan5 || moreThan10;

            Assert.That(subject, Must.Not.Be.SatisfiedBy(7));
            Assert.That(subject, Must.Be.SatisfiedBy(3).And(13));
        }
예제 #10
0
        public void MoreThan5_AndOp_LessThan10()
        {
            PredicateSpecification <int> lessThan10 = PredicateSpecification.For <int>(i => i < 10);
            var moreThan5 = new PredicateSpecification <int>(i => i > 5);
            PredicateSpecification <int> subject = lessThan10 && moreThan5;

            Assert.That(subject, Must.Be.SatisfiedBy(7));
            Assert.That(subject, Must.Not.Be.SatisfiedBy(3).Or(13));
        }
예제 #11
0
        public void Should_Be_Fine_If_None_Are_Null()
        {
            var p1 = new PredicateSpecification <int>(x => x == 5);
            var p2 = new PredicateSpecification <int>(x => x >= 5);

            var spec = new OrSpecification <int>(p1, p2);

            Assert.AreEqual(spec.Left, p1);
            Assert.AreEqual(spec.Right, p2);
        }
예제 #12
0
        public void Should_Be_Fine_If_None_Are_Null()
        {
            var p1 = new PredicateSpecification<int>(x => x == 5);
            var p2 = new PredicateSpecification<int>(x => x >= 5);

            var spec = new OrSpecification<int>(p1, p2);

            Assert.AreEqual(spec.Left, p1);
            Assert.AreEqual(spec.Right, p2);
        }
예제 #13
0
        public void Arguments_Should_Be_Saved()
        {
            var spec1 = new PredicateSpecification <int>(x => x > 5);
            var spec2 = new PredicateSpecification <int>(x => x < 10);

            var compositeSpecification = new Mock <CompositeSpecification <int> >(spec1, spec2);

            Assert.AreEqual(compositeSpecification.Object.Left, spec1);
            Assert.AreEqual(compositeSpecification.Object.Right, spec2);
        }
예제 #14
0
        public void Specification_Should_Save_And_Use_Predicate_Supplied_Correctly()
        {
            Predicate<int> predicate = x => x == 5;
            var specification = new PredicateSpecification<int>(predicate);
            Assert.AreEqual(specification.Predicate, predicate);

            Assert.IsTrue(specification.IsSatisfiedBy(5));

            Assert.IsFalse(specification.IsSatisfiedBy(3));
            Assert.IsFalse(specification.IsSatisfiedBy(6));
        }
예제 #15
0
        public void Specification_Should_Save_And_Use_Predicate_Supplied_Correctly()
        {
            Predicate <int> predicate     = x => x == 5;
            var             specification = new PredicateSpecification <int>(predicate);

            Assert.AreEqual(specification.Predicate, predicate);

            Assert.IsTrue(specification.IsSatisfiedBy(5));

            Assert.IsFalse(specification.IsSatisfiedBy(3));
            Assert.IsFalse(specification.IsSatisfiedBy(6));
        }
예제 #16
0
        public void IsSatisfiedByExample()
        {
            var goldCustomer = new Customer { Name = "Customer1", Type = CustomerType.Gold };
            var silverCustomer = new Customer { Name = "Customer2", Type = CustomerType.Silver };

            var notGoldSpecification = new PredicateSpecification<Customer>(x => x.Type == CustomerType.Gold).Not();

            // Gold customer
            Assert.IsFalse(notGoldSpecification.IsSatisfiedBy(goldCustomer));

            // Silver customer
            Assert.IsTrue(notGoldSpecification.IsSatisfiedBy(silverCustomer));
        }
예제 #17
0
        public void Arguments_Should_Be_Saved()
        {
            var spec1 = new PredicateSpecification <int>(x => x > 5);
            var spec2 = new PredicateSpecification <int>(x => x < 10);

            var mocks = new MockRepository();

            var compositeSpecification = mocks.Stub <CompositeSpecification <int> >(spec1, spec2);

            mocks.ReplayAll();

            Assert.AreEqual(compositeSpecification.Left, spec1);
            Assert.AreEqual(compositeSpecification.Right, spec2);

            mocks.VerifyAll();
        }
예제 #18
0
        public void Arguments_Should_Be_Saved()
        {
            var spec1 = new PredicateSpecification<int>(x => x > 5);
            var spec2 = new PredicateSpecification<int>(x => x < 10);

            var mocks = new MockRepository();

            var compositeSpecification = mocks.Stub<CompositeSpecification<int>>(spec1, spec2);

            mocks.ReplayAll();

            Assert.AreEqual(compositeSpecification.Left, spec1);
            Assert.AreEqual(compositeSpecification.Right, spec2);

            mocks.VerifyAll();
        }
예제 #19
0
        public void IsSatisfiedByExample()
        {
            var goldCustomer = new Customer {
                Name = "Customer1", Type = CustomerType.Gold
            };
            var silverCustomer = new Customer {
                Name = "Customer2", Type = CustomerType.Silver
            };

            var notGoldSpecification = new PredicateSpecification <Customer>(x => x.Type == CustomerType.Gold).Not();

            // Gold customer
            Assert.IsFalse(notGoldSpecification.IsSatisfiedBy(goldCustomer));

            // Silver customer
            Assert.IsTrue(notGoldSpecification.IsSatisfiedBy(silverCustomer));
        }
예제 #20
0
        public void IsSatisfiedByExample()
        {
            var divisibleByTwoSpecification = new PredicateSpecification<int>(x => x % 2 == 0);
            var divisibleByThreeSpecification = new PredicateSpecification<int>(x => x % 3 == 0);

            var xorSpecifciation = new XorSpecification<int>(divisibleByTwoSpecification, divisibleByThreeSpecification);

            // Not divisible by either two or three
            Assert.IsFalse(xorSpecifciation.IsSatisfiedBy(1));

            // Divisible by two only
            Assert.IsTrue(xorSpecifciation.IsSatisfiedBy(2));

            // Divisible by thee only
            Assert.IsTrue(xorSpecifciation.IsSatisfiedBy(3));

            // Divisible by two and three
            Assert.IsFalse(xorSpecifciation.IsSatisfiedBy(6));
        }
예제 #21
0
        public void IsSatisfiedByExample()
        {
            var divisibleByTwoSpecification   = new PredicateSpecification <int>(x => x % 2 == 0);
            var divisibleByThreeSpecification = new PredicateSpecification <int>(x => x % 3 == 0);

            var xorSpecifciation = new XorSpecification <int>(divisibleByTwoSpecification, divisibleByThreeSpecification);

            // Not divisible by either two or three
            Assert.IsFalse(xorSpecifciation.IsSatisfiedBy(1));

            // Divisible by two only
            Assert.IsTrue(xorSpecifciation.IsSatisfiedBy(2));

            // Divisible by thee only
            Assert.IsTrue(xorSpecifciation.IsSatisfiedBy(3));

            // Divisible by two and three
            Assert.IsFalse(xorSpecifciation.IsSatisfiedBy(6));
        }
        public void IsSatisfiedByExample()
        {
            var goldCustomer = new Customer { Name = "Customer1", Type = CustomerType.Gold };
            var silverCustomer = new Customer { Name = "Customer2", Type = CustomerType.Silver };
            var bronzeCustomer = new Customer { Name = "Customer3", Type = CustomerType.Bronze };

            var goldSpecification = new PredicateSpecification<Customer>(x => x.Type == CustomerType.Gold);
            var specificCustomerSpecification = new PredicateSpecification<Customer>(x => x.Name == "Customer3");

            // Gold customer
            Assert.IsTrue(goldSpecification.IsSatisfiedBy(goldCustomer));

            // Silver customer
            Assert.IsFalse(goldSpecification.IsSatisfiedBy(silverCustomer));

            // Customer 3
            Assert.IsTrue(specificCustomerSpecification.IsSatisfiedBy(bronzeCustomer));

            // Customer 2
            Assert.IsFalse(specificCustomerSpecification.IsSatisfiedBy(silverCustomer));
        }
예제 #23
0
        public void IsSatisfiedByExample()
        {
            // Our model to test on
            var p = new Person {Name = "Hilton", Surname = "Goosen"};

            // Build up two specifications to AND together
            var nameSpecification = new PredicateSpecification<Person>(x => x.Name == "Hilton");
            var surnameSpecification = new PredicateSpecification<Person>(x => x.Surname == "Goosen");

            // Create a new And Specification
            var specification = new AndSpecification<Person>(nameSpecification, surnameSpecification);

            Assert.IsTrue(specification.IsSatisfiedBy(p));

            // Set the surname to something else
            surnameSpecification = new PredicateSpecification<Person>(x => x.Surname == "Hanekom");
            specification = new AndSpecification<Person>(nameSpecification, surnameSpecification);

            // Surname specification is not satisfied by this person
            Assert.IsFalse(specification.IsSatisfiedBy(p));
        }
예제 #24
0
        public void IsSatisfiedByExample()
        {
            // Our model to test on
            var p = new Person {
                Name = "Hilton", Surname = "Goosen"
            };

            // Build up two specifications to AND together
            var nameSpecification    = new PredicateSpecification <Person>(x => x.Name == "Hilton");
            var surnameSpecification = new PredicateSpecification <Person>(x => x.Surname == "Goosen");

            // Create a new And Specification
            var specification = new AndSpecification <Person>(nameSpecification, surnameSpecification);

            Assert.IsTrue(specification.IsSatisfiedBy(p));

            // Set the surname to something else
            surnameSpecification = new PredicateSpecification <Person>(x => x.Surname == "Hanekom");
            specification        = new AndSpecification <Person>(nameSpecification, surnameSpecification);

            // Surname specification is not satisfied by this person
            Assert.IsFalse(specification.IsSatisfiedBy(p));
        }
 public void PredicateSpecification_PredicateFalse_False()
 {
     var specification = new PredicateSpecification<string>(s => false);
     Assert.AreEqual(false, specification.IsSatisfiedBy("test"));
 }
 public void PredicateSpecification_PredicateTrue_True()
 {
     var specification = new PredicateSpecification<string>(s => true);
     Assert.AreEqual(true, specification.IsSatisfiedBy("test"));
 }
예제 #27
0
 public void Can_satisfy_specifications()
 {
     ISpecification<bool> spec = new PredicateSpecification<bool>(v => v);
     Assert.AreEqual(true, spec.IsSatisfiedBy(true));
 }