From a3ff15484b1879f0abf54759dc1bb38ead7eb0ff Mon Sep 17 00:00:00 2001 From: dotasek Date: Fri, 14 Jan 2022 16:49:44 -0500 Subject: [PATCH] Tests for help display --- .../fhir/validation/cli/utils/Display.java | 16 +++--- .../validation/cli/utils/DisplayTests.java | 54 +++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/utils/DisplayTests.java diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Display.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Display.java index 4ce9ec86f..8e94d4d61 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Display.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/Display.java @@ -37,15 +37,19 @@ public class Display { final static String CURLY_START = "\\{\\{"; final static String CURLY_END = "\\}\\}"; + final static String getMoustacheString(final String string) { + return CURLY_START + string + CURLY_END; + } + final static String[][] PLACEHOLDERS = { - { CURLY_START + "XML_AND_JSON_FHIR_VERSIONS" + CURLY_END, "1.0, 1.4, 3.0, 4.0," + Constants.VERSION_MM }, - { CURLY_START + "TURTLE_FHIR_VERSIONS" + CURLY_END, "3.0, 4.0, " + Constants.VERSION_MM }, - { CURLY_START + "FHIR_MAJOR_VERSIONS" + CURLY_END, "1.0|1.4|3.0|" + VersionUtilities.CURRENT_VERSION}, - { CURLY_START + "FHIR_MINOR_VERSIONS" + CURLY_END, "1.0.2|1.4.0|3.0.2|4.0.1|" + VersionUtilities.CURRENT_FULL_VERSION }, - { CURLY_START + "FHIR_CURRENT_VERSION" + CURLY_END, VersionUtilities.CURRENT_VERSION}, + { getMoustacheString("XML_AND_JSON_FHIR_VERSIONS"), "1.0, 1.4, 3.0, 4.0," + Constants.VERSION_MM }, + { getMoustacheString("TURTLE_FHIR_VERSIONS"), "3.0, 4.0, " + Constants.VERSION_MM }, + { getMoustacheString("FHIR_MAJOR_VERSIONS"), "1.0|1.4|3.0|" + VersionUtilities.CURRENT_VERSION}, + { getMoustacheString("FHIR_MINOR_VERSIONS"), "1.0.2|1.4.0|3.0.2|4.0.1|" + VersionUtilities.CURRENT_FULL_VERSION }, + { getMoustacheString("FHIR_CURRENT_VERSION"), VersionUtilities.CURRENT_VERSION}, }; - final static String replacePlaceholders(String input, String[][] placeholders) { + final static String replacePlaceholders(final String input, final String[][] placeholders) { String output = input; for (String[] placeholder : placeholders) { output = output.replaceAll(placeholder[0], placeholder[1]); diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/utils/DisplayTests.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/utils/DisplayTests.java new file mode 100644 index 000000000..d70b7aae0 --- /dev/null +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/utils/DisplayTests.java @@ -0,0 +1,54 @@ +package org.hl7.fhir.validation.cli.utils; + +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.jupiter.api.Assertions.*; + +public class DisplayTests { + + @Test + public void displayHelpDetails() { + final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + + final PrintStream originalOut = System.out; + final PrintStream originalErr = System.err; + + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + + try { + Display.displayHelpDetails(); + + String output = outContent.toString(); + + for (String[] placeHolder: Display.PLACEHOLDERS) { + assertTrue(output.contains(placeHolder[1]), placeHolder[1] + " is not contained in output:\n" + output); + assertFalse(output.contains(placeHolder[0]), placeHolder[0] + " found in output:\n" + output); + } + } + finally { + System.setOut(originalOut); + System.setErr(originalErr); + } + } + + @Test + public void testReplacePlaceholdersBaseCase() { + + final String myTestString = "The {{DUMMY_A}} jumps over the {{DUMMY_B}}."; + final String[][] placeHolders = { + { "\\{\\{DUMMY_A\\}\\}", "quick brown fox"}, + { "\\{\\{DUMMY_B\\}\\}", "lazy dog"}, + }; + + final String expectedOutput = "The quick brown fox jumps over the lazy dog."; + final String output = Display.replacePlaceholders(myTestString, placeHolders); + + assertEquals(expectedOutput, output); + } + +}