Skip to content

Commit

Permalink
Manually write JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Aug 10, 2023
1 parent ebc4c16 commit c7c1e84
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
18 changes: 0 additions & 18 deletions src/Exercism.TestRunner.CSharp/TestRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,19 @@ internal enum TestStatus

internal class TestResult
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("status")]
public TestStatus Status { get; set; }

[JsonPropertyName("task_id")]
public int? TaskId { get; set; }

[JsonPropertyName("message")]
public string Message { get; set; }

[JsonPropertyName("output")]
public string Output { get; set; }

[JsonPropertyName("test_code")]
public string TestCode { get; set; }
}

internal class TestRun
{
[JsonPropertyName("version")]
public int Version { get; set; } = 3;

[JsonPropertyName("status")]
public TestStatus Status { get; set; }

[JsonPropertyName("message")]
public string Message { get; set; }

[JsonPropertyName("tests")]
public TestResult[] Tests { get; set; }
}
}
61 changes: 45 additions & 16 deletions src/Exercism.TestRunner.CSharp/TestRunWriter.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Exercism.TestRunner.CSharp
{
internal static class TestRunWriter
{
public static void WriteToFile(this TestRun testRun, Options options) =>
File.WriteAllText(options.ResultsJsonPath(), testRun.ToJson());
private static readonly JsonWriterOptions JsonWriterOptions = new() {Indented = true};

private static string ResultsJsonPath(this Options options) =>
Path.GetFullPath(Path.Combine(options.OutputDirectory, "results.json"));

private static string ToJson(this TestRun testRun) =>
JsonSerializer.Serialize(testRun, CreateJsonSerializerOptions());

private static JsonSerializerOptions CreateJsonSerializerOptions()
public static void WriteToFile(this TestRun testRun, Options options)
{
using var fileStream = File.Create(options.ResultsJsonPath());
using var jsonWriter = new Utf8JsonWriter(fileStream, JsonWriterOptions);
jsonWriter.WriteStartObject();
jsonWriter.WriteNumber("version", testRun.Version);
jsonWriter.WriteString("status", testRun.Status.ToString().ToLower());
jsonWriter.WriteStringIfNotEmpty("message", testRun.Message);
jsonWriter.WriteTestResults(testRun.Tests);
jsonWriter.WriteEndObject();
jsonWriter.Flush();
fileStream.WriteByte((byte)'\n');
}

private static void WriteTestResults(this Utf8JsonWriter jsonWriter, TestResult[] testResults)
{
var options = new JsonSerializerOptions
if (testResults.Length == 0)
return;

jsonWriter.WriteStartArray("tests");

foreach (var testResult in testResults)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true,
};
options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
jsonWriter.WriteStartObject();
jsonWriter.WriteString("name", testResult.Name);
jsonWriter.WriteString("status", testResult.Status.ToString().ToLower());
if (testResult.TaskId.HasValue)
jsonWriter.WriteNumber("task_id", testResult.TaskId.Value);
jsonWriter.WriteStringIfNotEmpty("message", testResult.Message);
jsonWriter.WriteStringIfNotEmpty("output", testResult.Output);
jsonWriter.WriteStringIfNotEmpty("test_code", testResult.TestCode);
jsonWriter.WriteEndObject();
}

jsonWriter.WriteEndArray();
}

return options;
private static void WriteStringIfNotEmpty(this Utf8JsonWriter jsonWriter, string property, string value)
{
if (string.IsNullOrWhiteSpace(value))
return;

jsonWriter.WriteString(property, value);
}


private static string ResultsJsonPath(this Options options) =>
Path.GetFullPath(Path.Combine(options.OutputDirectory, "results.json"));
}
}

0 comments on commit c7c1e84

Please sign in to comment.