コード例 #1
0
        public void OptionEqualityValue()
        {
            // Option value type
            var optionInt = Option <int> .Some(12);

            var emptyOptionInt = Option <int> .None;

            Assert.IsTrue(optionInt.Equals(12));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsTrue(optionInt.Equals((object)12));
            Assert.IsFalse(optionInt.Equals(42));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsFalse(optionInt.Equals((object)42));
            Assert.IsTrue(optionInt == 12);
            Assert.IsTrue(12 == optionInt);
            Assert.IsFalse(optionInt == 42);
            Assert.IsTrue(optionInt != 42);
            Assert.IsFalse(12 != optionInt);

            Assert.IsFalse(emptyOptionInt.Equals(12));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsFalse(emptyOptionInt.Equals((object)12));
            Assert.IsFalse(emptyOptionInt == 12);
            Assert.IsFalse(12 == emptyOptionInt);
            Assert.IsTrue(emptyOptionInt != 12);
            Assert.IsTrue(15 != emptyOptionInt);

            // Option reference type
            var person1       = new Person("Test");
            var optionPerson1 = Option <Person> .Some(person1);

            var optionPerson2 = Option <Person> .Some(new Person("Test"));

            var optionPerson3 = Option <Person> .Some(new Person("Test2"));

            var emptyOptionPerson1 = Option <Person> .None;

            Assert.IsTrue(optionPerson1.Equals(person1));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsTrue(optionPerson1.Equals((object)person1));
            Assert.IsFalse(optionPerson1.Equals(new Person("Test2")));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsFalse(optionPerson1.Equals((object)new Person("Test2")));
            Assert.IsTrue(optionPerson1 == person1);
            Assert.IsTrue(person1 == optionPerson1);
            Assert.IsFalse(optionPerson1 != person1);
            Assert.IsFalse(person1 != optionPerson1);

            Assert.IsTrue(optionPerson2.Equals(person1));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsTrue(optionPerson2.Equals((object)person1));
            Assert.IsTrue(optionPerson2 == person1);
            Assert.IsTrue(person1 == optionPerson2);
            Assert.IsFalse(optionPerson2 != person1);
            Assert.IsFalse(new Person("Test") != optionPerson2);

            Assert.IsFalse(optionPerson3.Equals(person1));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsFalse(optionPerson3.Equals((object)person1));
            Assert.IsFalse(optionPerson3 == person1);
            Assert.IsFalse(person1 == optionPerson3);
            Assert.IsTrue(optionPerson3 != person1);
            Assert.IsTrue(person1 != optionPerson3);

            Assert.IsFalse(emptyOptionPerson1.Equals(person1));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsFalse(emptyOptionPerson1.Equals((object)person1));
            Assert.IsFalse(emptyOptionPerson1 == person1);
            Assert.IsFalse(person1 == emptyOptionPerson1);
            Assert.IsTrue(emptyOptionPerson1 != person1);
            Assert.IsTrue(person1 != emptyOptionPerson1);

            var person2       = new PersonNotEquatable("Test");
            var person3       = new PersonNotEquatable("Test");
            var optionPerson4 = Option <PersonNotEquatable> .Some(person2);

            var optionPerson5 = Option <PersonNotEquatable> .Some(new PersonNotEquatable("Test"));

            var emptyOptionPerson2 = Option <PersonNotEquatable> .None;

            Assert.IsTrue(optionPerson4.Equals(person2));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsTrue(optionPerson4.Equals((object)person2));
            Assert.IsFalse(optionPerson4.Equals(person3));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsFalse(optionPerson4.Equals((object)person3));
            Assert.IsTrue(optionPerson4 == person2);
            Assert.IsTrue(person2 == optionPerson4);
            Assert.IsFalse(optionPerson4 != person2);
            Assert.IsFalse(person2 != optionPerson4);

            Assert.IsFalse(optionPerson5.Equals(person2));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsFalse(optionPerson5.Equals((object)person2));
            Assert.IsFalse(optionPerson5 == person2);
            Assert.IsFalse(person2 == optionPerson5);
            Assert.IsTrue(optionPerson5 != person2);
            Assert.IsTrue(person2 != optionPerson5);

            Assert.IsFalse(emptyOptionPerson2.Equals(person2));
            // ReSharper disable once SuspiciousTypeConversion.Global
            Assert.IsFalse(emptyOptionPerson2.Equals((object)person2));
            Assert.IsFalse(emptyOptionPerson2 == person2);
            Assert.IsFalse(person2 == emptyOptionPerson2);
            Assert.IsTrue(emptyOptionPerson2 != person2);
            Assert.IsTrue(person2 != emptyOptionPerson2);

            // With flatten
            var embedOptionInt = Option <Option <int> > .Some(Option <int> .Some(12));

            Assert.IsTrue(embedOptionInt == 12);
            Assert.IsFalse(embedOptionInt == 42);

            // On value type null is not a valid value consider implicitly casted to None
            Assert.IsFalse(optionInt == null);
            Assert.IsTrue(Option <int> .None == null);

            // Equals with a null value => These cases are not possible as they
            // suppose the option value is null which should never be the case!
            Assert.IsFalse(optionPerson1 == null);
        }
コード例 #2
0
        public void OptionEquality()
        {
            // Option value type
            var optionInt1 = Option <int> .Some(12);

            var optionInt2 = Option <int> .Some(12);

            var optionInt3 = Option <int> .Some(42);

            var emptyOptionInt1 = Option <int> .None;
            var emptyOptionInt2 = Option <int> .None;

            Assert.IsTrue(optionInt1.Equals(optionInt2));
            Assert.IsTrue(optionInt2.Equals(optionInt1));
            Assert.IsTrue(optionInt1 == optionInt2);
            Assert.IsTrue(optionInt2 == optionInt1);
            Assert.IsFalse(optionInt1 != optionInt2);
            Assert.IsFalse(optionInt2 != optionInt1);

            Assert.IsFalse(optionInt1.Equals(optionInt3));
            Assert.IsFalse(optionInt3.Equals(optionInt1));
            Assert.IsFalse(optionInt1 == optionInt3);
            Assert.IsFalse(optionInt3 == optionInt1);
            Assert.IsTrue(optionInt1 != optionInt3);
            Assert.IsTrue(optionInt3 != optionInt1);

            Assert.IsFalse(optionInt1.Equals(emptyOptionInt1));
            Assert.IsFalse(emptyOptionInt1.Equals(optionInt1));
            Assert.IsFalse(optionInt1 == emptyOptionInt1);
            Assert.IsFalse(emptyOptionInt1 == optionInt1);
            Assert.IsTrue(optionInt1 != emptyOptionInt1);
            Assert.IsTrue(emptyOptionInt1 != optionInt1);

            Assert.IsTrue(emptyOptionInt1.Equals(emptyOptionInt2));
            Assert.IsTrue(emptyOptionInt2.Equals(emptyOptionInt1));
            Assert.IsTrue(emptyOptionInt1 == emptyOptionInt2);
            Assert.IsTrue(emptyOptionInt2 == emptyOptionInt1);
            Assert.IsFalse(emptyOptionInt1 != emptyOptionInt2);
            Assert.IsFalse(emptyOptionInt2 != emptyOptionInt1);

            // Option reference type
            var person1       = new Person("Test");
            var optionPerson1 = Option <Person> .Some(person1);

            var optionPerson2 = Option <Person> .Some(person1);

            var optionPerson3 = Option <Person> .Some(new Person("Test"));

            var optionPerson4 = Option <Person> .Some(new Person("Test2"));

            var emptyOptionPerson1 = Option <Person> .None;
            var emptyOptionPerson2 = Option <Person> .None;

            Assert.IsTrue(optionPerson1.Equals(optionPerson2));
            Assert.IsTrue(optionPerson2.Equals(optionPerson1));
            Assert.IsTrue(optionPerson1 == optionPerson2);
            Assert.IsTrue(optionPerson2 == optionPerson1);
            Assert.IsFalse(optionPerson1 != optionPerson2);
            Assert.IsFalse(optionPerson2 != optionPerson1);

            Assert.IsTrue(optionPerson1.Equals(optionPerson3));
            Assert.IsTrue(optionPerson3.Equals(optionPerson1));
            Assert.IsTrue(optionPerson1 == optionPerson3);
            Assert.IsTrue(optionPerson3 == optionPerson1);
            Assert.IsFalse(optionPerson1 != optionPerson3);
            Assert.IsFalse(optionPerson3 != optionPerson1);

            Assert.IsFalse(optionPerson1.Equals(optionPerson4));
            Assert.IsFalse(optionPerson4.Equals(optionPerson1));
            Assert.IsFalse(optionPerson1 == optionPerson4);
            Assert.IsFalse(optionPerson4 == optionPerson1);
            Assert.IsTrue(optionPerson1 != optionPerson4);
            Assert.IsTrue(optionPerson4 != optionPerson1);

            Assert.IsFalse(optionPerson2.Equals(emptyOptionPerson1));
            Assert.IsFalse(emptyOptionPerson1.Equals(optionPerson2));
            Assert.IsFalse(emptyOptionPerson1 == optionPerson2);
            Assert.IsFalse(optionPerson2 == emptyOptionPerson1);
            Assert.IsTrue(optionPerson1 != emptyOptionPerson1);
            Assert.IsTrue(emptyOptionPerson1 != optionPerson1);

            Assert.IsTrue(emptyOptionPerson1.Equals(emptyOptionPerson2));
            Assert.IsTrue(emptyOptionPerson2.Equals(emptyOptionPerson1));
            Assert.IsTrue(emptyOptionPerson1 == emptyOptionPerson2);
            Assert.IsTrue(emptyOptionPerson2 == emptyOptionPerson1);
            Assert.IsFalse(emptyOptionPerson1 != emptyOptionPerson2);
            Assert.IsFalse(emptyOptionPerson2 != emptyOptionPerson1);

            var person2       = new PersonNotEquatable("Test");
            var optionPerson5 = Option <PersonNotEquatable> .Some(person2);

            var optionPerson6 = Option <PersonNotEquatable> .Some(person2);

            var optionPerson7 = Option <PersonNotEquatable> .Some(new PersonNotEquatable("Test"));

            var emptyOptionPerson3 = Option <PersonNotEquatable> .None;
            var emptyOptionPerson4 = Option <PersonNotEquatable> .None;

            Assert.IsTrue(optionPerson5.Equals(optionPerson6));
            Assert.IsTrue(optionPerson6.Equals(optionPerson5));
            Assert.IsTrue(optionPerson5 == optionPerson6);
            Assert.IsTrue(optionPerson6 == optionPerson5);
            Assert.IsFalse(optionPerson5 != optionPerson6);
            Assert.IsFalse(optionPerson6 != optionPerson5);

            Assert.IsFalse(optionPerson5.Equals(optionPerson7));
            Assert.IsFalse(optionPerson7.Equals(optionPerson5));
            Assert.IsFalse(optionPerson5 == optionPerson7);
            Assert.IsFalse(optionPerson7 == optionPerson5);
            Assert.IsTrue(optionPerson5 != optionPerson7);
            Assert.IsTrue(optionPerson7 != optionPerson5);

            Assert.IsFalse(optionPerson5.Equals(emptyOptionPerson3));
            Assert.IsFalse(emptyOptionPerson3.Equals(optionPerson5));
            Assert.IsFalse(optionPerson5 == emptyOptionPerson3);
            Assert.IsFalse(emptyOptionPerson3 == optionPerson5);
            Assert.IsTrue(optionPerson5 != emptyOptionPerson3);
            Assert.IsTrue(emptyOptionPerson3 != optionPerson5);

            Assert.IsTrue(emptyOptionPerson3.Equals(emptyOptionPerson4));
            Assert.IsTrue(emptyOptionPerson4.Equals(emptyOptionPerson3));
            Assert.IsTrue(emptyOptionPerson3 == emptyOptionPerson4);
            Assert.IsTrue(emptyOptionPerson4 == emptyOptionPerson3);
            Assert.IsFalse(emptyOptionPerson3 != emptyOptionPerson4);
            Assert.IsFalse(emptyOptionPerson4 != emptyOptionPerson3);

            // Mixed
            // ReSharper disable SuspiciousTypeConversion.Global
            Assert.IsFalse(optionInt1.Equals(optionPerson1));
            Assert.IsFalse(optionPerson1.Equals(optionInt1));
            Assert.IsFalse(optionPerson1.Equals(12));
            // ReSharper restore SuspiciousTypeConversion.Global

            // With flatten
            var embedOptionInt = Option <Option <int> > .Some(Option <int> .Some(12));

            Assert.IsTrue(optionInt1.Equals(embedOptionInt));
            var embedOptionInt2 = Option <Option <int> > .Some(Option <int> .Some(42));

            Assert.IsFalse(optionInt1.Equals(embedOptionInt2));

            var embedOptionClass = Option <Option <Person> > .Some(Option <Person> .Some(person1));

            Assert.IsTrue(optionPerson1.Equals(embedOptionClass));
            var embedOptionClass2 = Option <Option <Person> > .Some(Option <Person> .Some(new Person("Test3")));

            Assert.IsFalse(optionPerson1.Equals(embedOptionClass2));

            // Equals with an object value
            Assert.IsFalse(optionInt1.Equals((object)null));
            Assert.IsTrue(optionInt1.Equals((object)optionInt1));
            Assert.IsFalse(optionInt1.Equals((object)emptyOptionInt1));
        }