AsciidocBlockScanner { public List<AsciidocFormatBlock> scan(String source) { Context context = new Context(); StringBuilder sb = new StringBuilder(); for (char c: source.toCharArray()) { if (c=='\n') { context.addLine(sb.toString(), true); sb=new StringBuilder(); continue; } sb.append(c); } if (sb.length()>0 ) { context.addLine(sb.toString(), false); } context.closeBlock(); return context.asciidocFormatBlocks; }  List<AsciidocFormatBlock> scan(String source);  }
@Test public void empty_string_contains_no_blocks() { String line = ""; List<AsciidocFormatBlock> result = scannerToTest.scan(line); assertNotNull(result); assertEquals(0,result.size()); assertSourcesAddedLikeOrigin(result,line); }
@Test public void headline() { String line = "== headline"; List<AsciidocFormatBlock> result = scannerToTest.scan(line); assertNotNull(result); assertEquals(1,result.size()); AsciidocFormatBlock n = result.iterator().next(); assertEquals(AsciidocBlockType.HEADLINE, n.blockType); assertEquals("== headline", n.source.toString()); assertSourcesAddedLikeOrigin(result,line); }
@Test public void textBlock() { String line = "I am just a text\nwith multipe lines"; List<AsciidocFormatBlock> result = scannerToTest.scan(line); assertNotNull(result); assertEquals(1,result.size()); AsciidocFormatBlock n = result.iterator().next(); assertEquals(AsciidocBlockType.TEXT, n.blockType); assertEquals("I am just a text\nwith multipe lines", n.source.toString()); assertSourcesAddedLikeOrigin(result,line); }
@Test public void textBlocks_separated_by_one_empty_line() { String line = "I am just a text\nwith multipe lines\n\nNew textblock"; List<AsciidocFormatBlock> result = scannerToTest.scan(line); assertNotNull(result); assertEquals(3,result.size()); Iterator<AsciidocFormatBlock> iterator = result.iterator(); AsciidocFormatBlock n = iterator.next(); assertEquals(AsciidocBlockType.TEXT, n.blockType); assertEquals("I am just a text\nwith multipe lines\n", n.source.toString()); n = iterator.next(); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); assertEquals("\n", n.source.toString()); n = iterator.next(); assertEquals(AsciidocBlockType.TEXT, n.blockType); assertEquals("New textblock", n.source.toString()); assertSourcesAddedLikeOrigin(result,line); }
@Test public void textBlock_separated_by_one_empty_line_before() { String line = "\nI am just a text\nwith multipe lines"; List<AsciidocFormatBlock> result = scannerToTest.scan(line); assertNotNull(result); assertEquals(2,result.size()); Iterator<AsciidocFormatBlock> iterator = result.iterator(); AsciidocFormatBlock n = iterator.next(); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); assertEquals("\n", n.source.toString()); n = iterator.next(); assertEquals(AsciidocBlockType.TEXT, n.blockType); assertEquals("I am just a text\nwith multipe lines", n.source.toString()); assertSourcesAddedLikeOrigin(result,line); }
@Test public void textBlock_inside_two_empty_lines() { String line = "\nsomething\n\n"; List<AsciidocFormatBlock> result = scannerToTest.scan(line); assertNotNull(result); assertEquals(3,result.size()); Iterator<AsciidocFormatBlock> iterator = result.iterator(); AsciidocFormatBlock n = iterator.next(); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); assertEquals("\n", n.source.toString()); n = iterator.next(); assertEquals(AsciidocBlockType.TEXT,n.blockType); assertEquals("something\n", n.source.toString()); n = iterator.next(); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); assertEquals("\n", n.source.toString()); assertSourcesAddedLikeOrigin(result,line); }
@Test public void textBlock_inside_two_empty_lines_indented_with_one_space() { String line = "\n something\n\n"; List<AsciidocFormatBlock> result = scannerToTest.scan(line); assertNotNull(result); assertEquals(3,result.size()); Iterator<AsciidocFormatBlock> iterator = result.iterator(); AsciidocFormatBlock n = iterator.next(); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); assertEquals("\n", n.source.toString()); n = iterator.next(); assertEquals(AsciidocBlockType.TEXT,n.blockType); assertEquals(" something\n", n.source.toString()); n = iterator.next(); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); assertEquals("\n", n.source.toString()); assertSourcesAddedLikeOrigin(result,line); }
@Test public void empty_line_headline_empty_line_empty_line() { String line = "\n== headline\n\n\n"; List<AsciidocFormatBlock> result = scannerToTest.scan(line); assertNotNull(result); assertEquals(4,result.size()); Iterator<AsciidocFormatBlock> iterator = result.iterator(); AsciidocFormatBlock n = iterator.next(); assertEquals("\n", n.source.toString()); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); n = iterator.next(); assertEquals("== headline\n", n.source.toString()); assertEquals(AsciidocBlockType.HEADLINE, n.blockType); n = iterator.next(); assertEquals("\n", n.source.toString()); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); n = iterator.next(); assertEquals("\n", n.source.toString()); assertEquals(AsciidocBlockType.EMPTY_LINE, n.blockType); assertSourcesAddedLikeOrigin(result,line); }