コード例 #1
0
            public void It_will_parse_nested_languages()
            {
                StubLanguageCompiler stubLanguageCompiler = new StubLanguageCompiler();
                Queue<CompiledLanguage> stubCompiledLanguages = new Queue<CompiledLanguage>();
                stubCompiledLanguages.Enqueue(new CompiledLanguage("fnord", "fnord", new Regex("(source code with ){(.*?)}( in the curly braces)"), new List<string> { null, "style for part before nested language", "&nestedLanguageId", "style for part after nested language" }));
                stubCompiledLanguages.Enqueue(new CompiledLanguage("nestedLanguageId", "fnord", new Regex(".*"), new List<string> { "style for a nested language" }));
                stubLanguageCompiler.Compile__do = (language) => stubCompiledLanguages.Dequeue();
                StubLanguageRepository stubLanguageRepository = new StubLanguageRepository();
                stubLanguageRepository.FindById__do = (languageId) => new StubLanguage{ id__getValue = "nestedLanguageId", name__getValue = "fnord", rules__getValue = new List<LanguageRule>{ new LanguageRule("fnord", new Dictionary<int, string>{ {0, "fnord"}})}};
                LanguageParser languageParser = new LanguageParser(stubLanguageCompiler, stubLanguageRepository);
                StubLanguage stubLanguage = new StubLanguage();
                const string sourceCode = "source code with {a nested language} in the curly braces";
                int parseHandlerInvocations = 0;
                Stack<string> parsedSourceCodeStack = new Stack<string>();
                Stack<IList<Scope>> scopesStack = new Stack<IList<Scope>>();

                languageParser.Parse(sourceCode, stubLanguage, (parsedSourceCode, scopes) =>
                {
                    parseHandlerInvocations++;
                    parsedSourceCodeStack.Push(parsedSourceCode);
                    scopesStack.Push(scopes);
                });

                Assert.Equal(1, parseHandlerInvocations);
                string firstParsedSourceCode = parsedSourceCodeStack.Pop();
                IList<Scope> firstScopes = scopesStack.Pop();
                Assert.Equal("source code with {a nested language} in the curly braces", firstParsedSourceCode);
                Assert.Equal("style for part before nested language", firstScopes[0].Name);
                Assert.Equal("style for a nested language", firstScopes[1].Name);
                Assert.Equal("style for part after nested language", firstScopes[2].Name);
            }
コード例 #2
0
            public void It_will_compile_the_language()
            {
                StubLanguageCompiler stubLanguageCompiler = new StubLanguageCompiler();
                stubLanguageCompiler.Compile__return = new CompiledLanguage("fnord", "fnord", new Regex("fnord"), new List<string> { "fnord" });
                LanguageParser languageParser = new LanguageParser(stubLanguageCompiler, new StubLanguageRepository());
                StubLanguage stubLanguage = new StubLanguage();
                const string sourceCode = "fnord";

                languageParser.Parse(sourceCode, stubLanguage, (parsedSourceCode, scopes) => { });

                Assert.Equal(stubLanguage, stubLanguageCompiler.Compile__language);
            }
コード例 #3
0
            public void It_will_not_parse_empty_source_code()
            {
                StubLanguageCompiler stubLanguageCompiler = new StubLanguageCompiler();
                stubLanguageCompiler.Compile__return = new CompiledLanguage("fnord", "fnord", new Regex("fnord"), new List<string> { "fnord" });
                LanguageParser languageParser = new LanguageParser(stubLanguageCompiler, new StubLanguageRepository());
                StubLanguage stubLanguage = new StubLanguage();
                string sourceCode = string.Empty;
                int parseHandlerInvocations = 0;

                languageParser.Parse(sourceCode, stubLanguage, (parsedSourceCode, scopes) =>
                {
                    parseHandlerInvocations++;
                });

                Assert.Equal(0, parseHandlerInvocations);
            }
コード例 #4
0
            public void It_will_yield_wholly_matched_parsed_source_code()
            {
                StubLanguageCompiler stubLanguageCompiler = new StubLanguageCompiler();
                stubLanguageCompiler.Compile__return = new CompiledLanguage("fnord", "fnord", new Regex(".*"), new List<string> { "A Scope Name" });
                LanguageParser languageParser = new LanguageParser(stubLanguageCompiler, new StubLanguageRepository());
                StubLanguage stubLanguage = new StubLanguage();
                const string sourceCode = "source code that will be matched";
                int parseHandlerInvocations = 0;
                Stack<string> parsedSourceCodeStack = new Stack<string>();
                Stack<IList<Scope>> scopesStack = new Stack<IList<Scope>>();

                languageParser.Parse(sourceCode, stubLanguage, (parsedSourceCode, scopes) =>
                {
                    parseHandlerInvocations++;
                    parsedSourceCodeStack.Push(parsedSourceCode);
                    scopesStack.Push(scopes);
                });

                Assert.Equal(1, parseHandlerInvocations);
                string firstParsedSourceCode = parsedSourceCodeStack.Pop();
                IList<Scope> firstScopes = scopesStack.Pop();
                Assert.Equal("source code that will be matched", firstParsedSourceCode);
                Assert.Equal("A Scope Name", firstScopes[0].Name);
            }
コード例 #5
0
            public void It_will_yield_nested_scopes()
            {
                StubLanguageCompiler stubLanguageCompiler = new StubLanguageCompiler();
                stubLanguageCompiler.Compile__return = new CompiledLanguage("fnord", "fnord", new Regex("((part)ially) (matched)"), new List<string> { "scope name for the whole match", "scope name for the partially part", "scope name for the part part", "scope name for the matched part" });
                LanguageParser languageParser = new LanguageParser(stubLanguageCompiler, new StubLanguageRepository());
                StubLanguage stubLanguage = new StubLanguage();
                const string sourceCode = "source code that will be partially matched";
                int parseHandlerInvocations = 0;
                Stack<string> parsedSourceCodeStack = new Stack<string>();
                Stack<IList<Scope>> scopesStack = new Stack<IList<Scope>>();

                languageParser.Parse(sourceCode, stubLanguage, (parsedSourceCode, scopes) =>
                {
                    parseHandlerInvocations++;
                    parsedSourceCodeStack.Push(parsedSourceCode);
                    scopesStack.Push(scopes);
                });

                Assert.Equal(2, parseHandlerInvocations);
                string secondParsedSourceCode = parsedSourceCodeStack.Pop();
                string firstParsedSourceCode = parsedSourceCodeStack.Pop();
                IList<Scope> secondScopes = scopesStack.Pop();
                IList<Scope> firstScopes = scopesStack.Pop();
                Assert.Equal("source code that will be ", firstParsedSourceCode);
                Assert.Empty(firstScopes);
                Assert.Equal("partially matched", secondParsedSourceCode);
                Assert.Equal("scope name for the whole match", secondScopes[0].Name);
                Assert.Equal("scope name for the partially part", secondScopes[0].Children[0].Name);
                Assert.Equal("scope name for the part part", secondScopes[0].Children[0].Children[0].Name);
                Assert.Equal("scope name for the matched part", secondScopes[0].Children[1].Name);
            }
コード例 #6
0
            public void It_will_throw_if_the_nested_language_is_not_found()
            {
                StubLanguageCompiler stubLanguageCompiler = new StubLanguageCompiler();
                stubLanguageCompiler.Compile__do = (language) => new CompiledLanguage("fnord", "fnord", new Regex("(source code with ){(.*?)}( in the curly braces)"), new List<string> { null, "style for part before nested language", "&nestedLanguageId", "style for part after nested language" });
                LanguageParser languageParser = new LanguageParser(stubLanguageCompiler, new StubLanguageRepository());
                StubLanguage stubLanguage = new StubLanguage();
                const string sourceCode = "source code with {a nested language} in the curly braces";

                Exception ex = Record.Exception(() => languageParser.Parse(sourceCode, stubLanguage, (parsedSourceCode, scopes) => { }));

                Assert.IsType<InvalidOperationException>(ex);
                Assert.Contains("The nested language was not found in the language repository.", ex.Message);
            }