Example #1
0
        public void FilterRegexTests()
        {
            var testCases = new[] {
                new { Got = ("100"), Expected = DjangoVariable.Number("100", 0) },
                new { Got = ("100.0"), Expected = DjangoVariable.Number("100.0", 0) },
                new { Got = ("+100"), Expected = DjangoVariable.Number("+100", 0) },
                new { Got = ("-100"), Expected = DjangoVariable.Number("-100", 0) },
                new { Got = ("'fob'"), Expected = DjangoVariable.Constant("'fob'", 0) },
                new { Got = ("\"fob\""), Expected = DjangoVariable.Constant("\"fob\"", 0) },
                new { Got = ("fob"), Expected = DjangoVariable.Variable("fob", 0) },
                new { Got = ("fob.oar"), Expected = DjangoVariable.Variable("fob.oar", 0) },
                new { Got = ("fob|oar"), Expected = DjangoVariable.Variable("fob", 0, new DjangoFilter("oar", 4)) },
                new { Got = ("fob|oar|baz"), Expected = DjangoVariable.Variable("fob", 0, new DjangoFilter("oar", 4), new DjangoFilter("baz", 8)) },
                new { Got = ("fob|oar:'fob'"), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Constant("oar", 4, "'fob'", 8)) },
                new { Got = ("fob|oar:42"), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Number("oar", 4, "42", 8)) },
                new { Got = ("fob|oar:\"fob\""), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Constant("oar", 4, "\"fob\"", 8)) },
                new { Got = ("fob|oar:100"), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Number("oar", 4, "100", 8)) },
                new { Got = ("fob|oar:100.0"), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Number("oar", 4, "100.0", 8)) },
                new { Got = ("fob|oar:+100.0"), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Number("oar", 4, "+100.0", 8)) },
                new { Got = ("fob|oar:-100.0"), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Number("oar", 4, "-100.0", 8)) },
                new { Got = ("fob|oar:baz.quox"), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Variable("oar", 4, "baz.quox", 8)) },
                new { Got = ("fob|oar:baz"), Expected = DjangoVariable.Variable("fob", 0, DjangoFilter.Variable("oar", 4, "baz", 8)) },

                new { Got = ("{{ 100 }}"), Expected = DjangoVariable.Number("100", 3) },
                new { Got = ("{{ 100.0 }}"), Expected = DjangoVariable.Number("100.0", 3) },
                new { Got = ("{{ +100 }}"), Expected = DjangoVariable.Number("+100", 3) },
                new { Got = ("{{ -100 }}"), Expected = DjangoVariable.Number("-100", 3) },
                new { Got = ("{{ 'fob' }}"), Expected = DjangoVariable.Constant("'fob'", 3) },
                new { Got = ("{{ \"fob\" }}"), Expected = DjangoVariable.Constant("\"fob\"", 3) },
                new { Got = ("{{ fob }}"), Expected = DjangoVariable.Variable("fob", 3) },
                new { Got = ("{{ fob.oar }}"), Expected = DjangoVariable.Variable("fob.oar", 3) },
                new { Got = ("{{ fob|oar }}"), Expected = DjangoVariable.Variable("fob", 3, new DjangoFilter("oar", 7)) },
                new { Got = ("{{ fob|oar|baz }}"), Expected = DjangoVariable.Variable("fob", 3, new DjangoFilter("oar", 7), new DjangoFilter("baz", 11)) },
                new { Got = ("{{ fob|oar:'fob' }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Constant("oar", 7, "'fob'", 11)) },
                new { Got = ("{{ fob|oar:42 }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Number("oar", 7, "42", 11)) },
                new { Got = ("{{ fob|oar:\"fob\" }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Constant("oar", 7, "\"fob\"", 11)) },
                new { Got = ("{{ fob|oar:100 }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Number("oar", 7, "100", 11)) },
                new { Got = ("{{ fob|oar:100.0 }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Number("oar", 7, "100.0", 11)) },
                new { Got = ("{{ fob|oar:+100.0 }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Number("oar", 7, "+100.0", 11)) },
                new { Got = ("{{ fob|oar:-100.0 }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Number("oar", 7, "-100.0", 11)) },
                new { Got = ("{{ fob|oar:baz.quox }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Variable("oar", 7, "baz.quox", 11)) },
                new { Got = ("{{ fob|oar:baz }}"), Expected = DjangoVariable.Variable("fob", 3, DjangoFilter.Variable("oar", 7, "baz", 11)) },
            };

            foreach (var testCase in testCases)
            {
                Console.WriteLine(testCase.Got);

                var got = DjangoVariable.Parse(testCase.Got);

                ValidateFilter(testCase.Expected, got);
            }
        }
Example #2
0
        public void BlockParserTests()
        {
            var testCases = new[] {
                new {
                    Got         = ("for x in "),
                    Expected    = (DjangoBlock) new DjangoForBlock(new BlockParseInfo("for", "x in ", 0), 6, null, 9, -1, new[] { new Tuple <string, int>("x", 4) }),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 9,
                            Expected = new[] { "fob", "oar" }
                        },
                        new {
                            Position = 4,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("for x in oar"),
                    Expected    = (DjangoBlock) new DjangoForBlock(new BlockParseInfo("for", "x in oar", 0), 6, DjangoVariable.Variable("oar", 9), 12, -1, new[] { new Tuple <string, int>("x", 4) }),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 9,
                            Expected = new[] { "fob", "oar" }
                        },
                        new {
                            Position = 4,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("for x in b"),
                    Expected    = (DjangoBlock) new DjangoForBlock(new BlockParseInfo("for", "x in b", 0), 6, DjangoVariable.Variable("b", 9), 10, -1, new[] { new Tuple <string, int>("x", 4) }),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 10,
                            Expected = new [] { "fob", "oar" }
                        },
                        new {
                            Position = 4,
                            Expected = new string[0]
                        }
                    }
                },

                new {
                    Got         = ("autoescape"),
                    Expected    = (DjangoBlock) new DjangoAutoEscapeBlock(new BlockParseInfo("autoescape", "", 0), -1, -1),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 10,
                            Expected = new[] { "on", "off" }
                        }
                    }
                },
                new {
                    Got         = ("autoescape on"),
                    Expected    = (DjangoBlock) new DjangoAutoEscapeBlock(new BlockParseInfo("autoescape", " on", 0), 11, 2),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 10,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("comment"),
                    Expected    = (DjangoBlock) new DjangoArgumentlessBlock(new BlockParseInfo("comment", "", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 0,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("spaceless"),
                    Expected    = (DjangoBlock) new DjangoSpacelessBlock(new BlockParseInfo("spaceless", "", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 0,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("filter "),
                    Expected    = (DjangoBlock) new DjangoFilterBlock(new BlockParseInfo("filter", " ", 0), DjangoVariable.Variable("", 7)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 7,
                            Expected = new [] { "cut", "lower" }
                        }
                    }
                },
                new {
                    Got         = ("ifequal "),
                    Expected    = (DjangoBlock) new DjangoIfOrIfNotEqualBlock(new BlockParseInfo("ifequal", " ", 0), DjangoVariable.Variable("", 8)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 8,
                            Expected = new [] { "fob", "oar" }
                        }
                    }
                },
                new {
                    Got         = ("ifequal fob "),
                    Expected    = (DjangoBlock) new DjangoIfOrIfNotEqualBlock(new BlockParseInfo("ifequal", " fob ", 0), DjangoVariable.Variable("fob", 8)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 12,
                            Expected = new [] { "fob", "oar" }
                        }
                    }
                },
                new {
                    Got         = ("if "),
                    Expected    = (DjangoBlock) new DjangoIfBlock(new BlockParseInfo("if", " ", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 3,
                            Expected = new [] { "fob", "oar", "not" }
                        }
                    }
                },
                new {
                    Got         = ("if fob "),
                    Expected    = (DjangoBlock) new DjangoIfBlock(new BlockParseInfo("if", " fob ", 0), new BlockClassification(new Span(3, 3), Classification.Identifier)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 7,
                            Expected = new [] { "and", "or" }
                        }
                    }
                },
                new {
                    Got         = ("if fob and "),
                    Expected    = (DjangoBlock) new DjangoIfBlock(new BlockParseInfo("if", " fob and ", 0), new BlockClassification(new Span(3, 3), Classification.Identifier), new BlockClassification(new Span(7, 3), Classification.Keyword)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 11,
                            Expected = new [] { "fob", "oar", "not" }
                        }
                    }
                },
                new {
                    Got         = ("firstof "),
                    Expected    = (DjangoBlock) new DjangoMultiVariableArgumentBlock(new BlockParseInfo("firstof", " ", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 8,
                            Expected = new [] { "fob", "oar" }
                        }
                    }
                },
                new {
                    Got         = ("firstof fob|"),
                    Expected    = (DjangoBlock) new DjangoMultiVariableArgumentBlock(new BlockParseInfo("firstof", " fob|", 0), DjangoVariable.Variable("fob", 8)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 12,
                            Expected = new [] { "cut", "lower" }
                        }
                    }
                },
                new {
                    Got         = ("spaceless "),
                    Expected    = (DjangoBlock) new DjangoSpacelessBlock(new BlockParseInfo("spaceless", " ", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 10,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("widthratio "),
                    Expected    = (DjangoBlock) new DjangoWidthRatioBlock(new BlockParseInfo("widthratio", " ", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 11,
                            Expected = new [] { "fob", "oar" }
                        }
                    }
                },
                new {
                    Got         = ("templatetag "),
                    Expected    = (DjangoBlock) new DjangoTemplateTagBlock(new BlockParseInfo("templatetag", " ", 0), 11, null),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 11,
                            Expected = new [] { "openblock", "closeblock", "openvariable", "closevariable", "openbrace", "closebrace", "opencomment", "closecomment" }
                        }
                    }
                },
                new {
                    Got         = ("templatetag open"),
                    Expected    = (DjangoBlock) new DjangoTemplateTagBlock(new BlockParseInfo("templatetag", " open", 0), 11, null),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 15,
                            Expected = new [] { "openblock", "openvariable", "openbrace", "opencomment" }
                        }
                    }
                },
                new {
                    Got         = ("templatetag openblock "),
                    Expected    = (DjangoBlock) new DjangoTemplateTagBlock(new BlockParseInfo("templatetag", " openblock ", 0), 11, "openblock"),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 22,
                            Expected = new string[0]
                        }
                    }
                },
            };

            foreach (var testCase in testCases)
            {
                Console.WriteLine(testCase.Got);

                var got = DjangoBlock.Parse(testCase.Got);

                ValidateBlock(testCase.Expected, got);

                foreach (var completionCase in testCase.Completions)
                {
                    var completions = new HashSet <string>(got.GetCompletions(testCase.Context, completionCase.Position).Select(x => x.DisplayText));

                    Assert.AreEqual(completionCase.Expected.Length, completions.Count);
                    var expected = new HashSet <string>(completionCase.Expected);
                    foreach (var value in completions)
                    {
                        Assert.IsTrue(expected.Contains(value));
                    }
                }
            }
        }