From edc321aead47e26a47e8e0e28262c2dcaf79934e Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Sat, 17 Aug 2024 23:43:10 -0400 Subject: [PATCH 1/3] Append newline to printed documents --- .../Visitors/SDLPrinterFromManualASTTests.cs | 4 ++++ .../Visitors/SDLPrinterFromParsedTextTests.cs | 13 ++++++++----- .../Visitors/SDLPrinterSkipDirectivesTests.cs | 18 +++++++++++------- .../SDLPrinterVerticalIndentationTests.cs | 8 ++++---- ...nition.approved.DefinitionSortTests.graphql | 2 +- ...nition.approved.ExecutableSortTests.graphql | 2 +- ...hemaDefinition.approved.KitchenSink.graphql | 2 +- src/GraphQLParser/Visitors/SDLPrinter.cs | 6 ++++-- 8 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromManualASTTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromManualASTTests.cs index 6aecf7a5..e988f210 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromManualASTTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromManualASTTests.cs @@ -148,6 +148,7 @@ directive @null_locations on directive @empty_locations on scalar AAA + """); } @@ -186,6 +187,7 @@ enum EnumWithValuesDefinitionOfNullItems { enum EnumWithValuesDefinitionOfEmptyItems { } + """); } @@ -224,6 +226,7 @@ input InputWithFieldsDefinitionOfNullItems { input InputWithFieldsDefinitionOfEmptyItems { } + """); } @@ -262,6 +265,7 @@ type TypeWithFieldsDefinitionOfNullItems { type TypeWithFieldsDefinitionOfEmptyItems { } + """); } } diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs index 935d47b9..930d5dc1 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs @@ -869,7 +869,7 @@ public async Task SDLPrinter_Should_Print_Document( await printer.PrintAsync(document, writer).ConfigureAwait(false); var actual = writer.ToString(); - actual.ShouldBe(expected, $"Test {number} failed"); + actual.ShouldBe(expected + Environment.NewLine, $"Test {number} failed"); actual.Parse(); // should be parsed back } @@ -928,7 +928,7 @@ public async Task SDLPrinter_Should_Print_BlockStrings(int number, string input, var renderedOriginal = writer.ToString(); var lines = renderedOriginal.Split(Environment.NewLine); - var renderedDescription = string.Join(Environment.NewLine, lines.SkipLast(1)); + var renderedDescription = string.Join(Environment.NewLine, lines.SkipLast(2)); renderedDescription = renderedDescription.Replace("\r\n", "\n"); renderedDescription.ShouldBe(expected); @@ -947,9 +947,12 @@ public async Task SDLPrinter_Should_Print_BlockStrings(int number, string input, public async Task SDLPrinter_Should_Print_EscapedStrings(string stringValue) { string query = $"{{a(p:{stringValue})}}"; - string expected = @$"{{ - a(p: {stringValue}) -}}"; + string expected = $$""" + { + a(p: {{stringValue}}) + } + + """; var writer = new StringWriter(); var document = query.Parse(); diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs index 18746642..4a9665dd 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs @@ -14,7 +14,8 @@ public class SDLPrinterSkipDirectivesTests @"type Foo { f: Int @aliased k: Boolean -}")] +} +")] [InlineData(2, @"type Foo { f: Int @bad @aliased @@ -24,7 +25,8 @@ public class SDLPrinterSkipDirectivesTests @"type Foo { f: Int @aliased k: Boolean -}")] +} +")] [InlineData(3, @"type Foo { f: Int @aliased @bad @@ -34,7 +36,8 @@ public class SDLPrinterSkipDirectivesTests @"type Foo { f: Int @aliased k: Boolean -}")] +} +")] [InlineData(4, @"type Foo { f: Int @bad @@ -44,11 +47,12 @@ public class SDLPrinterSkipDirectivesTests @"type Foo { f: Int k: Boolean -}")] +} +")] public async Task Printer_Should_Print_Pretty_If_Directives_Skipped( -int number, -string text, -string expected) + int number, + string text, + string expected) { var printer = new MyPrinter(); var writer = new StringWriter(); diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs index a22b58b5..56202a0a 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs @@ -178,9 +178,9 @@ extend type A2 { a: Int }")] public async Task Printer_Should_Print_Pretty_If_Definitions_Skipped( -int number, -string text, -string expected) + int number, + string text, + string expected) { var printer = new PrintOnlyStartsWithA(); var writer = new StringWriter(); @@ -188,7 +188,7 @@ public async Task Printer_Should_Print_Pretty_If_Definitions_Skipped( await printer.PrintAsync(document, writer).ConfigureAwait(false); var actual = writer.ToString(); - actual.ShouldBe(expected, $"Test {number} failed"); + actual.ShouldBe(expected + Environment.NewLine, $"Test {number} failed"); actual.Parse(); // should be parsed back } diff --git a/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.DefinitionSortTests.graphql b/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.DefinitionSortTests.graphql index bf3bc9ed..f1146fdf 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.DefinitionSortTests.graphql +++ b/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.DefinitionSortTests.graphql @@ -27,4 +27,4 @@ input Type2 { f3: ID! } -scalar Type3 \ No newline at end of file +scalar Type3 diff --git a/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.ExecutableSortTests.graphql b/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.ExecutableSortTests.graphql index 5f092af4..ef3fbf19 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.ExecutableSortTests.graphql +++ b/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.ExecutableSortTests.graphql @@ -65,4 +65,4 @@ fragment frag3 on Type3 { dummy1 dummy2 dummy3 -} \ No newline at end of file +} diff --git a/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.KitchenSink.graphql b/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.KitchenSink.graphql index fbdd7562..6629e28c 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.KitchenSink.graphql +++ b/src/GraphQLParser.Tests/Visitors/SDLSorterTests.SortsSchemaDefinition.approved.KitchenSink.graphql @@ -139,4 +139,4 @@ extend type Foo { seven(argument: [String]): Type } -extend type Foo @onType \ No newline at end of file +extend type Foo @onType diff --git a/src/GraphQLParser/Visitors/SDLPrinter.cs b/src/GraphQLParser/Visitors/SDLPrinter.cs index 7945ef7d..097741a8 100644 --- a/src/GraphQLParser/Visitors/SDLPrinter.cs +++ b/src/GraphQLParser/Visitors/SDLPrinter.cs @@ -1097,13 +1097,15 @@ public SDLPrinter(SDLPrinterOptions options) } /// - public virtual ValueTask PrintAsync(ASTNode node, TextWriter writer, CancellationToken cancellationToken = default) + public virtual async ValueTask PrintAsync(ASTNode node, TextWriter writer, CancellationToken cancellationToken = default) { var context = new DefaultPrintContext(writer) { CancellationToken = cancellationToken, }; - return VisitAsync(node, context); + await VisitAsync(node, context).ConfigureAwait(false); + if (!context.NewLinePrinted && node is GraphQLDocument) + await writer.WriteLineAsync().ConfigureAwait(false); } } From 10315ddde19e60aef8e8aec553557c5a55792e3d Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Sat, 17 Aug 2024 23:58:21 -0400 Subject: [PATCH 2/3] Update --- .../Visitors/SDLPrinterFromParsedTextTests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs index 0ebbd4f5..234dab5d 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs @@ -1064,11 +1064,11 @@ public void UTF8_MemoryStream_Runs_Synchronously() } [Theory] - [InlineData("{ field1 }", "{\n field1\n}")] - [InlineData("query { field1 }", "{\n field1\n}")] - [InlineData("query q1 { field1 }", "query q1 {\n field1\n}")] - [InlineData("mutation { field1 }", "mutation {\n field1\n}")] - [InlineData("mutation m1 { field1 }", "mutation m1 {\n field1\n}")] + [InlineData("{ field1 }", "{\n field1\n}\n")] + [InlineData("query { field1 }", "{\n field1\n}\n")] + [InlineData("query q1 { field1 }", "query q1 {\n field1\n}\n")] + [InlineData("mutation { field1 }", "mutation {\n field1\n}\n")] + [InlineData("mutation m1 { field1 }", "mutation m1 {\n field1\n}\n")] public void OperationPrints(string input, string expected) { new SDLPrinter().Print(Parser.Parse(input)).ShouldBe(expected, StringCompareShould.IgnoreLineEndings); From 1d524bcc5ccfeba55213527f75aa1f6c455981df Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Sun, 18 Aug 2024 00:05:53 -0400 Subject: [PATCH 3/3] update --- .../SDLPrinterVerticalIndentationTests.cs | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs b/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs index 7e38a61f..952754f8 100644 --- a/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs +++ b/src/GraphQLParser.Tests/Visitors/SDLPrinterVerticalIndentationTests.cs @@ -19,7 +19,8 @@ scalar A query: Q } -extend schema @bad")] +extend schema @bad +")] [InlineData(2, @"scalar A1 scalar B @@ -28,7 +29,8 @@ scalar A2 ", @"scalar A1 -scalar A2")] +scalar A2 +")] [InlineData(3, @"scalar A1 scalar B @@ -38,7 +40,8 @@ scalar D ", @"scalar A1 -scalar A2")] +scalar A2 +")] [InlineData(4, @"query A1 { x } query B { y } @@ -50,7 +53,8 @@ query A2 { z } query A2 { z -}")] +} +")] [InlineData(5, @"directive @A1 on FIELD directive @B on FIELD @@ -58,7 +62,8 @@ directive @A2 on FIELD ", @"directive @A1 on FIELD -directive @A2 on FIELD")] +directive @A2 on FIELD +")] [InlineData(6, @"enum A1 { X Y } enum B { X Y } @@ -74,7 +79,8 @@ enum D { X Y } enum A2 { X Y -}")] +} +")] [InlineData(7, @"extend enum A1 { X Y } extend enum B { X Y } @@ -90,7 +96,8 @@ extend enum D { X Y } extend enum A2 { X Y -}")] +} +")] [InlineData(8, @"input A1 @vip input B @@ -102,7 +109,8 @@ input D input A2 { a: Int -}")] +} +")] [InlineData(9, @"type A1 @vip type B @@ -114,7 +122,8 @@ type D type A2 { a: Int -}")] +} +")] [InlineData(10, @"interface A1 @vip interface B @@ -126,7 +135,8 @@ interface D interface A2 { a: Int -}")] +} +")] [InlineData(11, @"extend interface A1 @vip extend interface B { a: Int } @@ -138,7 +148,8 @@ interface A2 { extend interface A2 { a: Int -}")] +} +")] [InlineData(12, @"union A1 @vip union B = X | Y @@ -148,7 +159,8 @@ extend interface A2 { ", @"union A1 @vip -union A2 = X | Y")] +union A2 = X | Y +")] [InlineData(13, @"extend input A1 { a: Int } extend input B { a: Int } @@ -162,7 +174,8 @@ extend interface A2 { extend input A2 { a: Int -}")] +} +")] [InlineData(14, @"extend type A1 { a: Int } extend type B { a: Int } @@ -176,7 +189,8 @@ extend input A2 { extend type A2 { a: Int -}")] +} +")] public async Task Printer_Should_Print_Pretty_If_Definitions_Skipped( int number, string text, @@ -188,7 +202,7 @@ public async Task Printer_Should_Print_Pretty_If_Definitions_Skipped( await printer.PrintAsync(document, writer); var actual = writer.ToString(); - actual.ShouldBe(expected + Environment.NewLine, $"Test {number} failed"); + actual.ShouldBe(expected, $"Test {number} failed"); actual.Parse(); // should be parsed back }