Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ on:
jobs:
build-and-test:
runs-on: windows-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8
# If preinstalled in windows-latest, this step can be skipped
# - name: Setup .NET ${{ matrix.dotnet-version }}
# uses: actions/setup-dotnet@v4
# with:
# dotnet-version: ${{ matrix.dotnet-version }}

- name: Build
run: dotnet build --configuration Release
Expand Down
67 changes: 64 additions & 3 deletions TouchSenderInterpreter.Test/InterpreterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ internal class TestDataGenerator
static readonly DeviceInfo FullDeviceInfo = new(Width: 1920, Height: 1080);
static readonly SingleTouch FullSingleTouch = new(X: 0.5, Y: 0.5);
static readonly TouchSenderPayload FullPayload = new(ExampleId, FullDeviceInfo, FullSingleTouch);

/// <summary>
/// Generate payloads with full data
/// </summary>
public static IEnumerable<object[]> FullPayloads()
{
yield return new object[]
Expand All @@ -22,7 +26,9 @@ public static IEnumerable<object[]> FullPayloads()
};
}

// Generate payloads with null values
/// <summary>
/// Generate payloads with some null values
/// </summary>
public static IEnumerable<object[]> PayloadsWithSingleTouchNull()
{
yield return new object[]
Expand All @@ -31,7 +37,9 @@ public static IEnumerable<object[]> PayloadsWithSingleTouchNull()
};
}

// Generate payloads with different values
/// <summary>
/// Generate payloads with different values
/// </summary>
public static IEnumerable<object[]> DifferenctPayloads()
{
var payload1 = new TouchSenderPayload(ExampleId, FullDeviceInfo, FullSingleTouch);
Expand All @@ -44,7 +52,9 @@ payload1 with {
};
}

// Generate payloads with same values
/// <summary>
/// Generate payloads with same values but different references
/// </summary>
public static IEnumerable<object[]> SamePayloads()
{
var payload1 = new TouchSenderPayload(ExampleId, FullDeviceInfo, FullSingleTouch);
Expand All @@ -54,9 +64,14 @@ public static IEnumerable<object[]> SamePayloads()
yield return new object[] { payload1, payload1 with { } };
}
}

// Inject ITestOutputHelper to write logs
public class InterpreterTest(ITestOutputHelper output)
{

/// <summary>
/// Test the Read method with a valid full JSON payload
/// </summary>
[Theory]
[MemberData(nameof(TestDataGenerator.FullPayloads), MemberType = typeof(TestDataGenerator))]
public void Read_ValidFullJson_ReturnsSuccessResult(TouchSenderPayload payload)
Expand All @@ -77,6 +92,9 @@ public void Read_ValidFullJson_ReturnsSuccessResult(TouchSenderPayload payload)
Assert.Equal(payload, result.Payload);
}

/// <summary>
/// Test the Read method with a valid JSON payload with null values
/// </summary>
[Theory]
[MemberData(nameof(TestDataGenerator.PayloadsWithSingleTouchNull), MemberType = typeof(TestDataGenerator))]
public void Read_ValidNullJson_ReturnsSuccessResult(TouchSenderPayload payload)
Expand All @@ -96,6 +114,10 @@ public void Read_ValidNullJson_ReturnsSuccessResult(TouchSenderPayload payload)
Assert.NotNull(result.Payload);
Assert.Equal(payload, result.Payload);
}

/// <summary>
/// Test the Read method with an invalid JSON payload
/// </summary>
[Theory]
[InlineData("invaid json")] // no closing brace
[InlineData("{")] // missing closing brace
Expand All @@ -116,6 +138,42 @@ public void Read_InvalidJson_ReturnsFailureResult(string invalidJson)
Assert.NotNull(result.ErrorMessage);
}

/// <summary>
/// Test the Read method with an empty JSON payload.
///
/// for .NET 9.0 or greater, it should return a failure result
/// for .NET 8.0 or lower, it should return a success result with null payload
/// </summary>
[Theory]
[InlineData("{}")]
[InlineData("{\"Id\":0,\"DeviceInfo\":null,\"SingleTouch\":null}")]
public void Read_EmptyJson_ReturnsUnsucceededResult(string invalidJson)
{
// Log
output.WriteLine(invalidJson);

// Arrange
var input = Encoding.UTF8.GetBytes(invalidJson);

// Act
var result = Interpreter.Read(input);
output.WriteLine(result.ToString());

// Assert
#if NET9_0_OR_GREATER
Assert.False(result.IsSuccess);
Assert.Null(result.Payload);
Assert.NotNull(result.ErrorMessage);
#else
Assert.True(result.IsSuccess);
Assert.NotNull(result.Payload);
Assert.Null(result.ErrorMessage);
#endif
}

/// <summary>
/// Test the Equals method with different payloads
/// </summary>
[Theory]
[MemberData(nameof(TestDataGenerator.DifferenctPayloads), MemberType = typeof(TestDataGenerator))]
public void Equals_DifferentPayloads_ReturnsFalse(TouchSenderPayload payload1, TouchSenderPayload payload2)
Expand All @@ -127,6 +185,9 @@ public void Equals_DifferentPayloads_ReturnsFalse(TouchSenderPayload payload1, T
Assert.False(result);
}

/// <summary>
/// Test the Equals method with same payloads
/// </summary>
[Theory]
[MemberData(nameof(TestDataGenerator.SamePayloads), MemberType = typeof(TestDataGenerator))]
public void Equals_SamePayloads_ReturnsTrue(TouchSenderPayload payload1, TouchSenderPayload payload2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
6 changes: 5 additions & 1 deletion TouchSenderInterpreter/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public class Interpreter

private static readonly JsonSerializerOptions _options = new()
{
PropertyNameCaseInsensitive = true
PropertyNameCaseInsensitive = true,
#if NET9_0_OR_GREATER
RespectNullableAnnotations = true,
RespectRequiredConstructorParameters = true,
#endif
};

public static TouchSenderResult Read(byte[] input)
Expand Down
2 changes: 1 addition & 1 deletion TouchSenderInterpreter/TouchSenderInterpreter.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Copyright>Copyright (c) Voltaney 2025</Copyright>
Expand Down
Loading