Beispiel #1
0
            public void It_will_yield_partially_matched_source_code_and_the_unmatched_source_code_after_it()
            {
                StubLanguageCompiler stubLanguageCompiler = new StubLanguageCompiler();

                stubLanguageCompiler.Compile__return = new CompiledLanguage("fnord", "fnord", new Regex("source code"), 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 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", firstParsedSourceCode);
                Assert.Equal("A Scope Name", firstScopes[0].Name);
                Assert.Equal(" that will be partially matched", secondParsedSourceCode);
                Assert.Empty(secondScopes);
            }
Beispiel #2
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);
            }
Beispiel #3
0
            public void It_will_compile_a_single_language_rule_with_partial_captures()
            {
                Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>();
                LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache);
                StubLanguage     stubLanguage     = new StubLanguage();

                stubLanguage.id__getValue    = "fnord";
                stubLanguage.name__getValue  = "fnord";
                stubLanguage.rules__getValue = new List <LanguageRule>
                {
                    new LanguageRule("(a) (language) (rule)", new Dictionary <int, string>
                    {
                        { 1, "style for the a part" },
                        { 2, "style for the language part" },
                        { 3, "style for the rule part" }
                    })
                };

                CompiledLanguage compiledLanguage = languageCompiler.Compile(stubLanguage);

                Assert.Equal(@"(?x)
(?-xis)(?m)((a) (language) (rule))(?x)", compiledLanguage.Regex.ToString());
                Assert.Null(compiledLanguage.Captures[0]);
                Assert.Null(compiledLanguage.Captures[1]);
                Assert.Equal("style for the a part", compiledLanguage.Captures[2]);
                Assert.Equal("style for the language part", compiledLanguage.Captures[3]);
                Assert.Equal("style for the rule part", compiledLanguage.Captures[4]);
            }
Beispiel #4
0
            public void It_will_yield_source_code_with_no_matches()
            {
                StubLanguageCompiler stubLanguageCompiler = new StubLanguageCompiler();

                stubLanguageCompiler.Compile__return = new CompiledLanguage("fnord", "fnord", new Regex("this won't match the source code"), new List <string> {
                    "fnord"
                });
                LanguageParser         languageParser          = new LanguageParser(stubLanguageCompiler, new StubLanguageRepository());
                StubLanguage           stubLanguage            = new StubLanguage();
                const string           sourceCode              = "source code that won't 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 won't be matched", firstParsedSourceCode);
                Assert.Empty(firstScopes);
            }
Beispiel #5
0
            public void It_will_retrieve_a_previously_compiled_language_from_the_cache_when_one_is_there()
            {
                Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>();
                CompiledLanguage stubCompiledLanguage = new CompiledLanguage("theLanguageId", "fnord", new Regex("fnord"), new List <string> {
                    "fnord"
                });

                stubCompiledLanguagesCache.Add(stubCompiledLanguage.Id, stubCompiledLanguage);
                LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache);
                StubLanguage     stubLanguage     = new StubLanguage();

                stubLanguage.id__getValue    = "theLanguageId";
                stubLanguage.name__getValue  = "fnord";
                stubLanguage.rules__getValue = new List <LanguageRule> {
                    new LanguageRule("fnord", new Dictionary <int, string> {
                        { 0, "fnord" }
                    })
                };

                CompiledLanguage compiledLanguage = languageCompiler.Compile(stubLanguage);

                Assert.Equal("theLanguageId", compiledLanguage.Id);
                Assert.False(stubLanguage.Name__getInvoked);
                Assert.False(stubLanguage.Rules__getInvoked);
            }
            public void Will_parse_the_source_code()
            {
                StubLanguageParser stubLanguageParser = new StubLanguageParser();
                CodeColorizer      codeColorizer      = new CodeColorizer(stubLanguageParser);
                const string       sourceCode         = "fnord";
                StubLanguage       stubLanguage       = new StubLanguage();

                codeColorizer.Colorize(sourceCode, stubLanguage, new StubFormatter(), new StubStyleSheet(), new StubTextWriter());

                Assert.Equal(sourceCode, stubLanguageParser.Parse__sourceCode);
                Assert.Equal(stubLanguage, stubLanguageParser.Parse__language);
            }
            public void Will_add_the_language_to_the_loaded_languages()
            {
                var stubLoadedLanguages = new Dictionary <string, ILanguage>();
                var languageRepository  = new LanguageRepository(stubLoadedLanguages);
                var stubLanguage        = new StubLanguage();

                stubLanguage.id__getValue = "fnord";

                languageRepository.Load(stubLanguage);

                Assert.Contains(stubLanguage, stubLoadedLanguages.Values);
            }
            public void Will_throw_when__the_language_identifier_is_empty()
            {
                var languageRepository = new LanguageRepository(new Dictionary <string, ILanguage>());
                var language           = new StubLanguage();

                language.id__getValue = string.Empty;

                Exception ex = Record.Exception(() => languageRepository.Load(language));

                Assert.IsType <ArgumentException>(ex);
                Assert.Contains("The language identifier must not be null or empty.", ex.Message);
                Assert.Equal("language", ((ArgumentException)ex).ParamName);
            }
            public void Will_find_a_loaded_language_with_a_matching_case_insensitive_identfier()
            {
                var expected = new StubLanguage();

                expected.id__getValue = "fnord";
                var loadedLanguages = new Dictionary <string, ILanguage>();

                loadedLanguages.Add(expected.Id, expected);
                var languageRepository = new LanguageRepository(loadedLanguages);

                ILanguage actual = languageRepository.FindById(expected.Id.ToUpper());

                Assert.Equal(expected, actual);
            }
            public void It_will_write_the_header_with_no_class_name_if_language_does_not_specify_one()
            {
                HtmlClassFormatter formatter      = new HtmlClassFormatter();
                StubStyleSheet     stubStyleSheet = new StubStyleSheet {
                    Name__getValue = "fnord", Styles__getValue = new StyleDictionary()
                };
                StubLanguage stubLanguage = new StubLanguage {
                    CssClassName_getValue = ""
                };
                StubTextWriter stubTextWriter = new StubTextWriter();

                formatter.WriteHeader(stubStyleSheet, stubLanguage, stubTextWriter);

                Assert.Equal("<div><pre>", stubTextWriter.Write__buffer.Trim());
            }
            public void Will_return_null_if_no_loaded_languages_match_the_language_identifier_nor_the_alias()
            {
                var expected = new StubLanguage();

                expected.id__getValue   = "fnord";
                expected.Alias_getValue = "alias";
                var loadedLanguages = new Dictionary <string, ILanguage>();

                loadedLanguages.Add(expected.Id, expected);
                var languageRepository = new LanguageRepository(loadedLanguages);

                ILanguage actual = languageRepository.FindById("fake");

                Assert.Null(actual);
            }
Beispiel #12
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);
            }
            public void Will_find_a_loaded_language_with_a_matching_alias()
            {
                var expected = new StubLanguage();

                expected.id__getValue   = "fnord";
                expected.Alias_getValue = "alias";
                var loadedLanguages = new Dictionary <string, ILanguage>();

                loadedLanguages.Add(expected.Id, expected);
                var languageRepository = new LanguageRepository(loadedLanguages);

                ILanguage actual = languageRepository.FindById(expected.Alias_getValue);

                Assert.Equal(expected, actual);
            }
            public void It_will_write_the_footer()
            {
                HtmlClassFormatter formatter      = new HtmlClassFormatter();
                StubStyleSheet     stubStyleSheet = new StubStyleSheet {
                    Name__getValue = "fnord", Styles__getValue = new StyleDictionary {
                    }
                };
                StubLanguage stubLanguage = new StubLanguage {
                    CssClassName_getValue = "fnord"
                };
                StubTextWriter stubTextWriter = new StubTextWriter();

                formatter.WriteFooter(stubStyleSheet, stubLanguage, stubTextWriter);

                Assert.Equal("</pre></div>", stubTextWriter.Write__buffer.Trim());
            }
Beispiel #15
0
            public void It_will_throw_when_the_language_is_not_in_cach_and_the_rules_collection_is_empty()
            {
                Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>();
                LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache);
                StubLanguage     stubLanguage     = new StubLanguage();

                stubLanguage.id__getValue    = "fnord";
                stubLanguage.name__getValue  = "fnord";
                stubLanguage.rules__getValue = new List <LanguageRule>();

                Exception ex = Record.Exception(() => languageCompiler.Compile(stubLanguage));

                Assert.IsType <ArgumentException>(ex);
                Assert.Contains("The language rules collection must not be empty.", ex.Message);
                Assert.Equal("language", ((ArgumentException)ex).ParamName);
            }
Beispiel #16
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);
            }
            public void It_will_write_the_language_name_into_the_header()
            {
                HtmlClassFormatter formatter = new HtmlClassFormatter();

                StubStyleSheet stubStyleSheet = new StubStyleSheet {
                    Name__getValue = "fnord", Styles__getValue = new StyleDictionary {
                    }
                };
                StubLanguage language = new StubLanguage {
                    CssClassName_getValue = "fnord"
                };
                StubTextWriter stubTextWriter = new StubTextWriter();

                formatter.WriteHeader(stubStyleSheet, language, stubTextWriter);

                Assert.Equal("<div class=\"fnord\"><pre>", stubTextWriter.Write__buffer.Trim());
            }
            public void Will_add_a_second_language_to_the_loaded_languages()
            {
                var loadedLanguages    = new Dictionary <string, ILanguage>();
                var languageRepository = new LanguageRepository(loadedLanguages);
                var language1          = new StubLanguage();

                language1.id__getValue = "fnord";
                var language2 = new StubLanguage();

                language2.id__getValue = "not fnord";

                languageRepository.Load(language1);
                languageRepository.Load(language2);

                Assert.Contains(language1, loadedLanguages.Values);
                Assert.Contains(language2, loadedLanguages.Values);
            }
            public void Will_replace_an_existing_language_with_same_identifier()
            {
                var loadedLanguages    = new Dictionary <string, ILanguage>();
                var languageRepository = new LanguageRepository(loadedLanguages);
                var language1          = new StubLanguage();

                language1.id__getValue = "fnord";
                var language2 = new StubLanguage();

                language2.id__getValue = "fnord";
                languageRepository.Load(language1);

                languageRepository.Load(language2);

                Assert.DoesNotContain(language1, loadedLanguages.Values);
                Assert.Contains(language2, loadedLanguages.Values);
            }
Beispiel #20
0
            public void It_will_parse_nested_languages_with_nested_scopes()
            {
                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("a (nested (lang)uage)"), new List <string> {
                    "style for a nested language", "style for nested language part", "style for lang part"
                }));
                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 nested language part", firstScopes[1].Children[0].Name);
                Assert.Equal("style for lang part", firstScopes[1].Children[0].Children[0].Name);
                Assert.Equal("style for part after nested language", firstScopes[2].Name);
            }
Beispiel #21
0
            public void It_will_use_the_language_name_for_the_compiled_language_name()
            {
                Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>();
                LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache);
                StubLanguage     stubLanguage     = new StubLanguage();

                stubLanguage.id__getValue    = "fnord";
                stubLanguage.name__getValue  = "The Language ScopeName";
                stubLanguage.rules__getValue = new List <LanguageRule> {
                    new LanguageRule("fnord", new Dictionary <int, string> {
                        { 0, "fnord" }
                    })
                };

                CompiledLanguage compiledLanguage = languageCompiler.Compile(stubLanguage);

                Assert.Equal("The Language ScopeName", compiledLanguage.Name);
            }
            public void Will_return_all_loaded_languages()
            {
                var language1 = new StubLanguage();

                language1.id__getValue = "fnord";
                var language2 = new StubLanguage();

                language2.id__getValue = "not fnord";
                var loadedLanguages = new Dictionary <string, ILanguage>();

                loadedLanguages.Add(language1.Id, language1);
                loadedLanguages.Add(language2.Id, language2);
                var languageRepository = new LanguageRepository(loadedLanguages);

                IEnumerable <ILanguage> all = languageRepository.All;

                Assert.Contains(language1, all);
                Assert.Contains(language2, all);
            }
Beispiel #23
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);
            }
Beispiel #24
0
            public void It_will_throw_when_the_language_identifier_is_empty()
            {
                Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>();
                LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache);
                StubLanguage     stubLanguage     = new StubLanguage();

                stubLanguage.id__getValue    = string.Empty;
                stubLanguage.name__getValue  = "fnord";
                stubLanguage.rules__getValue = new List <LanguageRule> {
                    new LanguageRule("fnord", new Dictionary <int, string> {
                        { 0, "fnord" }
                    })
                };

                Exception ex = Record.Exception(() => languageCompiler.Compile(stubLanguage));

                Assert.IsType <ArgumentException>(ex);
                Assert.Contains("The language identifier must not be null.", ex.Message);
                Assert.Equal("language", ((ArgumentException)ex).ParamName);
            }
Beispiel #25
0
            public void It_will_compile_a_single_language_rule_with_a_whole_capture()
            {
                Dictionary <string, CompiledLanguage> stubCompiledLanguagesCache = new Dictionary <string, CompiledLanguage>();
                LanguageCompiler languageCompiler = new LanguageCompiler(stubCompiledLanguagesCache);
                StubLanguage     stubLanguage     = new StubLanguage();

                stubLanguage.id__getValue    = "fnord";
                stubLanguage.name__getValue  = "fnord";
                stubLanguage.rules__getValue = new List <LanguageRule> {
                    new LanguageRule("a language rule", new Dictionary <int, string> {
                        { 0, "style for whole rule" }
                    })
                };

                CompiledLanguage compiledLanguage = languageCompiler.Compile(stubLanguage);

                Assert.Equal(@"(?x)
(?-xis)(?m)(a language rule)(?x)", compiledLanguage.Regex.ToString());
                Assert.Null(compiledLanguage.Captures[0]);
                Assert.Equal("style for whole rule", compiledLanguage.Captures[1]);
            }