|
| 1 | +package org.asciidoctor.jruby.ast.impl; |
| 2 | + |
| 3 | +import org.asciidoctor.Asciidoctor; |
| 4 | +import org.asciidoctor.Attributes; |
| 5 | +import org.asciidoctor.Options; |
| 6 | +import org.asciidoctor.ast.Document; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | + |
| 13 | +public class WhenDocumentIsLoaded { |
| 14 | + |
| 15 | + private Asciidoctor asciidoctor = Asciidoctor.Factory.create(); |
| 16 | + |
| 17 | + @Test |
| 18 | + public void should_return_empty_when_document_is_empty() { |
| 19 | + assertEmptySources(loadDocument("")); |
| 20 | + } |
| 21 | + |
| 22 | + private static void assertEmptySources(Document document) { |
| 23 | + String source = document.getSource(); |
| 24 | + assertThat(source).isEmpty(); |
| 25 | + List<String> sourceLines = document.getSourceLines(); |
| 26 | + assertThat(sourceLines).isEmpty(); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void should_return_source_and_source_lines() { |
| 31 | + final String asciidoc = asciidocSample(); |
| 32 | + |
| 33 | + Document document = loadDocument(asciidoc); |
| 34 | + |
| 35 | + String source = document.getSource(); |
| 36 | + assertThat(source).isEqualTo(asciidoc.trim()); |
| 37 | + List<String> sourceLines = document.getSourceLines(); |
| 38 | + assertThat(sourceLines) |
| 39 | + .containsExactly("= Document Title", |
| 40 | + "", |
| 41 | + "== Section A", |
| 42 | + "", |
| 43 | + "Section A paragraph.", |
| 44 | + "", |
| 45 | + "=== Section A Subsection", |
| 46 | + "", |
| 47 | + "Section A 'subsection' paragraph."); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void should_return_source_and_source_lines_without_trailing() { |
| 52 | + final String asciidoc = "= Document Title\n\n" + |
| 53 | + "== Section\n\n" + |
| 54 | + "Hello\t \n"; |
| 55 | + |
| 56 | + Document document = loadDocument(asciidoc); |
| 57 | + |
| 58 | + String source = document.getSource(); |
| 59 | + assertThat(source).isEqualTo("= Document Title\n\n== Section\n\nHello"); |
| 60 | + List<String> sourceLines = document.getSourceLines(); |
| 61 | + assertThat(sourceLines) |
| 62 | + .containsExactly("= Document Title", |
| 63 | + "", |
| 64 | + "== Section", |
| 65 | + "", |
| 66 | + "Hello"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void should_return_source_and_source_lines_without_resolving_attributes() { |
| 71 | + final String asciidoc = "= Document Title\n" + |
| 72 | + ":an-attribute: a-value\n\n" + |
| 73 | + "This is {an-attribute}"; |
| 74 | + |
| 75 | + Document document = loadDocument(asciidoc); |
| 76 | + |
| 77 | + String source = document.getSource(); |
| 78 | + assertThat(source).isEqualTo("= Document Title\n:an-attribute: a-value\n\nThis is {an-attribute}"); |
| 79 | + List<String> sourceLines = document.getSourceLines(); |
| 80 | + assertThat(sourceLines) |
| 81 | + .containsExactly("= Document Title", |
| 82 | + ":an-attribute: a-value", |
| 83 | + "", |
| 84 | + "This is {an-attribute}"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void should_return_source_and_source_lines_without_resolving_includes() { |
| 89 | + final String asciidoc = "= Document Title\n\n" + |
| 90 | + "== Section\n\n" + |
| 91 | + "include::partial.adoc[]"; |
| 92 | + |
| 93 | + Document document = loadDocument(asciidoc); |
| 94 | + |
| 95 | + String source = document.getSource(); |
| 96 | + assertThat(source).isEqualTo("= Document Title\n\n== Section\n\ninclude::partial.adoc[]"); |
| 97 | + List<String> sourceLines = document.getSourceLines(); |
| 98 | + assertThat(sourceLines) |
| 99 | + .containsExactly("= Document Title", |
| 100 | + "", |
| 101 | + "== Section", |
| 102 | + "", |
| 103 | + "include::partial.adoc[]"); |
| 104 | + } |
| 105 | + |
| 106 | + private Document loadDocument(String source) { |
| 107 | + Attributes attributes = Attributes.builder().build(); |
| 108 | + Options options = Options.builder().attributes(attributes).build(); |
| 109 | + return asciidoctor.load(source, options); |
| 110 | + } |
| 111 | + |
| 112 | + static String asciidocSample() { |
| 113 | + return "= Document Title\n\n" + |
| 114 | + "== Section A\n\n" + |
| 115 | + "Section A paragraph.\n\n" + |
| 116 | + "=== Section A Subsection\n\n" + |
| 117 | + "Section A 'subsection' paragraph.\n\n"; |
| 118 | + } |
| 119 | +} |
0 commit comments