Пример #1
0
        public async Task CreateParseTask_ParsesDocumentAndInvokesVisitor()
        {
            // Arrange
            var factory = SpanFactory.CreateCsHtml();
            var visitor = new Mock<ParserVisitor> { CallBase = true };
            var parser = new RazorParser(new CSharpCodeParser(), new HtmlMarkupParser());

            // Act
            var task = parser.CreateParseTask(new StringReader("foo @bar baz"), visitor.Object);
            task.Start();
            await task;

            // Assert
            visitor.Verify(v => v.VisitSpan(It.Is<Span>(s => s.Kind == SpanKind.Markup && s.Content == "foo ")));
            visitor.Verify(v => v.VisitSpan(It.Is<Span>(s => s.Kind == SpanKind.Transition && s.Content == "@")));
            visitor.Verify(v => v.VisitSpan(It.Is<Span>(s => s.Kind == SpanKind.Code && s.Content == "bar")));
            visitor.Verify(v => v.VisitSpan(It.Is<Span>(s => s.Kind == SpanKind.Markup && s.Content == " baz")));
        }
Пример #2
0
        public async Task CreateParseTask_ReturnsWithoutThrowingIfTaskIsCancelled()
        {
            // Arrange
            var factory = SpanFactory.CreateCsHtml();
            var visitor = new Mock<ParserVisitor> { CallBase = true };
            var parser = new RazorParser(new CSharpCodeParser(), new HtmlMarkupParser());
            var cancelTokenSource = new CancellationTokenSource();
            var visited = false;

            // Act
            var task = parser.CreateParseTask(new StringReader("foo @bar baz"),
                                              (span) =>
                                              {
                                                  visited = true;
                                                  Assert.Equal("foo ", span.Content);
                                                  cancelTokenSource.Cancel();
                                              },
                                              errorCallback: null,
                                              cancelToken: cancelTokenSource.Token);
            task.Start();
            await task;

            // Assert
            Assert.True(visited);
        }