Skip to content

Commit 69482cf

Browse files
committed
Add unit tests for print_record() Fix function. (#238)
1 parent 6f22490 commit 69482cf

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import org.mockito.Mock;
2626
import org.mockito.junit.jupiter.MockitoExtension;
2727

28+
import java.io.IOException;
2829
import java.util.Arrays;
30+
import java.util.function.Consumer;
31+
import java.util.function.Supplier;
2932

3033
/**
3134
* Tests Metafix record level methods. Following the cheat sheet
@@ -1854,6 +1857,102 @@ public void pasteWithLiteralStrings() {
18541857
});
18551858
}
18561859

1860+
private void shouldPrintRecord(final String before, final String args, final String after, final Consumer<Supplier<StreamReceiver>> consumer, final String expected) {
1861+
MetafixTestHelpers.assertStdout(expected, () ->
1862+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1863+
before,
1864+
"print_record(" + args + ")",
1865+
after
1866+
),
1867+
i -> {
1868+
i.startRecord("rec1");
1869+
i.literal("a", "eeny");
1870+
i.literal("a", "meeny");
1871+
i.startEntity("c");
1872+
i.literal("d", "moe");
1873+
i.endEntity();
1874+
i.endRecord();
1875+
}, o -> {
1876+
o.get().startRecord("rec1");
1877+
o.get().literal("a", "eeny");
1878+
o.get().literal("a", "meeny");
1879+
o.get().startEntity("c");
1880+
o.get().literal("d", "moe");
1881+
o.get().endEntity();
1882+
1883+
if (consumer != null) {
1884+
consumer.accept(o);
1885+
}
1886+
1887+
o.get().endRecord();
1888+
}
1889+
)
1890+
);
1891+
}
1892+
1893+
@Test
1894+
public void shouldPrintRecord() {
1895+
shouldPrintRecord("", "", "", null,
1896+
"{\"a\":[\"eeny\",\"meeny\"],\"c\":{\"d\":\"moe\"}}\n");
1897+
}
1898+
1899+
@Test
1900+
public void shouldPrintRecordWithPrefix() {
1901+
shouldPrintRecord("", "'<%d:%s>'", "", null,
1902+
"<1:rec1>{\"a\":[\"eeny\",\"meeny\"],\"c\":{\"d\":\"moe\"}}\n");
1903+
}
1904+
1905+
@Test
1906+
public void shouldPrintRecordWithPrefixAndIdField() {
1907+
shouldPrintRecord("", "'<%d:%s>', id: 'c.d'", "", null,
1908+
"<1:moe>{\"a\":[\"eeny\",\"meeny\"],\"c\":{\"d\":\"moe\"}}\n");
1909+
}
1910+
1911+
@Test
1912+
public void shouldPrintRecordWithHeader() {
1913+
shouldPrintRecord("", "header: '<%d:%s>'", "", null,
1914+
"<%d:%s>{\"a\":[\"eeny\",\"meeny\"],\"c\":{\"d\":\"moe\"}}\n");
1915+
}
1916+
1917+
@Test
1918+
public void shouldPrintRecordWithFooter() {
1919+
shouldPrintRecord("", "footer: '<%d:%s>'", "", null,
1920+
"{\"a\":[\"eeny\",\"meeny\"],\"c\":{\"d\":\"moe\"}}<%d:%s>");
1921+
}
1922+
1923+
@Test
1924+
public void shouldPrintRecordWithPrettyPrinting() {
1925+
shouldPrintRecord("", "pretty: 'true'", "", null,
1926+
"{\n" +
1927+
" \"a\" : [ \"eeny\", \"meeny\" ],\n" +
1928+
" \"c\" : {\n" +
1929+
" \"d\" : \"moe\"\n" +
1930+
" }\n" +
1931+
"}\n"
1932+
);
1933+
}
1934+
1935+
@Test
1936+
public void shouldPrintRecordAfterTransformation() {
1937+
shouldPrintRecord("add_field('x', '23')", "", "",
1938+
o -> o.get().literal("x", "23"),
1939+
"{\"a\":[\"eeny\",\"meeny\"],\"c\":{\"d\":\"moe\"},\"x\":\"23\"}\n");
1940+
}
1941+
1942+
@Test
1943+
public void shouldPrintRecordBeforeTransformation() {
1944+
shouldPrintRecord("", "", "add_field('x', '23')",
1945+
o -> o.get().literal("x", "23"),
1946+
"{\"a\":[\"eeny\",\"meeny\"],\"c\":{\"d\":\"moe\"}}\n");
1947+
}
1948+
1949+
@Test
1950+
public void shouldPrintRecordToFile() throws IOException {
1951+
MetafixTestHelpers.assertTempFile(
1952+
"{\"a\":[\"eeny\",\"meeny\"],\"c\":{\"d\":\"moe\"}}\n",
1953+
p -> shouldPrintRecord("", "destination: '" + p + "'", "", null, ""));
1954+
}
1955+
18571956
@Test
18581957
public void hashFromArray() {
18591958
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(

metafix/src/test/java/org/metafacture/metafix/MetafixTestHelpers.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
import org.mockito.Mockito;
2525
import org.mockito.exceptions.base.MockitoAssertionError;
2626

27+
import java.io.ByteArrayOutputStream;
28+
import java.io.File;
2729
import java.io.FileNotFoundException;
30+
import java.io.IOException;
31+
import java.io.PrintStream;
32+
import java.nio.file.Files;
2833
import java.util.ArrayList;
2934
import java.util.List;
3035
import java.util.Map;
@@ -76,6 +81,29 @@ private static void assertThrows(final Class<? extends Throwable> exceptionClass
7681
Assertions.assertEquals(expectedMessage, operator.apply(actualException.getMessage()));
7782
}
7883

84+
public static void assertStdout(final String expected, final Runnable runnable) {
85+
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
86+
final PrintStream originalStdout = System.out;
87+
88+
try (PrintStream printStream = new PrintStream(outputStream)) {
89+
System.setOut(printStream);
90+
runnable.run();
91+
}
92+
finally {
93+
System.setOut(originalStdout);
94+
}
95+
96+
Assertions.assertEquals(expected, outputStream.toString());
97+
}
98+
99+
public static void assertTempFile(final String expected, final Consumer<String> consumer) throws IOException {
100+
final File tempFile = File.createTempFile("metafixTestHelpers", "");
101+
tempFile.deleteOnExit();
102+
103+
consumer.accept(tempFile.getPath());
104+
Assertions.assertEquals(expected, new String(Files.readAllBytes(tempFile.toPath())));
105+
}
106+
79107
public static void assertFix(final StreamReceiver receiver, final List<String> fixDef, final Consumer<Metafix> in,
80108
final Consumer<Supplier<StreamReceiver>> out) {
81109
assertFix(receiver, fixDef, in, (s, f) -> out.accept(s), Metafix.NO_VARS);

0 commit comments

Comments
 (0)