forked from imurvai/brickcontroller2
-
Notifications
You must be signed in to change notification settings - Fork 4
Add multiple datagram payload tests with mocks and helpers #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
J0EK3R
wants to merge
16
commits into
vicocz:default
Choose a base branch
from
J0EK3R:merge/vicocz/UnitTests
base: default
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a6899ef
Add MK6 datagram payload tests with mocks and helpers
e8986b8
Potential fix for pull request finding
J0EK3R 75fc848
Fix payload index in assertion for expected identifier
cf23423
Shorten AppIdentifier to 2 bytes in tests
eab933f
Refactor tests to access TryGetTelegram directly
c4eddfc
Refactor MK6 datagram tests into base and derived classes
18be5bc
Refactor tests to use named constants and improve coverage
7827694
Add MK5 datagram unit tests with xUnit and FluentAssertions
348dfc9
Add MK4 datagram tests and refactor array initializers
88488ef
Add unit tests for MK3_8 datagram logic; update constructor
0950ab2
Refactor to use IJieStarDeviceManager interface in DI
003e4d5
Expand test access, add SCM4 datagram unit tests
64a9180
Add JieStarSCM8Datagram unit tests with edge cases
5b52765
Centralize telegram reset logic in device initialization
36c5539
Refactor test mocks to base classes, clean up usings
951b7fc
Add unit tests for command datagram instance interaction
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
BrickController2/BrickController2.Tests/DeviceManagement/JieStar/JieStarDatagramTestsBase.cs
This file contains hidden or 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,37 @@ | ||
| using BrickController2.DeviceManagement; | ||
| using BrickController2.DeviceManagement.JieStar; | ||
| using BrickController2.PlatformServices.BluetoothLE; | ||
| using Moq; | ||
|
|
||
| namespace BrickController2.Tests.DeviceManagement.JieStar; | ||
|
|
||
| public abstract class JieStarDatagramTestsBase | ||
| { | ||
| protected const byte AppIdentifier1 = 0x61; // This is the first byte of an randomly choosen AppIdentifier for UnitTesting | ||
| protected const byte AppIdentifier2 = 0x62; // This is the second byte of an randomly choosen AppIdentifier for UnitTesting | ||
|
|
||
| protected static readonly byte[] AppIdentifier = [AppIdentifier1, AppIdentifier2]; | ||
|
|
||
| /// <summary> | ||
| /// This class is a test implementation of the IJieStarPlatformService interface that simulates the behavior of the TryGetRfPayload method for testing purposes. | ||
| /// It always returns true and sets the rfPayload to the rawData provided. | ||
| /// </summary> | ||
| protected class TestJieStarPlatformService : IJieStarPlatformService | ||
| { | ||
| public bool TryGetRfPayload(byte ctxValue2, byte[] rawData, out byte[] rfPayload) | ||
| { | ||
| rfPayload = rawData; | ||
| return true; // Simulate success | ||
| } | ||
| } | ||
|
|
||
| protected readonly Mock<IBluetoothLEService> _bluetoothLEService = new(MockBehavior.Strict); | ||
| protected readonly Mock<IJieStarDeviceManager> _manager = new(MockBehavior.Strict); | ||
| protected readonly TestJieStarPlatformService _jieStarPlatformService = new(); | ||
| internal readonly Mock<IDeviceRepository> _deviceRepository = new(MockBehavior.Strict); | ||
|
|
||
| protected JieStarDatagramTestsBase() | ||
| { | ||
| _manager.Setup(x => x.GetAppId()).Returns(AppIdentifier); | ||
| } | ||
| } |
This file contains hidden or 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
186 changes: 186 additions & 0 deletions
186
BrickController2/BrickController2.Tests/DeviceManagement/JieStar/JieStarSCM4DatagramTests.cs
This file contains hidden or 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,186 @@ | ||
| using BrickController2.DeviceManagement.JieStar; | ||
| using FluentAssertions; | ||
| using System; | ||
| using Xunit; | ||
|
|
||
| namespace BrickController2.Tests.DeviceManagement.JieStar; | ||
|
|
||
| public sealed class JieStarSCM4DatagramTests : JieStarDatagramTestsBase | ||
| { | ||
| private const byte PayloadIdentifierConnect1 = 0xa4; | ||
| private const byte PayloadIdentifierConnect2 = 0x5b; | ||
| private const byte PayloadIdentifierCommand1 = 0x40; | ||
| private const byte PayloadIdentifierCommand2 = 0xbf; | ||
|
|
||
| /// <summary> | ||
| /// This test checks that the payload identifiers are correctly set in the connect datagram for each device address. | ||
| /// </summary> | ||
| /// <param name="deviceAddress">The address of the device to test.</param> | ||
| [Theory] | ||
| [InlineData(JieStarSCM4.Device1)] | ||
| [InlineData(JieStarSCM4.Device2)] | ||
| [InlineData(JieStarSCM4.Device3)] | ||
| public void TryGetTelegram_ConnectDatagram_PayloadIdentifier(string deviceAddress) | ||
| { | ||
| JieStarSCM4 device = new JieStarSCM4("JieStarSCM4", deviceAddress, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
|
|
||
| device.TryGetTelegram(true, out byte[] payload).Should().BeTrue(); | ||
| payload[0].Should().Be(PayloadIdentifierConnect1); | ||
| payload[7].Should().Be(PayloadIdentifierConnect2); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This test checks that the AppIdentifier is correctly included in the connect datagram payload for each device address. | ||
| /// </summary> | ||
| /// <param name="deviceAddress">The address of the device to test.</param> | ||
| [Theory] | ||
| [InlineData(JieStarSCM4.Device1)] | ||
| [InlineData(JieStarSCM4.Device2)] | ||
| [InlineData(JieStarSCM4.Device3)] | ||
| public void TryGetTelegram_ConnectDatagram_AppIdentifier(string deviceAddress) | ||
| { | ||
| JieStarSCM4 device = new JieStarSCM4("JieStarSCM4", deviceAddress, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
|
|
||
| device.TryGetTelegram(true, out byte[] payload).Should().BeTrue(); | ||
| payload[1].Should().Be(AppIdentifier1); | ||
| payload[2].Should().Be(AppIdentifier2); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This test checks that the payload identifiers are correctly set in the command datagram for each device address. | ||
| /// </summary> | ||
| /// <param name="deviceAddress">The address of the device to test.</param> | ||
| [Theory] | ||
| [InlineData(JieStarSCM4.Device1)] | ||
| [InlineData(JieStarSCM4.Device2)] | ||
| [InlineData(JieStarSCM4.Device3)] | ||
| public void TryGetTelegram_CommandDatagram_PayloadIdentifier(string deviceAddress) | ||
| { | ||
| JieStarSCM4 device = new JieStarSCM4("JieStarSCM4", deviceAddress, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
|
|
||
| device.TryGetTelegram(false, out byte[] payload).Should().BeTrue(); | ||
| payload[0].Should().Be(PayloadIdentifierCommand1); | ||
| payload[7].Should().Be(PayloadIdentifierCommand2); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This test checks that the AppIdentifier is correctly included in the command datagram payload for each device address. | ||
| /// </summary> | ||
| /// <param name="deviceAddress">The address of the device to test.</param> | ||
| [Theory] | ||
| [InlineData(JieStarSCM4.Device1)] | ||
| [InlineData(JieStarSCM4.Device2)] | ||
| [InlineData(JieStarSCM4.Device3)] | ||
| public void TryGetTelegram_CommandDatagram_AppIdentifier(string deviceAddress) | ||
| { | ||
| JieStarSCM4 device = new JieStarSCM4("JieStarSCM4", deviceAddress, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
|
|
||
| device.TryGetTelegram(false, out byte[] payload).Should().BeTrue(); | ||
| payload[1].Should().Be(AppIdentifier1); | ||
| payload[2].Should().Be(AppIdentifier2); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This test checks that the command datagram payload is correctly constructed based on the set output values for each device address. | ||
| /// </summary> | ||
| /// <param name="deviceAddress">The address of the device to test.</param> | ||
| /// <param name="setValues">The set values for each channel.</param> | ||
| /// <param name="expectedPayload">The expected payload for the command datagram.</param> | ||
| [Theory] | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0x00, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 1.0f, 0.0f, 0.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0xf0, 0x00, 0x80, 0x80, PayloadIdentifierCommand2 })] // channel 1 maximum, others neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 0.0f, 1.0f, 0.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x0f, 0x00, 0x80, 0x80, PayloadIdentifierCommand2 })] // channel 2 maximum, others neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 0.0f, 0.0f, 1.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0xf0, 0x80, 0x80, PayloadIdentifierCommand2 })] // channel 3 maximum, others neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 0.0f, 0.0f, 0.0f, 1.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0x0f, 0x80, 0x80, PayloadIdentifierCommand2 })] // channel 4 maximum, others neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { -1.0f, 0.0f, 0.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x70, 0x00, 0x80, 0x80, PayloadIdentifierCommand2 })] // channel 1 minimum, others neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 0.0f, -1.0f, 0.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x07, 0x00, 0x80, 0x80, PayloadIdentifierCommand2 })] // channel 2 minimum, others neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 0.0f, 0.0f, -1.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0x70, 0x80, 0x80, PayloadIdentifierCommand2 })] // channel 3 minimum, others neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 0.0f, 0.0f, 0.0f, -1.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0x07, 0x80, 0x80, PayloadIdentifierCommand2 })] // channel 4 minimum, others neutral | ||
| [InlineData(JieStarSCM4.Device1, new float[] { 9.0f, 9.0f, 9.0f, 9.0f, }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0xff, 0xff, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels above maximum, should be clamped to maximum | ||
| [InlineData(JieStarSCM4.Device1, new float[] { -9.0f, -9.0f, -9.0f, -9.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x77, 0x77, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels below minimum, should be clamped to minimum | ||
|
|
||
| [InlineData(JieStarSCM4.Device2, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0x00, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels neutral | ||
| [InlineData(JieStarSCM4.Device2, new float[] { 9.0f, 9.0f, 9.0f, 9.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0xff, 0xff, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels above maximum, should be clamped to maximum | ||
| [InlineData(JieStarSCM4.Device2, new float[] { -9.0f, -9.0f, -9.0f, -9.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x77, 0x77, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels below minimum, should be clamped to minimum | ||
|
|
||
| [InlineData(JieStarSCM4.Device3, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0x00, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels neutral | ||
| [InlineData(JieStarSCM4.Device3, new float[] { 9.0f, 9.0f, 9.0f, 9.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0xff, 0xff, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels above maximum, should be clamped to maximum | ||
| [InlineData(JieStarSCM4.Device3, new float[] { -9.0f, -9.0f, -9.0f, -9.0f }, new byte[] { PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x77, 0x77, 0x80, 0x80, PayloadIdentifierCommand2 })] // all channels below minimum, should be clamped to minimum | ||
| public void TryGetTelegram_CommandDatagram_Payload(string deviceAddress, float[] setValues, byte[] expectedPayload) | ||
| { | ||
| JieStarSCM4 device = new JieStarSCM4("JieStarSCM4", deviceAddress, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
|
|
||
| // Set the output values for the device | ||
| for (int i = 0; i < setValues.Length; i++) | ||
| { | ||
| device.SetOutput(i, setValues[i]); | ||
| } | ||
|
|
||
| // Get the command datagram payload | ||
| device.TryGetTelegram(false, out byte[] payload).Should().BeTrue(); | ||
|
|
||
| // Check that the payload matches the expected values | ||
| for (int i = 0; i < expectedPayload.Length; i++) | ||
| { | ||
| payload[i].Should().Be(expectedPayload[i]); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that setting an illegal channel index throws an ArgumentOutOfRangeException. | ||
| /// </summary> | ||
| /// <param name="deviceAddress">The address of the device to test.</param> | ||
| [Theory] | ||
| [InlineData(JieStarSCM4.Device1)] | ||
| [InlineData(JieStarSCM4.Device2)] | ||
| [InlineData(JieStarSCM4.Device3)] | ||
| public void TryGetTelegram_CommandDatagram_SetIllegalChannel(string deviceAddress) | ||
| { | ||
| JieStarSCM4 device = new JieStarSCM4("JieStarSCM4", deviceAddress, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
|
|
||
| Action action = () => device.SetOutput(device.NumberOfChannels, 0); | ||
|
|
||
| action.Should().Throw<ArgumentOutOfRangeException>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This test checks that the command datagram payload is correctly constructed based on the set output values for multiple devices, ensuring that each device's payload is independent of the others. | ||
| /// </summary> | ||
| [Fact] | ||
| public void TryGetTelegram_CommandDatagram_InstanceInteraction() | ||
| { | ||
| float[] setValues_1 = [1.0f, 1.0f, 1.0f, 1.0f]; | ||
| byte[] expectedPayload_1 = [PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0xff, 0xff, 0x80, 0x80, PayloadIdentifierCommand2]; | ||
|
|
||
| float[] setValues_2 = [0.0f, 0.0f, 0.0f, 0.0f]; | ||
| byte[] expectedPayload_2 = [PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0x00, 0x80, 0x80, PayloadIdentifierCommand2]; | ||
|
|
||
| float[] setValues_3 = [0.0f, 0.0f, 0.0f, 0.0f]; | ||
| byte[] expectedPayload_3 = [PayloadIdentifierCommand1, AppIdentifier1, AppIdentifier2, 0x00, 0x00, 0x80, 0x80, PayloadIdentifierCommand2]; | ||
|
|
||
| JieStarSCM4 device1 = new JieStarSCM4("JieStarSCM4", JieStarSCM4.Device1, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
| JieStarSCM4 device2 = new JieStarSCM4("JieStarSCM4", JieStarSCM4.Device2, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
| JieStarSCM4 device3 = new JieStarSCM4("JieStarSCM4", JieStarSCM4.Device3, [], _deviceRepository.Object, _bluetoothLEService.Object, _jieStarPlatformService, _manager.Object); | ||
|
|
||
| // Set the output values for the device | ||
| for (int i = 0; i < setValues_1.Length; i++) | ||
| { | ||
| device1.SetOutput(i, setValues_1[i]); | ||
| device2.SetOutput(i, setValues_2[i]); | ||
| device3.SetOutput(i, setValues_3[i]); | ||
| } | ||
|
|
||
| // Get the command datagram payload | ||
| device1.TryGetTelegram(false, out byte[] payload1).Should().BeTrue(); | ||
| device2.TryGetTelegram(false, out byte[] payload2).Should().BeTrue(); | ||
| device3.TryGetTelegram(false, out byte[] payload3).Should().BeTrue(); | ||
|
|
||
| // Check that the payload matches the expected values | ||
| for (int i = 0; i < expectedPayload_1.Length; i++) | ||
| { | ||
| payload1[i].Should().Be(expectedPayload_1[i]); | ||
| payload2[i].Should().Be(expectedPayload_2[i]); | ||
| payload3[i].Should().Be(expectedPayload_3[i]); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.