Skip to content

Commit

Permalink
Fix test failures on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
sormuras committed Oct 19, 2016
1 parent e345bce commit a1c790b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeMap;
import com.google.common.collect.TreeRangeSet;
import com.google.googlejavaformat.Newlines;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -209,7 +210,7 @@ private static RangeMap<Integer, String> buildReplacements(
// delete the import
int endPosition = importTree.getStartPosition() + importTree.getLength();
endPosition = Math.max(CharMatcher.isNot(' ').indexIn(contents, endPosition), endPosition);
String sep = System.lineSeparator();
String sep = Newlines.guessLineSeparator(contents);
if (endPosition + sep.length() < contents.length()
&& contents.subSequence(endPosition, endPosition + sep.length()).equals(sep)) {
endPosition += sep.length();
Expand Down
21 changes: 12 additions & 9 deletions core/src/test/java/com/google/googlejavaformat/java/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class MainTest {

@Rule public TemporaryFolder testFolder = new TemporaryFolder();

// PrintWriter instances used below are hard-coded to use system-default line separator.
private final Joiner joiner = Joiner.on(System.lineSeparator());

@Test
public void testUsageOutput() {
StringWriter out = new StringWriter();
Expand Down Expand Up @@ -145,11 +148,11 @@ public void javadoc() throws Exception {
"}",
"",
};
InputStream in = new ByteArrayInputStream(Joiner.on('\n').join(input).getBytes(UTF_8));
InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main = new Main(new PrintWriter(out, true), new PrintWriter(System.err, true), in);
assertThat(main.format("-")).isEqualTo(0);
assertThat(out.toString()).isEqualTo(Joiner.on('\n').join(expected));
assertThat(out.toString()).isEqualTo(joiner.join(expected));
}

// end to end import fixing test
Expand Down Expand Up @@ -177,11 +180,11 @@ public void imports() throws Exception {
" public static List<String> names;",
"}",
};
InputStream in = new ByteArrayInputStream(Joiner.on('\n').join(input).getBytes(UTF_8));
InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main = new Main(new PrintWriter(out, true), new PrintWriter(System.err, true), in);
assertThat(main.format("-", "--fix-imports-only")).isEqualTo(0);
assertThat(out.toString()).isEqualTo(Joiner.on('\n').join(expected));
assertThat(out.toString()).isEqualTo(joiner.join(expected));
}
{
String[] expected = {
Expand All @@ -191,13 +194,13 @@ public void imports() throws Exception {
" public static List<String> names;",
"}",
};
InputStream in = new ByteArrayInputStream(Joiner.on('\n').join(input).getBytes(UTF_8));
InputStream in = new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8));
StringWriter out = new StringWriter();
Main main = new Main(new PrintWriter(out, true), new PrintWriter(System.err, true), in);
assertThat(
main.format("-", "--fix-imports-only", "--experimental-remove-javadoc-only-imports"))
.isEqualTo(0);
assertThat(out.toString()).isEqualTo(Joiner.on('\n').join(expected));
assertThat(out.toString()).isEqualTo(joiner.join(expected));
}
}

Expand All @@ -224,9 +227,9 @@ public void importRemovalLines() throws Exception {
new Main(
new PrintWriter(out, true),
new PrintWriter(System.err, true),
new ByteArrayInputStream(Joiner.on('\n').join(input).getBytes(UTF_8)));
new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
assertThat(main.format("-", "-lines", "4")).isEqualTo(0);
assertThat(out.toString()).isEqualTo(Joiner.on('\n').join(expected));
assertThat(out.toString()).isEqualTo(joiner.join(expected));
}

// test that errors are reported on the right line when imports are removed
Expand All @@ -244,7 +247,7 @@ public void importRemoveErrorParseError() throws Exception {
new Main(
new PrintWriter(out, true),
new PrintWriter(err, true),
new ByteArrayInputStream(Joiner.on('\n').join(input).getBytes(UTF_8)));
new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
assertThat(main.format("-")).isEqualTo(1);
assertThat(err.toString()).contains("<stdin>:4:3: error: class, interface, or enum expected");
}
Expand Down

0 comments on commit a1c790b

Please sign in to comment.