Exemplo n.º 1
0
        /// <summary>
        /// Gets the jade template.
        /// </summary>
        /// <returns>A new jade template.</returns>
        public ITemplate Parse(string jade)
        {
            jade.CheckNull("jade");

            var lexer         = new JadeLexer(jade);
            var rawTokens     = new TokenStream(lexer.Tokenize());
            var elementParser = new ElementParser();

            var lines = rawTokens.GetLines();

            DocType docType  = null;
            var     elements = new List <JElement>();

            if (!lines.IsAtEnd())
            {
                var firstLine = lines.Current;
                if (firstLine.Is(JadeTokenType.Doctype))
                {
                    firstLine.Consume();
                    firstLine.GetAny(TokenType.WhiteSpace);
                    docType = new DocType(firstLine.GetAny(TokenType.Word));
                    lines.Consume();
                }

                while (!lines.IsAtEnd())
                {
                    elementParser.ParseNextElement(lines).Do(elements.Add);
                }
            }

            return(new Template(docType, elements));
        }
Exemplo n.º 2
0
        public void ShouldThrowAnInvalidOperationExceptionWhenWhenPasingTheNextElementAndTheTokenLineStreamIsEmpty()
        {
            var lines = new Mock <TokenLineStream>(Enumerable.Empty <TokenLine>());

            lines.Setup(l => l.IsAtEnd()).Returns(true);

            var elementParser = new ElementParser();

            elementParser.ParseNextElement(lines.Object);
        }
Exemplo n.º 3
0
        public void ShouldThrowAnUnexpectedTokenExceptionWhenPasingTheNextElementAndThereAreTokensRemaining()
        {
            var tokenLine = new Mock <TokenLine>(Enumerable.Empty <StringToken>());

            tokenLine.Setup(l => l.Current).Returns(new StringToken(TokenType.QuotedString, 1, 1, 1, "hello"));
            var lines = new Mock <TokenLineStream>(Enumerable.Empty <TokenLine>());

            lines.Setup(l => l.Current).Returns(tokenLine.Object);

            var elementParser = new ElementParser();

            elementParser.ParseNextElement(lines.Object);
        }
Exemplo n.º 4
0
        public void ShouldReturnNullWhenPasingTheNextElementAndThereAreNoElementsToParse()
        {
            var tokenLine = new Mock <TokenLine>(Enumerable.Empty <StringToken>());

            tokenLine.Setup(l => l.IsAtEnd()).Returns(true);
            var lines = new Mock <TokenLineStream>(Enumerable.Empty <TokenLine>());

            lines.Setup(l => l.Current).Returns(tokenLine.Object);

            var elementParser = new ElementParser();
            var element       = elementParser.ParseNextElement(lines.Object);

            Assert.IsNull(element);
        }
Exemplo n.º 5
0
        public void ShouldReturnAnElementWhenPasingTheNextElement()
        {
            var tokenLine = new Mock <TokenLine>((IEnumerable <StringToken>) new[] { new StringToken(TokenType.Word, 1, 1, 1, "hello") });

            tokenLine.CallBase = true;
            var lines = new Mock <TokenLineStream>(Enumerable.Empty <TokenLine>());

            lines.Setup(l => l.Current).Returns(tokenLine.Object);

            var elementParser = new ElementParser();
            var element       = elementParser.ParseNextElement(lines.Object);

            Assert.IsNotNull(element);
            Assert.IsTrue(tokenLine.Object.IsAtEnd());
        }
Exemplo n.º 6
0
        public void ShouldGetTheCurrentLineWhenPasingTheNextElement()
        {
            var tokenLine = new Mock <TokenLine>(Enumerable.Empty <StringToken>());

            tokenLine.Setup(l => l.IsAtEnd()).Returns(true);
            var lines = new Mock <TokenLineStream>(Enumerable.Empty <TokenLine>());

            lines.Setup(l => l.Current).Returns(tokenLine.Object);

            var elementParser = new ElementParser();

            elementParser.ParseNextElement(lines.Object);

            Assert.IsTrue(tokenLine.Object.IsAtEnd());
            lines.Verify(l => l.Current, Times.Once);
        }
Exemplo n.º 7
0
        public void ShouldConsumeTheFirstLineWhenPasingTheNextElementAndThereAreNoMoreTokensInTheLine()
        {
            var tokenLine = new Mock <TokenLine>((IEnumerable <StringToken>) new[] { new StringToken(TokenType.Word, 1, 1, 1, "hello") });

            tokenLine.CallBase = true;
            var lines = new Mock <TokenLineStream>(Enumerable.Empty <TokenLine>());

            lines.Setup(l => l.Current).Returns(tokenLine.Object);

            var elementParser = new ElementParser();

            elementParser.ParseNextElement(lines.Object);

            Assert.IsTrue(tokenLine.Object.IsAtEnd());
            lines.Verify(l => l.Consume(), Times.Once);
        }
Exemplo n.º 8
0
        public void ShouldThrowAnArgumentNullExceptionWhenWhenPasingTheNextElementAndTheTokenLineStreamIsNull()
        {
            var elementParser = new ElementParser();

            elementParser.ParseNextElement(null);
        }