예제 #1
0
        public void TestAccountDepositWithdraw()
        {
#if !DEBUG
            Assert.Multiple(() => {
#endif

            var accountClass = new TypeAssert<Account>();
            dynamic account = accountClass.New("John Smith", 20m);
            Assert.AreEqual(20m, account.Balance, "Initial balance");
            account.Deposit(15.5m);
            Assert.AreEqual(35.5m, account.Balance, "20 + 15.5");
            account = accountClass.New("John Smith", 20m);
            account.Withdraw(15.5m);
            Assert.AreEqual(4.5m, account.Balance, "20 - 15.5");
            account = accountClass.New("John Smith", 20m);
            account.Deposit(-0.5m);
            Assert.AreEqual(20m, account.Balance, "cannot deposit negative amount.");
            account = accountClass.New("John Smith", 20m);

            Action tester = () =>
            {
                account.Withdraw(25m);
            };
            string output = tester.Run();
            Assert.AreEqual(20m, account.Balance, "cannot deposit negative amount.");
            output.Assert("Withdrawal amount exceeded account balance.");
#if !DEBUG
            });
#endif
        }
예제 #2
0
        public void TestAccountClass()
        {
#if !DEBUG
            Assert.Multiple(() => {
#endif

            var accountClass = new TypeAssert<Account>();

            var balanceField = accountClass.Field<decimal>("_balance",
               BindingFlags.NonPublic |
               BindingFlags.Instance |
               BindingFlags.GetField |
               BindingFlags.SetField
               ).NonPublic();

            var nameProperty = accountClass.Property<string>("Name",
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.GetProperty);

            var balanceProperty = accountClass.Property<decimal>("Balance",
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.GetProperty);

            balanceProperty.NotAutoImplemented();

            var constructor = accountClass.Constructor(
                BindingFlags.Public | BindingFlags.Instance,
                new Param<string>("name"),
                new Param<decimal>("balance"));

            accountClass.Method("Deposit",
                BindingFlags.Public |
                BindingFlags.Instance,
                new Param<decimal>("depositAmount"));

            accountClass.Method("Withdraw",
                BindingFlags.Public |
                BindingFlags.Instance,
                new Param<decimal>("withdrawAmount")
                );

            // testing

            dynamic account;

            account = accountClass.New("Darth Vader", -1.1m);
            Assert.AreEqual(0m, balanceField.GetValue(account));
            Assert.AreEqual("Darth Vader", account.Name);
            Assert.AreEqual(0m, account.Balance);

            account = accountClass.New("Padme Amidala", 1.49m);
            Assert.AreEqual(1.49m, balanceField.GetValue(account));
            Assert.AreEqual("Padme Amidala", account.Name);
            Assert.AreEqual(1.49m, account.Balance);
#if !DEBUG
            });
#endif
        }
예제 #3
0
        public void TestParkInstanceMethods()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var park = new TypeAssert<Park>();
            dynamic actual = park.New(
                "Park 1",
                "123 Test Drive",
                (FacilityType)Enum.Parse(typeof(FacilityType), "National"),
                "444-432-9876", 5, 18, 12.34m);
            Assert.AreEqual("Park 1 National Park 5 AM to 6 PM $12.34", actual.Info);
            Assert.AreEqual("444-432-9876 123 Test Drive", actual.Contact);
            Assert.AreEqual(419.56m, actual.CalculateFee(34));

            park.Catch<ArgumentOutOfRangeException>(
                () => actual = park.New(
                "Park 1",
                "123 Test Drive",
                (FacilityType)Enum.Parse(typeof(FacilityType), "National"),
                "444-432-9876", 6, 5, 12.34m)
            );
#if !DEBUG
});
#endif
        }
예제 #4
0
        public void TestCondoData()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var condo = new TypeAssert<Condo>();
            dynamic o;
            Catch<ArgumentOutOfRangeException>(() => condo.New("One Main Street", 1799, 100_000m, "A", 1m, false));
            Catch<ArgumentOutOfRangeException>(() => condo.New("Two Main Street", 2000, 0m, "A", 1m, true));
            Catch<ArgumentOutOfRangeException>(() => condo.New("Two Main Street", 2000, 100m, "A", -1m, false));
            Catch<ArgumentOutOfRangeException>(() => condo.New("Two Main Street", 2050, 100m, "A", 1m, false));
            o = condo.New("Two Main Street", 1800, 100_000m, "A", 2000m, false);
            Assert.AreEqual(2000m, o.Fee);
            Assert.AreEqual(100_000m, o.Price);
            Assert.AreEqual(102_000m, o.TotalCost);
            Assert.AreEqual(1800, o.YearBuilt);
            Assert.AreEqual("A", o.UnitNumber);
            Assert.IsFalse(o.IsRental);
            Catch<InvalidOperationException>(() => o.GetMonthlyRate());
            Assert.AreEqual("Two Main Street 1800 A $102,000.00", o.ToString());

            o.IsRental = true;
            Assert.AreEqual(2000m, o.Fee);
            Assert.AreEqual(100_000m, o.Price);
            Assert.AreEqual(102_000m, o.TotalCost);
            Assert.AreEqual(1800, o.YearBuilt);
            Assert.AreEqual("A", o.UnitNumber);
            Assert.IsTrue(o.IsRental);
            Assert.AreEqual(425, o.GetMonthlyRate());
            Assert.AreEqual("Two Main Street 1800 A $425.00", o.ToString());
#if !DEBUG
});
#endif
        }
예제 #5
0
        public void GPA()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var assert = new TypeAssert<Student>();
            assert.Catch<ArgumentOutOfRangeException>(() => assert.New("User", "Test", "7654321", 4.5m, "MIS"));

            dynamic student = assert.New("User", "Test", "7654321");
            assert.Catch<ArgumentOutOfRangeException>(
                () => student.GPA = -0.01m
            );
            assert.Catch<ArgumentOutOfRangeException>(
                () => student.GPA = 4.01m
            );
            student.GPA = 4.0m;
            Assert.AreEqual(4.0m, student.GPA);
            student.GPA = 0.0m;
            Assert.AreEqual(0.0m, student.GPA);
            student.GPA = 2.0m;
            Assert.AreEqual(2.0m, student.GPA);
            student.GPA = 3.0m;
            Assert.AreEqual(3.0m, student.GPA);
#if !DEBUG
});
#endif
        }
        public void TestTwoCalcData()
        {
            var     assert = new TypeAssert <TwoCalc>();
            dynamic calc;

            calc = assert.New(1, 2);
            Assert.AreEqual(5, calc.Calculate());
            Assert.AreEqual("1, 2", calc.ToString());
            calc = assert.New(-10, 4);
            Assert.AreEqual(-2, calc.Calculate());
            Assert.AreEqual("-10, 4", calc.ToString());
        }
예제 #7
0
        public void TestParkClassMethods()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var park = new TypeAssert<Park>();
            Park[] parks = new Park[] {
                park.New(
               "Park 1",
               "123 Test Drive",
               (FacilityType)Enum.Parse(typeof(FacilityType), "National"),
               "444-432-9876", 5, 11, 4.56m),
                park.New(
               "Park 2",
               "1 Park Street",
               (FacilityType)Enum.Parse(typeof(FacilityType), "Local"),
               "777-888-3332", 13, 18, 7.89m),
                park.New(
               "Park 3",
               "1 Midnight Lane",
               (FacilityType)Enum.Parse(typeof(FacilityType), "State"),
               "893-221-1234", 0, 12, 12.34m)
            };
            var show = park.Method(
                "Show",
                BindingFlags.Public |
                BindingFlags.Static,
                new Param<Park[]>("parks")
            );

            var calculate = park.Method<decimal>(
                "CalculateFee",
                BindingFlags.Public |
                BindingFlags.Static,
                new Param<int>("numberOfVisitors"),
                new Param<Park[]>("parks")
            );
            // Park.Show(parks);
            Action call = () => show.Invoke(null, new object[] { parks });
            string actual = call.Run();
            actual.Assert(
                "Park 1 National Park 5 AM to 11 AM $4.56 444-432-9876 123 Test Drive",
                "Park 2 Local Park 1 PM to 6 PM $7.89 777-888-3332 1 Park Street",
                "Park 3 State Park 12 AM to 12 PM $12.34 893-221-1234 1 Midnight Lane"
            );
            // decimal actualFee = Park.CalculateFee(765, parks);
            decimal actualFee = (decimal)calculate.Invoke(null, new object[] { 765, parks });
            Assert.AreEqual(18964.35m, actualFee);
#if !DEBUG
});
#endif
        }
        public void TestSingleFamilyData()
        {
            var     singleFamily = new TypeAssert <SingleFamily>();
            dynamic o;

            Catch <ArgumentOutOfRangeException>(() => singleFamily.New("One Main Street", 1799, 100_000m));
            Catch <ArgumentOutOfRangeException>(() => singleFamily.New("Two Main Street", 2000, 0m));
            o = singleFamily.New("Two Main Street", 1800, 100_000m);
            Assert.AreEqual(100_000m, o.Price);
            Assert.AreEqual(100_000m, o.TotalCost);
            Assert.AreEqual(1800, o.YearBuilt);
            Assert.AreEqual("Two Main Street 1800 $100,000.00", o.ToString());
        }
예제 #9
0
        public void TestTwoCalc()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var assert = new TypeAssert<TwoCalc>();
            assert.Class();
            assert.Implements<ICalc>();
            assert.Constructor(
                BindingFlags.Public |
                BindingFlags.Instance,
                new Param<int>("a"),
                new Param<int>("b")
            );

            assert.Field<int>(
                "_a",
                BindingFlags.NonPublic |
                BindingFlags.Instance
            ).Private().ReadOnly();

            assert.Field<int>(
                "_b",
                BindingFlags.NonPublic |
                BindingFlags.Instance
            ).Private().ReadOnly();

            assert.Method<int>(
                "Calculate",
                BindingFlags.Public |
                BindingFlags.Instance
            ).Virtual();

            assert.Method<string>(
                "ToString",
                BindingFlags.Public |
                BindingFlags.Instance
            ).Override();

            dynamic calc;
            calc = assert.New(1, 2);
            Assert.AreEqual(5, calc.Calculate());
            Assert.AreEqual("1, 2", calc.ToString());
            calc = assert.New(-10, 4);
            Assert.AreEqual(-2, calc.Calculate());
            Assert.AreEqual("-10, 4", calc.ToString());
#if !DEBUG
    });
#endif
        }
        public void TestAggregateData()
        {
            var average = new TypeAssert <Average>();

            Assert.Catch <ArgumentNullException>(() => average.New(null));
            int[]   array = { 1, 4, 2, 7, 3, 7, 2, 2, 6, 3, -3, 2 };
            dynamic o     = average.New(array);

            Assert.AreEqual(1, o.GetValue(1));
            Assert.AreEqual(4, o.GetValue(6));
            Assert.AreEqual(3, o.GetValue(30));
            Assert.Catch <DivideByZeroException>(() => o.GetValue(0));
            Assert.Catch <ArgumentOutOfRangeException>(() => o.GetValue(-1));
            Assert.AreEqual("1 4 2 7 3 7 2 2 6 3 -3 2", o.ToString());
        }
        public void Debit()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var assert = new TypeAssert<CheckingAccount>();
            assert.Method<bool>(
                "Debit",
                BindingFlags.Instance |
                BindingFlags.Public,
                new Param<decimal>("amount")
            ).Override();

            decimal fee = 3.5m;
            dynamic o;
            // new CheckingAccount(1000m, fee);
            o = assert.New(1000m, fee);
            Assert.IsTrue(o.Debit(500m));
            Assert.AreEqual(496.5m, o.Balance);
            Assert.IsFalse(o.Debit(494.0m));
            Assert.AreEqual(496.5m, o.Balance);
#if !DEBUG
});
#endif
        }
        public void Constructor()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var assert = new TypeAssert<CheckingAccount>();
            assert.Extends<Account>();
            assert.Constructor(
                BindingFlags.Instance |
                BindingFlags.Public,
                new Param<decimal>("initialBalance"),
                new Param<decimal>("transactionFee")
            );

            assert.NonConstructor(
                BindingFlags.Instance |
                BindingFlags.Public,
                new Param<decimal>("initialBalance")
            );
            // new CheckingAccount(11.5m, -0.05m);
            assert.Catch<ArgumentOutOfRangeException>(() => assert.New(11.5m, -0.05m));
#if !DEBUG
        });
#endif
        }
예제 #13
0
        public void Debit()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var assert = new TypeAssert<SavingsAccount>();
            assert.Method<bool>(
                "Debit",
                BindingFlags.Instance |
                BindingFlags.Public,
                new Param<decimal>("amount")
            ).Override();

            decimal rate = 0.035m;
            dynamic o;
            // new SavingsAccount(1000m, rate);
            o = assert.New(1000m, rate);
            Assert.IsTrue(o.Debit(491m));
            Assert.AreEqual(509m, o.Balance);
            Assert.IsFalse(o.Debit(509.01m));
            Assert.AreEqual(509m, o.Balance);
            Assert.IsTrue(o.Debit(509m));
            Assert.AreEqual(0m, o.Balance);
#if !DEBUG
});
#endif
        }
        public void TestSquareData()
        {
            var assert     = new TypeAssert <Square>();
            var dimension1 = assert.Field <int>(
                "Dimension1",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            var dimension2 = assert.Field <int>(
                "Dimension2",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            // var obj = new Square(1, 2 ,3);
            dynamic obj = assert.New(1, 2, 3);

            Assert.AreEqual("Square", obj.Name);
            Assert.AreEqual(1, obj.X);
            Assert.AreEqual(2, obj.Y);
            Assert.AreEqual(3, obj.Side);
            Assert.AreEqual(3, dimension1.GetValue(obj));
            Assert.AreEqual(3, dimension2.GetValue(obj));
            Assert.AreEqual(9.0, obj.Area, 1E-10);
            Assert.AreEqual("(1, 2) side: 3", obj.ToString());

            obj.Side = 7;
            Assert.AreEqual(7, dimension1.GetValue(obj));
            Assert.AreEqual(7, dimension2.GetValue(obj));
            Assert.AreEqual(49.0, obj.Area, 1E-10);
            Assert.AreEqual("(1, 2) side: 7", obj.ToString());
        }
        public void TestInvoiceObject()
        {
#if !DEBUG
            Assert.Multiple(() => {
#endif
            var invoiceClass = new TypeAssert<Invoice>();

            dynamic invoice = invoiceClass.New("1234", "Hammer", 2, 14.95m);
            Assert.AreEqual("1234", invoice.PartNumber, "Initial Part Number");
            Assert.AreEqual("Hammer", invoice.PartDescription, "Initial Part Description");
            Assert.AreEqual(2, invoice.Quantity, "Initial Quantity");
            Assert.AreEqual(14.95m, invoice.PricePerItem, "Initial Price Per Item");
            Assert.AreEqual(29.90m, invoice.GetInvoiceAmount(), "Initial GetInvoiceAmount()");

            invoice.PartNumber = "001234";
            invoice.PartDescription = "Yellow Hammer";
            invoice.Quantity = 3;
            invoice.PricePerItem = 19.49m;

            Assert.AreEqual("001234", invoice.PartNumber, "Updated Part Number");
            Assert.AreEqual("Yellow Hammer", invoice.PartDescription, "Updated Part Description");
            Assert.AreEqual(3, invoice.Quantity, "Updated Quantity");
            Assert.AreEqual(19.49m, invoice.PricePerItem, "Updated Price Per Item");
            Assert.AreEqual(58.47m, invoice.GetInvoiceAmount(), "Updated GetInvoiceAmount()");

            invoice.Quantity = -4;
            invoice.PricePerItem = -0.01m;
            Assert.AreEqual(3, invoice.Quantity, "Updated Quantity with a negative value");
            Assert.AreEqual(19.49m, invoice.PricePerItem, "Updated Price Per Item with a negative value");
            Assert.AreEqual(58.47m, invoice.GetInvoiceAmount(), "Updated GetInvoiceAmount() after alterations");
#if !DEBUG
            });
#endif
        }
        public void Constructor()
        {
            var assert = new TypeAssert <Account>();

            assert.Constructor(
                BindingFlags.Instance |
                BindingFlags.Public,
                new Param <decimal>("balance")
                );
            dynamic account;

            // new Account(1000.5m);
            account = assert.New(1000.5m);
            Assert.AreEqual(1000.5m, account.Balance);
            assert.Catch <ArgumentOutOfRangeException>(() => account = assert.New(-0.5m));
        }
예제 #17
0
        public void Balance()
        {
#if !DEBUG
            Assert.Multiple(() =>
            {
#endif
            var assert = new TypeAssert<Account>();
            var p = assert.Property<decimal>(
                "Balance",
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.GetProperty |
                BindingFlags.SetProperty
            ).NonVirtual();

            dynamic account;
            account = assert.New(0m);
            Assert.AreEqual(0m, account.Balance);
            account.Balance = 123.45m;
            Assert.AreEqual(123.45m, account.Balance);
            assert.Catch<ArgumentOutOfRangeException>(() => account.Balance = -0.05m);
            Assert.AreEqual(123.45m, account.Balance);
#if !DEBUG
            });
#endif
        }
예제 #18
0
        public void Debit()
        {
#if !DEBUG
            Assert.Multiple(() =>
            {
#endif
            var assert = new TypeAssert<Account>();
            var p = assert.Method<bool>(
                "Debit",
                BindingFlags.Instance |
                BindingFlags.Public,
                new Param<decimal>("amount")
            ).Virtual();

            dynamic account;
            account = assert.New(100m);
            Assert.AreEqual(100m, account.Balance);
            bool debit = account.Debit(23.45m);
            Assert.AreEqual(76.55m, account.Balance);
            Assert.IsTrue(debit);
            debit = account.Debit(76.56m);
            Assert.IsFalse(debit);
            Assert.AreEqual(76.55m, account.Balance);
            assert.Catch<ArgumentOutOfRangeException>(() => account.Debit(-0.01m));
            Assert.AreEqual(76.55m, account.Balance);
#if !DEBUG
            });
#endif
        }
예제 #19
0
        public void TestCircleData()
        {
            var assert = new TypeAssert <Circle>();

            var dimension1 = assert.Field <int>(
                "Dimension1",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            var dimension2 = assert.Field <int>(
                "Dimension2",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            // var obj = new Circle(1, 2 ,3);
            dynamic obj = assert.New(1, 2, 3);

            Assert.AreEqual("Circle", obj.Name);
            Assert.AreEqual(1, obj.X);
            Assert.AreEqual(2, obj.Y);
            Assert.AreEqual(3, obj.Radius);
            Assert.AreEqual(3, dimension1.GetValue(obj));
            Assert.AreEqual(3, dimension2.GetValue(obj));
            Assert.AreEqual(28.274333882308138, obj.Area, 1E-10);
            Assert.AreEqual("(1, 2) radius: 3", obj.ToString());

            obj.Radius = 7;
            Assert.AreEqual(7, dimension1.GetValue(obj));
            Assert.AreEqual(7, dimension2.GetValue(obj));
            Assert.AreEqual(153.93804002589985, obj.Area, 1E-10);
            Assert.AreEqual("(1, 2) radius: 7", obj.ToString());
        }
예제 #20
0
        public void NewMoney()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var assert = new TypeAssert<Money>();
            dynamic money;
            // money = new Money(12, 34);
            money = assert.New(12, 34);
            Assert.AreEqual(12, money.Dollars);
            Assert.AreEqual(34, money.Cents);
            Assert.AreEqual($"{12.34:C}", money.ToString());
            assert.Catch<ArgumentOutOfRangeException>(() => money = assert.New(-1, 99));
#if !DEBUG
    });
#endif
        }
        public void TestThreeCalcData()
        {
            var     assert = new TypeAssert <ThreeCalc>();
            dynamic calc;

            calc = assert.New(1, 2, 3);
            Assert.AreEqual(14, calc.Calculate());
            Assert.AreEqual("1, 2, 3", calc.ToString());

            calc = assert.New(1, 3, 5);
            Assert.AreEqual(22, calc.Calculate());
            Assert.AreEqual("1, 3, 5", calc.ToString());

            calc = assert.New(-10, 4, -100);
            Assert.AreEqual(-302, calc.Calculate());
            Assert.AreEqual("-10, 4, -100", calc.ToString());
        }
        public void TestCountData()
        {
            var count = new TypeAssert <Count>();

            Assert.Catch <ArgumentNullException>(() => count.New(null));
            int[]   array = { 1, 4, 2, 7, 3, 7, 2, 2, 6, 3, -3, 2 };
            dynamic o     = count.New(array);

            Assert.AreEqual(0, o.GetValue(0));
            Assert.AreEqual(1, o.GetValue(1));
            Assert.AreEqual(4, o.GetValue(2));
            Assert.AreEqual(2, o.GetValue(3));
            Assert.AreEqual(1, o.GetValue(4));
            Assert.AreEqual(0, o.GetValue(5));
            Assert.AreEqual(1, o.GetValue(6));
            Assert.AreEqual(2, o.GetValue(7));
            Assert.AreEqual(0, o.GetValue(8));
            Assert.AreEqual("1 4 2 7 3 7 2 2 6 3 -3 2", o.ToString());
        }
예제 #23
0
        public void TestHomeShow()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var home = new TypeAssert<Home>();
            var show = home.Method(
                "Show",
                BindingFlags.Public |
                BindingFlags.Static,
                new Param<Home[]>("homes")
                {
                    Params = true
                }
            );
            var condo = new TypeAssert<Condo>();
            var singleFamily = new TypeAssert<SingleFamily>();
            dynamic condo1, condo2;
            dynamic[] homes = {
                singleFamily.New("Two Main Street", 1800, 100_000m),
                condo1 = condo.New("One Main Street", 1900, 100_000m, "A", 2000m, false),
                condo2 = condo.New("Three Main Street", 2000, 200_000m, "A", 1000m, true),
                singleFamily.New("Four Main Street", 2010, 300_000m)
            };

            // Asserting: Home.Show(homes);
            Action showApp = () => show.Invoke(null, new object[] { homes.Cast<Home>().ToArray() });
            var actual = showApp.Run();
            actual.Assert(
                "Two Main Street 1800 $100,000.00",
                "One Main Street 1900 A $102,000.00",
                "One Main Street 1900 A $425.00",
                "Three Main Street 2000 A $201,000.00",
                "Three Main Street 2000 A $837.50",
                "Four Main Street 2010 $300,000.00"
            );
            Assert.IsFalse(condo1.IsRental, "The rental option must be reverted");
            Assert.IsTrue(condo2.IsRental, "The rental option must be reverted");
#if !DEBUG
});
#endif
        }
예제 #24
0
        public void NewStudent()
        {
#if !DEBUG
        Assert.Multiple(() => {
#endif
            var assert = new TypeAssert<Student>();
            dynamic student = assert.New("Test", "User", "1234567");
            Assert.AreEqual("Test", student.FirstName);
            Assert.AreEqual("User", student.LastName);
            Assert.AreEqual("1234567", student.StudentNumber);

            student = assert.New("User", "Test", "7654321", 3.5m, "MIS");
            Assert.AreEqual("User", student.FirstName);
            Assert.AreEqual("Test", student.LastName);
            Assert.AreEqual("7654321", student.StudentNumber);
            Assert.AreEqual(3.5m, student.GPA);
            Assert.AreEqual("MIS", student.Major);
#if !DEBUG
    });
#endif
        }
예제 #25
0
        public void TestAppend()
        {
#if !DEBUG
            Assert.Multiple(() =>
            {
#endif      
            var count = new TypeAssert<Count>();
            var sum = new TypeAssert<Sum>();
            var average = new TypeAssert<Average>();
            dynamic a = count.New(new int[] { 1, 2, 3, 4 });
            dynamic b = sum.New(new int[] { 5, 6, 7 });
            dynamic c = average.New(new int[] { 4, 3, 2, 1, 6 });
            average.Catch<ArgumentNullException>(() => average.New(null));
            average.Catch<ArgumentNullException>(() => sum.New(null));
            average.Catch<ArgumentNullException>(() => count.New(null));
            average.Catch<ArgumentOutOfRangeException>(() => average.New(new int[0]));

            average.Catch<ArgumentNullException>(() => a.Append(null));
            average.Catch<ArgumentNullException>(() => b.Append(null));
            average.Catch<ArgumentNullException>(() => c.Append(null));
            a.Append(b);
            Assert.AreEqual("1 2 3 4 5 6 7", a.ToString());
            Assert.AreEqual(7, a.Value);
            Assert.AreEqual("5 6 7", b.ToString());
            Assert.AreEqual(18, b.Value);
            a = sum.New(new[] { 3, 2 });
            b.Append(a);
            Assert.AreEqual("8 8 7", b.ToString());
            Assert.AreEqual(23, b.Value);
            b.Append(c);
            Assert.AreEqual("12 11 9 1 6", b.ToString());
            Assert.AreEqual(39, b.Value);
            Assert.AreEqual("4 3 2 1 6", c.ToString());
            Assert.AreEqual(3, c.Value);
#if !DEBUG
            });
#endif
        }
예제 #26
0
        public void CalculateInterest()
        {
            var assert = new TypeAssert <SavingsAccount>();

            assert.Method <decimal>(
                "CalculateInterest",
                BindingFlags.Instance |
                BindingFlags.Public
                ).DeclaredIn <SavingsAccount>();

            dynamic o;
            decimal rate = 0.035m;

            o = assert.New(1234m, rate);
            Assert.AreEqual(43.19m, o.CalculateInterest());
        }
        public void TestEmployeeClass()
        {
#if !DEBUG
            Assert.Multiple(() => {
#endif
            var employeeClass = new TypeAssert<Employee>();
            var firstName = employeeClass.Property<string>(
                "FirstName",
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.GetProperty
                ).AutoImplemented();
            var lastName = employeeClass.Property<string>(
                "LastName",
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.GetProperty
                ).AutoImplemented();
            var monthlySalaryField = employeeClass.Field<decimal>(
                "_monthlySalary",
                BindingFlags.NonPublic |
                BindingFlags.Instance |
                BindingFlags.GetField
                );
            var monthlySalary = employeeClass.Property<decimal>(
                "MonthlySalary",
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.GetProperty |
                BindingFlags.SetProperty
                ).NotAutoImplemented();

            var annualSalary = employeeClass.Property<decimal>(
                "AnnualSalary",
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.GetProperty
                ).NotAutoImplemented();

            Employee employee = employeeClass.New("A", "B", 1234.56m);
            Assert.AreEqual(1234.56m, monthlySalaryField.GetValue(employee), "Private field must contain an intial value.");

#if !DEBUG
            });
#endif
        }
예제 #28
0
        public void TestSphereData()
        {
            var assert = new TypeAssert <Sphere>();
            // var obj = new Sphere(1, 2 ,3);

            var dimension1 = assert.Field <int>(
                "Dimension1",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            var dimension2 = assert.Field <int>(
                "Dimension2",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            var dimension3 = assert.Field <int>(
                "Dimension3",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            dynamic obj = assert.New(2, 1, 4);

            Assert.AreEqual("Sphere", obj.Name);
            Assert.AreEqual(2, obj.X);
            Assert.AreEqual(1, obj.Y);
            Assert.AreEqual(4, obj.Radius);
            Assert.AreEqual(4, dimension1.GetValue(obj));
            Assert.AreEqual(4, dimension2.GetValue(obj));
            Assert.AreEqual(4, dimension3.GetValue(obj));
            Assert.AreEqual(201.06192982974676, obj.Area, 1E-10);
            Assert.AreEqual(268.08257310632899, obj.Volume, 1E-10);
            Assert.AreEqual("(2, 1) radius: 4", obj.ToString());

            obj.Radius = 7;
            Assert.AreEqual(7, dimension1.GetValue(obj));
            Assert.AreEqual(7, dimension2.GetValue(obj));
            Assert.AreEqual(615.75216010359941, obj.Area, 1E-10);
            Assert.AreEqual(1436.7550402417321, obj.Volume, 1E-10);
            Assert.AreEqual("(2, 1) radius: 7", obj.ToString());
        }
        public void TestCubeData()
        {
            var assert     = new TypeAssert <Cube>();
            var dimension1 = assert.Field <int>(
                "Dimension1",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            var dimension2 = assert.Field <int>(
                "Dimension2",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            var dimension3 = assert.Field <int>(
                "Dimension3",
                BindingFlags.NonPublic |
                BindingFlags.Instance
                );

            // var obj = new Sphere(1, 2 ,3);
            dynamic obj = assert.New(3, 5, 2);

            Assert.AreEqual("Cube", obj.Name);
            Assert.AreEqual(3, obj.X);
            Assert.AreEqual(5, obj.Y);
            Assert.AreEqual(2, obj.Side);
            Assert.AreEqual(2, dimension1.GetValue(obj));
            Assert.AreEqual(2, dimension2.GetValue(obj));
            Assert.AreEqual(2, dimension3.GetValue(obj));
            Assert.AreEqual(24, obj.Area, 1E-10);
            Assert.AreEqual(8, obj.Volume, 1E-10);
            Assert.AreEqual("(3, 5) side: 2", obj.ToString());

            obj.Side = 7;
            Assert.AreEqual(7, dimension1.GetValue(obj));
            Assert.AreEqual(7, dimension2.GetValue(obj));
            Assert.AreEqual(294, obj.Area, 1E-10);
            Assert.AreEqual(343, obj.Volume, 1E-10);
            Assert.AreEqual("(3, 5) side: 7", obj.ToString());
        }
예제 #30
0
        public void Constructor()
        {
            var assert = new TypeAssert <SavingsAccount>();

            assert.Extends <Account>();
            assert.Constructor(
                BindingFlags.Instance |
                BindingFlags.Public,
                new Param <decimal>("initialBalance"),
                new Param <decimal>("transactionFee")
                );

            assert.NonConstructor(
                BindingFlags.Instance |
                BindingFlags.Public,
                new Param <decimal>("initialBalance")
                );
            // new SavingsAccount(11.5m, -0.05m);
            assert.Catch <ArgumentOutOfRangeException>(() => assert.New(11.5m, -0.05m));
        }