From 8cf50285dda9ddefe8e4a7e95510a77840fddc38 Mon Sep 17 00:00:00 2001 From: Richard Chen Date: Tue, 16 Jan 2024 19:25:15 -0500 Subject: [PATCH] Standardized line separators. --- .../amazon/smithy/cli/FormatCommandTest.java | 50 ++++++++-------- .../node/internal/PrettyPrintWriter.java | 2 +- .../model/node/DefaultNodeWriterTest.java | 40 ++++++------- .../smithy/model/node/NodeSerializerTest.java | 58 +++++++++---------- .../amazon/smithy/model/node/NodeTest.java | 2 +- ...ontextualValidationEventFormatterTest.java | 25 ++++---- 6 files changed, 87 insertions(+), 90 deletions(-) diff --git a/smithy-cli/src/it/java/software/amazon/smithy/cli/FormatCommandTest.java b/smithy-cli/src/it/java/software/amazon/smithy/cli/FormatCommandTest.java index efc888aacd8..da96fdd5afd 100644 --- a/smithy-cli/src/it/java/software/amazon/smithy/cli/FormatCommandTest.java +++ b/smithy-cli/src/it/java/software/amazon/smithy/cli/FormatCommandTest.java @@ -32,14 +32,14 @@ public void formatsSingleFile() { IntegUtils.run("bad-formatting", ListUtils.of("format", "model/other.smithy"), result -> { assertThat(result.getExitCode(), equalTo(0)); - String model = result.getFile("model/other.smithy").replace("\r\n", "\n"); - assertThat(model, equalTo("$version: \"2.0\"\n" - + "\n" - + "namespace smithy.example\n" - + "\n" - + "string MyString\n" - + "\n" - + "string MyString2\n")); + String model = result.getFile("model/other.smithy"); + assertThat(model, equalTo(String.format("$version: \"2.0\"%n" + + "%n" + + "namespace smithy.example%n" + + "%n" + + "string MyString%n" + + "%n" + + "string MyString2%n"))); }); } @@ -48,24 +48,24 @@ public void formatsDirectory() { IntegUtils.run("bad-formatting", ListUtils.of("format", "model"), result -> { assertThat(result.getExitCode(), equalTo(0)); - String main = result.getFile("model/main.smithy").replace("\r\n", "\n"); - assertThat(main, equalTo("$version: \"2.0\"\n" - + "\n" - + "metadata this_is_a_long_string = {\n" - + " this_is_a_long_string1: \"a\"\n" - + " this_is_a_long_string2: \"b\"\n" - + " this_is_a_long_string3: \"c\"\n" - + " this_is_a_long_string4: \"d\"\n" - + "}\n")); + String main = result.getFile("model/main.smithy"); + assertThat(main, equalTo(String.format("$version: \"2.0\"%n" + + "%n" + + "metadata this_is_a_long_string = {%n" + + " this_is_a_long_string1: \"a\"%n" + + " this_is_a_long_string2: \"b\"%n" + + " this_is_a_long_string3: \"c\"%n" + + " this_is_a_long_string4: \"d\"%n" + + "}%n"))); - String other = result.getFile("model/other.smithy").replace("\r\n", "\n"); - assertThat(other, equalTo("$version: \"2.0\"\n" - + "\n" - + "namespace smithy.example\n" - + "\n" - + "string MyString\n" - + "\n" - + "string MyString2\n")); + String other = result.getFile("model/other.smithy"); + assertThat(other, equalTo(String.format("$version: \"2.0\"%n" + + "%n" + + "namespace smithy.example%n" + + "%n" + + "string MyString%n" + + "%n" + + "string MyString2%n"))); }); } } diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/node/internal/PrettyPrintWriter.java b/smithy-model/src/main/java/software/amazon/smithy/model/node/internal/PrettyPrintWriter.java index bd04c65513d..7847e27265d 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/node/internal/PrettyPrintWriter.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/node/internal/PrettyPrintWriter.java @@ -95,7 +95,7 @@ private boolean writeNewLine() throws IOException { if (indentString == null) { return false; } - writer.write('\n'); + writer.write(System.lineSeparator()); for (int i = 0; i < indent; i++) { writer.write(indentString); } diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/node/DefaultNodeWriterTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/node/DefaultNodeWriterTest.java index d03d1297141..bafd4e05372 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/node/DefaultNodeWriterTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/node/DefaultNodeWriterTest.java @@ -42,21 +42,21 @@ public static Collection data() { {new NullNode(SourceLocation.none()), "null"}, {new ArrayNode( Arrays.asList(Node.from(1), Node.from(2)), - SourceLocation.none()), String.format("[\n 1,\n 2\n]")}, + SourceLocation.none()), String.format("[%n 1,%n 2%n]")}, {Node.objectNode() .withMember("foo", Node.from("foo")) .withMember("baz", Node.from(1)) .withMember("bar", Node.objectNode() .withMember("qux", Node.arrayNode() .withValue(Node.from("ipsum")))), - String.format("{\n" - + " \"foo\": \"foo\",\n" - + " \"baz\": 1,\n" - + " \"bar\": {\n" - + " \"qux\": [\n" - + " \"ipsum\"\n" - + " ]\n" - + " }\n" + String.format("{%n" + + " \"foo\": \"foo\",%n" + + " \"baz\": 1,%n" + + " \"bar\": {%n" + + " \"qux\": [%n" + + " \"ipsum\"%n" + + " ]%n" + + " }%n" + "}") }, {Node.objectNode() @@ -66,17 +66,17 @@ public static Collection data() { .withMember("bam", Node.arrayNode() .withValue(Node.objectNode() .withMember("abc", Node.from(123)))), - String.format("{\n" - + " \"foo\": {\n" - + " \"bar\": [\n" - + " \"baz\"\n" - + " ]\n" - + " },\n" - + " \"bam\": [\n" - + " {\n" - + " \"abc\": 123\n" - + " }\n" - + " ]\n" + String.format("{%n" + + " \"foo\": {%n" + + " \"bar\": [%n" + + " \"baz\"%n" + + " ]%n" + + " },%n" + + " \"bam\": [%n" + + " {%n" + + " \"abc\": 123%n" + + " }%n" + + " ]%n" + "}") } }); diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeSerializerTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeSerializerTest.java index db7d284bb94..dd95726f024 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeSerializerTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeSerializerTest.java @@ -11,17 +11,17 @@ public void serializesNodes() { Node node = Node.parse("{\"foo\": \"bar\", \"baz\": true, \"bam\": false, \"boo\": 10}"); assertThat(Node.printJson(node), equalTo("{\"foo\":\"bar\",\"baz\":true,\"bam\":false,\"boo\":10}")); - assertThat(Node.prettyPrintJson(node), equalTo("{\n" - + " \"foo\": \"bar\",\n" - + " \"baz\": true,\n" - + " \"bam\": false,\n" - + " \"boo\": 10\n" + assertThat(Node.prettyPrintJson(node), equalTo("{%n" + + " \"foo\": \"bar\",%n" + + " \"baz\": true,%n" + + " \"bam\": false,%n" + + " \"boo\": 10%n" + "}")); - assertThat(Node.prettyPrintJson(node, "\t"), equalTo("{\n" - + "\t\"foo\": \"bar\",\n" - + "\t\"baz\": true,\n" - + "\t\"bam\": false,\n" - + "\t\"boo\": 10\n" + assertThat(Node.prettyPrintJson(node, "\t"), equalTo("{%n" + + "\t\"foo\": \"bar\",%n" + + "\t\"baz\": true,%n" + + "\t\"bam\": false,%n" + + "\t\"boo\": 10%n" + "}")); } @@ -45,24 +45,24 @@ public void serializesComplexNodes() { assertThat(Node.printJson(node), equalTo( "[{\"foo\":\"\uD83D\uDCA9\",\"baz\":true,\"bam\":false," + "\"boo\":10},10,true,false,{},[],\"\",\" \",null,-1,-1.0]")); - assertThat(Node.prettyPrintJson(node), equalTo( - "[\n" - + " {\n" - + " \"foo\": \"\uD83D\uDCA9\",\n" - + " \"baz\": true,\n" - + " \"bam\": false,\n" - + " \"boo\": 10\n" - + " },\n" - + " 10,\n" - + " true,\n" - + " false,\n" - + " {},\n" // optimized empty object - + " [],\n" // optimized empty array - + " \"\",\n" - + " \" \",\n" - + " null,\n" - + " -1,\n" - + " -1.0\n" - + "]")); + assertThat(Node.prettyPrintJson(node), equalTo(String.format( + "[%n" + + " {%n" + + " \"foo\": \"\uD83D\uDCA9\",%n" + + " \"baz\": true,%n" + + " \"bam\": false,%n" + + " \"boo\": 10%n" + + " },%n" + + " 10,%n" + + " true,%n" + + " false,%n" + + " {},%n" // optimized empty object + + " [],%n" // optimized empty array + + " \"\",%n" + + " \" \",%n" + + " null,%n" + + " -1,%n" + + " -1.0%n" + + "]"))); } } diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeTest.java index 91c646e5e5f..d0a124289b0 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/node/NodeTest.java @@ -178,7 +178,7 @@ public void deepSortsNodesWithComparator() { @Test public void prettyPrintsJson() { assertThat(Node.prettyPrintJson(Node.parse("{\"foo\": true}")), - equalTo(String.format("{\n \"foo\": true\n}"))); + equalTo(String.format("{%n \"foo\": true%n}"))); } @Test diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/validation/ContextualValidationEventFormatterTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/validation/ContextualValidationEventFormatterTest.java index b8e1ec644cd..5b69b8df82f 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/validation/ContextualValidationEventFormatterTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/validation/ContextualValidationEventFormatterTest.java @@ -31,17 +31,15 @@ public void loadsContext() { .build(); String format = new ContextualValidationEventFormatter().format(event); - // Normalize line endings for Windows. - format = format.replace("\r\n", "\n"); assertThat(format, startsWith("ERROR: example.smithy#Foo (foo)")); - assertThat(format, containsString("\n @ ")); - assertThat(format, endsWith( - "\n |" - + "\n 3 | structure Foo {" - + "\n | ^" - + "\n = This is the message" - + "\n")); + assertThat(format, containsString(String.format("%n @ "))); + assertThat(format, endsWith(String.format( + "%n |" + + "%n 3 | structure Foo {" + + "%n | ^" + + "%n = This is the message" + + "%n"))); } @Test @@ -54,12 +52,11 @@ public void doesNotLoadSourceLocationNone() { .build(); String format = new ContextualValidationEventFormatter().format(event); - // Normalize line endings for Windows. - format = format.replace("\r\n", "\n"); - assertThat(format, equalTo( + assertThat(format, equalTo(String.format( "ERROR: - (foo)" - + "\n = This is the message" - + "\n")); + + "%n = This is the message" + + "%n") + )); } }