Exemplo n.º 1
0
        private void TokenizerTest(string text, bool unclosed, params TemplateTokenResult[] expected)
        {
            var tokenizer = new TemplateTokenizer(new StringReader(text));
            var tokens    = tokenizer.GetTokens().ToArray();

            bool passed = false;

            try {
                Assert.AreEqual(expected.Length, tokens.Length);
                Assert.AreEqual(0, tokens[0].Start);
                Assert.AreEqual(text.Length - 1, tokens[tokens.Length - 1].End);

                for (int i = 0; i < expected.Length; i++)
                {
                    var expectedToken = expected[i].Token;

                    Assert.AreEqual(expectedToken.Kind, tokens[i].Kind);
                    Assert.AreEqual(expectedToken.Start, tokens[i].Start);
                    Assert.AreEqual(expectedToken.End, tokens[i].End);
                    switch (expectedToken.Kind)
                    {
                    case TemplateTokenKind.Block:
                    case TemplateTokenKind.Comment:
                    case TemplateTokenKind.Variable:
                        Assert.AreEqual('{', text[expectedToken.Start]);
                        if (!unclosed)
                        {
                            Assert.AreEqual('}', text[expectedToken.End]);
                        }
                        break;
                    }
                    if (expected[i].Start != null)
                    {
                        Assert.AreEqual(expected[i].Start, text[expectedToken.Start]);
                    }
                    if (expected[i].End != null)
                    {
                        Assert.AreEqual(expected[i].End, text[expectedToken.End]);
                    }
                }
                passed = true;
            } finally {
                if (!passed)
                {
                    List <string> res = new List <string>();
                    for (int i = 0; i < tokens.Length; i++)
                    {
                        res.Add(
                            String.Format("new TemplateToken(TemplateTokenKind.{0}, {1}, {2})",
                                          tokens[i].Kind,
                                          tokens[i].Start,
                                          tokens[i].End
                                          )
                            );
                    }
                    Console.WriteLine(String.Join(",\r\n", res));
                }
            }
        }
Exemplo n.º 2
0
        public void GetArtifacts(ITextProvider text, ArtifactCollection artifactCollection)
        {
            var reader    = new TextProviderReader(text);
            var tokenizer = new TemplateTokenizer(reader);

            foreach (var token in tokenizer.GetTokens())
            {
                if (token.Kind != TemplateTokenKind.Text)
                {
                    var range    = TextRange.FromBounds(token.Start, token.End + 1);
                    var artifact = TemplateArtifact.Create(token.Kind, range, token.IsClosed);
                    artifactCollection.Add(artifact);
                }
            }
        }