Skip to content

Commit 7a2adb3

Browse files
authored
Add SDLPrinter synchronous extension methods (#277)
1 parent c8f57ff commit 7a2adb3

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/GraphQLParser.ApiTests/GraphQLParser.approved.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,12 @@ namespace GraphQLParser.Visitors
884884
public System.IO.TextWriter Writer { get; }
885885
}
886886
}
887+
public static class SDLPrinterExtensions
888+
{
889+
public static string Print(this GraphQLParser.Visitors.SDLPrinter printer, GraphQLParser.AST.ASTNode node) { }
890+
public static void Print(this GraphQLParser.Visitors.SDLPrinter printer, GraphQLParser.AST.ASTNode node, System.Text.StringBuilder stringBuilder) { }
891+
public static void Print(this GraphQLParser.Visitors.SDLPrinter printer, GraphQLParser.AST.ASTNode node, System.IO.MemoryStream memoryStream, System.Text.Encoding? encoding = null) { }
892+
}
887893
public class SDLPrinterOptions
888894
{
889895
public SDLPrinterOptions() { }

src/GraphQLParser.Tests/Visitors/SDLPrinterTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@ namespace GraphQLParser.Tests.Visitors;
55

66
public class SDLPrinterTests
77
{
8+
[Fact]
9+
public async Task Print_Matches_PrintAsync_String()
10+
{
11+
var query = "KitchenSink".ReadGraphQLFile().Parse();
12+
var writer = new SDLPrinter();
13+
var sw = new StringWriter();
14+
await writer.PrintAsync(query, sw);
15+
sw.Flush();
16+
var txt = sw.ToString();
17+
writer.Print(query).ShouldBe(txt);
18+
}
19+
20+
[Fact]
21+
public async Task Print_Matches_PrintAsync_Bytes()
22+
{
23+
var query = "KitchenSink".ReadGraphQLFile().Parse();
24+
var writer = new SDLPrinter();
25+
var ms1 = new MemoryStream();
26+
var sw = new StreamWriter(ms1);
27+
await writer.PrintAsync(query, sw);
28+
sw.Flush();
29+
30+
var ms2 = new MemoryStream();
31+
writer.Print(query, ms2);
32+
var bytes1 = ms1.ToArray();
33+
var bytes2 = ms2.ToArray();
34+
bytes1.ShouldBe(bytes2);
35+
}
36+
837
[Fact]
938
public void SDLPrinter_Should_Have_Default_Options()
1039
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Text;
2+
using GraphQLParser.AST;
3+
4+
namespace GraphQLParser.Visitors;
5+
6+
/// <summary>
7+
/// Extension methods for <see cref="SDLPrinter"/>.
8+
/// </summary>
9+
public static class SDLPrinterExtensions
10+
{
11+
/// <summary>
12+
/// Returns the specified AST printed as a SDL document.
13+
/// </summary>
14+
public static string Print(this SDLPrinter printer, ASTNode node)
15+
{
16+
var sb = new StringBuilder();
17+
printer.Print(node, sb);
18+
return sb.ToString();
19+
}
20+
21+
/// <summary>
22+
/// Prints the specified AST into the specified <see cref="StringBuilder"/> as a SDL document.
23+
/// </summary>
24+
public static void Print(this SDLPrinter printer, ASTNode node, StringBuilder stringBuilder)
25+
#pragma warning disable CA2012 // Use ValueTasks correctly
26+
=> printer.PrintAsync(node, new StringWriter(stringBuilder), default).GetAwaiter().GetResult();
27+
#pragma warning restore CA2012 // Use ValueTasks correctly
28+
29+
/// <summary>
30+
/// Prints the specified AST into the specified <see cref="MemoryStream"/> as a SDL document.
31+
/// If no encoding is specified, the document is written in UTF-8 format without a byte order mark.
32+
/// </summary>
33+
public static void Print(this SDLPrinter printer, ASTNode node, MemoryStream memoryStream, Encoding? encoding = null)
34+
{
35+
using var streamWriter = new StreamWriter(memoryStream, encoding, -1 /* default */, true);
36+
#pragma warning disable CA2012 // Use ValueTasks correctly
37+
printer.PrintAsync(node, streamWriter, default).GetAwaiter().GetResult();
38+
#pragma warning restore CA2012 // Use ValueTasks correctly
39+
// flush encoder state to stream
40+
streamWriter.Flush();
41+
}
42+
}

0 commit comments

Comments
 (0)