-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix saving empty delays and flags in meta.json
- Loading branch information
1 parent
c5c3f00
commit 679f37d
Showing
5 changed files
with
254 additions
and
8 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
Importer.Tests/RSI/State/RsiStateTests.cs → Importer.Tests/RSI/RsiDimensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Importer.Directions; | ||
using Importer.RSI; | ||
using NUnit.Framework; | ||
|
||
namespace Importer.Tests.RSI | ||
{ | ||
[TestFixture] | ||
[TestOf(typeof(Rsi))] | ||
public class RsiSaveTest | ||
{ | ||
[Test] | ||
public async Task EmptyIgnoredTest() | ||
{ | ||
var state = new RsiState | ||
{ | ||
Delays = new List<List<float>>(), | ||
Flags = new Dictionary<string, object>() | ||
}; | ||
var states = new List<RsiState>() {state}; | ||
|
||
Assert.IsEmpty(states[0].Delays); | ||
Assert.IsEmpty(states[0].Flags); | ||
|
||
var rsi = new Rsi(states: states); | ||
var stream = new MemoryStream(); | ||
|
||
await rsi.SaveRsiToStream(stream); | ||
var json = await new StreamReader(stream).ReadToEndAsync(); | ||
await stream.DisposeAsync(); | ||
|
||
Assert.That(json, Is.Not.Empty); | ||
Assert.That(json, Does.Not.Contain("delays")); | ||
Assert.That(json, Does.Not.Contain("flags")); | ||
} | ||
|
||
[Test] | ||
public async Task NotEmptySerializedTest() | ||
{ | ||
var state = new RsiState | ||
{ | ||
Delays = new List<List<float>> {new() {5}}, | ||
Flags = new Dictionary<string, object> {["Key"] = "Value"} | ||
}; | ||
var states = new List<RsiState>() {state}; | ||
|
||
Assert.IsNotEmpty(states[0].Delays); | ||
Assert.IsNotEmpty(states[0].Flags); | ||
|
||
var rsi = new Rsi(states: states); | ||
var stream = new MemoryStream(); | ||
|
||
await rsi.SaveRsiToStream(stream); | ||
var json = await new StreamReader(stream).ReadToEndAsync(); | ||
await stream.DisposeAsync(); | ||
|
||
Assert.That(json, Is.Not.Empty); | ||
|
||
Assert.That(json, Does.Contain("delays")); | ||
Assert.That(json, Does.Contain("5")); | ||
|
||
Assert.That(json, Does.Contain("flags")); | ||
Assert.That(json, Does.Contain("Key")); | ||
Assert.That(json, Does.Contain("Value")); | ||
} | ||
|
||
[Test] | ||
public async Task ConsistencyTest() | ||
{ | ||
var state = new RsiState | ||
{ | ||
Name = "RsiState", | ||
Directions = DirectionType.Diagonal, | ||
Delays = new List<List<float>> {new() {5}}, | ||
Flags = new Dictionary<string, object> {["Key"] = "Value"}, | ||
}; | ||
var states = new List<RsiState>() {state}; | ||
|
||
Assert.IsNotEmpty(states[0].Delays); | ||
Assert.IsNotEmpty(states[0].Flags); | ||
|
||
var rsi = new Rsi(x: 42, y: 42) | ||
{ | ||
Version = 5, | ||
License = "License", | ||
Copyright = "Copyright", | ||
States = states | ||
}; | ||
var stream = new MemoryStream(); | ||
|
||
await rsi.SaveRsiToStream(stream); | ||
rsi = await Rsi.FromMetaJson(stream); | ||
await stream.DisposeAsync(); | ||
|
||
Assert.That(rsi.Version, Is.EqualTo(5)); | ||
Assert.That(rsi.License, Is.EqualTo("License")); | ||
Assert.That(rsi.Copyright, Is.EqualTo("Copyright")); | ||
Assert.That(rsi.Size, Is.EqualTo(new RsiSize(42, 42))); | ||
|
||
Assert.That(rsi.States.Count, Is.EqualTo(1)); | ||
|
||
state = rsi.States[0]; | ||
Assert.That(state.Name, Is.EqualTo("RsiState")); | ||
Assert.That(state.Directions, Is.EqualTo(DirectionType.Diagonal)); | ||
|
||
Assert.NotNull(state.Delays); | ||
Assert.That(state.Delays.Count, Is.EqualTo(1)); | ||
Assert.That(state.Delays[0].Count, Is.EqualTo(1)); | ||
Assert.That(state.Delays[0][0], Is.EqualTo(5)); | ||
|
||
Assert.NotNull(state.Flags); | ||
Assert.That(state.Flags.Count, Is.EqualTo(1)); | ||
Assert.That(state.Flags["Key"].ToString(), Is.EqualTo("Value")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Importer.RSI | ||
{ | ||
public class RsiStateConverter : JsonConverter<RsiState> | ||
{ | ||
public static readonly RsiStateConverter Instance = new(); | ||
|
||
public override RsiState Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, RsiState value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
writer.WriteString("name", value.Name); | ||
writer.WriteNumber("directions", (int) value.Directions); | ||
|
||
if (value.Delays is {Count: > 0}) | ||
{ | ||
writer.WriteStartArray("delays"); | ||
|
||
foreach (var delays in value.Delays) | ||
{ | ||
writer.WriteStartArray(); | ||
|
||
foreach (var delay in delays) | ||
{ | ||
writer.WriteNumberValue(delay); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
|
||
writer.WriteEndArray(); | ||
} | ||
|
||
if (value.Flags is {Count: > 0}) | ||
{ | ||
writer.WriteStartObject("flags"); | ||
|
||
foreach (var kvPair in value.Flags) | ||
{ | ||
writer.WriteString(kvPair.Key.ToString()!, kvPair.Value.ToString()); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |