Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Apr 10, 2023
1 parent 8cec203 commit 78e0a63
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class RubyGemsPreloader {
private static final Map<String, String> optionToRequiredGem = Map.of(
Options.ERUBY, "require 'erubis'",
Options.TEMPLATE_DIRS, "require 'tilt'",
Attributes.SOURCE_HIGHLIGHTER, "require 'coderay'",
Attributes.DATA_URI, "require 'base64'",
Attributes.CACHE_URI, "require 'open-uri/cached'",
Attributes.DATA_URI, "require 'base64'",
Attributes.SOURCE_HIGHLIGHTER, "require 'coderay'",
EPUB3, "require 'asciidoctor-epub3'",
PDF, "require 'asciidoctor-pdf'",
REVEALJS, "require 'asciidoctor-revealjs'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.runner.RunWith;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
Expand All @@ -32,7 +32,7 @@ public void currentLineNumberShouldBeReturned() {
javaExtensionRegistry.preprocessor(NumberLinesPreprocessor.class);

File inputFile = classpath.getResource("rendersample.asciidoc");
asciidoctor.convertFile(inputFile, new HashMap<String, Object>());
asciidoctor.convertFile(inputFile, Map.of());

File outpuFile = new File(inputFile.getParent(), "rendersample.asciidoc");
assertThat(outpuFile.exists(), is(true));
Expand All @@ -48,10 +48,10 @@ public void hasMoreLinesShouldBeReturned() {

asciidoctor.convertFile(
classpath.getResource("rendersample.asciidoc"),
new HashMap<String, Object>());
Map.of());

File inputFile = classpath.getResource("rendersample.asciidoc");
asciidoctor.convertFile(inputFile, new HashMap<String, Object>());
asciidoctor.convertFile(inputFile, Map.of());

File outpuFile = new File(inputFile.getParent(), "rendersample.asciidoc");
assertThat(outpuFile.exists(), is(true));
Expand All @@ -67,10 +67,10 @@ public void isNextLineEmptyShouldBeReturned() {

asciidoctor.convertFile(
classpath.getResource("rendersample.asciidoc"),
new HashMap<String, Object>());
Map.of());

File inputFile = classpath.getResource("rendersample.asciidoc");
asciidoctor.convertFile(inputFile, new HashMap<String, Object>());
asciidoctor.convertFile(inputFile, Map.of());

File outpuFile = new File(inputFile.getParent(), "rendersample.asciidoc");
assertThat(outpuFile.exists(), is(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.asciidoctor.SafeMode;
import org.asciidoctor.arquillian.api.Unshared;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.ImageReference;
Expand All @@ -11,8 +12,10 @@
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
Expand All @@ -28,6 +31,9 @@ public class WhenReadingImagesFromCatalogAsset {
@ArquillianResource(Unshared.class)
private Asciidoctor asciidoctor;

@ArquillianResource
private TemporaryFolder testFolder;

static final TestImageReference[] BLOCK_IMAGES = new TestImageReference[]{
new TestImageReference("images/block-image.jpg")
};
Expand All @@ -37,7 +43,6 @@ public class WhenReadingImagesFromCatalogAsset {
new TestImageReference("images/inline-image.png")
};


@Test
public void shouldReturnEmptyWhenThereAreNoImages() {
final Options options = Options.builder()
Expand All @@ -56,7 +61,7 @@ public void shouldReturnNullImagesDirWhenNotSet() {
final Options options = Options.builder()
.catalogAssets(true)
.build();
final String content = getAsciiDodWithImagesDocument();
final String content = getAsciiDocWithImagesContent();

Document document = asciidoctor.load(content, options);
List<ImageReference> images = document.getCatalog().getImages();
Expand All @@ -74,7 +79,7 @@ public void shouldReturnImagesDirWhenSet() {
.imagesDir("some-path")
.build())
.build();
final String content = getAsciiDodWithImagesDocument();
final String content = getAsciiDocWithImagesContent();

Document document = asciidoctor.load(content, options);
List<ImageReference> images = document.getCatalog().getImages();
Expand All @@ -89,7 +94,7 @@ public void shouldNotCatalogInlineImagesWhenNotConverting() {
final Options options = Options.builder()
.catalogAssets(true)
.build();
final String content = getAsciiDodWithImagesDocument();
final String content = getAsciiDocWithImagesContent();

Document document = asciidoctor.load(content, options);

Expand All @@ -100,11 +105,11 @@ public void shouldNotCatalogInlineImagesWhenNotConverting() {
}

@Test
public void shouldCatalogInlineImagesWhenProcessingContent() {
public void shouldCatalogInlineImagesWhenProcessingContentAfterLoad() {
final Options options = Options.builder()
.catalogAssets(true)
.build();
final String content = getAsciiDodWithImagesDocument();
final String content = getAsciiDocWithImagesContent();

Document document = asciidoctor.load(content, options);
document.getContent();
Expand All @@ -116,11 +121,11 @@ public void shouldCatalogInlineImagesWhenProcessingContent() {
}

@Test
public void shouldNotCatalogInlineImagesWhenCatalogAssetsIsFalse() {
public void shouldNotCatalogImagesWhenCatalogAssetsIsFalse() {
final Options options = Options.builder()
.catalogAssets(false)
.build();
final String content = getAsciiDodWithImagesDocument();
final String content = getAsciiDocWithImagesContent();

Document document = asciidoctor.load(content, options);
document.getContent();
Expand All @@ -129,11 +134,55 @@ public void shouldNotCatalogInlineImagesWhenCatalogAssetsIsFalse() {
assertThat(images).isEmpty();
}

private String getAsciiDodWithImagesDocument() {
@Test
public void shouldCatalogAllImagesWhenUsingConvertFile() throws IOException {
final Options options = Options.builder()
.catalogAssets(true)
.safe(SafeMode.UNSAFE)
.toFile(testFolder.newFile())
.build();
final File file = getAsciiDocWithImagesFile();

var document = asciidoctor.convertFile(file, options, Document.class);

assertThat(document)
.isInstanceOf(Document.class);

List<ImageReference> images = document.getCatalog().getImages();
assertThat(images)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrder(ALL_IMAGES);
}

@Test
public void shouldCatalogAllImagesWhenUsingConvert() throws IOException {
final Options options = Options.builder()
.catalogAssets(true)
.safe(SafeMode.UNSAFE)
.toFile(testFolder.newFile())
.build();
final String content = getAsciiDocWithImagesContent();

var document = asciidoctor.convert(content, options, Document.class);

assertThat(document)
.isInstanceOf(Document.class);

List<ImageReference> images = document.getCatalog().getImages();
assertThat(images)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrder(ALL_IMAGES);
}

private String getAsciiDocWithImagesContent() {
try {
return Files.readString(classpath.getResource("sample-with-images.adoc").toPath());
return Files.readString(getAsciiDocWithImagesFile().toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private File getAsciiDocWithImagesFile() {
return classpath.getResource("sample-with-images.adoc");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Options;
import org.asciidoctor.arquillian.api.Unshared;
import org.asciidoctor.ast.*;
import org.asciidoctor.ast.ContentNode;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.ImageReference;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.converter.StringConverter;
import org.asciidoctor.jruby.ast.impl.TestImageReference;
import org.asciidoctor.util.ClasspathResources;
Expand Down Expand Up @@ -31,6 +34,10 @@ public class WhenReadingImagesFromCatalogAssetFromConverter {

static final String CONVERTER_BACKEND = "custom-backend";

static final TestImageReference[] BLOCK_IMAGES = new TestImageReference[]{
new TestImageReference("images/block-image.jpg")
};

static final TestImageReference[] ALL_IMAGES = new TestImageReference[]{
new TestImageReference("images/block-image.jpg"),
new TestImageReference("images/inline-image.png")
Expand All @@ -45,8 +52,8 @@ public void beforeEach() {
javaConverterRegistry.converters().clear();
javaConverterRegistry.register(TestConverter.class, CONVERTER_BACKEND);

imagesBeforeConvert = List.of();
imagesAfterConvert = List.of();
imagesBeforeConvert = null;
imagesAfterConvert = null;
}

@Test
Expand All @@ -60,7 +67,7 @@ public void shouldReturnEmptyWhenThereAreNoImages() {
}

@Test
public void shouldReturnEmptyWhenThereUsingLoad() {
public void shouldReturnEmptyWhenUsingLoad() {
final String content = getAsciiDodWithImagesDocument();

load(content);
Expand All @@ -73,27 +80,33 @@ public void shouldReturnEmptyWhenThereUsingLoad() {
public void shouldReturnAllImages() {
final String content = getAsciiDodWithImagesDocument();

var options = Options.builder().backend(CONVERTER_BACKEND).build();
Document load = asciidoctor.load(content, options);
String content1 = (String) load.getContent();
convert(content);

Catalog catalog = load.getCatalog();
assertThat(imagesBeforeConvert).isEmpty();
assertThat(imagesBeforeConvert)
.usingRecursiveFieldByFieldElementComparator()
.containsExactly(BLOCK_IMAGES);
assertThat(imagesAfterConvert)
.usingRecursiveFieldByFieldElementComparator()
.containsExactly(ALL_IMAGES);
}

private String convert(String document) {
var options = Options.builder().backend(CONVERTER_BACKEND).build();
var options = optionsWithConverter();
return asciidoctor.convert(document, options);
}

private void load(String document) {
var options = Options.builder().backend(CONVERTER_BACKEND).build();
var options = optionsWithConverter();
asciidoctor.load(document, options);
}

private static Options optionsWithConverter() {
return Options.builder()
.catalogAssets(true)
.backend(CONVERTER_BACKEND)
.build();
}

private String getAsciiDodWithImagesDocument() {
try {
return Files.readString(classpath.getResource("sample-with-images.adoc").toPath());
Expand All @@ -120,6 +133,7 @@ public String convert(ContentNode node, String transform, Map<Object, Object> op
if (node instanceof Document) {
var doc = (Document) node;
imagesBeforeConvert = doc.getCatalog().getImages();
// force content to process inline images
content = (String) doc.getContent();
imagesAfterConvert = doc.getCatalog().getImages();
} else if (node instanceof StructuralNode) {
Expand Down

0 comments on commit 78e0a63

Please sign in to comment.