コード例 #1
0
        public void IsSatisfiedBy_WhenLeftAndRightSpecificationsAreSatisfied_ShouldReturnTrue()
        {
            object obj = _fixture.Create <object>();

            _mockLeftSpecification.Setup(call => call.IsSatisfiedBy(obj)).Returns(true);
            _mockRightSpecification.Setup(call => call.IsSatisfiedBy(obj)).Returns(true);

            bool isSatisfiedBy = _andSpecification.IsSatisfiedBy(obj);

            isSatisfiedBy.Should().BeTrue();
        }
コード例 #2
0
        public void IsSatisfiedBy_GivenLeftSpecificationEvalAsTrueAndRightSpecificationEvalAsTrue_EvalsBothSpecificationsAndReturnsTrue()
        {
            // Arrange
            object expectedInput = new object();

            Mock <ISpecification <object> > mockLeftSpecification = new Mock <ISpecification <object> >();

            mockLeftSpecification.Setup(x => x.IsSatisfiedBy(It.Is <object>(o => o == expectedInput)))
            .Returns(true);
            Mock <ISpecification <object> > mockRightSpecification = new Mock <ISpecification <object> >();

            mockRightSpecification.Setup(x => x.IsSatisfiedBy(It.Is <object>(o => o == expectedInput)))
            .Returns(true);

            ISpecification <object> expectedLeftSpecification  = mockLeftSpecification.Object;
            ISpecification <object> expectedRightSpecification = mockRightSpecification.Object;

            AndSpecification <object> sut = new AndSpecification <object>(expectedLeftSpecification, expectedRightSpecification);

            // Act
            bool actual = sut.IsSatisfiedBy(expectedInput);

            // Assert
            actual.Should().BeTrue();

            mockLeftSpecification.Verify(x => x.IsSatisfiedBy(It.Is <object>(o => o == expectedInput)), Times.Once());
            mockRightSpecification.Verify(x => x.IsSatisfiedBy(It.Is <object>(o => o == expectedInput)), Times.Once());
        }
コード例 #3
0
        public void ShouldReturnCorrectResults(bool leftResult, bool rightResult, bool andResult)
        {
            leftSpecification.Expect(x => x.IsSatisfiedBy(Arg <string> .Is.Anything)).Return(leftResult);
            rightSpecification.Expect(x => x.IsSatisfiedBy(Arg <string> .Is.Anything)).Return(rightResult);

            Assert.AreEqual(sut.IsSatisfiedBy(Arg <string> .Is.Anything), andResult);
        }
コード例 #4
0
        public void IsSatisfiedBy_GivenLeftSpecificationEvalAsFalse_ShortCircuitsDoesNotEvalRightSpecificationAndReturnsFalse()
        {
            // Arrange
            object expectedInput = new object();

            Mock <ISpecification <object> > mockLeftSpecification = new Mock <ISpecification <object> >();

            mockLeftSpecification.Setup(x => x.IsSatisfiedBy(It.Is <object>(o => o == expectedInput)))
            .Returns(false);
            Mock <ISpecification <object> > mockRightSpecification = new Mock <ISpecification <object> >();

            mockRightSpecification.Setup(x => x.IsSatisfiedBy(It.Is <object>(o => o == expectedInput)))
            .Returns(false);

            ISpecification <object> expectedLeftSpecification  = mockLeftSpecification.Object;
            ISpecification <object> expectedRightSpecification = mockRightSpecification.Object;

            AndSpecification <object> sut = new AndSpecification <object>(expectedLeftSpecification, expectedRightSpecification);

            // Act
            bool actual = sut.IsSatisfiedBy(expectedInput);

            // Assert
            actual.Should().BeFalse();

            mockLeftSpecification.Verify(x => x.IsSatisfiedBy(It.Is <object>(o => o == expectedInput)), Times.Once());
            mockRightSpecification.Verify(x => x.IsSatisfiedBy(It.Is <object>(o => o == expectedInput)), Times.Never());
        }
コード例 #5
0
            public void TwoFailingSpecsShouldFail()
            {
                var sut = new AndSpecification<int>(new FunctionSpecification<int>(i => i > 0), new FunctionSpecification<int>(i => i > 0));

                var result = sut.IsSatisfiedBy(0);

                result.Should().BeFalse();
            }
コード例 #6
0
        public void AndSpecification_1wrong_Test()
        {
            var andSpec = new AndSpecification <TestObjects.Member>(_spec1, _spec2);

            member.Age = 43;

            Assert.That(!andSpec.IsSatisfiedBy(member));
        }
コード例 #7
0
            public void TwoPassingSpecsShouldPass()
            {
                var sut = new AndSpecification<int>(new FunctionSpecification<int>(i => i > 0), new FunctionSpecification<int>(i => i > 0));

                var result = sut.IsSatisfiedBy(1);

                result.Should().BeTrue();
            }
コード例 #8
0
            public void TwoPassingSpecsShouldPass()
            {
                var sut = new AndSpecification <int>(new FunctionSpecification <int>(i => i > 0), new FunctionSpecification <int>(i => i > 0));

                var result = sut.IsSatisfiedBy(1);

                result.Should().BeTrue();
            }
コード例 #9
0
            public void OnePassingSpecShouldFail()
            {
                var sut = new AndSpecification <int>(new FunctionSpecification <int>(i => i < 0), new FunctionSpecification <int>(i => i > 0));

                var result = sut.IsSatisfiedBy(0);

                result.Should().BeFalse();
            }
コード例 #10
0
        public void IsSatisfiedByReturnsTrueIfBothAreSatisfiedBy()
        {
            _left = SpecificationGenerator.GetSpecification(true);
            _right = SpecificationGenerator.GetSpecification(true);
            var andSpecification = new AndSpecification<object>(_left, _right);

            Assert.True(andSpecification.IsSatisfiedBy(string.Empty));
        }
コード例 #11
0
        public void IsSatisfiedByReturnsFalseIBothArentSatisfied()
        {
            _left = SpecificationGenerator.GetSpecification(false);
            _right = SpecificationGenerator.GetSpecification(false);
            var andSpecification = new AndSpecification<object>(_left, _right);

            Assert.False(andSpecification.IsSatisfiedBy(string.Empty));
        }
コード例 #12
0
        public void AndSpecification_ReturnsFalse_ForFalseSpec()
        {
            FalseSpecification    falseSpec    = new FalseSpecification();
            TrueBoolSpecification trueBoolSpec = new TrueBoolSpecification();

            AndSpecification <IBoolSpecification> andSpec = new AndSpecification <IBoolSpecification>(trueBoolSpec, trueBoolSpec);

            Assert.IsFalse(andSpec.IsSatisfiedBy(falseSpec), "AndSpecification returned true for false bool value.");
        }
コード例 #13
0
        public void AndSpecification_ReturnsTrue_ForTrueSpec()
        {
            TrueSpecification     trueSpec     = new TrueSpecification();
            TrueBoolSpecification trueBoolSpec = new TrueBoolSpecification();

            AndSpecification <IBoolSpecification> andSpec = new AndSpecification <IBoolSpecification>(trueBoolSpec, trueBoolSpec);

            Assert.IsTrue(andSpec.IsSatisfiedBy(trueSpec), "AndSpecification returned false for true bool value.");
        }
コード例 #14
0
        public void WhenSpecificationsAreDefinedThenResultIsAndSpecification()
        {
            var hasDrm = new HasDrmSpecification();
            var hasEps = new HasAtLeaseOneEpisodeSpecification();
            var sut = new AndSpecification<TvShow>(hasDrm, hasEps);

            Assert.IsInstanceOf<AndSpecification<TvShow>>(sut);
            Assert.IsTrue(sut.IsSatisfiedBy(new TvShow { Drm = true, EpisodeCount = 1 }));
        }
コード例 #15
0
        public bool IsSatisfied_Is_True_If_Both_Left_And_Right_Is_True(bool leftResult, bool rightResult)
        {
            var left  = new DirectSpecification <object>(o => leftResult);
            var right = new DirectSpecification <object>(o => rightResult);

            var spec = new AndSpecification <object>(left, right);

            return(spec.IsSatisfiedBy(new object()));
        }
コード例 #16
0
        public void IsSatisfiedByReturnsTrueWhenAllSpecifactionsAreTrue(TestObject testObject, bool result)
        {
            var specification1 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty);
            var specification2 = new ExpressionSpecification <TestObject>(p => p.BooleanProperty1);

            var andSpecification = new AndSpecification <TestObject>(specification1, specification2);

            andSpecification.IsSatisfiedBy(testObject).ShouldBe(result);
        }
コード例 #17
0
            public void IncorrectData_ReturnFalse(bool leftResult, bool rightResult)
            {
                var left = MockSpecification.Create(leftResult);
                var right = MockSpecification.Create(rightResult);
                var sut = new AndSpecification<object>(left, right);

                var result = sut.IsSatisfiedBy(new object());

                Assert.False(result);
            }
コード例 #18
0
            public void NullCandidate_NoException()
            {
                var left = MockSpecification.True();
                var right = MockSpecification.True();
                var sut = new AndSpecification<object>(left, right);

                var exception = Record.Exception(() => sut.IsSatisfiedBy(null));

                Assert.Null(exception);
            }
コード例 #19
0
        public void IsSatisfiedByTest()
        {
            var left  = new ExpressionSpecification <String>(x => true);
            var right = new ExpressionSpecification <String>(x => true);

            var target = new AndSpecification <String>(left, right);
            var actual = target.IsSatisfiedBy(String.Empty);

            Assert.True(actual);
        }
コード例 #20
0
        public void IsStatisfiedBy_Should_Returns_True_When_Both_Left_And_Right_Is_True()
        {
            _leftHandSide.Expect(s => s.IsSatisfiedBy(It.IsAny <IDummyObject>())).Returns(true);
            _rightHandSide.Expect(s => s.IsSatisfiedBy(It.IsAny <IDummyObject>())).Returns(true);

            var candidate = new Mock <IDummyObject>();
            var result    = _specificaton.IsSatisfiedBy(candidate.Object);

            Assert.True(result);
        }
コード例 #21
0
        public void Should_False_When_FalseAndFalse()
        {
            // Arrange
            var spec = new AndSpecification <object>(new FalseSpecification <object>(), new FalseSpecification <object>());

            // Act
            bool result = spec.IsSatisfiedBy(new object());

            // Assert
            result.Should().BeFalse();
        }
コード例 #22
0
        public bool Match(MethodInfo method)
        {
            TypeInformationExtractor typeExtractor = new TypeInformationExtractor(_parameter);

            ISpecification<MethodInfo> exactlyOneParameter = new ExactlyOneMethodParameterSpecification();
            ISpecification<MethodInfo> parameterTypeMatch = new FirstMethodParameterTypeSpecification(typeExtractor.ExtractType());

            ISpecification<MethodInfo> exactlyOneParameterAndParameterTypeMatch = new AndSpecification<MethodInfo>(exactlyOneParameter, parameterTypeMatch);

            return exactlyOneParameterAndParameterTypeMatch.IsSatisfiedBy(method);
        }
コード例 #23
0
ファイル: IsSatisfiedBy.cs プロジェクト: GTuritto/ngenerics
        public void And_Should_Return_False_If_Different_Or_Both_Are_False()
        {
            var mocks = new MockRepository();
            var s1 = mocks.StrictMock<ISpecification<int>>();
            var s2 = mocks.StrictMock<ISpecification<int>>();

            Expect.Call(s1.IsSatisfiedBy(5)).Return(true);
            Expect.Call(s2.IsSatisfiedBy(5)).Return(false);

            Expect.Call(s1.IsSatisfiedBy(5)).Return(false);

            mocks.ReplayAll();

            var andSpecification = new AndSpecification<int>(s1, s2);

            Assert.AreEqual(andSpecification.IsSatisfiedBy(5), false);
            Assert.AreEqual(andSpecification.IsSatisfiedBy(5), false);

            mocks.VerifyAll();
        }
コード例 #24
0
        public void And_Should_Return_False_If_Different_Or_Both_Are_False()
        {
            var mocks = new MockRepository();
            var s1    = mocks.StrictMock <ISpecification <int> >();
            var s2    = mocks.StrictMock <ISpecification <int> >();

            Expect.Call(s1.IsSatisfiedBy(5)).Return(true);
            Expect.Call(s2.IsSatisfiedBy(5)).Return(false);

            Expect.Call(s1.IsSatisfiedBy(5)).Return(false);

            mocks.ReplayAll();

            var andSpecification = new AndSpecification <int>(s1, s2);

            Assert.AreEqual(andSpecification.IsSatisfiedBy(5), false);
            Assert.AreEqual(andSpecification.IsSatisfiedBy(5), false);

            mocks.VerifyAll();
        }
コード例 #25
0
        public void LeftTrue_RightTrue_EqualsTrue()
        {
            // Arrange
            var trueSpec = new GenericSpecification(true);
            var andSpec  = new AndSpecification <TestAggregateRoot, long>(trueSpec, trueSpec);

            // Act
            var actualSpecResult = andSpec.IsSatisfiedBy(null);

            // Assert
            Assert.IsTrue(actualSpecResult);
        }
コード例 #26
0
        public void IsSatisfiedBy_NoChildren_False()
        {
            // Arrange
            var spec    = new AndSpecification <AlertEvaluationMessage>();
            var message = new AlertEvaluationMessage();

            // Act
            var result = spec.IsSatisfiedBy(message);

            // Assert
            result.Should().BeFalse();
        }
コード例 #27
0
            public void CorrectData_ReturnExpectedResultObject(bool leftResult, bool rightResult,
                SpecificationResult expected)
            {
                var left = MockValidationSpecification.Create(leftResult);
                var right = MockValidationSpecification.Create(rightResult);
                var sut = new AndSpecification<object>(left, right);

                var overall = sut.IsSatisfiedBy(new object(), out var result);

                Assert.True(overall);
                Assert.Equal(expected, result, new SpecificationResultComparer());
            }
コード例 #28
0
        public void And_Should_Return_False_If_Different()
        {
            var s1 = new Mock <ISpecification <int> >();
            var s2 = new Mock <ISpecification <int> >();

            s1.Setup(x => x.IsSatisfiedBy(5)).Returns(true);
            s2.Setup(x => x.IsSatisfiedBy(5)).Returns(false);

            var andSpecification = new AndSpecification <int>(s1.Object, s2.Object);

            Assert.AreEqual(andSpecification.IsSatisfiedBy(5), false);
        }
コード例 #29
0
        public void And_Should_Return_True_If_Both_Arguments_Are_True()
        {
            var s1 = new Mock <ISpecification <int> >();
            var s2 = new Mock <ISpecification <int> >();

            s1.Setup(x => x.IsSatisfiedBy(5)).Returns(true);
            s2.Setup(x => x.IsSatisfiedBy(5)).Returns(true);

            var andSpecification = new AndSpecification <int>(s1.Object, s2.Object);

            Assert.AreEqual(andSpecification.IsSatisfiedBy(5), true);
        }
コード例 #30
0
        public void LeftFalse_RightFalse_EqualsFalse()
        {
            // Arrange
            var falseSpec = new GenericSpecification(false);
            var andSpec   = new AndSpecification <TestAggregateRoot, long>(falseSpec, falseSpec);

            // Act
            var actualSpecResult = andSpec.IsSatisfiedBy(null);

            // Assert
            Assert.IsFalse(actualSpecResult);
        }
コード例 #31
0
        public void AndSpecificationSatisfiedTest()
        {
            // Arrange
            var specification = new AndSpecification<int>(
                new Specification<int>(x => x > 0), new Specification<int>(y => y < 10));

            // Act
            var sut = specification.IsSatisfiedBy().Compile().Invoke(5);

            // Assert
            Assert.True(sut);
        }
コード例 #32
0
            public void CompositeRight_GroupingTraceMessage()
            {
                var left = MockValidationSpecification.True();
                var right = new OrSpecification<object>(MockValidationSpecification.True(),
                    MockValidationSpecification.True());
                var sut = new AndSpecification<object>(left, right);

                sut.IsSatisfiedBy(new object(), out var result);

                Assert.Equal(
                    "TrueMockValidationSpecification And (TrueMockValidationSpecification Or TrueMockValidationSpecification)",
                    result.Trace);
            }
コード例 #33
0
            public void RelatedTypes_NoException()
            {
                var left = MockSpecification<IEnumerable<char>>.True();
                var right = MockSpecification<ChildFakeType>.True();

                var exception = Record.Exception(() =>
                {
                    var sut = new AndSpecification<ChildFakeType>(left, right);
                    sut.IsSatisfiedBy(new ChildFakeType());
                });

                Assert.Null(exception);
            }
 private void AssertIsSatisfiedBy(bool leftSpecificationIsSatisfiedBy, bool rightSpecificationIsSatisfiedBy,
     bool expected)
 {
     var mockRepository = new MockRepository(MockBehavior.Strict);
     var leftSpecification = mockRepository.CreateSpecificationMock<string>(leftSpecificationIsSatisfiedBy);
     var rightSpecification = !leftSpecificationIsSatisfiedBy
                                  ? mockRepository.CreateSpecificationMock<string>()
                                  : mockRepository.CreateSpecificationMock<string>(
                                      rightSpecificationIsSatisfiedBy);
     var specification = new AndSpecification<string>(leftSpecification, rightSpecification);
     Assert.AreEqual(expected, specification.IsSatisfiedBy("test"));
     mockRepository.VerifyAll();
 }
コード例 #35
0
        public void AndSpecification_ShouldBeSatisfiedWhenBothSpecsAreTrue()
        {
            var spec1 = new UsernameSpecification();
            var spec2 = new PasswordSpecification();

            var and = new AndSpecification <User>(spec1, spec2);

            var user1 = new User {
                username = "******", password = "******"
            };

            and.IsSatisfiedBy(user1).Should().BeTrue();
        }
コード例 #36
0
        public void IsSatisfiedBy_OneTrueChild_True()
        {
            // Arrange
            var spec = new AndSpecification <AlertEvaluationMessage>();

            spec.AddChildSpecification(new TrueSpecification <AlertEvaluationMessage>());
            var message = new AlertEvaluationMessage();

            // Act
            var result = spec.IsSatisfiedBy(message);

            // Assert
            result.Should().BeTrue();
        }
コード例 #37
0
                public When_First_Option_Is_True()
                {
                    subject = new object();
                    option = new AndSpecification(
                        new TrueSpecification(),
                        new Specification(
                            s => {
                                funcSubject = s;
                                funcCalled = true;
                                return false;
                            }
                        )
                    );

                    option.IsSatisfiedBy(subject);
                }
コード例 #38
0
                public When_Invoked()
                {
                    subject = new object();

                    option = new AndSpecification(
                        new Specification(
                            s => {
                                option1Subject = s;
                                option1Called = true;
                                return false;
                            }
                        ),
                        new NoSpecification()
                    );

                    option.IsSatisfiedBy(subject);
                }
コード例 #39
0
        public void IsSatisfiedBy_03()
        {
            // arrange:
            object candidate             = new object();
            ISpecification <object> left = MockRepository.GenerateMock <ISpecification <object> >();

            left.Expect(s => s.IsSatisfiedBy(candidate)).Return(false);
            ISpecification <object> right = MockRepository.GenerateMock <ISpecification <object> >();


            // act:
            ISpecification <object> target = new AndSpecification <object>(left, right);
            bool satisfied = target.IsSatisfiedBy(candidate);

            // assert:
            Assert.IsFalse(satisfied);
            left.VerifyAllExpectations();
            right.AssertWasNotCalled(s => s.IsSatisfiedBy(candidate));
        }
コード例 #40
0
		public void ItShouldReturnTheCorrectValueForAllPossibleLHSAndRHSCombinations(bool lhsResult, bool rhsResult, bool expected)
		{
			// Arrange
			var item = new TestType();

			var lhs = A.Fake<ISpecification<TestType>>();
			A.CallTo(() => lhs.IsSatisfiedBy(item)).Returns(lhsResult);

			var rhs = A.Fake<ISpecification<TestType>>();
			A.CallTo(() => rhs.IsSatisfiedBy(item)).Returns(rhsResult);

			var sut = new AndSpecification<TestType>(lhs, rhs);

			// Act
			var result = sut.IsSatisfiedBy(item);

			// Assert
			result.Should().Be(expected);
		}
コード例 #41
0
        public void TestMethod1()
        {
            //
            // TODO: 在此处添加测试逻辑
            //

            var array         = new Int32[] { -10, -7, -6, 0, 1, 2, 4, 5, 6, 7, 8 };
            var even          = new EvenSpecification();
            var positive      = new PositiveSpecification();
            var third         = new ThirdSpecification();
            var specification = new AndSpecification <Int32>(even, positive).And(third);
            var list          = array.Where(item => specification.IsSatisfiedBy(item));

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

            System.Threading.Thread.Sleep(100000);
        }
コード例 #42
0
ファイル: SpecificationTests.cs プロジェクト: guangxb/learn
        //[ExpectedException(typeof(ArgumentNullException))]
        public void CreateAndSpecificationNullRightSpecThrowArgumentNullExceptionTest()
        {
            //Arrange
            DirectSpecification <SampleEntity> leftAdHocSpecification;
            DirectSpecification <SampleEntity> rightAdHocSpecification;

            Expression <Func <SampleEntity, bool> > rightSpec = s => s.Id == Guid.NewGuid();
            Expression <Func <SampleEntity, bool> > leftSpec  = s => s.SampleProperty.Length > 2;

            leftAdHocSpecification  = new DirectSpecification <SampleEntity>(leftSpec);
            rightAdHocSpecification = new DirectSpecification <SampleEntity>(rightSpec);

            //Act
            AndSpecification <SampleEntity> composite = new AndSpecification <SampleEntity>(leftAdHocSpecification, null);

            Assert.IsTrue(composite.IsSatisfiedBy(new SampleEntity()
            {
                SampleProperty = "asdasd"
            }));
        }
コード例 #43
0
ファイル: Program.cs プロジェクト: beginor/DesignPatterns
        public static void Main(string[] args)
        {
            var mobiles = new List<Mobile>() {
                new Mobile(MobileBrand.Apple, MobileType.Smart),
                new Mobile(MobileBrand.Samsung, MobileType.Smart),
                new Mobile(MobileBrand.Samsung, MobileType.Basic)
            };

            var smartSpecification = new ExpressionSpecification<Mobile>(mobile => mobile.Type == MobileType.Smart);
            var appleSpecification = new ExpressionSpecification<Mobile>(mobile => mobile.Brand == MobileBrand.Apple);
            // find all smart mobiles;
            var smartMobiles = mobiles.FindAll(mobile => smartSpecification.IsSatisfiedBy(mobile));
            // find all apple smart mobiles;
            var andSpecification = new AndSpecification<Mobile>(smartSpecification, appleSpecification);
            var appleSmartMobiles = mobiles.FindAll(mobile => andSpecification.IsSatisfiedBy(mobile));

            Console.WriteLine("Hello World!");

            Console.ReadKey();
        }
コード例 #44
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));
        }
コード例 #45
0
        public void IsSatisfiedByTestCase()
        {
            var left = new ExpressionSpecification<String>( x => true );
            var right = new ExpressionSpecification<String>( x => true );

            var target = new AndSpecification<String>( left, right );
            var actual = target.IsSatisfiedBy( String.Empty );
            Assert.IsTrue( actual );
        }
コード例 #46
0
                public When_First_Is_True_And_Second_Is_False()
                {
                    option = new AndSpecification(
                        new FalseSpecification(),
                        new TrueSpecification()
                    );

                    result = option.IsSatisfiedBy(new object());
                }
コード例 #47
0
                public When_Both_Are_True()
                {
                    option = new AndSpecification(
                        new TrueSpecification(),
                        new TrueSpecification()
                    );

                    result = option.IsSatisfiedBy(new object());
                }