Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append newline to printed documents #391

Merged
merged 4 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ directive @null_locations on
directive @empty_locations on

scalar AAA

""");
}

Expand Down Expand Up @@ -186,6 +187,7 @@ enum EnumWithValuesDefinitionOfNullItems {

enum EnumWithValuesDefinitionOfEmptyItems {
}

""");
}

Expand Down Expand Up @@ -224,6 +226,7 @@ input InputWithFieldsDefinitionOfNullItems {

input InputWithFieldsDefinitionOfEmptyItems {
}

""");
}

Expand Down Expand Up @@ -262,6 +265,7 @@ type TypeWithFieldsDefinitionOfNullItems {

type TypeWithFieldsDefinitionOfEmptyItems {
}

""");
}
}
23 changes: 13 additions & 10 deletions src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public async Task SDLPrinter_Should_Print_Document(

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

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

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

Expand All @@ -1025,9 +1025,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}})
}

""";
using var writer = new StringWriter();

var document = query.Parse();
Expand Down Expand Up @@ -1061,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);
Expand Down
18 changes: 11 additions & 7 deletions src/GraphQLParser.Tests/Visitors/SDLPrinterSkipDirectivesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class SDLPrinterSkipDirectivesTests
@"type Foo {
f: Int @aliased
k: Boolean
}")]
}
")]
[InlineData(2,
@"type Foo {
f: Int @bad @aliased
Expand All @@ -24,7 +25,8 @@ public class SDLPrinterSkipDirectivesTests
@"type Foo {
f: Int @aliased
k: Boolean
}")]
}
")]
[InlineData(3,
@"type Foo {
f: Int @aliased @bad
Expand All @@ -34,7 +36,8 @@ public class SDLPrinterSkipDirectivesTests
@"type Foo {
f: Int @aliased
k: Boolean
}")]
}
")]
[InlineData(4,
@"type Foo {
f: Int @bad
Expand All @@ -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();
using var writer = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ scalar A
query: Q
}

extend schema @bad")]
extend schema @bad
")]
[InlineData(2,
@"scalar A1
scalar B
Expand All @@ -28,7 +29,8 @@ scalar A2
",
@"scalar A1

scalar A2")]
scalar A2
")]
[InlineData(3,
@"scalar A1
scalar B
Expand All @@ -38,7 +40,8 @@ scalar D
",
@"scalar A1

scalar A2")]
scalar A2
")]
[InlineData(4,
@"query A1 { x }
query B { y }
Expand All @@ -50,15 +53,17 @@ query A2 { z }

query A2 {
z
}")]
}
")]
[InlineData(5,
@"directive @A1 on FIELD
directive @B on FIELD
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 }
Expand All @@ -74,7 +79,8 @@ enum D { X Y }
enum A2 {
X
Y
}")]
}
")]
[InlineData(7,
@"extend enum A1 { X Y }
extend enum B { X Y }
Expand All @@ -90,7 +96,8 @@ extend enum D { X Y }
extend enum A2 {
X
Y
}")]
}
")]
[InlineData(8,
@"input A1 @vip
input B
Expand All @@ -102,7 +109,8 @@ input D

input A2 {
a: Int
}")]
}
")]
[InlineData(9,
@"type A1 @vip
type B
Expand All @@ -114,7 +122,8 @@ type D

type A2 {
a: Int
}")]
}
")]
[InlineData(10,
@"interface A1 @vip
interface B
Expand All @@ -126,7 +135,8 @@ interface D

interface A2 {
a: Int
}")]
}
")]
[InlineData(11,
@"extend interface A1 @vip
extend interface B { a: Int }
Expand All @@ -138,7 +148,8 @@ interface A2 {

extend interface A2 {
a: Int
}")]
}
")]
[InlineData(12,
@"union A1 @vip
union B = X | Y
Expand All @@ -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 }
Expand All @@ -162,7 +174,8 @@ extend interface A2 {

extend input A2 {
a: Int
}")]
}
")]
[InlineData(14,
@"extend type A1 { a: Int }
extend type B { a: Int }
Expand All @@ -176,11 +189,12 @@ extend input A2 {

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();
using var writer = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ input Type2 {
f3: ID!
}

scalar Type3
scalar Type3
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ fragment frag3 on Type3 {
dummy1
dummy2
dummy3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,4 @@ extend type Foo {
seven(argument: [String]): Type
}

extend type Foo @onType
extend type Foo @onType
6 changes: 4 additions & 2 deletions src/GraphQLParser/Visitors/SDLPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,13 +1158,15 @@ public SDLPrinter(SDLPrinterOptions options)
}

/// <inheritdoc cref="SDLPrinter{TContext}"/>
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes were made only within PrintAsync and only for printed documents, not for individual nodes being printed, or where VisitAsync was called directly and the context could be examined to determine whether or not a newline had been printed.

}
}

Expand Down
Loading