diff --git a/src/main/java/net/revelc/code/formatter/LineEnding.java b/src/main/java/net/revelc/code/formatter/LineEnding.java index f16ebf863..20eb6c93c 100755 --- a/src/main/java/net/revelc/code/formatter/LineEnding.java +++ b/src/main/java/net/revelc/code/formatter/LineEnding.java @@ -30,6 +30,10 @@ public String getChars() { return this.chars; } + public boolean isSystem() { + return System.lineSeparator().equals(getChars()); + } + /** * Returns the most occurring line-ending characters in the file text or null if no line-ending occurs the most. */ diff --git a/src/test/java/net/revelc/code/formatter/AbstractFormatterTest.java b/src/test/java/net/revelc/code/formatter/AbstractFormatterTest.java index 8fe099525..3b1f651e4 100755 --- a/src/test/java/net/revelc/code/formatter/AbstractFormatterTest.java +++ b/src/test/java/net/revelc/code/formatter/AbstractFormatterTest.java @@ -13,6 +13,7 @@ */ package net.revelc.code.formatter; +import static java.util.Collections.emptyMap; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -20,10 +21,11 @@ import java.io.File; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.Collections; import java.util.Map; +import java.util.stream.IntStream; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.SystemStreamLog; @@ -86,33 +88,45 @@ public String getCompilerCodegenTargetPlatform() { } } - protected void doTestFormat(Formatter formatter, String fileUnderTest, String expectedSha512, - FormatCycle formatCycle) throws IOException { - if (System.lineSeparator().equals("\n")) { - doTestFormat(Collections.emptyMap(), formatter, fileUnderTest, expectedSha512, LineEnding.LF, formatCycle); - } else { - doTestFormat(Collections.emptyMap(), formatter, fileUnderTest, expectedSha512, LineEnding.CRLF, - formatCycle); - } + protected void singlePassTest(Formatter formatter, String fileUnderTest, String expectedSha512, + LineEnding lineEnding) { + multiPassTest(1, emptyMap(), formatter, fileUnderTest, expectedSha512, lineEnding); + } + + protected void twoPassTest(Map options, Formatter formatter, String fileUnderTest, + String expectedSha512, LineEnding lineEnding) { + multiPassTest(2, options, formatter, fileUnderTest, expectedSha512, lineEnding); + } + + private void multiPassTest(int numPasses, Map options, Formatter formatter, String fileUnderTest, + String expectedSha512, LineEnding lineEnding) { + IntStream.rangeClosed(1, numPasses).forEachOrdered(passNumber -> { + try { + doTestFormat(options, formatter, fileUnderTest, expectedSha512, lineEnding, passNumber); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); } - protected void doTestFormat(Map options, Formatter formatter, String fileUnderTest, - String expectedSha512, LineEnding lineEnding, FormatCycle formatCycle) throws IOException { + private void doTestFormat(Map options, Formatter formatter, String fileUnderTest, + String expectedSha512, LineEnding lineEnding, int formatCycle) throws IOException { // Set the used resource location for test (either first pass or second pass) String resourceLocation; - if (FormatCycle.SECOND == formatCycle) { - resourceLocation = RESOURCE_LOCATION_SECONDARY; - } else { - resourceLocation = RESOURCE_LOCATION_PRIMARY; - } - // Set the used output directory for test (either first pass or second pass) File testOutputDir; - if (FormatCycle.SECOND == formatCycle) { - testOutputDir = TEST_OUTPUT_SECONDARY_DIR; - } else { + switch (formatCycle) { + case 1: + resourceLocation = RESOURCE_LOCATION_PRIMARY; testOutputDir = TEST_OUTPUT_PRIMARY_DIR; + break; + case 2: + resourceLocation = RESOURCE_LOCATION_SECONDARY; + testOutputDir = TEST_OUTPUT_SECONDARY_DIR; + break; + default: + throw new IllegalStateException("Unrecognized format cycle: " + formatCycle); } // Set original file and file to use for test @@ -133,11 +147,17 @@ protected void doTestFormat(Map options, Formatter formatter, St // Write the file we formatte4d FileUtils.fileWrite(sourceFile, StandardCharsets.UTF_8.name(), formattedCode); - // Run assertions on formatted file, if not valid, reject and tester can look at resulting files to debug issue. - if (FormatCycle.SECOND == formatCycle) { - assertEquals(originalCode, formattedCode); - } else { + // Run assertions on formatted file, if not valid, reject and tester can look at resulting files + // to debug issue. + switch (formatCycle) { + case 1: assertNotEquals(originalCode, formattedCode); + break; + case 2: + assertEquals(originalCode, formattedCode); + break; + default: + throw new IllegalStateException("Unrecognized format cycle: " + formatCycle); } // We are hashing this as set in stone in case for some reason our source file changes unexpectedly. diff --git a/src/test/java/net/revelc/code/formatter/FormatCycle.java b/src/test/java/net/revelc/code/formatter/FormatCycle.java deleted file mode 100644 index 3b37d4d4b..000000000 --- a/src/test/java/net/revelc/code/formatter/FormatCycle.java +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package net.revelc.code.formatter; - -public enum FormatCycle { - FIRST, SECOND -} diff --git a/src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java b/src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java index a6d4a9bbb..58b834f43 100644 --- a/src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java +++ b/src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java @@ -13,15 +13,14 @@ */ package net.revelc.code.formatter.css; +import static java.util.Collections.emptyMap; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Collections; - import org.junit.jupiter.api.Test; import net.revelc.code.formatter.AbstractFormatterTest; -import net.revelc.code.formatter.FormatCycle; +import net.revelc.code.formatter.LineEnding; /** * @author yoshiman @@ -31,30 +30,18 @@ class CssFormatterTest extends AbstractFormatterTest { @Test void testDoFormatFile() throws Exception { // FIXME Handle linux vs windows since this formatter does not accept line endings - if (System.lineSeparator().equals("\n")) { - doTestFormat(new CssFormatter(), "someFile.css", - "72b2c33020774b407c2e49a849e47990941d3c80d982b1a4ef2e0ffed605b85e2680fca57cfdbc9d6cd2fc6fc0236dbeb915fd75f530689c7e90a3745316b6a3", - FormatCycle.FIRST); - doTestFormat(new CssFormatter(), "someFile.css", - "72b2c33020774b407c2e49a849e47990941d3c80d982b1a4ef2e0ffed605b85e2680fca57cfdbc9d6cd2fc6fc0236dbeb915fd75f530689c7e90a3745316b6a3", - FormatCycle.SECOND); - } else { - doTestFormat(new CssFormatter(), "someFile.css", - "684255d79eb28c6f4cfa340b6930fe1cfd9de16a1c6abf5f54e8f6837694b599101ef247ed00b8aea5460aa64cda60b418cebefd8ea28d5e747ed9cf4c3a9274", - FormatCycle.FIRST); - doTestFormat(new CssFormatter(), "someFile.css", - "684255d79eb28c6f4cfa340b6930fe1cfd9de16a1c6abf5f54e8f6837694b599101ef247ed00b8aea5460aa64cda60b418cebefd8ea28d5e747ed9cf4c3a9274", - FormatCycle.SECOND); - } + String expectedHash = LineEnding.LF.isSystem() + ? "72b2c33020774b407c2e49a849e47990941d3c80d982b1a4ef2e0ffed605b85e2680fca57cfdbc9d6cd2fc6fc0236dbeb915fd75f530689c7e90a3745316b6a3" + : "684255d79eb28c6f4cfa340b6930fe1cfd9de16a1c6abf5f54e8f6837694b599101ef247ed00b8aea5460aa64cda60b418cebefd8ea28d5e747ed9cf4c3a9274"; + LineEnding lineEnding = LineEnding.LF.isSystem() ? LineEnding.LF : LineEnding.CRLF; + twoPassTest(emptyMap(), new CssFormatter(), "someFile.css", expectedHash, lineEnding); } @Test void testIsIntialized() throws Exception { CssFormatter cssFormatter = new CssFormatter(); assertFalse(cssFormatter.isInitialized()); - cssFormatter.init(Collections.emptyMap(), - new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); - + cssFormatter.init(emptyMap(), new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); assertTrue(cssFormatter.isInitialized()); } diff --git a/src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java b/src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java index 5a086fa19..9d0e4f6b3 100644 --- a/src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java +++ b/src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java @@ -13,15 +13,14 @@ */ package net.revelc.code.formatter.html; +import static java.util.Collections.emptyMap; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Collections; - import org.junit.jupiter.api.Test; import net.revelc.code.formatter.AbstractFormatterTest; -import net.revelc.code.formatter.FormatCycle; +import net.revelc.code.formatter.LineEnding; /** * @author yoshiman @@ -31,31 +30,20 @@ class HTMLFormatterTest extends AbstractFormatterTest { @Test void testDoFormatFile() throws Exception { // FIXME Handle linux vs windows since this formatter does not accept line endings - if (System.lineSeparator().equals("\n")) { - doTestFormat(new HTMLFormatter(), "someFile.html", - "1cfe5e48635d8618be4d490a5e7f690ef8e1dfc7e24303457030e281068bbebac44b552ae52ac88f03bf10e72ed0582904d665afc54bade395fd3d183abe0cba", - FormatCycle.FIRST); - // TODO: jsoup has further bugs to fix so this always fails currently - // doTestFormat(new HTMLFormatter(), "someFile.html", - // "1cfe5e48635d8618be4d490a5e7f690ef8e1dfc7e24303457030e281068bbebac44b552ae52ac88f03bf10e72ed0582904d665afc54bade395fd3d183abe0cba", - // FormatCycle.SECOND); - } else { - doTestFormat(new HTMLFormatter(), "someFile.html", - "57b5eae0562d6abc4d4e874b675c8351282b0c4797a19891c82bb5e1c50c5ede9bda6d1d9490a775e0d5f56f0521854d321de78782760d5fb8567680a25c307c", - FormatCycle.FIRST); - // TODO: jsoup has further bugs to fix so this always fails currently - // doTestFormat(new HTMLFormatter(), "someFile.html", - // "57b5eae0562d6abc4d4e874b675c8351282b0c4797a19891c82bb5e1c50c5ede9bda6d1d9490a775e0d5f56f0521854d321de78782760d5fb8567680a25c307c", - // FormatCycle.SECOND); - } + String expectedHash = LineEnding.LF.isSystem() + ? "1cfe5e48635d8618be4d490a5e7f690ef8e1dfc7e24303457030e281068bbebac44b552ae52ac88f03bf10e72ed0582904d665afc54bade395fd3d183abe0cba" + : "57b5eae0562d6abc4d4e874b675c8351282b0c4797a19891c82bb5e1c50c5ede9bda6d1d9490a775e0d5f56f0521854d321de78782760d5fb8567680a25c307c"; + LineEnding lineEnding = LineEnding.LF.isSystem() ? LineEnding.LF : LineEnding.CRLF; + singlePassTest(new HTMLFormatter(), "someFile.html", expectedHash, lineEnding); + // TODO: jsoup has further bugs to fix so this always fails currently + // twoPassTest(emptyMap(), new HTMLFormatter(), "someFile.html", expectedHash, lineEnding); } @Test void testIsIntialized() throws Exception { HTMLFormatter htmlFormatter = new HTMLFormatter(); assertFalse(htmlFormatter.isInitialized()); - htmlFormatter.init(Collections.emptyMap(), - new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); + htmlFormatter.init(emptyMap(), new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); assertTrue(htmlFormatter.isInitialized()); } diff --git a/src/test/java/net/revelc/code/formatter/java/JavaFormatterTest.java b/src/test/java/net/revelc/code/formatter/java/JavaFormatterTest.java index ad2c1246c..9a739d106 100755 --- a/src/test/java/net/revelc/code/formatter/java/JavaFormatterTest.java +++ b/src/test/java/net/revelc/code/formatter/java/JavaFormatterTest.java @@ -13,15 +13,13 @@ */ package net.revelc.code.formatter.java; +import static java.util.Collections.emptyMap; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Collections; - import org.junit.jupiter.api.Test; import net.revelc.code.formatter.AbstractFormatterTest; -import net.revelc.code.formatter.FormatCycle; import net.revelc.code.formatter.LineEnding; /** @@ -31,59 +29,36 @@ class JavaFormatterTest extends AbstractFormatterTest { @Test void testDoFormatFile() throws Exception { - if (System.lineSeparator().equals("\n")) { - doTestFormat(new JavaFormatter(), "AnyClass.java", - "5d41510e74b87c6b38c8e4692d53aa9de3d7a85d08c72697b77c48541534147a028e799289d49c05cc9a3cc601e64c86bb954bb62b03b7277616b71ecc5bd716", - FormatCycle.FIRST); - doTestFormat(new JavaFormatter(), "AnyClass.java", - "5d41510e74b87c6b38c8e4692d53aa9de3d7a85d08c72697b77c48541534147a028e799289d49c05cc9a3cc601e64c86bb954bb62b03b7277616b71ecc5bd716", - FormatCycle.SECOND); - } else { - doTestFormat(new JavaFormatter(), "AnyClass.java", - "fe7bdeec160a33a744209602d1ae99f94bd8ff433dd3ab856bcf6857588170d5c69b027f15c72bd7a6c0ae6e3659a9ab62196fa198366ec0c0722286257cbdca", - FormatCycle.FIRST); - doTestFormat(new JavaFormatter(), "AnyClass.java", - "fe7bdeec160a33a744209602d1ae99f94bd8ff433dd3ab856bcf6857588170d5c69b027f15c72bd7a6c0ae6e3659a9ab62196fa198366ec0c0722286257cbdca", - FormatCycle.SECOND); - } + String expectedHash = LineEnding.LF.isSystem() + ? "5d41510e74b87c6b38c8e4692d53aa9de3d7a85d08c72697b77c48541534147a028e799289d49c05cc9a3cc601e64c86bb954bb62b03b7277616b71ecc5bd716" + : "fe7bdeec160a33a744209602d1ae99f94bd8ff433dd3ab856bcf6857588170d5c69b027f15c72bd7a6c0ae6e3659a9ab62196fa198366ec0c0722286257cbdca"; + LineEnding lineEnding = LineEnding.LF.isSystem() ? LineEnding.LF : LineEnding.CRLF; + twoPassTest(emptyMap(), new JavaFormatter(), "AnyClass.java", expectedHash, lineEnding); } @Test void testDoFormatFileKeepLineFeedLF() throws Exception { - doTestFormat(Collections.emptyMap(), new JavaFormatter(), "AnyClassLF.java", - "5d41510e74b87c6b38c8e4692d53aa9de3d7a85d08c72697b77c48541534147a028e799289d49c05cc9a3cc601e64c86bb954bb62b03b7277616b71ecc5bd716", - LineEnding.KEEP, FormatCycle.FIRST); - doTestFormat(Collections.emptyMap(), new JavaFormatter(), "AnyClassLF.java", - "5d41510e74b87c6b38c8e4692d53aa9de3d7a85d08c72697b77c48541534147a028e799289d49c05cc9a3cc601e64c86bb954bb62b03b7277616b71ecc5bd716", - LineEnding.KEEP, FormatCycle.SECOND); + String expectedHash = "5d41510e74b87c6b38c8e4692d53aa9de3d7a85d08c72697b77c48541534147a028e799289d49c05cc9a3cc601e64c86bb954bb62b03b7277616b71ecc5bd716"; + twoPassTest(emptyMap(), new JavaFormatter(), "AnyClassLF.java", expectedHash, LineEnding.KEEP); } @Test void testDoFormatFileKeepLineFeedCR() throws Exception { - doTestFormat(Collections.emptyMap(), new JavaFormatter(), "AnyClassCR.java", - "cf44c525667d8c49c80d390215e4d1995c10e8966583da0920e3917837188e5b95159f9dc7b4ae2559fbfa4550cbbaca166edc8991907d5fd4bbc74a1402e97e", - LineEnding.KEEP, FormatCycle.FIRST); - doTestFormat(Collections.emptyMap(), new JavaFormatter(), "AnyClassCR.java", - "cf44c525667d8c49c80d390215e4d1995c10e8966583da0920e3917837188e5b95159f9dc7b4ae2559fbfa4550cbbaca166edc8991907d5fd4bbc74a1402e97e", - LineEnding.KEEP, FormatCycle.SECOND); + String expectedHash = "cf44c525667d8c49c80d390215e4d1995c10e8966583da0920e3917837188e5b95159f9dc7b4ae2559fbfa4550cbbaca166edc8991907d5fd4bbc74a1402e97e"; + twoPassTest(emptyMap(), new JavaFormatter(), "AnyClassCR.java", expectedHash, LineEnding.KEEP); } @Test void testDoFormatFileKeepLineFeedCRLF() throws Exception { - doTestFormat(Collections.emptyMap(), new JavaFormatter(), "AnyClassCRLF.java", - "fe7bdeec160a33a744209602d1ae99f94bd8ff433dd3ab856bcf6857588170d5c69b027f15c72bd7a6c0ae6e3659a9ab62196fa198366ec0c0722286257cbdca", - LineEnding.KEEP, FormatCycle.FIRST); - doTestFormat(Collections.emptyMap(), new JavaFormatter(), "AnyClassCRLF.java", - "fe7bdeec160a33a744209602d1ae99f94bd8ff433dd3ab856bcf6857588170d5c69b027f15c72bd7a6c0ae6e3659a9ab62196fa198366ec0c0722286257cbdca", - LineEnding.KEEP, FormatCycle.SECOND); + String expectedHash = "fe7bdeec160a33a744209602d1ae99f94bd8ff433dd3ab856bcf6857588170d5c69b027f15c72bd7a6c0ae6e3659a9ab62196fa198366ec0c0722286257cbdca"; + twoPassTest(emptyMap(), new JavaFormatter(), "AnyClassCRLF.java", expectedHash, LineEnding.KEEP); } @Test void testIsIntialized() throws Exception { JavaFormatter javaFormatter = new JavaFormatter(); assertFalse(javaFormatter.isInitialized()); - javaFormatter.init(Collections.emptyMap(), - new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); + javaFormatter.init(emptyMap(), new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); assertTrue(javaFormatter.isInitialized()); } @@ -91,12 +66,8 @@ void testIsIntialized() throws Exception { void testDoFormatFileWithExclusions() throws Exception { JavaFormatter formatter = new JavaFormatter(); formatter.setExclusionPattern("\\b(from\\([^;]*\\.end[^;]*?\\));"); - doTestFormat(Collections.emptyMap(), formatter, "AnyClassExclusionLF.java", - "ea4580e667895a179a2baccd4822077e87caa62f2ebb2db0409407de48890b06fa1f7a070db617a4ab156a4e9223d5f2aa99a69209e1f0bdb263a0af7359d43e", - LineEnding.KEEP, FormatCycle.FIRST); - doTestFormat(Collections.emptyMap(), formatter, "AnyClassExclusionLF.java", - "ea4580e667895a179a2baccd4822077e87caa62f2ebb2db0409407de48890b06fa1f7a070db617a4ab156a4e9223d5f2aa99a69209e1f0bdb263a0af7359d43e", - LineEnding.KEEP, FormatCycle.SECOND); + String expectedHash = "ea4580e667895a179a2baccd4822077e87caa62f2ebb2db0409407de48890b06fa1f7a070db617a4ab156a4e9223d5f2aa99a69209e1f0bdb263a0af7359d43e"; + twoPassTest(emptyMap(), formatter, "AnyClassExclusionLF.java", expectedHash, LineEnding.KEEP); } } diff --git a/src/test/java/net/revelc/code/formatter/javascript/JavascriptFormatterTest.java b/src/test/java/net/revelc/code/formatter/javascript/JavascriptFormatterTest.java index 353ddd18b..900c37dee 100755 --- a/src/test/java/net/revelc/code/formatter/javascript/JavascriptFormatterTest.java +++ b/src/test/java/net/revelc/code/formatter/javascript/JavascriptFormatterTest.java @@ -13,43 +13,31 @@ */ package net.revelc.code.formatter.javascript; +import static java.util.Collections.emptyMap; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Collections; - import org.junit.jupiter.api.Test; import net.revelc.code.formatter.AbstractFormatterTest; -import net.revelc.code.formatter.FormatCycle; +import net.revelc.code.formatter.LineEnding; class JavascriptFormatterTest extends AbstractFormatterTest { @Test void testDoFormatFile() throws Exception { - if (System.lineSeparator().equals("\n")) { - doTestFormat(new JavascriptFormatter(), "AnyJS.js", - "d2a196d7aaddc3285b783d929965c884d1c1cacf15e777f0a7a7315355822b51f903fdb5b47f7f6c79ffc35e2f2dee58b605578a8b18afb91cdfed4624f03d7d", - FormatCycle.FIRST); - doTestFormat(new JavascriptFormatter(), "AnyJS.js", - "d2a196d7aaddc3285b783d929965c884d1c1cacf15e777f0a7a7315355822b51f903fdb5b47f7f6c79ffc35e2f2dee58b605578a8b18afb91cdfed4624f03d7d", - FormatCycle.SECOND); - } else { - doTestFormat(new JavascriptFormatter(), "AnyJS.js", - "33020bfa1ecebd935b6d6ba8e482bc14433ad52899ca63bd892fbb85d20e835ad183dba1e0a6203a72fbbb3d859b6f6872e320a8ea2fa93c9b2ca301ae7c6ec8", - FormatCycle.FIRST); - doTestFormat(new JavascriptFormatter(), "AnyJS.js", - "33020bfa1ecebd935b6d6ba8e482bc14433ad52899ca63bd892fbb85d20e835ad183dba1e0a6203a72fbbb3d859b6f6872e320a8ea2fa93c9b2ca301ae7c6ec8", - FormatCycle.SECOND); - } + String expectedHash = LineEnding.LF.isSystem() + ? "d2a196d7aaddc3285b783d929965c884d1c1cacf15e777f0a7a7315355822b51f903fdb5b47f7f6c79ffc35e2f2dee58b605578a8b18afb91cdfed4624f03d7d" + : "33020bfa1ecebd935b6d6ba8e482bc14433ad52899ca63bd892fbb85d20e835ad183dba1e0a6203a72fbbb3d859b6f6872e320a8ea2fa93c9b2ca301ae7c6ec8"; + LineEnding lineEnding = LineEnding.LF.isSystem() ? LineEnding.LF : LineEnding.CRLF; + twoPassTest(emptyMap(), new JavascriptFormatter(), "AnyJS.js", expectedHash, lineEnding); } @Test void testIsIntialized() throws Exception { JavascriptFormatter jsFormatter = new JavascriptFormatter(); assertFalse(jsFormatter.isInitialized()); - jsFormatter.init(Collections.emptyMap(), - new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); + jsFormatter.init(emptyMap(), new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); assertTrue(jsFormatter.isInitialized()); } diff --git a/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java b/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java index 4ef4bb1ee..5962a4771 100644 --- a/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java +++ b/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java @@ -13,6 +13,7 @@ */ package net.revelc.code.formatter.json; +import static java.util.Collections.emptyMap; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -22,7 +23,6 @@ import org.junit.jupiter.api.Test; import net.revelc.code.formatter.AbstractFormatterTest; -import net.revelc.code.formatter.FormatCycle; import net.revelc.code.formatter.LineEnding; /** @@ -32,32 +32,21 @@ class JsonFormatterTest extends AbstractFormatterTest { @Test void testDoFormatFile() throws Exception { - // Since we set the line endings via options for json, we cannot rely on CRLF inside doTestFormat. + // Since we set the line endings via options for json, we cannot rely on CRLF inside twoPassTest. // The option will not be available inside json formatter init so it will use whatever the system // default is regardless of requesting it to be CRLF later which is ignored. - if (System.lineSeparator().equals("\n")) { - doTestFormat(new JsonFormatter(), "someFile.json", - "1c8b8931b79a7dfaa4d2ab1986ebfe5967716b63877aa0311091214bf870f5480469a80e920fc825a98ad265f252e94e1ca4b94a55a279d0d2d302a20dcb4fa3", - FormatCycle.FIRST); - doTestFormat(new JsonFormatter(), "someFile.json", - "1c8b8931b79a7dfaa4d2ab1986ebfe5967716b63877aa0311091214bf870f5480469a80e920fc825a98ad265f252e94e1ca4b94a55a279d0d2d302a20dcb4fa3", - FormatCycle.SECOND); - } else { - doTestFormat(new JsonFormatter(), "someFile.json", - "c6e19e9d042d8d2045eb17d2966f105e6c538d5c05c614c556eb88dfb020645cb2d410cf059643a14ca193487b888e24194499ee8be2c337afdc89067a23e4cd", - FormatCycle.FIRST); - doTestFormat(new JsonFormatter(), "someFile.json", - "c6e19e9d042d8d2045eb17d2966f105e6c538d5c05c614c556eb88dfb020645cb2d410cf059643a14ca193487b888e24194499ee8be2c337afdc89067a23e4cd", - FormatCycle.SECOND); - } + String expectedHash = LineEnding.LF.isSystem() + ? "1c8b8931b79a7dfaa4d2ab1986ebfe5967716b63877aa0311091214bf870f5480469a80e920fc825a98ad265f252e94e1ca4b94a55a279d0d2d302a20dcb4fa3" + : "c6e19e9d042d8d2045eb17d2966f105e6c538d5c05c614c556eb88dfb020645cb2d410cf059643a14ca193487b888e24194499ee8be2c337afdc89067a23e4cd"; + LineEnding lineEnding = LineEnding.LF.isSystem() ? LineEnding.LF : LineEnding.CRLF; + twoPassTest(emptyMap(), new JsonFormatter(), "someFile.json", expectedHash, lineEnding); } @Test void testIsInitialized() { JsonFormatter jsonFormatter = new JsonFormatter(); assertFalse(jsonFormatter.isInitialized()); - jsonFormatter.init(new HashMap(), - new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); + jsonFormatter.init(emptyMap(), new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); assertTrue(jsonFormatter.isInitialized()); } @@ -68,24 +57,14 @@ void testDoFormatFileWithConfig() throws Exception { jsonFormattingOptions.put("spaceBeforeSeparator", "false"); jsonFormattingOptions.put("alphabeticalOrder", "true"); - // Since we set the line endings via options for json, we cannot rely on CRLF inside doTestFormat. + // Since we set the line endings via options for json, we cannot rely on CRLF inside twoPassTest. // The option will not be available inside json formatter init so it will use whatever the system // default is regardless of requesting it to be CRLF later which is ignored. - if (System.lineSeparator().equals("\n")) { - doTestFormat(jsonFormattingOptions, new JsonFormatter(), "someFile.json", - "0ca303fef968b92f3f798ff1615cd6c501ea3b754fd18f54932fd07c1dce86d2df9845817b8f521a2254c98c6e0d35b0bced3ea12113e961d3789111868897d7", - LineEnding.LF, FormatCycle.FIRST); - doTestFormat(jsonFormattingOptions, new JsonFormatter(), "someFile.json", - "0ca303fef968b92f3f798ff1615cd6c501ea3b754fd18f54932fd07c1dce86d2df9845817b8f521a2254c98c6e0d35b0bced3ea12113e961d3789111868897d7", - LineEnding.LF, FormatCycle.SECOND); - } else { - doTestFormat(jsonFormattingOptions, new JsonFormatter(), "someFile.json", - "5d433f2700a2fdabfabdb309d5f807df91ad86f7a94658d4a3f2f3699ae78b2efb1de451c141f61905f1c814cd647f312ae9651454e65d124510be0573082e86", - LineEnding.CRLF, FormatCycle.FIRST); - doTestFormat(jsonFormattingOptions, new JsonFormatter(), "someFile.json", - "5d433f2700a2fdabfabdb309d5f807df91ad86f7a94658d4a3f2f3699ae78b2efb1de451c141f61905f1c814cd647f312ae9651454e65d124510be0573082e86", - LineEnding.CRLF, FormatCycle.SECOND); - } + String expectedHash = LineEnding.LF.isSystem() + ? "0ca303fef968b92f3f798ff1615cd6c501ea3b754fd18f54932fd07c1dce86d2df9845817b8f521a2254c98c6e0d35b0bced3ea12113e961d3789111868897d7" + : "5d433f2700a2fdabfabdb309d5f807df91ad86f7a94658d4a3f2f3699ae78b2efb1de451c141f61905f1c814cd647f312ae9651454e65d124510be0573082e86"; + LineEnding lineEnding = LineEnding.LF.isSystem() ? LineEnding.LF : LineEnding.CRLF; + twoPassTest(jsonFormattingOptions, new JsonFormatter(), "someFile.json", expectedHash, lineEnding); } } diff --git a/src/test/java/net/revelc/code/formatter/xml/XMLFormatterTest.java b/src/test/java/net/revelc/code/formatter/xml/XMLFormatterTest.java index 41a529c2f..b5a18db9c 100644 --- a/src/test/java/net/revelc/code/formatter/xml/XMLFormatterTest.java +++ b/src/test/java/net/revelc/code/formatter/xml/XMLFormatterTest.java @@ -13,15 +13,14 @@ */ package net.revelc.code.formatter.xml; +import static java.util.Collections.emptyMap; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Collections; - import org.junit.jupiter.api.Test; import net.revelc.code.formatter.AbstractFormatterTest; -import net.revelc.code.formatter.FormatCycle; +import net.revelc.code.formatter.LineEnding; /** * @author yoshiman @@ -31,32 +30,21 @@ class XMLFormatterTest extends AbstractFormatterTest { @Test void testDoFormatFile() throws Exception { - // Since we set the line endings via options for xml, we cannot rely on CRLF inside doTestFormat. + // Since we set the line endings via options for xml, we cannot rely on CRLF inside twoPassTest. // The option will not be available inside xml formatter init so it will use whatever the system // default is regardless of requesting it to be CRLF later which is ignored. - if (System.lineSeparator().equals("\n")) { - doTestFormat(new XMLFormatter(), "someFile.xml", - "a5bfe48d45504b624d5f610bcbb935b117c8190de7c27957a8ba3658df6f3879682c485d77378443399a5d092899105988386c56b14308ac21faf02a82bfdffb", - FormatCycle.FIRST); - doTestFormat(new XMLFormatter(), "someFile.xml", - "a5bfe48d45504b624d5f610bcbb935b117c8190de7c27957a8ba3658df6f3879682c485d77378443399a5d092899105988386c56b14308ac21faf02a82bfdffb", - FormatCycle.SECOND); - } else { - doTestFormat(new XMLFormatter(), "someFile.xml", - "1fc08d47972da8debc97ef4071bc1a67a7df588513242e0af8a5df507c469a5921e52f096a436713c2291107ca20e1c215069abcd9dc04bed9ccbcb0418932e0", - FormatCycle.FIRST); - doTestFormat(new XMLFormatter(), "someFile.xml", - "1fc08d47972da8debc97ef4071bc1a67a7df588513242e0af8a5df507c469a5921e52f096a436713c2291107ca20e1c215069abcd9dc04bed9ccbcb0418932e0", - FormatCycle.SECOND); - } + String expectedHash = LineEnding.LF.isSystem() + ? "a5bfe48d45504b624d5f610bcbb935b117c8190de7c27957a8ba3658df6f3879682c485d77378443399a5d092899105988386c56b14308ac21faf02a82bfdffb" + : "1fc08d47972da8debc97ef4071bc1a67a7df588513242e0af8a5df507c469a5921e52f096a436713c2291107ca20e1c215069abcd9dc04bed9ccbcb0418932e0"; + LineEnding lineEnding = LineEnding.LF.isSystem() ? LineEnding.LF : LineEnding.CRLF; + twoPassTest(emptyMap(), new XMLFormatter(), "someFile.xml", expectedHash, lineEnding); } @Test void testIsIntialized() throws Exception { XMLFormatter xmlFormatter = new XMLFormatter(); assertFalse(xmlFormatter.isInitialized()); - xmlFormatter.init(Collections.emptyMap(), - new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); + xmlFormatter.init(emptyMap(), new AbstractFormatterTest.TestConfigurationSource(TEST_OUTPUT_PRIMARY_DIR)); assertTrue(xmlFormatter.isInitialized()); }