HasFieldsWithSameValues not working with Enum and int properties in the same class
Closed this issue · 6 comments
Hi,
We're facing an issue with the HasFieldsWithSameValues
method in NFluent 2.2.0 (all .NET framework versions) when we're checking an instance of a class with 2 properties, one of type int
and another of type custom enum
: the HasFieldsWithSameValues
method is not checking the enum
property if the int
property has a value corresponding to an existing value of the enum
.
For instance, the following snippet is successful whereas it should not :
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NFluent;
namespace NFluentTests
{
[TestClass]
public class UnitTest
{
[TestMethod]
public void Test_Enum_That_Should_Be_In_Error_But_Is_Not()
{
var testClass1 = new TestClass
{
IntProperty = 1,
TestEnumProperty = TestEnum.Test2
};
var testClass2 = new TestClass
{
IntProperty = 1,
TestEnumProperty = TestEnum.Test3
};
Check.That(testClass2).HasFieldsWithSameValues(testClass1);
}
public class TestClass
{
public int IntProperty { get; set; }
public TestEnum TestEnumProperty { get; set; }
}
public enum TestEnum
{
Test1,
Test2,
Test3,
Test4
}
}
}
However, the same test with int
property set to 99 is failing as expected :
[TestMethod]
public void Test_Enum_That_Should_Be_In_Error_And_Is_Really_In_Error()
{
var testClass1 = new TestClass
{
IntProperty = 99,
TestEnumProperty = TestEnum.Test2
};
var testClass2 = new TestClass
{
IntProperty = 99,
TestEnumProperty = TestEnum.Test3
};
Check.That(testClass2).HasFieldsWithSameValues(testClass1);
}
Do you know if this is a bug or if I'm not using the NFluent API properly ?
Thank you for your feedback.
Hi
Thanks for reporting this. It looks like a bug. I will try to reproduce later today.
I did reproduce the issue. It looks like a bug: the anti recursion safety mechanism may have undesired side effects with enum (and any type derived from a primitive type).
Working on a fix.
I have a fix, but I need more time to analyze risk raised by the issue and make sure problem is completly fixed.
This is good news ! Thanks a lot for your quick feedback.
I will publish a beta on MyGet soon.
Just to clarify the behaviour you observed, the problem is that both IntProperty and TestEnumProperty have the exact same value (1, Int32).
The anti-infinite-recursion safety prevented evaluating the enum because it assumed it already happened (infinite recursions lead to success).
That's why setting IntProperty to anything but 1 or 2 fixed the issue.
Thanks for this report, it helps me improve NFluent, and it likely helps other as well.
New beta (V 2.3.0-168) is available here: https://www.myget.org/feed/dupdobnightly/package/nuget/NFluent
@dimitribarbot .
As a reminder, beta versions are stable and tested and can be used for production projects
Next official will likely be in June.