Bundle(String name, FormatterFunc function) throws IOException { file = setFile("src/test." + name).toContent("CCC"); FormatterStep step = FormatterStep.createNeverUpToDate(name, function); check = createCheckTask(name, step); apply = createApplyTask(name, step); }
@Test public void testStepCheckFail() throws IOException { File testFile = setFile("testFile").toContent("apple"); checkTask.setTarget(Collections.singleton(testFile)); checkTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p"))); String diff = String.join("\n", " @@ -1 +1 @@", " -apple", " +aple"); Assertions.assertThatThrownBy(() -> execute(checkTask)).hasStackTraceContaining(diff); assertFile(testFile).hasContent("apple"); }
@Test public void whitespaceProblem() throws Exception { SpotlessTask task = create(setFile("testFile").toContent("A \nB\t\nC \n")); task.addStep(FormatterStep.createNeverUpToDate("trimTrailing", input -> { Pattern pattern = Pattern.compile("[ \t]+$", Pattern.UNIX_LINES | Pattern.MULTILINE); return pattern.matcher(input).replaceAll(""); })); assertTaskFailure(task, " testFile", " @@ -1,3 +1,3 @@", " -A·", " -B\\t", " -C··", " +A", " +B", " +C"); }
@Test public void testStepCheckPass() throws Exception { File testFile = setFile("testFile").toContent("aple"); checkTask.setTarget(Collections.singleton(testFile)); checkTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p"))); execute(checkTask); assertFile(testFile).hasContent("aple"); }
@Test public void testStepApply() throws Exception { File testFile = setFile("testFile").toContent("apple"); applyTask.setTarget(Collections.singleton(testFile)); applyTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p"))); execute(applyTask); assertFile(testFile).hasContent("aple"); } }
private void testCase(FormatterFunc step, String input, PaddedCell.Type expectedOutputType, String expectedSteps, String canonical, boolean misbehaved) throws IOException { List<FormatterStep> formatterSteps = new ArrayList<>(); formatterSteps.add(FormatterStep.createNeverUpToDate("step", step)); try (Formatter formatter = Formatter.builder() .lineEndingsPolicy(LineEnding.UNIX.createPolicy()) .encoding(StandardCharsets.UTF_8) .rootDir(folder.getRoot().toPath()) .steps(formatterSteps).build()) { File file = folder.newFile(); Files.write(file.toPath(), input.getBytes(StandardCharsets.UTF_8)); PaddedCell result = PaddedCell.check(formatter, file); Assert.assertEquals(misbehaved, result.misbehaved()); Assert.assertEquals(expectedOutputType, result.type()); String actual = String.join(",", result.steps()); Assert.assertEquals(expectedSteps, actual); if (canonical == null) { try { result.canonical(); Assert.fail("Expected exception"); } catch (IllegalArgumentException expected) {} } else { Assert.assertEquals(canonical, result.canonical()); } } }