From 9fa93677bf83de5af3429cbbb467662b662bc726 Mon Sep 17 00:00:00 2001 From: FantasyTeddy Date: Fri, 5 Jun 2026 14:16:49 +0200 Subject: [PATCH] Enable nullable reference type warnings --- Directory.Build.props | 1 + .../BaseExample.cs | 4 +-- .../ExampleCalibrationSteering.cs | 2 +- .../ExampleRampUp.cs | 4 +-- .../SharpBrick.PoweredUp.Examples/Program.cs | 8 +++-- .../Program.cs | 4 +-- .../BlueGigaBLEPoweredUpBluetoothAdapater.cs | 3 +- src/SharpBrick.PoweredUp.Cli/Program.cs | 14 ++++----- .../Deployment/HubExtensions.cs | 2 +- src/SharpBrick.PoweredUp/Hubs/HubFactory.cs | 13 ++++++-- src/SharpBrick.PoweredUp/PoweredUpHost.cs | 2 +- .../Protocol/Formatter/MessageEncoder.cs | 2 +- .../ILegoWirelessProtocolExtensions.cs | 2 +- .../Devices/VoltageTest.cs | 2 +- .../Formatter/HubActionsEncoderTest.cs | 5 ++-- .../Protocol/Formatter/HubAlertEncoderTest.cs | 9 +++--- .../Formatter/HubAttachedIOEncoderTest.cs | 22 +++++++------- .../Formatter/HubPropertiesEncoderTest.cs | 9 +++--- .../Formatter/PortInformationEncoderTest.cs | 30 ++++++++++--------- .../PortInputFormatCombinedModeEncoderTest.cs | 11 +++---- .../PortInputFormatSingleEncoderTest.cs | 11 +++---- .../PortValueCombinedModeEncoderTest.cs | 5 ++-- .../Formatter/PortValueEncoderTest.cs | 5 ++-- .../Program.cs | 2 +- .../TestScriptExecutionContext.cs | 2 +- 25 files changed, 99 insertions(+), 75 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index f7ed530..bf41b95 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -8,5 +8,6 @@ MIT true $(NoWarn);CS1591 + warnings \ No newline at end of file diff --git a/examples/SharpBrick.PoweredUp.Examples/BaseExample.cs b/examples/SharpBrick.PoweredUp.Examples/BaseExample.cs index 280eb6c..99f4df1 100644 --- a/examples/SharpBrick.PoweredUp.Examples/BaseExample.cs +++ b/examples/SharpBrick.PoweredUp.Examples/BaseExample.cs @@ -31,7 +31,7 @@ public async Task InitExampleAndDiscoverAsync(IServiceProvider serviceProvider, Host = serviceProvider.GetService(); - Log = serviceProvider.GetService().CreateLogger("Example"); + Log = serviceProvider.GetRequiredService().CreateLogger("Example"); var enableTrace = bool.TryParse(configuration["EnableTrace"], out var x) && x; @@ -49,7 +49,7 @@ public virtual Task DiscoverAsync(bool enableTrace) // add this when you are interested in a tracing of the message ("human readable") if (enableTrace) { - var tracer = hub.ServiceProvider.GetService(); + var tracer = hub.ServiceProvider.GetRequiredService(); await tracer.ExecuteAsync(); } diff --git a/examples/SharpBrick.PoweredUp.Examples/ExampleCalibrationSteering.cs b/examples/SharpBrick.PoweredUp.Examples/ExampleCalibrationSteering.cs index 49f9ee2..d5773c1 100644 --- a/examples/SharpBrick.PoweredUp.Examples/ExampleCalibrationSteering.cs +++ b/examples/SharpBrick.PoweredUp.Examples/ExampleCalibrationSteering.cs @@ -14,7 +14,7 @@ public override async Task ExecuteAsync() var motor = technicMediumHub.A.GetDevice(); - var calibration = ServiceProvider.GetService(); + var calibration = ServiceProvider.GetRequiredService(); await calibration.ExecuteAsync(motor); await technicMediumHub.WaitButtonClickAsync(); diff --git a/examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs b/examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs index 7614b9d..6064bfa 100644 --- a/examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs +++ b/examples/SharpBrick.PoweredUp.Examples/ExampleRampUp.cs @@ -18,7 +18,7 @@ public override async Task ExecuteAsync() var motor = technicMediumHub.A.GetDevice(); // ramp up with linear speed - var rampUp = ServiceProvider.GetService(); + var rampUp = ServiceProvider.GetRequiredService(); await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red); @@ -33,7 +33,7 @@ public override async Task ExecuteAsync() await technicMediumHub.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Orange); // ramp down with linear speed - var rampDown = ServiceProvider.GetService(); + var rampDown = ServiceProvider.GetRequiredService(); var beforeOrangePhase = stopWatch.ElapsedMilliseconds; await rampDown.ExecuteAsync(motor, 100, 0, 100, 20_000); diff --git a/examples/SharpBrick.PoweredUp.Examples/Program.cs b/examples/SharpBrick.PoweredUp.Examples/Program.cs index aae6e40..9c1413d 100644 --- a/examples/SharpBrick.PoweredUp.Examples/Program.cs +++ b/examples/SharpBrick.PoweredUp.Examples/Program.cs @@ -35,7 +35,7 @@ static async Task Main(string[] args) var typeToExecute = Assembly .GetExecutingAssembly() .GetTypes() - .FirstOrDefault(x => x.FullName.StartsWith($"Example.Example{exampleToExecute}")); + .FirstOrDefault(x => x.FullName?.StartsWith($"Example.Example{exampleToExecute}") == true); if (typeToExecute is null) { @@ -45,7 +45,11 @@ static async Task Main(string[] args) } var example = Activator.CreateInstance(typeToExecute) as Example.BaseExample; - + if (example is null) + { + Console.WriteLine("Could not create example instance."); + return; + } // (2) build the DI container var serviceCollection = new ServiceCollection(); diff --git a/examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs b/examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs index d932a5b..b6cda9d 100644 --- a/examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs +++ b/examples/SharpBrick.PoweredUp.ExampplesOnProtocol/Program.cs @@ -15,8 +15,8 @@ .BuildServiceProvider(); // getting utilities -var bt = serviceProvider.GetService(); -var host = serviceProvider.GetService(); +var bt = serviceProvider.GetRequiredService(); +var host = serviceProvider.GetRequiredService(); // discover a LWP bluetooth device var tcs = new TaskCompletionSource(); diff --git a/src/SharpBrick.PoweredUp.BlueGigaBLE/BlueGigaBLEPoweredUpBluetoothAdapater.cs b/src/SharpBrick.PoweredUp.BlueGigaBLE/BlueGigaBLEPoweredUpBluetoothAdapater.cs index e3d70d9..6418a68 100644 --- a/src/SharpBrick.PoweredUp.BlueGigaBLE/BlueGigaBLEPoweredUpBluetoothAdapater.cs +++ b/src/SharpBrick.PoweredUp.BlueGigaBLE/BlueGigaBLEPoweredUpBluetoothAdapater.cs @@ -85,8 +85,7 @@ async void GAPScanResponseEvent(object sender, BleScanResponseReceivedEventArgs //we only want to discover Lego-devices //filter for correct service-GUID for LEGO-Hub, containing manufacturerData and the name var addressNumber = BlueGigaBLEHelper.ByteArrayToUlong(e.Address); - var bleAdvertisingListFound = bleParsedData.TryGetValue(addressNumber, out var actualListAdvertisingData); - if (!bleAdvertisingListFound) + if (!bleParsedData.TryGetValue(addressNumber, out var actualListAdvertisingData)) { actualListAdvertisingData = bleParsedData.AddOrUpdate(addressNumber, new List(), (oldkey, oldvalue) => oldvalue = new List()); } diff --git a/src/SharpBrick.PoweredUp.Cli/Program.cs b/src/SharpBrick.PoweredUp.Cli/Program.cs index 42bccbd..bf6656a 100644 --- a/src/SharpBrick.PoweredUp.Cli/Program.cs +++ b/src/SharpBrick.PoweredUp.Cli/Program.cs @@ -86,9 +86,9 @@ static async Task Main(string[] args) await AddTraceWriterAsync(scopedServiceProvider, enableTrace); - scopedServiceProvider.GetService().BluetoothDeviceInfo = bluetoothDeviceInfo; + scopedServiceProvider.GetRequiredService().BluetoothDeviceInfo = bluetoothDeviceInfo; - var deviceListCli = scopedServiceProvider.GetService(); // ServiceLocator ok: transient factory + var deviceListCli = scopedServiceProvider.GetRequiredService(); // ServiceLocator ok: transient factory await deviceListCli.ExecuteAsync(systemType); } @@ -138,11 +138,11 @@ static async Task Main(string[] args) await AddTraceWriterAsync(scopedServiceProvider, enableTrace); - scopedServiceProvider.GetService().BluetoothDeviceInfo = bluetoothDeviceInfo; + scopedServiceProvider.GetRequiredService().BluetoothDeviceInfo = bluetoothDeviceInfo; - var dumpStaticPortInfoCommand = scopedServiceProvider.GetService(); // ServiceLocator ok: transient factory + var dumpStaticPortInfoCommand = scopedServiceProvider.GetRequiredService(); // ServiceLocator ok: transient factory - var port = byte.Parse(portOption.Value()); + var port = byte.TryParse(portOption.Value(), out var parsedPort) ? parsedPort : (byte)0; await dumpStaticPortInfoCommand.ExecuteAsync(systemType, port, headerEnabled); } @@ -193,7 +193,7 @@ static async Task Main(string[] args) await AddTraceWriterAsync(scopedServiceProvider, enableTrace); - var prettyPrintCommand = scopedServiceProvider.GetService(); // ServiceLocator ok: transient factory + var prettyPrintCommand = scopedServiceProvider.GetRequiredService(); // ServiceLocator ok: transient factory TextReader reader = Console.In; @@ -273,7 +273,7 @@ public static async Task AddTraceWriterAsync(IServiceProvider serviceProvider, b { if (enableTrace) { - var traceMessages = serviceProvider.GetService(); // ServiceLocator ok: transient factory + var traceMessages = serviceProvider.GetRequiredService(); // ServiceLocator ok: transient factory await traceMessages.ExecuteAsync(); } diff --git a/src/SharpBrick.PoweredUp/Deployment/HubExtensions.cs b/src/SharpBrick.PoweredUp/Deployment/HubExtensions.cs index 8388468..4b23fc3 100644 --- a/src/SharpBrick.PoweredUp/Deployment/HubExtensions.cs +++ b/src/SharpBrick.PoweredUp/Deployment/HubExtensions.cs @@ -105,7 +105,7 @@ private static Action LogErrors(Hub self) { return errors => { - var logger = self.ServiceProvider.GetService().CreateLogger(); + var logger = self.ServiceProvider.GetRequiredService().CreateLogger(); if (errors.Length > 0) { diff --git a/src/SharpBrick.PoweredUp/Hubs/HubFactory.cs b/src/SharpBrick.PoweredUp/Hubs/HubFactory.cs index ffb46ac..039990c 100644 --- a/src/SharpBrick.PoweredUp/Hubs/HubFactory.cs +++ b/src/SharpBrick.PoweredUp/Hubs/HubFactory.cs @@ -14,7 +14,16 @@ public HubFactory(IServiceProvider serviceProvider) public Hub CreateByBluetoothManufacturerData(byte[] manufacturerData) { - var hub = (manufacturerData is null || manufacturerData.Length < 3) ? null : Create(GetTypeFromSystemType(GetSystemTypeFromManufacturerData((PoweredUpHubManufacturerData)manufacturerData[1]))); + if (manufacturerData is null) + { + throw new ArgumentNullException(nameof(manufacturerData)); + } + if (manufacturerData.Length < 3) + { + throw new ArgumentException("Manufacturer data must be at least 3 bytes long.", nameof(manufacturerData)); + } + + var hub = Create(GetTypeFromSystemType(GetSystemTypeFromManufacturerData((PoweredUpHubManufacturerData)manufacturerData[1]))); hub.Configure(0x00); return hub; @@ -22,7 +31,7 @@ public Hub CreateByBluetoothManufacturerData(byte[] manufacturerData) public THub Create() where THub : Hub { - var hub = Create(typeof(THub)) as THub; + var hub = (THub)Create(typeof(THub)); hub.Configure(0x00); return hub; diff --git a/src/SharpBrick.PoweredUp/PoweredUpHost.cs b/src/SharpBrick.PoweredUp/PoweredUpHost.cs index 851b364..d1209eb 100644 --- a/src/SharpBrick.PoweredUp/PoweredUpHost.cs +++ b/src/SharpBrick.PoweredUp/PoweredUpHost.cs @@ -123,7 +123,7 @@ public IServiceScope CreateProtocolScope(IPoweredUpBluetoothDeviceInfo bluetooth var scopedServiceProvider = scope.ServiceProvider; // initialize scoped bluetooth kernel to bluetooth address. - var kernel = scopedServiceProvider.GetService(); + var kernel = scopedServiceProvider.GetRequiredService(); kernel.BluetoothDeviceInfo = bluetoothDeviceInfo; return scope; diff --git a/src/SharpBrick.PoweredUp/Protocol/Formatter/MessageEncoder.cs b/src/SharpBrick.PoweredUp/Protocol/Formatter/MessageEncoder.cs index adf6de9..1d46257 100644 --- a/src/SharpBrick.PoweredUp/Protocol/Formatter/MessageEncoder.cs +++ b/src/SharpBrick.PoweredUp/Protocol/Formatter/MessageEncoder.cs @@ -83,7 +83,7 @@ public static LegoWirelessMessage Decode(in Span data, ProtocolKnowledge k if (encoder is not null) { - var message = encoder?.Decode(hubId, content); + var message = encoder.Decode(hubId, content); message.Length = length; message.HubId = hubId; diff --git a/src/SharpBrick.PoweredUp/Protocol/ILegoWirelessProtocolExtensions.cs b/src/SharpBrick.PoweredUp/Protocol/ILegoWirelessProtocolExtensions.cs index 20c4e3e..9da9bc2 100644 --- a/src/SharpBrick.PoweredUp/Protocol/ILegoWirelessProtocolExtensions.cs +++ b/src/SharpBrick.PoweredUp/Protocol/ILegoWirelessProtocolExtensions.cs @@ -29,6 +29,6 @@ public static async Task SendPortOutputCommandAsync(this ILegoWire var response = await self.SendMessageReceiveResultAsync(message, msg => msg.Feedbacks.Any(f => f.PortId == portId)); - return response.Feedbacks.FirstOrDefault(f => f.PortId == portId).Feedback; + return response.Feedbacks.First(f => f.PortId == portId).Feedback; } } diff --git a/test/SharpBrick.PoweredUp.Test/Devices/VoltageTest.cs b/test/SharpBrick.PoweredUp.Test/Devices/VoltageTest.cs index 806998c..d43ae3c 100644 --- a/test/SharpBrick.PoweredUp.Test/Devices/VoltageTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Devices/VoltageTest.cs @@ -76,7 +76,7 @@ await mock.WriteUpstreamAsync(new PortInputFormatSingleMessage(0x20, 1, 0, true) var poweredUpBluetoothAdapterMock = serviceProvider.GetMockBluetoothAdapter(); - var protocol = serviceProvider.GetService(); + var protocol = serviceProvider.GetRequiredService(); protocol.ConnectAsync(knownSystemType).Wait(); diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubActionsEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubActionsEncoderTest.cs index d3e3b2e..51aec51 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubActionsEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubActionsEncoderTest.cs @@ -46,9 +46,10 @@ public void HubActionsEncoder_Decode(HubAction expectedAction, string dataAsStri var data = BytesStringUtil.StringToData(dataAsString); // act - var message = MessageEncoder.Decode(data, null) as HubActionMessage; + var message = MessageEncoder.Decode(data, null); // assert - Assert.Equal(expectedAction, message.Action); + var hubActionMessage = Assert.IsType(message); + Assert.Equal(expectedAction, hubActionMessage.Action); } } diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAlertEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAlertEncoderTest.cs index 3c0f154..4bd9fb5 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAlertEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAlertEncoderTest.cs @@ -42,11 +42,12 @@ public void HubAlertEncoder_Decode(HubAlert expectedAlert, HubAlertOperation exp var data = BytesStringUtil.StringToData(dataAsString); // act - var message = MessageEncoder.Decode(data, null) as HubAlertMessage; + var message = MessageEncoder.Decode(data, null); // assert - Assert.Equal(expectedAlert, message.Alert); - Assert.Equal(expectedOperation, message.Operation); - Assert.Equal(expectedPayload, message.DownstreamPayload); + var hubAlertMessage = Assert.IsType(message); + Assert.Equal(expectedAlert, hubAlertMessage.Alert); + Assert.Equal(expectedOperation, hubAlertMessage.Operation); + Assert.Equal(expectedPayload, hubAlertMessage.DownstreamPayload); } } diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs index ba1a0c4..ac8cf50 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubAttachedIOEncoderTest.cs @@ -23,13 +23,14 @@ public void HubAttachedIOEncoder_Decode_Attached(string messageAsString, Devi var data = BytesStringUtil.StringToData(messageAsString).AsSpan()[3..]; // act - var message = new HubAttachedIOEncoder().Decode(0x00, data) as HubAttachedIOForAttachedDeviceMessage; + var message = new HubAttachedIOEncoder().Decode(0x00, data); // assert - Assert.Equal(expectedPortId, message.PortId); - Assert.Equal(expectedType, message.IOTypeId); - Assert.Equal(new Version(expectedHwVersion), message.HardwareRevision); - Assert.Equal(new Version(expectedSwVersion), message.SoftwareRevision); + var hubAttachedIOForAttachedDeviceMessage = Assert.IsType(message); + Assert.Equal(expectedPortId, hubAttachedIOForAttachedDeviceMessage.PortId); + Assert.Equal(expectedType, hubAttachedIOForAttachedDeviceMessage.IOTypeId); + Assert.Equal(new Version(expectedHwVersion), hubAttachedIOForAttachedDeviceMessage.HardwareRevision); + Assert.Equal(new Version(expectedSwVersion), hubAttachedIOForAttachedDeviceMessage.SoftwareRevision); // reverse test var reverseMessage = new HubAttachedIOForAttachedDeviceMessage(expectedPortId, expectedType, Version.Parse(expectedHwVersion), Version.Parse(expectedSwVersion)); @@ -50,12 +51,13 @@ public void HubAttachedIOEncoder_Decode_AttachedVirutalIO(string messageAsString var data = BytesStringUtil.StringToData(messageAsString).AsSpan()[3..]; // act - var message = new HubAttachedIOEncoder().Decode(0x00, data) as HubAttachedIOForAttachedVirtualDeviceMessage; + var message = new HubAttachedIOEncoder().Decode(0x00, data); // assert - Assert.Equal(expectedPortId, message.PortId); - Assert.Equal(expectedType, message.IOTypeId); - Assert.Equal(portA, message.PortAId); - Assert.Equal(portB, message.PortBId); + var hubAttachedIOForAttachedVirtualDeviceMessage = Assert.IsType(message); + Assert.Equal(expectedPortId, hubAttachedIOForAttachedVirtualDeviceMessage.PortId); + Assert.Equal(expectedType, hubAttachedIOForAttachedVirtualDeviceMessage.IOTypeId); + Assert.Equal(portA, hubAttachedIOForAttachedVirtualDeviceMessage.PortAId); + Assert.Equal(portB, hubAttachedIOForAttachedVirtualDeviceMessage.PortBId); } } diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubPropertiesEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubPropertiesEncoderTest.cs index b85d449..7d746ba 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubPropertiesEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/HubPropertiesEncoderTest.cs @@ -25,12 +25,13 @@ public void HubPropertiesEncoder_Decode_UpdateUpstream(string messageAsString var data = BytesStringUtil.StringToData(messageAsString).AsSpan()[3..]; // act - var message = new HubPropertiesEncoder().Decode(0x00, data) as HubPropertyMessage; + var message = new HubPropertiesEncoder().Decode(0x00, data); // assert - Assert.Equal(expectedProperty, message.Property); - Assert.Equal(expectedPropertyOperation, message.Operation); - Assert.Equal(payload, message.Payload); + var hubPropertyMessage = Assert.IsType>(message); + Assert.Equal(expectedProperty, hubPropertyMessage.Property); + Assert.Equal(expectedPropertyOperation, hubPropertyMessage.Operation); + Assert.Equal(payload, hubPropertyMessage.Payload); } [Theory] diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInformationEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInformationEncoderTest.cs index a4c1194..a5983b2 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInformationEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInformationEncoderTest.cs @@ -18,18 +18,19 @@ public void PortInformationEncoder_Decode_PortInformationForModeInfoMessage(byte var data = BytesStringUtil.StringToData(dataAsString); // act - var message = MessageEncoder.Decode(data, null) as PortInformationForModeInfoMessage; + var message = MessageEncoder.Decode(data, null); // assert - Assert.Equal(expectedPort, message.PortId); - Assert.Equal(expectedType, message.InformationType); - Assert.Equal(expectedOutput, message.OutputCapability); - Assert.Equal(expectedInput, message.InputCapability); - Assert.Equal(expectedLogicalCombinable, message.LogicalCombinableCapability); - Assert.Equal(expectedLogicalSynchronizable, message.LogicalSynchronizableCapability); - Assert.Equal(expectedTotalCount, message.TotalModeCount); - Assert.Equal(expectedInputModes, message.InputModes); - Assert.Equal(expectedOutputModes, message.OutputModes); + var portInformationForModeInfoMessage = Assert.IsType(message); + Assert.Equal(expectedPort, portInformationForModeInfoMessage.PortId); + Assert.Equal(expectedType, portInformationForModeInfoMessage.InformationType); + Assert.Equal(expectedOutput, portInformationForModeInfoMessage.OutputCapability); + Assert.Equal(expectedInput, portInformationForModeInfoMessage.InputCapability); + Assert.Equal(expectedLogicalCombinable, portInformationForModeInfoMessage.LogicalCombinableCapability); + Assert.Equal(expectedLogicalSynchronizable, portInformationForModeInfoMessage.LogicalSynchronizableCapability); + Assert.Equal(expectedTotalCount, portInformationForModeInfoMessage.TotalModeCount); + Assert.Equal(expectedInputModes, portInformationForModeInfoMessage.InputModes); + Assert.Equal(expectedOutputModes, portInformationForModeInfoMessage.OutputModes); } @@ -43,11 +44,12 @@ public void PortInformationEncoder_Decode_PortInformationForPossibleModeCombinat var data = BytesStringUtil.StringToData(dataAsString); // act - var message = MessageEncoder.Decode(data, null) as PortInformationForPossibleModeCombinationsMessage; + var message = MessageEncoder.Decode(data, null); // assert - Assert.Equal(expectedPort, message.PortId); - Assert.Equal(expectedType, message.InformationType); - Assert.Collection(message.ModeCombinations, expectedCombinations.Select>(combination => { return (item) => Assert.Equal(combination, item); }).ToArray()); + var portInformationForPossibleModeCombinationsMessage = Assert.IsType(message); + Assert.Equal(expectedPort, portInformationForPossibleModeCombinationsMessage.PortId); + Assert.Equal(expectedType, portInformationForPossibleModeCombinationsMessage.InformationType); + Assert.Collection(portInformationForPossibleModeCombinationsMessage.ModeCombinations, expectedCombinations.Select>(combination => { return (item) => Assert.Equal(combination, item); }).ToArray()); } } diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInputFormatCombinedModeEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInputFormatCombinedModeEncoderTest.cs index 3927994..74be05c 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInputFormatCombinedModeEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInputFormatCombinedModeEncoderTest.cs @@ -16,12 +16,13 @@ public void PortInputFormatCombinedModeEncoder_Decode(string dataAsString, byte var data = BytesStringUtil.StringToData(dataAsString); // act - var message = MessageEncoder.Decode(data, null) as PortInputFormatCombinedModeMessage; + var message = MessageEncoder.Decode(data, null); // assert - Assert.Equal(expectedPortId, message.PortId); - Assert.Equal(expectedCombinationIndex, message.UsedCombinationIndex); - Assert.Equal(expectedEnabled, message.MultiUpdateEnabled); - Assert.Collection(message.ConfiguredModeDataSetIndex, expectedIndexes.Select>(exp => (act) => Assert.Equal(exp, act)).ToArray()); + var portInputFormatCombinedModeMessage = Assert.IsType(message); + Assert.Equal(expectedPortId, portInputFormatCombinedModeMessage.PortId); + Assert.Equal(expectedCombinationIndex, portInputFormatCombinedModeMessage.UsedCombinationIndex); + Assert.Equal(expectedEnabled, portInputFormatCombinedModeMessage.MultiUpdateEnabled); + Assert.Collection(portInputFormatCombinedModeMessage.ConfiguredModeDataSetIndex, expectedIndexes.Select>(exp => (act) => Assert.Equal(exp, act)).ToArray()); } } diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInputFormatSingleEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInputFormatSingleEncoderTest.cs index 6b48d42..8757c59 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInputFormatSingleEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortInputFormatSingleEncoderTest.cs @@ -14,13 +14,14 @@ public void PortInputFormatSingleEncoder_Decode(string dataAsString, byte expect var data = BytesStringUtil.StringToData(dataAsString); // act - var message = MessageEncoder.Decode(data, null) as PortInputFormatSingleMessage; + var message = MessageEncoder.Decode(data, null); // assert - Assert.Equal(expectedPortId, message.PortId); - Assert.Equal(expectedMode, message.ModeIndex); - Assert.Equal(expectedDeltaInterval, message.DeltaInterval); - Assert.Equal(expectedNotificationEnabled, message.NotificationEnabled); + var portInputFormatSingleMessage = Assert.IsType(message); + Assert.Equal(expectedPortId, portInputFormatSingleMessage.PortId); + Assert.Equal(expectedMode, portInputFormatSingleMessage.ModeIndex); + Assert.Equal(expectedDeltaInterval, portInputFormatSingleMessage.DeltaInterval); + Assert.Equal(expectedNotificationEnabled, portInputFormatSingleMessage.NotificationEnabled); // reverse var reverseMessage = new PortInputFormatSingleMessage(expectedPortId, expectedMode, expectedDeltaInterval, expectedNotificationEnabled) diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortValueCombinedModeEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortValueCombinedModeEncoderTest.cs index a18e01d..6857477 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortValueCombinedModeEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortValueCombinedModeEncoderTest.cs @@ -39,8 +39,9 @@ public void PortValueCombinedModeEncoder_Decode(string dataAsString, byte expect var message = MessageEncoder.Decode(data, knowledge) as PortValueCombinedModeMessage; // assert - Assert.Equal(expectedPortId, message.PortId); - Assert.Collection(message.Data, Enumerable.Range(0, expectedModes.Length).Select>(pos => portValueData => + var portValueCombinedModeMessage = Assert.IsType(message); + Assert.Equal(expectedPortId, portValueCombinedModeMessage.PortId); + Assert.Collection(portValueCombinedModeMessage.Data, Enumerable.Range(0, expectedModes.Length).Select>(pos => portValueData => { Assert.Equal(expectedPortId, portValueData.PortId); Assert.Equal(expectedDataType[pos], portValueData.DataType); diff --git a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortValueEncoderTest.cs b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortValueEncoderTest.cs index 59b28e1..e5a726f 100644 --- a/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortValueEncoderTest.cs +++ b/test/SharpBrick.PoweredUp.Test/Protocol/Formatter/PortValueEncoderTest.cs @@ -31,10 +31,11 @@ private void PortValueSingleEncoder_Decode(string dat var data = BytesStringUtil.StringToData(dataAsString); // act - var message = MessageEncoder.Decode(data, knowledge) as PortValueSingleMessage; + var message = MessageEncoder.Decode(data, knowledge); // assert - Assert.Collection(message.Data, + var portValueSingleMessage = Assert.IsType(message); + Assert.Collection(portValueSingleMessage.Data, d => { Assert.Equal(d.PortId, expectedPortId); diff --git a/test/SharpBrick.PoweredUp.TestScript/Program.cs b/test/SharpBrick.PoweredUp.TestScript/Program.cs index 61753ad..73507e0 100644 --- a/test/SharpBrick.PoweredUp.TestScript/Program.cs +++ b/test/SharpBrick.PoweredUp.TestScript/Program.cs @@ -45,7 +45,7 @@ static async Task Main(string[] args) var serviceProvider = serviceCollection.BuildServiceProvider(); - var host = serviceProvider.GetService(); + var host = serviceProvider.GetRequiredService(); IEnumerable scripts = new ITestScript[] { new TechnicMotorTestScript(), diff --git a/test/SharpBrick.PoweredUp.TestScript/TestScriptExecutionContext.cs b/test/SharpBrick.PoweredUp.TestScript/TestScriptExecutionContext.cs index 07ba81f..83abd61 100644 --- a/test/SharpBrick.PoweredUp.TestScript/TestScriptExecutionContext.cs +++ b/test/SharpBrick.PoweredUp.TestScript/TestScriptExecutionContext.cs @@ -18,7 +18,7 @@ public Task ConfirmAsync(string question) { Console.Write(question + " (Y/n)"); - var confirm = Console.ReadLine().Trim().ToUpperInvariant() != "N"; + var confirm = Console.ReadLine()?.Trim().ToUpperInvariant() != "N"; if (confirm) {