-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1121 from abelsromero/expose-sections-sectnum
Expose Section's sectnum property
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
asciidoctorj-core/src/test/java/org/asciidoctor/jruby/ast/impl/SectionImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.asciidoctor.jruby.ast.impl; | ||
|
||
import org.asciidoctor.Asciidoctor; | ||
import org.asciidoctor.Attributes; | ||
import org.asciidoctor.Options; | ||
import org.asciidoctor.ast.Document; | ||
import org.asciidoctor.ast.Section; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class SectionImplTest { | ||
|
||
private Asciidoctor asciidoctor = Asciidoctor.Factory.create(); | ||
|
||
|
||
@Test | ||
public void should_return_valid_sectnum_when_sectionNumbers_are_enabled() { | ||
final String source = sectionsSample(); | ||
|
||
Document document = loadDocument(source, true); | ||
|
||
Section node1 = findSectionNode(document, 1); | ||
assertThat(node1.getSectnum()).isEqualTo("1."); | ||
assertThat(node1.isNumbered()).isTrue(); | ||
|
||
Section node2 = findSectionNode(document, 2); | ||
assertThat(node2.getSectnum()).isEqualTo("1.1."); | ||
assertThat(node2.isNumbered()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void should_return_invalid_sectnum_when_sectionNumbers_are_not_enabled() { | ||
final String source = sectionsSample(); | ||
|
||
Document document = loadDocument(source, false); | ||
|
||
Section node1 = findSectionNode(document, 1); | ||
assertThat(node1.getSectnum()).isEqualTo("."); | ||
assertThat(node1.isNumbered()).isFalse(); | ||
|
||
Section node2 = findSectionNode(document, 2); | ||
assertThat(node2.getSectnum()).isEqualTo(".."); | ||
assertThat(node2.isNumbered()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void should_return_sectnum_with_custom_delimiter_when_sectionNumbers_are_enabled() { | ||
final String source = sectionsSample(); | ||
|
||
Document document = loadDocument(source, true); | ||
|
||
Section node1 = findSectionNode(document, 1); | ||
assertThat(node1.getSectnum("_")).isEqualTo("1_"); | ||
|
||
Section node2 = findSectionNode(document, 2); | ||
assertThat(node2.getSectnum("*")).isEqualTo("1*1*"); | ||
} | ||
|
||
@Test | ||
public void should_return_sectnum_with_custom_delimiter_when_sectionNumbers_are_not_enabled() { | ||
final String source = sectionsSample(); | ||
|
||
Document document = loadDocument(source, false); | ||
|
||
Section node1 = findSectionNode(document, 1); | ||
assertThat(node1.getSectnum("_")).isEqualTo("_"); | ||
|
||
Section node2 = findSectionNode(document, 2); | ||
assertThat(node2.getSectnum("*")).isEqualTo("**"); | ||
} | ||
|
||
private Document loadDocument(String source, boolean sectionNumbers) { | ||
Attributes attributes = Attributes.builder().sectionNumbers(sectionNumbers).build(); | ||
Options options = Options.builder().attributes(attributes).build(); | ||
Document document = asciidoctor.load(source, options); | ||
return document; | ||
} | ||
|
||
private Section findSectionNode(Document document, int level) { | ||
return (Section) document.findBy(Collections.singletonMap("context", ":section")) | ||
.stream() | ||
.filter(n -> n.getLevel() == level) | ||
.findFirst() | ||
.get(); | ||
} | ||
|
||
static String sectionsSample() { | ||
return "= Document Title\n\n" + | ||
"== Section A\n\n" + | ||
"Section A paragraph.\n\n" + | ||
"=== Section A Subsection\n\n" + | ||
"Section A 'subsection' paragraph.\n\n"; | ||
} | ||
} |