1 package org.unicode.icu.tool.cldrtoicu.ant;
3 import com.google.common.collect.ImmutableList;
4 import com.google.common.truth.BooleanSubject;
5 import com.google.common.truth.Truth;
7 import org.junit.runner.RunWith;
8 import org.junit.runners.JUnit4;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.StringReader;
13 import java.util.Arrays;
15 @RunWith(JUnit4.class)
16 public class CleanOutputDirectoryTaskTest {
17 // Not using the original field since we want this test to fail if this changes unexpectedly.
18 private static final String WAS_GENERATED_LABEL =
19 "Generated using tools/cldr/cldr-to-icu/build-icu-data.xml";
21 // Commented version of the label for test data.
22 private static final String WAS_GENERATED_LINE = "// " + WAS_GENERATED_LABEL;
24 private final static ImmutableList<String> HEADER = ImmutableList.of(
30 public void testWasFileAutoGenerated_lenientMatching() throws IOException {
31 // Testing comment prefixes (// or # supported equally)
32 assertWasAutogenerated("// Header#1", "// Header#2").isTrue();
33 assertWasAutogenerated("# Header#1", "# Header#2").isTrue();
34 assertWasAutogenerated("# Header#1", "// Header#2").isTrue();
35 assertWasAutogenerated("// Header#1", "//Header#2").isTrue();
37 assertWasAutogenerated("// Header#1", "// Header#2", "// More comment", "Not a comment").isTrue();
38 // BOM is ignored on first line
39 assertWasAutogenerated("\uFEFF// Header#1", "// Header#2").isTrue();
43 public void testWasFileAutoGenerated_lenientMatching_fail() throws IOException {
45 assertWasAutogenerated("", "// Header#1", "// Header#2").isFalse();
47 assertWasAutogenerated("// Header#1", "// Header#1").isFalse();
49 assertWasAutogenerated("// Header#2", "// Header#1").isFalse();
51 assertWasAutogenerated("Header#1", "Header#2").isFalse();
53 assertWasAutogenerated("// Header#1", "", "// Header#2").isFalse();
55 assertWasAutogenerated("// Header#1", "\uFEFF// Header#2").isFalse();
59 public void testWasFileAutoGenerated_withLabel() throws IOException {
60 // With the label in the header comment section everything passes.
61 assertWasAutogenerated("// Header#1", "// Header#2", WAS_GENERATED_LINE).isTrue();
62 assertWasAutogenerated("// Hello", "// World", WAS_GENERATED_LINE).isTrue();
63 assertWasAutogenerated("// Shorter Header", WAS_GENERATED_LINE).isTrue();
64 assertWasAutogenerated("// This", "// Is", "// A", "// Longer", "// Header", WAS_GENERATED_LINE).isTrue();
65 assertWasAutogenerated("// Where the label is", WAS_GENERATED_LINE, "// Does not matter").isTrue();
66 assertWasAutogenerated(WAS_GENERATED_LINE).isTrue();
70 public void testWasFileAutoGenerated_matchingEndsAfterComments() throws IOException {
71 assertWasAutogenerated("// Header#1", "// Header#Changed", WAS_GENERATED_LINE).isTrue();
72 // Label outside the header comment section does not count.
73 assertWasAutogenerated("// Header#1", "// Header#Changed", "Not a comment", WAS_GENERATED_LINE).isFalse();
76 private static BooleanSubject assertWasAutogenerated(String... fileLines) throws IOException {
77 return Truth.assertWithMessage("wasAutogenerated: %s", Arrays.asList(fileLines))
78 .that(CleanOutputDirectoryTask.wasFileAutoGenerated(testFile(fileLines), HEADER));
81 private static BufferedReader testFile(String... lines) {
82 return new BufferedReader(new StringReader(String.join("\n", lines) + "\n"));