Skip to content

feat: add null-safe TryFromValue signature and tests - #577

Open
teesofttech wants to merge 3 commits into
ardalis:mainfrom
teesofttech:main
Open

teesofttech wants to merge 3 commits into
ardalis:mainfrom
teesofttech:main

Conversation

@teesofttech

Copy link
Copy Markdown

feat: add null-safe TryFromValue signature and tests

Copilot AI review requested due to automatic review settings July 4, 2026 17:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the SmartEnum<TEnum, TValue>.TryFromValue API to be null-safe and adds unit tests covering TryFromValue behavior for both int-backed and string-backed smart enums.

Changes:

  • Update TryFromValue to accept nullable input (TValue?) and avoid exceptions on null input.
  • Add new unit tests validating TryFromValue success/failure cases for value and string enums.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/SmartEnum/SmartEnum.cs Adjusts TryFromValue signature/implementation to handle nullable inputs.
test/SmartEnum.UnitTests/SmartEnumTryFromValue.cs Adds tests exercising TryFromValue behavior for matching/non-matching values and null string input.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/SmartEnum/SmartEnum.cs Outdated
Comment thread test/SmartEnum.UnitTests/SmartEnumTryFromValue.cs
@teesofttech

Copy link
Copy Markdown
Author

@ardalis Can you review this PR please

@ardalis

ardalis commented Jul 6, 2026

Copy link
Copy Markdown
Owner

Looks like it doesn't work with netstandard and has some other things needed to change based on the broken build:

Warning: /home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnumNameConverterTests.cs(112,9): warning S125: Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125) [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnum.SystemTextJson.UnitTests.csproj::TargetFramework=net8.0]
Warning: /home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnumValueConverterTests.cs(133,9): warning S125: Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125) [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnum.SystemTextJson.UnitTests.csproj::TargetFramework=net8.0]
Warning: /home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs(68,32): warning S3887: Use an immutable collection or reduce the accessibility of the non-private readonly field 'Instance'. (https://rules.sonarsource.com/csharp/RSPEC-3887) [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnum.SystemTextJson.UnitTests.csproj::TargetFramework=net8.0]
Warning: /home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs(74,32): warning S3887: Use an immutable collection or reduce the accessibility of the non-private readonly field 'Instance'. (https://rules.sonarsource.com/csharp/RSPEC-3887) [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnum.SystemTextJson.UnitTests.csproj::TargetFramework=net8.0]
Warning: /usr/share/dotnet/sdk/10.0.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(308,5): warning NETSDK1206: Found version-specific or distribution-specific runtime identifier(s): alpine-arm, alpine-arm64, alpine-x64. Affected libraries: SQLitePCLRaw.lib.e_sqlite3. In .NET 8.0 and higher, assets for version-specific and distribution-specific runtime identifiers will not be found by default. See https://aka.ms/dotnet/rid-usage for details. [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.EFCore.IntegrationTests/SmartEnum.EFCore.IntegrationTests.csproj::TargetFramework=net8.0]
Error: /home/runner/work/SmartEnum/SmartEnum/src/SmartEnum/SmartEnum.cs(274,57): error CS0246: The type or namespace name 'NotNullWhenAttribute' could not be found (are you missing a using directive or an assembly reference?) [/home/runner/work/SmartEnum/SmartEnum/src/SmartEnum/SmartEnum.csproj::TargetFramework=netstandard2.0]
Error: /home/runner/work/SmartEnum/SmartEnum/src/SmartEnum/SmartEnum.cs(274,57): error CS0246: The type or namespace name 'NotNullWhen' could not be found (are you missing a using directive or an assembly reference?) [/home/runner/work/SmartEnum/SmartEnum/src/SmartEnum/SmartEnum.csproj::TargetFramework=netstandard2.0]
222 Warning(s)
2 Error(s)

@teesofttech

Copy link
Copy Markdown
Author

Looks like it doesn't work with netstandard and has some other things needed to change based on the broken build:

Warning: /home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnumNameConverterTests.cs(112,9): warning S125: Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125) [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnum.SystemTextJson.UnitTests.csproj::TargetFramework=net8.0] Warning: /home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnumValueConverterTests.cs(133,9): warning S125: Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125) [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnum.SystemTextJson.UnitTests.csproj::TargetFramework=net8.0] Warning: /home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs(68,32): warning S3887: Use an immutable collection or reduce the accessibility of the non-private readonly field 'Instance'. (https://rules.sonarsource.com/csharp/RSPEC-3887) [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnum.SystemTextJson.UnitTests.csproj::TargetFramework=net8.0] Warning: /home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/TestEnums.cs(74,32): warning S3887: Use an immutable collection or reduce the accessibility of the non-private readonly field 'Instance'. (https://rules.sonarsource.com/csharp/RSPEC-3887) [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.SystemTextJson.UnitTests/SmartEnum.SystemTextJson.UnitTests.csproj::TargetFramework=net8.0] Warning: /usr/share/dotnet/sdk/10.0.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(308,5): warning NETSDK1206: Found version-specific or distribution-specific runtime identifier(s): alpine-arm, alpine-arm64, alpine-x64. Affected libraries: SQLitePCLRaw.lib.e_sqlite3. In .NET 8.0 and higher, assets for version-specific and distribution-specific runtime identifiers will not be found by default. See https://aka.ms/dotnet/rid-usage for details. [/home/runner/work/SmartEnum/SmartEnum/test/SmartEnum.EFCore.IntegrationTests/SmartEnum.EFCore.IntegrationTests.csproj::TargetFramework=net8.0] Error: /home/runner/work/SmartEnum/SmartEnum/src/SmartEnum/SmartEnum.cs(274,57): error CS0246: The type or namespace name 'NotNullWhenAttribute' could not be found (are you missing a using directive or an assembly reference?) [/home/runner/work/SmartEnum/SmartEnum/src/SmartEnum/SmartEnum.csproj::TargetFramework=netstandard2.0] Error: /home/runner/work/SmartEnum/SmartEnum/src/SmartEnum/SmartEnum.cs(274,57): error CS0246: The type or namespace name 'NotNullWhen' could not be found (are you missing a using directive or an assembly reference?) [/home/runner/work/SmartEnum/SmartEnum/src/SmartEnum/SmartEnum.csproj::TargetFramework=netstandard2.0] 222 Warning(s) 2 Error(s)

Looking into this now

@teesofttech

teesofttech commented Jul 6, 2026

Copy link
Copy Markdown
Author
image

Fixed now

@ardalis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants