]> granicus.if.org Git - icu/blob
bc8d8542a62a08cb353f1159f3607d1b45fc36bc
[icu] /
1 package org.unicode.icu.tool.cldrtoicu.ant;
2
3 import com.google.common.collect.ImmutableList;
4 import com.google.common.truth.BooleanSubject;
5 import com.google.common.truth.Truth;
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.junit.runners.JUnit4;
9
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.StringReader;
13 import java.util.Arrays;
14
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";
20
21     // Commented version of the label for test data.
22     private static final String WAS_GENERATED_LINE = "// " + WAS_GENERATED_LABEL;
23
24     private final static ImmutableList<String> HEADER = ImmutableList.of(
25         "Header#1",
26         "Header#2",
27         WAS_GENERATED_LABEL);
28
29     @Test
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();
36         // Extra lines
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();
40     }
41
42     @Test
43     public void testWasFileAutoGenerated_lenientMatching_fail() throws IOException {
44         // New blank line.
45         assertWasAutogenerated("", "// Header#1", "// Header#2").isFalse();
46         // Duplicated line.
47         assertWasAutogenerated("// Header#1", "// Header#1").isFalse();
48         // Reversed lines.
49         assertWasAutogenerated("// Header#2", "// Header#1").isFalse();
50         // Not commented.
51         assertWasAutogenerated("Header#1", "Header#2").isFalse();
52         // Extra blank line.
53         assertWasAutogenerated("// Header#1", "", "// Header#2").isFalse();
54         // Misplaced BOM.
55         assertWasAutogenerated("// Header#1", "\uFEFF// Header#2").isFalse();
56     }
57
58     @Test
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();
67     }
68
69     @Test
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();
74     }
75
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));
79     }
80
81     private static BufferedReader testFile(String... lines) {
82         return new BufferedReader(new StringReader(String.join("\n", lines) + "\n"));
83     }
84 }