tests: add converter tests
This commit is contained in:
@@ -4,6 +4,8 @@ public class DiscordEventConverterTests
|
|||||||
{
|
{
|
||||||
private readonly JsonSerializerOptions _options = new()
|
private readonly JsonSerializerOptions _options = new()
|
||||||
{
|
{
|
||||||
|
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
Converters =
|
Converters =
|
||||||
{
|
{
|
||||||
new DiscordEventConverter()
|
new DiscordEventConverter()
|
||||||
@@ -12,36 +14,31 @@ public class DiscordEventConverterTests
|
|||||||
private readonly Type _discordEventType = typeof(DiscordEvent);
|
private readonly Type _discordEventType = typeof(DiscordEvent);
|
||||||
private readonly DiscordEventConverter _converter = new();
|
private readonly DiscordEventConverter _converter = new();
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public void Read_WhenCalledWithUnknownOpCode_ItShouldReturnDiscordEvent()
|
[MemberData(nameof(TestData))]
|
||||||
|
public void Read_WhenCalledWithOpCode_ItShouldReturnDiscordEvent(object data, Type expectedType)
|
||||||
{
|
{
|
||||||
var data = new
|
|
||||||
{
|
|
||||||
op = 99,
|
|
||||||
s = null as int?,
|
|
||||||
t = null as string,
|
|
||||||
d = null as object
|
|
||||||
};
|
|
||||||
|
|
||||||
var result = Read(data);
|
var result = Read(data);
|
||||||
|
|
||||||
result.Should().BeOfType<DiscordEvent>();
|
result.Should().BeOfType(expectedType);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Read_WhenCalledWithDispatchOpCodeAndNoType_ItShouldReturnDispatchEvent()
|
public void Write_WhenCalledWithDiscordEvent_ItShouldReturnJson()
|
||||||
{
|
{
|
||||||
var data = new
|
var discordEvent = new DiscordEvent
|
||||||
{
|
{
|
||||||
op = DiscordOpCodes.Dispatch,
|
OpCode = DiscordOpCodes.Dispatch,
|
||||||
s = null as int?,
|
Sequence = null,
|
||||||
t = null as string,
|
Type = null,
|
||||||
d = null as object
|
Data = null
|
||||||
};
|
};
|
||||||
|
|
||||||
var result = Read(data);
|
var result = JsonSerializer.Serialize(discordEvent, _options);
|
||||||
|
|
||||||
result.Should().BeOfType<DiscordEvent>();
|
var expectedJson = /*lang=json,strict*/ "{\"op\":0,\"s\":null,\"t\":null,\"d\":null}";
|
||||||
|
|
||||||
|
result.Should().Be(expectedJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DiscordEvent? Read(object data)
|
private DiscordEvent? Read(object data)
|
||||||
@@ -51,4 +48,58 @@ public class DiscordEventConverterTests
|
|||||||
var reader = new Utf8JsonReader(utf8Json);
|
var reader = new Utf8JsonReader(utf8Json);
|
||||||
return _converter.Read(ref reader, _discordEventType, _options);
|
return _converter.Read(ref reader, _discordEventType, _options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static TheoryData<object, Type> TestData => new()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new
|
||||||
|
{
|
||||||
|
op = DiscordOpCodes.Hello,
|
||||||
|
s = null as int?,
|
||||||
|
t = null as string,
|
||||||
|
d = null as object,
|
||||||
|
},
|
||||||
|
typeof(HelloDiscordEvent)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
new
|
||||||
|
{
|
||||||
|
op = DiscordOpCodes.Dispatch,
|
||||||
|
s = null as int?,
|
||||||
|
t = null as string,
|
||||||
|
d = null as object
|
||||||
|
},
|
||||||
|
typeof(DiscordEvent)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
new
|
||||||
|
{
|
||||||
|
op = DiscordOpCodes.Dispatch,
|
||||||
|
s = null as int?,
|
||||||
|
t = DiscordEventTypes.Ready,
|
||||||
|
d = null as object
|
||||||
|
},
|
||||||
|
typeof(ReadyDiscordEvent)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
new
|
||||||
|
{
|
||||||
|
op = DiscordOpCodes.HeartbeatAck,
|
||||||
|
s = null as int?,
|
||||||
|
t = null as string,
|
||||||
|
d = null as object
|
||||||
|
},
|
||||||
|
typeof(HeartbeatAckDiscordEvent)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
new
|
||||||
|
{
|
||||||
|
op = -1,
|
||||||
|
s = null as int?,
|
||||||
|
t = null as string,
|
||||||
|
d = null as object
|
||||||
|
},
|
||||||
|
typeof(DiscordEvent)
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
global using System.Net.WebSockets;
|
global using System.Net.WebSockets;
|
||||||
global using System.Text;
|
global using System.Text;
|
||||||
global using System.Text.Json;
|
global using System.Text.Json;
|
||||||
|
global using System.Text.Json.Serialization;
|
||||||
|
|
||||||
global using Microsoft.AspNetCore.Builder;
|
global using Microsoft.AspNetCore.Builder;
|
||||||
global using Microsoft.AspNetCore.Hosting;
|
global using Microsoft.AspNetCore.Hosting;
|
||||||
|
|||||||
@@ -2,12 +2,6 @@ namespace StevesBot.Worker.Discord.Events;
|
|||||||
|
|
||||||
internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
||||||
{
|
{
|
||||||
private readonly JsonSerializerOptions _jsonSerializerOptions = new()
|
|
||||||
{
|
|
||||||
ReferenceHandler = ReferenceHandler.IgnoreCycles,
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
};
|
|
||||||
|
|
||||||
private const string OpPropertyName = "op";
|
private const string OpPropertyName = "op";
|
||||||
|
|
||||||
public override DiscordEvent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override DiscordEvent? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
@@ -15,29 +9,46 @@ internal sealed class DiscordEventConverter : JsonConverter<DiscordEvent>
|
|||||||
using var jsonDoc = JsonDocument.ParseValue(ref reader);
|
using var jsonDoc = JsonDocument.ParseValue(ref reader);
|
||||||
var root = jsonDoc.RootElement;
|
var root = jsonDoc.RootElement;
|
||||||
var op = root.GetProperty(OpPropertyName).GetInt32();
|
var op = root.GetProperty(OpPropertyName).GetInt32();
|
||||||
|
var modifiedOptions = CopyAndRemoveConverter(options);
|
||||||
|
|
||||||
return op switch
|
return op switch
|
||||||
{
|
{
|
||||||
DiscordOpCodes.Dispatch => DeserializeDispatchEvent(root, options),
|
DiscordOpCodes.Dispatch => DeserializeDispatchEvent(root, options, modifiedOptions),
|
||||||
DiscordOpCodes.Hello => JsonSerializer.Deserialize<HelloDiscordEvent>(root.GetRawText(), options),
|
DiscordOpCodes.Hello => JsonSerializer.Deserialize<HelloDiscordEvent>(root.GetRawText(), options),
|
||||||
DiscordOpCodes.HeartbeatAck => JsonSerializer.Deserialize<HeartbeatAckDiscordEvent>(root.GetRawText(), options),
|
DiscordOpCodes.HeartbeatAck => JsonSerializer.Deserialize<HeartbeatAckDiscordEvent>(root.GetRawText(), options),
|
||||||
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), _jsonSerializerOptions),
|
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), modifiedOptions),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Write(Utf8JsonWriter writer, DiscordEvent value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, DiscordEvent value, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
JsonSerializer.Serialize(writer, value, value.GetType(), options);
|
var modifiedOptions = CopyAndRemoveConverter(options);
|
||||||
|
JsonSerializer.Serialize(writer, value, value.GetType(), modifiedOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DiscordEvent? DeserializeDispatchEvent(JsonElement root, JsonSerializerOptions options)
|
private static DiscordEvent? DeserializeDispatchEvent(JsonElement root, JsonSerializerOptions options, JsonSerializerOptions modifiedOptions)
|
||||||
{
|
{
|
||||||
var type = root.GetProperty("t").GetString();
|
var type = root.GetProperty("t").GetString();
|
||||||
|
|
||||||
return type switch
|
return type switch
|
||||||
{
|
{
|
||||||
DiscordEventTypes.Ready => JsonSerializer.Deserialize<ReadyDiscordEvent>(root.GetRawText(), options),
|
DiscordEventTypes.Ready => JsonSerializer.Deserialize<ReadyDiscordEvent>(root.GetRawText(), options),
|
||||||
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), _jsonSerializerOptions),
|
_ => JsonSerializer.Deserialize<DiscordEvent>(root.GetRawText(), modifiedOptions),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static JsonSerializerOptions CopyAndRemoveConverter(JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
var newOptions = new JsonSerializerOptions(options);
|
||||||
|
|
||||||
|
foreach (var converter in options.Converters)
|
||||||
|
{
|
||||||
|
if (converter is DiscordEventConverter)
|
||||||
|
{
|
||||||
|
newOptions.Converters.Remove(converter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newOptions;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user