Bomret / NeverNull

A Option type that prevents using null in your code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug in inequality operator of Option in Version 5.0.0

BADF00D opened this issue · comments

Hi,
I just stumbled upon an error.

var x = Option.From(1);
var y = Option.From(2);

Console.WriteLine(x != y); // outputs false

The problem is, that the implementation of the inequality operator is exactly the same as the equality operator:

public static bool operator ==(Option<T> left, Option<T> right) =>
            ((IStructuralEquatable)left).Equals(right, EqualityComparer<object>.Default);

public static bool operator !=(Option<T> left, Option<T> right) =>
            ((IStructuralEquatable)left).Equals(right, EqualityComparer<object>.Default);

The inequality operator should be something like:

public static bool operator !=(Option<T> left, Option<T> right) =>
            !((IStructuralEquatable)left).Equals(right, EqualityComparer<object>.Default);

//or
public static bool operator !=(Option<T> left, Option<T> right) => !(left == right);

Because there were no tests for verification, I would like to add some:

[TestFixture]
class If_Equality_operator_is_called {
     [Test]
    public bool Result_should_be_correct(Option<int> x, Option<int> y){
        return x == y;
    }
    private static IEnumerable<TestCaseData> EqulityOperatorTestCases{
        get{
            return TestCaseData(Option<int>.None, Option.None).Returns(true);
            return TestCaseData(Option<int>.None, Option.From(1)).Returns(false);
            return TestCaseData(Option.From(1), Option<int>.None).Returns(false);
            return TestCaseData(Option.From(1), Option.From(2)).Returns(false);
            return TestCaseData(Option.From(1), Option.From(1)).Returns(true);
        }
    }
}
[TestFixture]
class If_inequality_operator_is_called {
     [Test]
    public bool Result_should_be_correct(Option<int> x, Option<int> y){
        return x != y;
    }
    private static IEnumerable<TestCaseData> EqulityOperatorTestCases{
        get{
            return TestCaseData(Option<int>.None, Option.None).Returns(false);
            return TestCaseData(Option<int>.None, Option.From(1)).Returns(true);
            return TestCaseData(Option.From(1), Option<int>.None).Returns(true);
            return TestCaseData(Option.From(1), Option.From(2)).Returns(true);
            return TestCaseData(Option.From(1), Option.From(1)).Returns(false);
        }
    }
}

I would have created a pull request, but unfortunately I was not able to build the project.

Ahh, damn! Good catch! I’ll try to fix this ASAP.

I saw you fixed the issue, but forgot the release NuGet package 5.0.1.

Take a look at the time of the latest commits. I did not forget it 😉 I'll release it as soon as possible.

fixed in 5.1.0