Inheritance: ILanguage
            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);
            }
            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);
            }
            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_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_find_a_loaded_language_with_a_matching_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);

                Assert.Equal(expected, actual);
            }
            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);
            }
            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);
            }
            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);
            }
            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);
            }
            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);
            }
            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);
            }
            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_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_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);
            }
            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);
            }