Skip to content

Commit

Permalink
Clean up two-pass implementation for PR #528
Browse files Browse the repository at this point in the history
Refactor to make the two-pass testing more maintainable and easier to follow
  • Loading branch information
ctubbsii committed Oct 31, 2021
1 parent 1efe4df commit 143f47f
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 205 deletions.
4 changes: 4 additions & 0 deletions src/main/java/net/revelc/code/formatter/LineEnding.java
Expand Up @@ -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.
*/
Expand Down
68 changes: 44 additions & 24 deletions src/test/java/net/revelc/code/formatter/AbstractFormatterTest.java
Expand Up @@ -13,17 +13,19 @@
*/
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;
import static org.junit.jupiter.api.Assertions.assertTrue;

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;
Expand Down Expand Up @@ -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<String, String> options, Formatter formatter, String fileUnderTest,
String expectedSha512, LineEnding lineEnding) {
multiPassTest(2, options, formatter, fileUnderTest, expectedSha512, lineEnding);
}

private void multiPassTest(int numPasses, Map<String, String> 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<String, String> options, Formatter formatter, String fileUnderTest,
String expectedSha512, LineEnding lineEnding, FormatCycle formatCycle) throws IOException {
private void doTestFormat(Map<String, String> 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
Expand All @@ -133,11 +147,17 @@ protected void doTestFormat(Map<String, String> 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.
Expand Down
18 changes: 0 additions & 18 deletions src/test/java/net/revelc/code/formatter/FormatCycle.java

This file was deleted.

29 changes: 8 additions & 21 deletions src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java
Expand Up @@ -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
Expand All @@ -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());
}

Expand Down
32 changes: 10 additions & 22 deletions src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java
Expand Up @@ -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
Expand All @@ -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());
}

Expand Down
59 changes: 15 additions & 44 deletions src/test/java/net/revelc/code/formatter/java/JavaFormatterTest.java
Expand Up @@ -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;

/**
Expand All @@ -31,72 +29,45 @@ 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());
}

@Test
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);
}

}

0 comments on commit 143f47f

Please sign in to comment.