public void TestBuildAndMultipleElements()
        {
            const int count = 5;
            // Arrange
            var mocks = new List <Mock <ISpecification <T> > >();

            for (var i = 0; i < count; ++i)
            {
                var spec = new Mock <ISpecification <T> >();
                spec.Setup(s => s.IsSatisfiedBy(It.IsAny <T>())).Returns(true);
                mocks.Add(spec);
            }

            // Act
            var builder       = mocks.Aggregate(new SpecificationBuilderAnd <T>() as ISpecificationContainerBuilder <T>, (current, item) => current.Add(new SimpleSpecificationBuilder <T>(() => item.Object)));
            var specification = builder.Build();

            // Assert
            Assert.That(Value.Of(builder.Items).Count().Is().EqualTo(count), "No of Items");
            Assert.That(Value.Of(specification).Is().TypeOf(typeof(AndSpecification <T>)), "Correct spec type");
            Assert.That(Value.Of(specification.IsSatisfiedBy(RandomValues.Value <T>())).Is().True(), "IsSatisfied = true");
            var idx = 0;

            foreach (var mock in mocks)
            {
                mock.Verify(s => s.IsSatisfiedBy(It.IsAny <T>()), Times.Once(), string.Format("Mock {0} not called", idx++));
            }
        }
        public void TestSpecificationPredicate()
        {
            // Arrange
            var candidate     = RandomValues.Value <T>();
            var mockAction    = new Mock <IInvokeDelegate>();
            var specification = Specification.Create <T>(mockAction.Object.Predicate);

            // Act
            specification.IsSatisfiedBy(candidate);

            // Assert
            mockAction.Verify(a => a.Predicate(candidate), Times.Once());
        }
        public void TestSpecification()
        {
            // Arrange
            var candidate         = RandomValues.Value <T>();
            var mockSpecification = new Mock <ISpecification <T> >();
            var specification     = mockSpecification.Object;

            // Act
            specification.IsSatisfiedBy(candidate);

            // Assert
            mockSpecification.Verify(s => s.IsSatisfiedBy(candidate), Times.Once());
        }
Пример #4
0
        public void TestAction()
        {
            // Arrange
            var subject    = RandomValues.Value <T>();
            var mockAction = new Mock <IInvokeDelegate>();
            var rule       = Rule.Create <T>(mockAction.Object.Action);

            // Act
            rule.Process(subject);

            // Assert
            mockAction.Verify(a => a.Action(subject), Times.Once());
        }
        public void TestSpecificationNot([Values(false, true)] bool operand)
        {
            // Arrange
            var candidate          = RandomValues.Value <T>();
            var innerSpecification = Specification.Create <T>(__ => operand);
            var specification      = innerSpecification.Not();

            // Act
            var isSatisfied = specification.IsSatisfiedBy(candidate);

            // Assert
            var expected = !operand;

            Assert.That(Value.Of(isSatisfied).Is().EqualTo(expected), string.Format("Not {0}", operand));
        }
        public void TestSpecificationOr([Values(false, true)] bool lhs, [Values(false, true)] bool rhs)
        {
            // Arrange
            var candidate        = RandomValues.Value <T>();
            var lhsSpecification = Specification.Create <T>(__ => lhs);
            var rhsSpecification = Specification.Create <T>(__ => rhs);
            var specification    = lhsSpecification.Or(rhsSpecification);

            // Act
            var isSatisfied = specification.IsSatisfiedBy(candidate);

            // Assert
            var expected = lhs || rhs;

            Assert.That(Value.Of(isSatisfied).Is().EqualTo(expected), string.Format("{0} Or {1}", lhs, rhs));
        }
Пример #7
0
        public void TestFollowOnAction()
        {
            // Arrange
            var subject     = RandomValues.Value <T>();
            var mockAction2 = new Mock <IInvokeDelegate>();
            var rule2       = Rule.Create <T>(mockAction2.Object.Action);
            var mockAction1 = new Mock <IInvokeDelegate>();
            var rule1       = Rule.Create <T>(mockAction1.Object.Action).FollowOn(rule2);

            // Act
            rule1.Process(subject);

            // Assert
            mockAction1.Verify(a => a.Action(subject), Times.Once());
            mockAction2.Verify(a => a.Action(subject), Times.Once());
        }
        public void TestSpecificationOr([Values(false, true)] bool p1, [Values(false, true)] bool p2, [Values(false, true)] bool p3)
        {
            // Arrange
            var candidate      = RandomValues.Value <T>();
            var specification1 = Specification.Create <T>(__ => p1);
            var specification2 = Specification.Create <T>(__ => p2);
            var specification3 = Specification.Create <T>(__ => p3);
            var specification  = specification1.Or(specification2).Or(specification3);

            // Act
            var isSatisfied = specification.IsSatisfiedBy(candidate);

            // Assert
            var expected = p1 || p2 || p3;

            Assert.That(Value.Of(isSatisfied).Is().EqualTo(expected), string.Format("{0} OR {1} OR {2}", p1, p2, p3));
        }
Пример #9
0
        public void TestDecisionUnsatisfied()
        {
            // Arrange
            var subject               = RandomValues.Value <T>();
            var mockActionSatisfied   = new Mock <IInvokeDelegate>();
            var ruleSatisfied         = Rule.Create <T>(mockActionSatisfied.Object.Action);
            var mockActionUnsatisfied = new Mock <IInvokeDelegate>();
            var ruleUnsatisfied       = Rule.Create <T>(mockActionUnsatisfied.Object.Action);
            var mockSpecification     = new Mock <ISpecification <T> >();

            mockSpecification.Setup(s => s.IsSatisfiedBy(It.IsAny <T>())).Returns(false);
            var rule = Rule.Create(mockSpecification.Object, ruleSatisfied, ruleUnsatisfied);

            // Act
            rule.Process(subject);

            // Assert
            mockActionSatisfied.Verify(a => a.Action(subject), Times.Never());
            mockActionUnsatisfied.Verify(a => a.Action(subject), Times.Once());
        }
Пример #10
0
        public void TestChainOfResponsibilityRule()
        {
            // Arrange
            var subject         = RandomValues.Value <T>();
            var mockActionTrue  = new Mock <IInvokeDelegate>();
            var ruleTrue        = Rule.Create <T>(mockActionTrue.Object.Action);
            var mockActionFalse = new Mock <IInvokeDelegate>();
            var ruleFalse       = Rule.Create <T>(mockActionFalse.Object.Action);
            var rule            = Rule.CreateChainOfResponsibility(new[]
            {
                Tuple.Create(Specification.False <T>(), ruleFalse),
                Tuple.Create(Specification.False <T>(), ruleFalse),
                Tuple.Create(Specification.True <T>(), ruleTrue),
                Tuple.Create(Specification.True <T>(), ruleFalse)
            });

            // Act
            rule.Process(subject);

            // Assert
            mockActionTrue.Verify(a => a.Action(subject), Times.Once());
            mockActionFalse.Verify(a => a.Action(It.IsAny <T>()), Times.Never);
        }