SimpleHeadlineParser { public List<AsciiDoctorHeadline> parse(String asciidoctorScript) { List<AsciiDoctorHeadline> list = new ArrayList<AsciiDoctorHeadline>(); if (asciidoctorScript == null) { return list; } StringBuilder current = new StringBuilder(); int start=0; int currentPos = -1; for (char c : asciidoctorScript.toCharArray()) { currentPos++; if (c == '\n') { int end = currentPos-1; addHeadlineWhenCurrentNotEmptyAndHasASpaceAfterLastEqual(asciidoctorScript, list, current, start, end); current = new StringBuilder(); start =currentPos+1; continue; } if (current != null) { current.append(c); if (current.charAt(0) != '=') { current = null; } } } addHeadlineWhenCurrentNotEmptyAndHasASpaceAfterLastEqual(asciidoctorScript, list, current, start, currentPos); return list; }  List<AsciiDoctorHeadline> parse(String asciidoctorScript);  }
@Test public void two_headline4_with_text_are_recognized_as_headlines_4() throws Exception { String text = "==== headline1\n==== headline2"; List<AsciiDoctorHeadline> result = parserToTest.parse(text); assertHeadlines(result). hasHeadline("headline1"). withPosition(0). withEnd(13). and(). hasHeadline("headline2"). withPosition(15). withEnd(28); }
@Test public void two_headline4_with_no_text_are_NOT_recognized_as_headlines_4() throws Exception { String text = "====\n===="; List<AsciiDoctorHeadline> result = parserToTest.parse(text); assertHeadlines(result). hasNoHeadlines(); }
@Test public void headline1_new_line_headline2__headline1_has_pos_0_headline2_pos_12() throws Exception { String text = "= headline1\n= headline2"; List<AsciiDoctorHeadline> result = parserToTest.parse(text); assertHeadlines(result). hasHeadline("headline1"). withPosition(0). withEnd(10). and(). hasHeadline("headline2"). withPosition(12). withEnd(22); }
@Test public void headline1_new_line_headline2__headline2_has_deep1() throws Exception { String text = "= headline1\n= headline2"; List<AsciiDoctorHeadline> result = parserToTest.parse(text); assertHeadlines(result).hasHeadlines(2).hasHeadline("headline2").withDeep(1); }
@Test public void wrongheadline1_new_line_headline2__headline2_has_deep1() throws Exception { String text = "=wrongheadline1\n= headline2"; List<AsciiDoctorHeadline> result = parserToTest.parse(text); assertHeadlines(result).hasHeadlines(1).hasHeadline("headline2").withDeep(1); }
@Test public void empty_string_has_no_parser_results() { Collection<AsciiDoctorHeadline> result = parserToTest.parse(""); assertNotNull(result); assertTrue(result.isEmpty()); }
@Test public void one_line_headline1__has_one_result_with_deep_1() { List<AsciiDoctorHeadline> result = parserToTest.parse("= headline1"); assertHeadlines(result).hasHeadlines(1).hasHeadline("headline1").withDeep(1); }
@Test public void one_line_space_headline1__has_one_result() { Collection<AsciiDoctorHeadline> result = parserToTest.parse("= headline1"); assertEquals(1, result.size()); AsciiDoctorHeadline headlineFound = result.iterator().next(); assertEquals(1,headlineFound.getDeep()); assertEquals("headline1",headlineFound.getName()); }
@Test public void three_headlines__has_3_result() { Collection<AsciiDoctorHeadline> result = parserToTest.parse("= headline1\n== headline2\n= headline3"); assertEquals(3, result.size()); Iterator<AsciiDoctorHeadline> iterator = result.iterator(); assertEquals(1,iterator.next().getDeep()); assertEquals(2,iterator.next().getDeep()); assertEquals(1,iterator.next().getDeep()); }