示例#1
0
        public void ConsoleControllerHandleInputErrors3()
        {
            // MSTest Extended
            ConsoleController controller = new ConsoleController();

            ConsoleAssert.WritesOut(
                () => controller.HandleInput(""),
                "Please provide a command");

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <ConsoleController> _controller = test.CreateVariable <ConsoleController>();

            test.Arrange(_controller, Expr(() => new ConsoleController()));
            test.ConsoleAssert.WritesOut(
                Lambda(Expr(_controller, c => c.HandleInput(""))),
                Const("Please provide a command"));
            test.Execute();
        }
        public void EmployeeToStringReturnsExpectedOutput()
        {
            Employee employee = new Employee("Joe Stevens")
            {
                Title = "Programmer"
            };

            Assert.AreEqual("Employee Joe Stevens (Programmer)", employee.ToString());

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <Employee> _employee = test.CreateVariable <Employee>(nameof(_employee));

            test.Arrange(_employee, Expr(() => new Employee("Joe Stevens")
            {
                Title = "Programmer"
            }));
            test.Assert.AreEqual(Const("Employee Joe Stevens (Programmer)"), Expr(_employee, e => e.ToString()));
            test.Execute();
        }
        public void PersonCalculateBMIThrowsNotOldEnoughException()
        {
            Person person = new Person("abc")
            {
                Age = 15
            };

            Assert.ThrowsException <NotOldEnoughException>(() => person.CalculateBMI());

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <Person> _person = test.CreateVariable <Person>();

            test.Arrange(_person, Expr(() => new Person("abc")
            {
                Age = 15
            }));
            test.Assert.ThrowsExceptionOn <NotOldEnoughException>(Expr(_person, p => p.CalculateBMI()));
            test.Execute();
        }
        public void ObservableCollectionAddEmitsCollectionChangedEvents()
        {
            bool isCalled = false;
            ObservableCollection <int> collection = new ObservableCollection <int>();

            collection.CollectionChanged += (sender, e) => isCalled = true;

            collection.Add(5);

            Assert.IsTrue(isCalled, "The ObservableCollection<T>.CollectionChanged event is never emitted");

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <ObservableCollection <int> > _collection = test.CreateVariable <ObservableCollection <int> >();

            test.Arrange(_collection, Expr(() => new ObservableCollection <int>()));
            test.DelegateAssert.IsInvoked(Lambda <NotifyCollectionChangedEventHandler>(handler => Expr(_collection, c => c.AddCollectionChanged(handler))));
            test.Act(Expr(_collection, c => c.Add(5)));
            test.Execute();
        }
示例#5
0
        public void BankAccountHighBalanceThresholdBelowLowBalanceThresholdThrowsArgumentException()
        {
            BankAccount account = new BankAccount()
            {
                LowBalanceThreshold = 0
            };

            Assert.ThrowsException <ArgumentException>(() => account.HighBalanceThreshold = -1M);

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <BankAccount> _account = test.CreateVariable <BankAccount>();

            test.Arrange(_account, Expr(() => new BankAccount()
            {
                LowBalanceThreshold = 0
            }));
            test.Assert.ThrowsExceptionOn <ArgumentException>(Expr(_account, a => a.SetHighBalanceThreshold(-1M)));
            test.Execute();
        }
        public void SortedCollectionContainsReturnsTrue()
        {
            SortedCollection <int> collection = new SortedCollection <int>()
            {
                1, 2, 3
            };

            Assert.IsTrue(collection.Contains(2));

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >();

            test.Arrange(_collection, Expr(() => new SortedCollection <int>()
            {
                1, 2, 3
            }));
            test.Assert.IsTrue(Expr(_collection, c => c.Contains(2)));
            test.Execute();
        }
        public void ManagerToStringReturnsExpectedOutput()
        {
            Manager manager = new Manager("Mary Stevens")
            {
                Title = "Software Engineer"
            };

            Assert.AreEqual("Manager Mary Stevens (Software Engineer)", manager.ToString());

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <Manager> _manager = test.CreateVariable <Manager>(nameof(_manager));

            test.Arrange(_manager, Expr(() => new Manager("Mary Stevens")
            {
                Title = "Software Engineer"
            }));
            test.Assert.AreEqual(Const("Manager Mary Stevens (Software Engineer)"), Expr(_manager, m => m.ToString()));
            test.Execute();
        }
        public void SortedCollectionGetAllReversedReturnsReversedEnumerationOfCollection2()
        {
            SortedCollection <int> collection = new SortedCollection <int>()
            {
                1, 2, 3, 4
            };

            Assert.IsTrue(collection.GetAllReversed(x => x % 2 == 0).SequenceEqual(new int[] { 4, 2 }));

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >();

            test.Arrange(_collection, Expr(() => new SortedCollection <int>()
            {
                1, 2, 3, 4
            }));
            test.Assert.IsTrue(Expr(_collection, c => c.GetAllReversed(x => x % 2 == 0).SequenceEqual(new[] { 4, 2 })));
            test.Execute();
        }
示例#9
0
        public void MyQueuePeekDoesNotModifyQueue()
        {
            MyQueue <int> queue = new MyQueue <int>(5);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Peek();

            Assert.AreEqual(1, queue.Peek());

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <MyQueue <int> > _queue = test.CreateVariable <MyQueue <int> >();

            test.Arrange(_queue, Expr(() => new MyQueue <int>(5)));
            test.Act(Expr(_queue, q => q.Enqueue(1)));
            test.Act(Expr(_queue, q => q.Enqueue(2)));
            test.Act(Expr(_queue, q => q.Peek()));
            test.Assert.AreEqual(Const(1), Expr(_queue, q => q.Peek()));
            test.Execute();
        }
示例#10
0
        public void EmployeeCalculateYearlySalaryAddsTenProcentForSeniorityLevelTen()
        {
            Employee employee = new Employee("abc")
            {
                MonthlySalary = 35250,
                Seniority     = 10
            };

            Assert.AreEqual(296100M, employee.CalculateYearlySalary());

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <Employee> _employee = test.CreateVariable <Employee>(nameof(_employee));

            test.Arrange(_employee, Expr(() => new Employee("abc")
            {
                MonthlySalary = 35250, Seniority = 10
            }));
            test.Assert.AreEqual(Const(296100M), Expr(_employee, e => e.CalculateYearlySalary()));
            test.Execute();
        }
示例#11
0
        public void DogToStringReturnsExpectedResult()
        {
            Dog dog = new Dog()
            {
                ID   = 3,
                Name = "Bella"
            };

            Assert.AreEqual("Dog Bella (3)", dog.ToString());

            // TestTools Code
            UnitTest           test = Factory.CreateTest();
            TestVariable <Dog> _dog = test.CreateVariable <Dog>();

            test.Arrange(_dog, Expr(() => new Dog()
            {
                ID = 3, Name = "Bella"
            }));
            test.Assert.AreEqual(Const("Dog Bella (3)"), Expr(_dog, d => d.ToString()));
            test.Execute();
        }
        public void SortedCollectionAddAddsElementInOrder()
        {
            SortedCollection <int> collection = new SortedCollection <int>();

            collection.Add(3);
            collection.Add(5);
            collection.Add(2);

            Assert.IsTrue(collection.SequenceEqual(new int[] { 2, 3, 5 }));

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >();

            test.Arrange(_collection, Expr(() => new SortedCollection <int>()));
            test.Act(Expr(_collection, c => c.Add(3)));
            test.Act(Expr(_collection, c => c.Add(5)));
            test.Act(Expr(_collection, c => c.Add(2)));
            test.Assert.IsTrue(Expr(_collection, c => c.SequenceEqual(new[] { 2, 3, 5 })));
            test.Execute();
        }
        public void SortedCollectionReturnsCorrectElement()
        {
            SortedCollection <int> collection = new SortedCollection <int>()
            {
                1, 2, 3
            };

            Assert.AreEqual(collection[1], 2);

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >();

            test.Arrange(_collection, Expr(() => new SortedCollection <int>()
            {
                1, 2, 3
            }));
            test.Assert.AreEqual(Expr(_collection, c => c[1]), Const(2));

            test.Execute();
        }
示例#14
0
        public void ConsoleConstrollerAddCommandAddsCommand()
        {
            // MSTest Extended
            ConsoleController controller = new ConsoleController();

            controller.AddCommand("SayGoodbye", s => Console.WriteLine("Goodbye " + s));

            ConsoleAssert.WritesOut(
                () => controller.HandleInput("SayGoodbye World"),
                "Goodbye World");

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <ConsoleController> _controller = test.CreateVariable <ConsoleController>();

            test.Arrange(_controller, Expr(() => new ConsoleController()));
            test.Act(Expr(_controller, (c) => c.AddCommand("SayGoodbye", s => Console.WriteLine("Goodbye " + s))));
            test.ConsoleAssert.WritesOut(
                Lambda(Expr(_controller, c => c.HandleInput("SayGoodbye World"))),
                Const("Goodbye World"));
            test.Execute();
        }
示例#15
0
        public void ManagerCalculateYearlySalaryAddsTenProcentForSeniorityLevelTen()
        {
            Manager manager = new Manager("abc")
            {
                MonthlySalary = 35250,
                Bonus         = 34000,
                Seniority     = 10
            };

            Assert.AreEqual(330100M, manager.CalculateYearlySalary());

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <Manager> _manager = test.CreateVariable <Manager>(nameof(_manager));

            test.Arrange(_manager, Expr(() => new Manager("abc")
            {
                MonthlySalary = 35250, Bonus = 34000, Seniority = 10
            }));
            test.Assert.AreEqual(Const(330100M), Expr(_manager, e => e.CalculateYearlySalary()));;
            test.Execute();
        }
        public void PersonGetClassificationReturnsObese()
        {
            Person person = new Person("abc")
            {
                Height = 1.85,
                Weight = 120.0,
                Age    = 18
            };

            Assert.AreEqual("obese", person.GetClassification());

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <Person> _person = test.CreateVariable <Person>(nameof(_person));

            test.Arrange(_person, Expr(() => new Person("abc")
            {
                Height = 1.85, Weight = 120.0, Age = 18
            }));
            test.Assert.AreEqual(Const("obese"), Expr(_person, p => p.GetClassification()));
            test.Execute();
        }
        public void SortedCollectionRemoveRemovesElement()
        {
            SortedCollection <int> collection = new SortedCollection <int>()
            {
                1, 2, 3
            };

            collection.Remove(1);

            Assert.IsTrue(collection.SequenceEqual(new int[] { 2, 3 }));

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >();

            test.Arrange(_collection, Expr(() => new SortedCollection <int>()
            {
                1, 2, 3
            }));
            test.Act(Expr(_collection, c => c.Remove(1)));
            test.Assert.IsTrue(Expr(_collection, c => c.SequenceEqual(new[] { 2, 3 })));
            test.Execute();
        }
        public void SortedCollectionClearRemovesAllElements()
        {
            SortedCollection <int> collection = new SortedCollection <int>()
            {
                1, 2, 3
            };

            collection.Clear();

            Assert.IsFalse(collection.Any());

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <SortedCollection <int> > _collection = test.CreateVariable <SortedCollection <int> >();

            test.Arrange(_collection, Expr(() => new SortedCollection <int>()
            {
                1, 2, 3
            }));
            test.Act(Expr(_collection, c => c.Clear()));
            test.Assert.IsFalse(Expr(_collection, c => c.Any()));
            test.Execute();
        }
示例#19
0
        public void ConsoleViewRunDoesNotEmitInputOnEmptyLineInput()
        {
            bool        isCalled = false;
            ConsoleView view     = new ConsoleView();

            view.Input += (input) => isCalled = true;

            ConsoleInputter.Clear();
            ConsoleInputter.WriteLine();
            view.Run();

            Assert.IsFalse(isCalled, "The ConsoleView.Run event was emitted");

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <ConsoleView> _view = test.CreateVariable <ConsoleView>();

            test.Arrange(_view, Expr(() => new ConsoleView()));
            test.DelegateAssert.IsNotInvoked(Lambda <InputHandler>(handler => Expr(_view, v => v.AddInput(handler))));
            test.Act(Expr(() => ConsoleInputter.Clear()));
            test.Act(Expr(() => ConsoleInputter.WriteLine()));
            test.Act(Expr(_view, v => v.Run()));
            test.Execute();
        }
        public void PersonCalculateBMIReturnsExpectedOutput()
        {
            double expectedBMI = 80 / (1.80 * 1.80);

            Person person = new Person("abc")
            {
                Height = 1.80,
                Weight = 80,
                Age    = 18
            };

            Assert.AreEqual(expectedBMI, person.CalculateBMI(), 0.001);

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <Person> _person = test.CreateVariable <Person>(nameof(_person));

            test.Arrange(_person, Expr(() => new Person("abc")
            {
                Height = 1.80, Weight = 80, Age = 18
            }));
            test.Assert.AreEqual(Const(expectedBMI), Expr(_person, p => p.CalculateBMI()), 0.001);
            test.Execute();
        }
        public void PrintFamilyPrintsCorrectly()
        {
            // Extended MSTest
            Person person = new Person()
            {
                FirstName = "Robin",
                LastName  = "Rich",
                Age       = 10,
                Mother    = new Person()
                {
                    FirstName = "Anna",
                    LastName  = "Smith",
                    Age       = 38
                },
                Father = new Person()
                {
                    FirstName = "Warren",
                    LastName  = "Rich",
                    Age       = 36,
                    Mother    = new Person()
                    {
                        FirstName = "Elsa",
                        LastName  = "Johnson",
                        Age       = 65
                    },
                    Father = new Person()
                    {
                        FirstName = "Gustav",
                        LastName  = "Rich",
                        Age       = 66
                    }
                }
            };
            PersonPrinter printer = new PersonPrinter();

            string expectedOutput = string.Join(
                Environment.NewLine,
                "Robin Rich (10)",
                "Warren Rich (36)",
                "Gustav Rich (66)",
                "Elsa Johnson (65)",
                "Anna Smith (38)");

            ConsoleAssert.WritesOut(() => printer.PrintFamily(person), expectedOutput);

            // TestTools Code
            UnitTest test = Factory.CreateTest();
            TestVariable <Person>        _person  = test.CreateVariable <Person>("person");
            TestVariable <PersonPrinter> _printer = test.CreateVariable <PersonPrinter>("printer");

            test.Arrange(_person, Expr(() => new Person()
            {
                FirstName = "Robin",
                LastName  = "Rich",
                Age       = 10,
                Mother    = new Person()
                {
                    FirstName = "Anna",
                    LastName  = "Smith",
                    Age       = 38
                },
                Father = new Person()
                {
                    FirstName = "Warren",
                    LastName  = "Rich",
                    Age       = 36,
                    Mother    = new Person()
                    {
                        FirstName = "Elsa",
                        LastName  = "Johnson",
                        Age       = 65
                    },
                    Father = new Person()
                    {
                        FirstName = "Gustav",
                        LastName  = "Rich",
                        Age       = 66
                    }
                }
            }));
            test.ConsoleAssert.WritesOut(
                Lambda(Expr(_printer, _person, (p1, p2) => p1.PrintFamily(p2))),
                Const(expectedOutput));
            test.Execute();
        }