Commit 6f75ebc
authored
Fixed bug that would cause a struct to not match and throw a runtime error. (#895)
* Resolved a bug that would cause a struct to not match and throw a runtime error.
When a struct had an empty constructor that modified a field/property to a non-default value. The matcher would fail and throw a runtime error. This bug fix attempts to fix that issue by essentially zero initializing the struct the same way that default(T) does through the use of a runtime helper method. Simply, we replace Activator.CreateInstance with GetUninitializedObject.
The idea came from this post
https://stackoverflow.com/questions/1005336/c-sharp-using-reflection-to-create-a-struct
Which contains and deprecated method as of .NET8. Microsoft has a recommended fix for that here.
https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib0050
* Add tests for struct with default constructor
- Added test method `Any_on_struct_with_default_constructor_should_work` to ensure method calls with struct arguments do not throw exceptions.
- Introduced `StructWithDefaultConstructor` struct:
- Contains a property `Value`.
- Has a default constructor initializing `Value` to 42.
- Created `IWithStructWithDefaultConstructor` interface:
- Declares `MethodWithStruct` that accepts `StructWithDefaultConstructor` as an argument.
* Fixed class constructor curly brace format
* Refactor default value handling logic
Updated `GetDefault` methods in `AutoObservableProvider.cs` and `AutoTaskProvider.cs` to use the `DefaultForType` class for determining default values, improving code modularity and reusability.1 parent c2381f4 commit 6f75ebc
File tree
4 files changed
+36
-3
lines changed- src/NSubstitute
- Core
- Routing/AutoValues
- tests/NSubstitute.Acceptance.Specs
4 files changed
+36
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
50 | 59 | | |
51 | 60 | | |
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | | - | |
| 36 | + | |
| 37 | + | |
36 | 38 | | |
37 | 39 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
866 | 866 | | |
867 | 867 | | |
868 | 868 | | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
869 | 890 | | |
870 | 891 | | |
871 | 892 | | |
| |||
0 commit comments