Skip to content

Commit

Permalink
Bump xunit and related fixes (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored and Shane32 committed Oct 12, 2023
1 parent 4911e52 commit ff81872
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 25 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- master
- develop
- v8
- v8
paths:
- src/**
- .github/workflows/**
Expand Down Expand Up @@ -39,7 +39,6 @@ jobs:
with:
dotnet-version: |
3.1.x
5.0.x
6.0.x
7.0.x
source-url: https://nuget.pkg.github.com/graphql-dotnet/index.json
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQLParser.ApiTests/GraphQLParser.ApiTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1" />
<PackageReference Include="PublicApiGenerator" Version="11.0.0" />
</ItemGroup>

Expand Down
5 changes: 3 additions & 2 deletions src/GraphQLParser.Tests/GraphQLParser.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
</PropertyGroup>

<PropertyGroup Condition="'$(SingleTestPlatform)' != 'true'">
<TargetFrameworks>netcoreapp3.1;net5;net6;net7</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net6;net7</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,7 +27,7 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions src/GraphQLParser.Tests/Visitors/ASTVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ public void ASTVisitor_Should_Handle_Null()
}

[Fact]
public void ASTVisitor_Should_Throw_On_Unknown_Node()
public async Task ASTVisitor_Should_Throw_On_Unknown_Node()
{
var visitor = new ASTVisitor<Context>();
var context = new Context();

var ex = Should.Throw<NotSupportedException>(() => visitor.VisitAsync(new MySuperNode(), context).GetAwaiter().GetResult());
var ex = await Should.ThrowAsync<NotSupportedException>(async () => await visitor.VisitAsync(new MySuperNode(), context));
ex.Message.ShouldBe("Unknown node 'MySuperNode'.");
}

[Fact]
public void ASTVisitor_Should_Pass_CancellationToken()
public async Task ASTVisitor_Should_Pass_CancellationToken()
{
var document = "scalar JSON".Parse();
var visitor = new MyVisitor();
using var cts = new CancellationTokenSource(500);
var context = new Context { CancellationToken = cts.Token };
context.CancellationToken.ThrowIfCancellationRequested();

Should.Throw<OperationCanceledException>(() => visitor.VisitAsync(document, context).GetAwaiter().GetResult());
await Should.ThrowAsync<OperationCanceledException>(async () => await visitor.VisitAsync(document, context));
}

private class MyVisitor : ASTVisitor<Context>
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQLParser.Tests/Visitors/CountVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task CountVisitor_Should_Count_Nodes(string text, int expectedCount

var document = text.Parse();

await visitor.VisitAsync(document, context).ConfigureAwait(false);
await visitor.VisitAsync(document, context);
context.Count.ShouldBe(expectedCount);
document.AllNestedCount().ShouldBe(expectedCount);
}
Expand All @@ -55,7 +55,7 @@ public async Task CountVisitor_Should_Count_Zero_Nodes(string text)

var document = text.Parse();

await visitor.VisitAsync(document, context).ConfigureAwait(false);
await visitor.VisitAsync(document, context);
context.Count.ShouldBe(0);
}
}
2 changes: 1 addition & 1 deletion src/GraphQLParser.Tests/Visitors/MaxDepthVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task MaxDepthVisitor_Should_Work(string text, int expectedMaxDepth)

var document = text.Parse();

await visitor.VisitAsync(document, context).ConfigureAwait(false);
await visitor.VisitAsync(document, context);
context.MaxDepth.ShouldBe(expectedMaxDepth);
document.MaxNestedDepth().ShouldBe(expectedMaxDepth);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task Printer_Should_Print_Pretty_If_Directives_Skipped(
var writer = new StringWriter();
var document = text.Parse();

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

Expand Down
10 changes: 5 additions & 5 deletions src/GraphQLParser.Tests/Visitors/SDLPrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ public async Task SDLPrinter_Should_Print_Document(
var writer = new StringWriter();
var document = text.Parse();

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

Expand Down Expand Up @@ -640,11 +640,11 @@ public async Task SDLPrinter_Should_Print_BlockStrings(int number, string input,
var document = (input + " scalar a").Parse();

var printer = new SDLPrinter();
await printer.PrintAsync(document, writer).ConfigureAwait(false);
await printer.PrintAsync(document, writer);
var renderedOriginal = writer.ToString();

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

Expand All @@ -671,7 +671,7 @@ public async Task SDLPrinter_Should_Print_EscapedStrings(string stringValue)
var document = query.Parse();

var printer = new SDLPrinter();
await printer.PrintAsync(document, writer).ConfigureAwait(false);
await printer.PrintAsync(document, writer);
var rendered = writer.ToString();
rendered.ShouldBe(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public async Task Printer_Should_Print_Pretty_If_Definitions_Skipped(
var writer = new StringWriter();
var document = text.Parse();

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

Expand Down
43 changes: 40 additions & 3 deletions src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public async Task StructurePrinter_Should_Print_Tree(string text, string expecte
{
var writer = new StringWriter();
var document = text.Parse();
await _structPrinter1.PrintAsync(document, writer).ConfigureAwait(false);
await _structPrinter1.PrintAsync(document, writer);
var actual = writer.ToString();
actual.ShouldBe(expected);
}
Expand All @@ -304,7 +304,7 @@ public async Task StructurePrinter_Should_Print_Tree_Without_Names(string text,
{
var writer = new StringWriter();
var document = text.Parse();
await _structPrinter2.PrintAsync(document, writer).ConfigureAwait(false);
await _structPrinter2.PrintAsync(document, writer);
var actual = writer.ToString();
actual.ShouldBe(expected);
}
Expand Down Expand Up @@ -550,7 +550,44 @@ public async Task StructurePrinter_Should_Print_Tree_With_Locations(string text,
var writer = new StringWriter();

var document = text.Parse(new ParserOptions { Ignore = option });
await _structPrinter3.PrintAsync(document, writer).ConfigureAwait(false);
await _structPrinter3.PrintAsync(document, writer);
var actual = writer.ToString();
actual.ShouldBe(expected);
}
}

[Theory]
[InlineData("{a}", @"Document
OperationDefinition
SelectionSet
Field
Name
", 2)]
[InlineData("{a}", @"Document
OperationDefinition
SelectionSet
Field
Name
", 3)]
[InlineData("{a}", @"Document
OperationDefinition
SelectionSet
Field
Name
", 0)]
public async Task StructurePrinter_Should_Print_Tree_With_Custom_Indentation(string text, string expected, int indentSize, bool ignoreComments = true)
{
text = text.Replace("\r\n", "\n");
foreach (var option in new[] { IgnoreOptions.None, IgnoreOptions.Comments })
{
if (option == IgnoreOptions.Comments && !ignoreComments)
continue;

var writer = new StringWriter();

var document = text.Parse(new ParserOptions { Ignore = option });
var printer = new StructurePrinter(new StructurePrinterOptions { PrintNames = false, IndentSize = indentSize });

Check failure on line 589 in src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'StructurePrinterOptions' does not contain a definition for 'IndentSize'

Check failure on line 589 in src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'StructurePrinterOptions' does not contain a definition for 'IndentSize'

Check failure on line 589 in src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'StructurePrinterOptions' does not contain a definition for 'IndentSize'

Check failure on line 589 in src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'StructurePrinterOptions' does not contain a definition for 'IndentSize'

Check failure on line 589 in src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'StructurePrinterOptions' does not contain a definition for 'IndentSize'

Check failure on line 589 in src/GraphQLParser.Tests/Visitors/StructurePrinterTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

'StructurePrinterOptions' does not contain a definition for 'IndentSize'
await printer.PrintAsync(document, writer);
var actual = writer.ToString();
actual.ShouldBe(expected);
}
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQLParser/Visitors/PrintContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static ValueTask WriteAsync<TContext>(this TContext context, ROM value)
where TContext : IPrintContext
{
var task =
#if NETSTANDARD2_0
#if NETSTANDARD2_0 || NET462
// no cancellationToken support on netstandard2.0
context.Writer.WriteAsync(value.ToString()); //ISSUE: allocation - either WriteAsync(value.ToString()) or Write(char value) in a loop
#elif NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
Expand Down
12 changes: 11 additions & 1 deletion src/GraphQLParser/Visitors/SDLPrinterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ public static string Print(this SDLPrinter printer, ASTNode node)
public static void Print(this SDLPrinter printer, ASTNode node, StringBuilder stringBuilder)
=> printer.PrintAsync(node, new StringWriter(stringBuilder), default).AsTask().GetAwaiter().GetResult();

#if !NET6_0_OR_GREATER
private static readonly Encoding _uTF8NoBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
#endif

/// <summary>
/// Prints the specified AST into the specified <see cref="MemoryStream"/> as a SDL document.
/// If no encoding is specified, the document is written in UTF-8 format without a byte order mark.
/// </summary>
public static void Print(this SDLPrinter printer, ASTNode node, MemoryStream memoryStream, Encoding? encoding = null)
{
using var streamWriter = new StreamWriter(memoryStream, encoding, -1 /* default */, true);
int bufferSize = -1;
#if !NET6_0_OR_GREATER
encoding ??= _uTF8NoBOM;
if (bufferSize == -1)
bufferSize = 1024;
#endif
using var streamWriter = new StreamWriter(memoryStream, encoding, bufferSize, true);
printer.PrintAsync(node, streamWriter, default).AsTask().GetAwaiter().GetResult();
// flush encoder state to stream
streamWriter.Flush();
Expand Down

0 comments on commit ff81872

Please sign in to comment.