Skip to content

Commit a440946

Browse files
authored
Append newline to printed documents (#391)
1 parent b201d09 commit a440946

8 files changed

+66
-39
lines changed

src/GraphQLParser.Tests/Visitors/SDLPrinterFromManualASTTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ directive @null_locations on
148148
directive @empty_locations on
149149
150150
scalar AAA
151+
151152
""");
152153
}
153154

@@ -186,6 +187,7 @@ enum EnumWithValuesDefinitionOfNullItems {
186187
187188
enum EnumWithValuesDefinitionOfEmptyItems {
188189
}
190+
189191
""");
190192
}
191193

@@ -224,6 +226,7 @@ input InputWithFieldsDefinitionOfNullItems {
224226
225227
input InputWithFieldsDefinitionOfEmptyItems {
226228
}
229+
227230
""");
228231
}
229232

@@ -262,6 +265,7 @@ type TypeWithFieldsDefinitionOfNullItems {
262265
263266
type TypeWithFieldsDefinitionOfEmptyItems {
264267
}
268+
265269
""");
266270
}
267271
}

src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ public async Task SDLPrinter_Should_Print_Document(
946946

947947
await printer.PrintAsync(document, writer);
948948
var actual = writer.ToString();
949-
actual.ShouldBe(expected, $"Test {number} failed");
949+
actual.ShouldBe(expected + Environment.NewLine, $"Test {number} failed");
950950

951951
actual.Parse(); // should be parsed back
952952
}
@@ -1006,7 +1006,7 @@ public async Task SDLPrinter_Should_Print_BlockStrings(int number, string input,
10061006
var renderedOriginal = writer.ToString();
10071007

10081008
var lines = renderedOriginal.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
1009-
var renderedDescription = string.Join(Environment.NewLine, lines.Take(lines.Length - 1));
1009+
var renderedDescription = string.Join(Environment.NewLine, lines.Take(lines.Length - 2));
10101010
renderedDescription = renderedDescription.Replace("\r\n", "\n");
10111011
renderedDescription.ShouldBe(expected);
10121012

@@ -1025,9 +1025,12 @@ public async Task SDLPrinter_Should_Print_BlockStrings(int number, string input,
10251025
public async Task SDLPrinter_Should_Print_EscapedStrings(string stringValue)
10261026
{
10271027
string query = $"{{a(p:{stringValue})}}";
1028-
string expected = @$"{{
1029-
a(p: {stringValue})
1030-
}}";
1028+
string expected = $$"""
1029+
{
1030+
a(p: {{stringValue}})
1031+
}
1032+
1033+
""";
10311034
using var writer = new StringWriter();
10321035

10331036
var document = query.Parse();
@@ -1061,11 +1064,11 @@ public void UTF8_MemoryStream_Runs_Synchronously()
10611064
}
10621065

10631066
[Theory]
1064-
[InlineData("{ field1 }", "{\n field1\n}")]
1065-
[InlineData("query { field1 }", "{\n field1\n}")]
1066-
[InlineData("query q1 { field1 }", "query q1 {\n field1\n}")]
1067-
[InlineData("mutation { field1 }", "mutation {\n field1\n}")]
1068-
[InlineData("mutation m1 { field1 }", "mutation m1 {\n field1\n}")]
1067+
[InlineData("{ field1 }", "{\n field1\n}\n")]
1068+
[InlineData("query { field1 }", "{\n field1\n}\n")]
1069+
[InlineData("query q1 { field1 }", "query q1 {\n field1\n}\n")]
1070+
[InlineData("mutation { field1 }", "mutation {\n field1\n}\n")]
1071+
[InlineData("mutation m1 { field1 }", "mutation m1 {\n field1\n}\n")]
10691072
public void OperationPrints(string input, string expected)
10701073
{
10711074
new SDLPrinter().Print(Parser.Parse(input)).ShouldBe(expected, StringCompareShould.IgnoreLineEndings);

src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public class SDLPrinterSkipDirectivesTests
1414
@"type Foo {
1515
f: Int @aliased
1616
k: Boolean
17-
}")]
17+
}
18+
")]
1819
[InlineData(2,
1920
@"type Foo {
2021
f: Int @bad @aliased
@@ -24,7 +25,8 @@ public class SDLPrinterSkipDirectivesTests
2425
@"type Foo {
2526
f: Int @aliased
2627
k: Boolean
27-
}")]
28+
}
29+
")]
2830
[InlineData(3,
2931
@"type Foo {
3032
f: Int @aliased @bad
@@ -34,7 +36,8 @@ public class SDLPrinterSkipDirectivesTests
3436
@"type Foo {
3537
f: Int @aliased
3638
k: Boolean
37-
}")]
39+
}
40+
")]
3841
[InlineData(4,
3942
@"type Foo {
4043
f: Int @bad
@@ -44,11 +47,12 @@ public class SDLPrinterSkipDirectivesTests
4447
@"type Foo {
4548
f: Int
4649
k: Boolean
47-
}")]
50+
}
51+
")]
4852
public async Task Printer_Should_Print_Pretty_If_Directives_Skipped(
49-
int number,
50-
string text,
51-
string expected)
53+
int number,
54+
string text,
55+
string expected)
5256
{
5357
var printer = new MyPrinter();
5458
using var writer = new StringWriter();

src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ scalar A
1919
query: Q
2020
}
2121
22-
extend schema @bad")]
22+
extend schema @bad
23+
")]
2324
[InlineData(2,
2425
@"scalar A1
2526
scalar B
@@ -28,7 +29,8 @@ scalar A2
2829
",
2930
@"scalar A1
3031
31-
scalar A2")]
32+
scalar A2
33+
")]
3234
[InlineData(3,
3335
@"scalar A1
3436
scalar B
@@ -38,7 +40,8 @@ scalar D
3840
",
3941
@"scalar A1
4042
41-
scalar A2")]
43+
scalar A2
44+
")]
4245
[InlineData(4,
4346
@"query A1 { x }
4447
query B { y }
@@ -50,15 +53,17 @@ query A2 { z }
5053
5154
query A2 {
5255
z
53-
}")]
56+
}
57+
")]
5458
[InlineData(5,
5559
@"directive @A1 on FIELD
5660
directive @B on FIELD
5761
directive @A2 on FIELD
5862
",
5963
@"directive @A1 on FIELD
6064
61-
directive @A2 on FIELD")]
65+
directive @A2 on FIELD
66+
")]
6267
[InlineData(6,
6368
@"enum A1 { X Y }
6469
enum B { X Y }
@@ -74,7 +79,8 @@ enum D { X Y }
7479
enum A2 {
7580
X
7681
Y
77-
}")]
82+
}
83+
")]
7884
[InlineData(7,
7985
@"extend enum A1 { X Y }
8086
extend enum B { X Y }
@@ -90,7 +96,8 @@ extend enum D { X Y }
9096
extend enum A2 {
9197
X
9298
Y
93-
}")]
99+
}
100+
")]
94101
[InlineData(8,
95102
@"input A1 @vip
96103
input B
@@ -102,7 +109,8 @@ input D
102109
103110
input A2 {
104111
a: Int
105-
}")]
112+
}
113+
")]
106114
[InlineData(9,
107115
@"type A1 @vip
108116
type B
@@ -114,7 +122,8 @@ type D
114122
115123
type A2 {
116124
a: Int
117-
}")]
125+
}
126+
")]
118127
[InlineData(10,
119128
@"interface A1 @vip
120129
interface B
@@ -126,7 +135,8 @@ interface D
126135
127136
interface A2 {
128137
a: Int
129-
}")]
138+
}
139+
")]
130140
[InlineData(11,
131141
@"extend interface A1 @vip
132142
extend interface B { a: Int }
@@ -138,7 +148,8 @@ interface A2 {
138148
139149
extend interface A2 {
140150
a: Int
141-
}")]
151+
}
152+
")]
142153
[InlineData(12,
143154
@"union A1 @vip
144155
union B = X | Y
@@ -148,7 +159,8 @@ extend interface A2 {
148159
",
149160
@"union A1 @vip
150161
151-
union A2 = X | Y")]
162+
union A2 = X | Y
163+
")]
152164
[InlineData(13,
153165
@"extend input A1 { a: Int }
154166
extend input B { a: Int }
@@ -162,7 +174,8 @@ extend interface A2 {
162174
163175
extend input A2 {
164176
a: Int
165-
}")]
177+
}
178+
")]
166179
[InlineData(14,
167180
@"extend type A1 { a: Int }
168181
extend type B { a: Int }
@@ -176,11 +189,12 @@ extend input A2 {
176189
177190
extend type A2 {
178191
a: Int
179-
}")]
192+
}
193+
")]
180194
public async Task Printer_Should_Print_Pretty_If_Definitions_Skipped(
181-
int number,
182-
string text,
183-
string expected)
195+
int number,
196+
string text,
197+
string expected)
184198
{
185199
var printer = new PrintOnlyStartsWithA();
186200
using var writer = new StringWriter();

src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.DefinitionSortTests.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ input Type2 {
2727
f3: ID!
2828
}
2929

30-
scalar Type3
30+
scalar Type3

src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.ExecutableSortTests.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ fragment frag3 on Type3 {
6565
dummy1
6666
dummy2
6767
dummy3
68-
}
68+
}

src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.KitchenSink.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,4 @@ extend type Foo {
139139
seven(argument: [String]): Type
140140
}
141141

142-
extend type Foo @onType
142+
extend type Foo @onType

src/GraphQLParser/Visitors/SDLPrinter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,13 +1158,15 @@ public SDLPrinter(SDLPrinterOptions options)
11581158
}
11591159

11601160
/// <inheritdoc cref="SDLPrinter{TContext}"/>
1161-
public virtual ValueTask PrintAsync(ASTNode node, TextWriter writer, CancellationToken cancellationToken = default)
1161+
public virtual async ValueTask PrintAsync(ASTNode node, TextWriter writer, CancellationToken cancellationToken = default)
11621162
{
11631163
var context = new DefaultPrintContext(writer)
11641164
{
11651165
CancellationToken = cancellationToken,
11661166
};
1167-
return VisitAsync(node, context);
1167+
await VisitAsync(node, context).ConfigureAwait(false);
1168+
if (!context.NewLinePrinted && node is GraphQLDocument)
1169+
await writer.WriteLineAsync().ConfigureAwait(false);
11681170
}
11691171
}
11701172

0 commit comments

Comments
 (0)