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 1 commit
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 {
}

""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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);

Expand All @@ -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();
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();
var writer = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@ 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();
var document = text.Parse();

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
}
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 @@ -1097,13 +1097,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