public void Equals_Should_Return_True_For_CookedFood_With_Equal_Properties()
        {
            var bakedApple1 = new CookedFood(100, "Apple", FoodGroup.Fruit, "Baked");
            var bakedApple2 = new CookedFood(100, "Apple", FoodGroup.Fruit, "Baked");

            Assert.IsTrue(bakedApple1.Equals(bakedApple2));
        }
Beispiel #2
0
        public void DerivedType_ObjectEquals_CookedFoodWithSameNameSameTypeSameCookingMethod_ExpectEquality()
        {
            CookedFood banana1 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");
            CookedFood banana2 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");

            Assert.IsTrue(banana1.Equals(banana2));
        }
        public void Inequality_Operator_Should_Return_True_For_CookedFood_With_Different_Properties()
        {
            var apple  = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");
            var banana = new CookedFood(200, "banana", FoodGroup.Fruit, "Baked");

            Assert.IsTrue(apple != banana);
        }
        public void Inequality_Operator_Should_Return_False_For_CookedFood_With_Equal_Properties()
        {
            var apple1 = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");
            var apple2 = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");

            Assert.IsFalse(apple1 != apple2);
        }
        public void GetHashCode_Should_Return_The_Same_Hashes_For_CookedFood_With_Equal_Properties()
        {
            var bakedApple1 = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");
            var bakedApple2 = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");

            Assert.AreEqual(bakedApple1.GetHashCode(), bakedApple2.GetHashCode());
        }
        public void GetHashCode_Should_Return_The_Same_Hashes_For_CookedFood_With_Different_Properties()
        {
            var bakedApple  = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");
            var bakedBanana = new CookedFood(100, "banana", FoodGroup.Fruit, "Baked");

            Assert.AreNotEqual(bakedApple.GetHashCode(), bakedBanana.GetHashCode());
        }
Beispiel #7
0
        public void DerivedType_EqualityOperator_CookedFoodWithDifferentNameSameTypeDifferentCookingMethod_ExpectInequality()
        {
            CookedFood banana1 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");
            CookedFood banana2 = new CookedFood("Pie", Shared.FoodGroup.Fruit, "Fried");

            Assert.IsFalse(banana1 == banana2);
        }
        public void Equals_Should_Return_False_For_CookedFood_With_Different_Properties()
        {
            var bakedApple  = new CookedFood(100, "Apple", FoodGroup.Fruit, "Baked");
            var bakedBanana = new CookedFood(100, "banana", FoodGroup.Fruit, "Baked");

            Assert.IsFalse(bakedApple.Equals(bakedBanana));
        }
Beispiel #9
0
        public void DerivedType_ObjectEquals_CookedFoodWithNameCapitalizationDifference_ExpectEquality()
        {
            CookedFood banana1 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");
            CookedFood banana2 = new CookedFood("banAna", Shared.FoodGroup.Fruit, "Baked");

            Assert.IsTrue(banana1.Equals(banana2));
        }
Beispiel #10
0
        public void DerivedType_ObjectEquals_CookedFoodWithDifferentNameDifferentTypeDifferentCookingMethod_ExpectInequality()
        {
            CookedFood banana1 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");
            CookedFood banana2 = new CookedFood("Pie", Shared.FoodGroup.Sweets, "Fried");

            Assert.IsFalse(banana1.Equals(banana2));
        }
Beispiel #11
0
        public void DerivedType_ObjectEquals_EqualFoods_ExpectOrderDoesntMatter()
        {
            CookedFood banana1 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");
            CookedFood banana2 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");

            bool equals1 = banana1.Equals(banana2);
            bool equals2 = banana2.Equals(banana1);

            Assert.AreEqual(equals1, equals2);
        }
Beispiel #12
0
        public void DerivedType_GetHashCode_EquivalentItems_ExpectEqualHashCodes()
        {
            CookedFood banana1 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");
            CookedFood banana2 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");

            int b1hc = banana1.GetHashCode();
            int b2hc = banana2.GetHashCode();

            Assert.AreEqual(b1hc, b2hc);
        }
Beispiel #13
0
        public void DerivedType_InequalityOperator_InequalFoods_ExpectOrderDoesntMatter()
        {
            CookedFood banana1 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");
            CookedFood banana2 = new CookedFood("Pie", Shared.FoodGroup.Sweets, "Fried");

            bool equals1 = banana1 != banana2;
            bool equals2 = banana2 != banana1;

            Assert.AreEqual(equals1, equals2);
        }
Beispiel #14
0
        public void DerivedType_InequalityOperator_InequalValues_ExpectOppositeOfEqualityOperator()
        {
            CookedFood banana1 = new CookedFood("Banana", Shared.FoodGroup.Fruit, "Baked");
            CookedFood banana2 = new CookedFood("Pie", Shared.FoodGroup.Sweets, "Fried");

            bool equalityOperator   = banana1 == banana2;
            bool inequalityOperator = banana1 != banana2;

            Assert.AreNotEqual(equalityOperator, inequalityOperator);
        }
        public void HasSet_Should_Have_Two_CookedFood_When_They_Have_Different_HashCodes()
        {
            var foodItem1 = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");
            var foodItem2 = new CookedFood(100, "banana", FoodGroup.Fruit, "Baked");

            var hashSet = new HashSet <CookedFood>
            {
                foodItem1, foodItem2
            };

            Assert.AreNotEqual(foodItem1.GetHashCode(), foodItem2.GetHashCode());
            Assert.AreEqual(hashSet.Count, 2);
        }
        public void HashSet_Should_Have_Only_One_CookedFood_Despite_Inserting_Two_With_The_Same_HashCode()
        {
            var apple1 = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");
            var apple2 = new CookedFood(100, "apple", FoodGroup.Fruit, "Baked");

            var hashSet = new HashSet <CookedFood>
            {
                apple1, apple2
            };

            Assert.AreEqual(apple1.GetHashCode(), apple2.GetHashCode());
            Assert.AreEqual(hashSet.Count, 1);
        }
Beispiel #17
0
        public override bool Equals(object obj)
        {
            if (!base.Equals(obj))
            {
                return(false);
            }

            CookedFood rhs = obj as CookedFood;

            if (rhs == null)
            {
                return(false);
            }

            return(this.cookingMethod == rhs.cookingMethod);
        }
Beispiel #18
0
        //
        static void Main(string[] args)
        {
            Console.WriteLine("Equality Implementation for Reference Type:\n");

            Food apple = new Food("apple", FoodType.Fruit);
            //Console.WriteLine(apple);
            CookedFood stewedApple = new CookedFood("stewed", "apple", FoodType.Fruit);
            //Console.WriteLine(stewedApple);
            CookedFood bakedApple   = new CookedFood("baked", "apple", FoodType.Fruit);
            CookedFood stewedApple2 = new CookedFood("stewed", "apple", FoodType.Fruit);
            Food       apple2       = new Food("apple", FoodType.Fruit);

            DisplayWhetherEqual(apple, stewedApple);
            DisplayWhetherEqual(stewedApple, bakedApple);
            DisplayWhetherEqual(stewedApple, stewedApple2);
            DisplayWhetherEqual(apple, apple2);
            DisplayWhetherEqual(apple, apple);

            Console.WriteLine("\nPlease enter any key to exit...");
            Console.ReadKey();
        }