예제 #1
0
        public void Visit_applies_handlers_to_every_context_in_a_context_hierarchy()
        {
            // Arrange
              var fixture = new Fixture();
              new RenderingContextCustomisation().Customize(fixture);

              var contexts = Enumerable.Range(0,7).Select(x => fixture.Create<RenderingContext>()).ToArray();
              var topContext = contexts[0];
              RenderingContext[]
            secondContexts  = contexts.Skip(1).Take(2).ToArray(),
            thirdContexts   = contexts.Skip(3).Take(2).ToArray(),
            fourthContexts  = contexts.Skip(5).Take(2).ToArray();

              var handlers = Enumerable.Range(0, 2).Select(x => new Mock<IAttributeHandler>(MockBehavior.Strict)).ToArray();

              handlers[0]
            .Setup(x => x.Handle(topContext))
            .Returns((RenderingContext ctx) => new AttributeHandlingResult(secondContexts, true));
              handlers[1]
            .Setup(x => x.Handle(secondContexts[0]))
            .Returns((RenderingContext ctx) => new AttributeHandlingResult(thirdContexts, true));
              handlers[1]
            .Setup(x => x.Handle(secondContexts[1]))
            .Returns((RenderingContext ctx) => new AttributeHandlingResult(fourthContexts, true));

              var expectedContexts = thirdContexts.Union(fourthContexts);

              var sut = new TalVisitor(handlers: handlers.Select(x => x.Object).ToArray(),
                               errorHandler: Mock.Of<IAttributeHandler>());

              // Act
              var result = sut.Visit(topContext);

              // Assert
              Assert.NotNull(result, "Result nullability");
              try
              {
            CollectionAssert.AreEquivalent(expectedContexts, result, "All expected contexts returned");
              }
              catch(AssertionException)
              {
            _logger.ErrorFormat("{0} contexts returned", result.Count());
            throw;
              }
        }
예제 #2
0
        public void Visit_visits_newly_exposes_contexts()
        {
            // Arrange
              var fixture = new Fixture();
              new RenderingContextCustomisation().Customize(fixture);

              var contexts = Enumerable.Range(0,3).Select(x => fixture.Create<RenderingContext>()).ToArray();
              RenderingContext
            topContext = contexts[0],
            secondContext = contexts[1],
            thirdContext = contexts[2];

              var handler = new Mock<IAttributeHandler>(MockBehavior.Strict);

              handler
            .Setup(x => x.Handle(topContext))
            .Returns(new AttributeHandlingResult(new RenderingContext[0], true, new [] { secondContext }));
              handler
            .Setup(x => x.Handle(secondContext))
            .Returns(new AttributeHandlingResult(new [] { thirdContext }, true));

              var expectedElements = new [] { thirdContext };

              var sut = new TalVisitor(handlers: new [] { handler.Object },
                               errorHandler: Mock.Of<IAttributeHandler>());

              // Act
              var result = sut.Visit(topContext);

              // Assert
              Assert.NotNull(result, "Result nullability");
              Assert.IsTrue(expectedElements.Intersect(result).Count() == expectedElements.Count(),
                    "All expected elements contained");
              Assert.IsTrue(result.Intersect(expectedElements).Count() == result.Count(),
                    "No unwanted elements contained");
        }