public bool Equals(Boxed <T> that) { if (ReferenceEquals(null, that)) { return(false); } if (ReferenceEquals(this, that)) { return(true); } return(EqualityComparer <T> .Default.Equals(this.Value, that.Value)); }
public void TestEquals() { #pragma warning disable SA1131 // Use readable conditions var none = None; // ReSharper disable once EqualExpressionComparison #pragma warning disable CS1718 // Comparison made to same variable Assert.True(none == none); #pragma warning restore CS1718 // Comparison made to same variable var intNone = Maybe <Boxed <int> > .None; Assert.True(none == intNone); Assert.True(intNone == none); Assert.True(none == null); Assert.True(null == none); var nullableOne = 1.ToNullable(); Assert.True(nullableOne.Equals(1)); Assert.True(nullableOne == 1); Assert.True(1 == nullableOne); var one = Boxed.Of(1); var optionOne = Some(one); Assert.True(optionOne.Equals(one)); Assert.True(optionOne == one); Assert.True(one == optionOne); Assert.True(optionOne.Equals((object)optionOne)); Assert.True(intNone.Equals(none)); // ReSharper disable once EqualExpressionComparison #pragma warning disable CS1718 // Comparison made to same variable Assert.True(optionOne == optionOne); #pragma warning restore CS1718 // Comparison made to same variable Assert.True(optionOne == Some(one)); Assert.True(optionOne != Some(Boxed.Of(2))); // ReSharper disable once SuspiciousTypeConversion.Global Assert.False(Equals(optionOne, Some(Boxed.Of(1L)))); Assert.True(none != optionOne); Assert.True(optionOne != none); var someArray = new[] { 1 }.ToMaybe(); Assert.True(someArray != new[] { 1 }); Assert.True(someArray != none); Assert.True(none != someArray); Assert.True(someArray != null); Assert.True(null != someArray); }
public void TestSome() { var one = Boxed.Of(1); var some = Some(one); Assert.True(some.HasValue); Assert.AreEqual(some.Value, one); Assert.AreEqual(one, some.GetValueOrDefault(0)); Assert.AreEqual(one, some.GetValueOrDefault()); Assert.AreEqual(Some(Boxed.Of(2)), some.Select(x => Boxed.Of(x + 1))); Assert.AreEqual(one, some[0]); Assert.Throws <InvalidOperationException>(() => { var _ = some[1]; }); // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => Some(default(Boxed <int>))); }