|
25 | 25 | import org.mockito.Mock;
|
26 | 26 | import org.mockito.junit.jupiter.MockitoExtension;
|
27 | 27 |
|
| 28 | +import java.io.IOException; |
28 | 29 | import java.util.Arrays;
|
| 30 | +import java.util.function.Consumer; |
| 31 | +import java.util.function.Supplier; |
29 | 32 |
|
30 | 33 | /**
|
31 | 34 | * Tests Metafix record level methods. Following the cheat sheet
|
@@ -1854,6 +1857,102 @@ public void pasteWithLiteralStrings() {
|
1854 | 1857 | });
|
1855 | 1858 | }
|
1856 | 1859 |
|
| 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 | + |
1857 | 1956 | @Test
|
1858 | 1957 | public void hashFromArray() {
|
1859 | 1958 | MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
|
|
0 commit comments