Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / C#

Who cares about Domain Rules?

Rate me:
Please Sign up or sign in to vote.
4.96/5 (88 votes)
20 Nov 2006MIT12 min read 96.2K   85   118  
With this article, I am evolving a domain problem towards the best possible solution.
using NMock2;

using NUnit.Framework;

namespace BusinessRules.Evolution_Two.Tests
{
    [TestFixture]
    public class InvestorFixture
    {
        private Mockery mocks = null;
        private ICurrentCultureInfo currentCulture = null;
        private IInvestor investor = null;

        /// <summary>
        /// The Setup method is called before each test method is called. 
        /// </summary>
        [SetUp]
        public void Setup()
        {
            mocks = new Mockery();
            currentCulture = mocks.NewMock<ICurrentCultureInfo>();
        }

        [Test]
        public void TaxRateCalculatedCorrectlyForUS()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-US"));

            // Create the instance of the investor
            investor = Investor.CreateInvestor(currentCulture, 0.0);

            // Validate the calculations works as expected

            Assert.AreEqual(0.10, investor.TaxRate);
            investor.Income = 1500.00;
            Assert.AreEqual(0.10, investor.TaxRate);
            investor.Income = 7550.99;
            Assert.AreEqual(0.10, investor.TaxRate);

            investor.Income = 7551.00;
            Assert.AreEqual(0.15, investor.TaxRate);
            investor.Income = 28000.00;
            Assert.AreEqual(0.15, investor.TaxRate);
            investor.Income = 30650.99;
            Assert.AreEqual(0.15, investor.TaxRate);

            investor.Income = 30651.00;
            Assert.AreEqual(0.25, investor.TaxRate);
            investor.Income = 65000.00;
            Assert.AreEqual(0.25, investor.TaxRate);
            investor.Income = 74200.99;
            Assert.AreEqual(0.25, investor.TaxRate);

            investor.Income = 74201.00;
            Assert.AreEqual(0.28, investor.TaxRate);
            investor.Income = 84526.00;
            Assert.AreEqual(0.28, investor.TaxRate);
            investor.Income = 154800.99;
            Assert.AreEqual(0.28, investor.TaxRate);

            investor.Income = 154801.00;
            Assert.AreEqual(0.33, investor.TaxRate);
            investor.Income = 205456.44;
            Assert.AreEqual(0.33, investor.TaxRate);
            investor.Income = 336550.99;
            Assert.AreEqual(0.33, investor.TaxRate);

            investor.Income = 336551.00;
            Assert.AreEqual(0.35, investor.TaxRate);
            investor.Income = 536551.00;
            Assert.AreEqual(0.35, investor.TaxRate);
            investor.Income = double.MaxValue - 0.01;
            Assert.AreEqual(0.35, investor.TaxRate);
        }

        [Test]
        public void TaxLiabilityCalculatedCorrectlyForUS()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-US"));

            // Create the instance of the investor
            investor = Investor.CreateInvestor(currentCulture, 0.0);

            // Validate the calculations works as expected

            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 1500.00;
            Assert.AreEqual(150.00, investor.TaxLiability, 2.0);
            investor.Income = 7550.99;
            Assert.AreEqual(755.10, investor.TaxLiability, 2.0);

            investor.Income = 7551.00;
            Assert.AreEqual(755.10, investor.TaxLiability, 2.0);
            investor.Income = 28000.00;
            Assert.AreEqual(3822.45, investor.TaxLiability, 2.0);
            investor.Income = 30650.99;
            Assert.AreEqual(4220.10, investor.TaxLiability, 2.0);

            investor.Income = 30651.00;
            Assert.AreEqual(4220.10, investor.TaxLiability, 2.0);
            investor.Income = 65000.00;
            Assert.AreEqual(12807.35, investor.TaxLiability, 2.0);
            investor.Income = 74200.99;
            Assert.AreEqual(15107.60, investor.TaxLiability, 2.0);

            investor.Income = 74201.00;
            Assert.AreEqual(15107.60, investor.TaxLiability, 2.0);
            investor.Income = 84526.00;
            Assert.AreEqual(17998.60, investor.TaxLiability, 2.0);
            investor.Income = 154800.99;
            Assert.AreEqual(37675.60, investor.TaxLiability, 2.0);

            investor.Income = 154801.00;
            Assert.AreEqual(37675.60, investor.TaxLiability, 2.0);
            investor.Income = 205456.44;
            Assert.AreEqual(54391.89, investor.TaxLiability, 2.0);
            investor.Income = 336550.99;
            Assert.AreEqual(97653.09, investor.TaxLiability, 2.0);

            investor.Income = 336551.00;
            Assert.AreEqual(97653.09, investor.TaxLiability, 2.0);
            investor.Income = 536551.00;
            Assert.AreEqual(167653.09, investor.TaxLiability, 2.0);
            investor.Income = double.MaxValue - 0.01;
            Assert.AreEqual(6.2919259720181043E+307, investor.TaxLiability, 2.0);
        }

        [Test]
        public void TaxRateCalculatedCorrectlyForAustralia()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-AU"));

            // Create the instance of the investor
            investor = Investor.CreateInvestor(currentCulture, 0.0);

            // Validate the calculations works as expected

            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 1500.00;
            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 6000.99;
            Assert.AreEqual(0.0, investor.TaxRate);

            investor.Income = 6001.00;
            Assert.AreEqual(0.15, investor.TaxRate);
            investor.Income = 22258.32;
            Assert.AreEqual(0.15, investor.TaxRate);
            investor.Income = 25000.99;
            Assert.AreEqual(0.15, investor.TaxRate);

            investor.Income = 25001.00;
            Assert.AreEqual(0.30, investor.TaxRate);
            investor.Income = 65000.00;
            Assert.AreEqual(0.30, investor.TaxRate);
            investor.Income = 75000.99;
            Assert.AreEqual(0.30, investor.TaxRate);

            investor.Income = 75001.00;
            Assert.AreEqual(0.40, investor.TaxRate);
            investor.Income = 84526.00;
            Assert.AreEqual(0.40, investor.TaxRate);
            investor.Income = 150000.99;
            Assert.AreEqual(0.40, investor.TaxRate);

            investor.Income = 150001.00;
            Assert.AreEqual(0.45, investor.TaxRate);
            investor.Income = 205456.44;
            Assert.AreEqual(0.45, investor.TaxRate);
            investor.Income = double.MaxValue - 0.01;
            Assert.AreEqual(0.45, investor.TaxRate);
        }

        [Test]
        public void TaxLiabilityCalculatedCorrectlyForAustralia()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-AU"));

            // Create the instance of the investor
            investor = Investor.CreateInvestor(currentCulture, 0.0);

            // Validate the calculations works as expected

            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 1500.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 6000.99;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);

            investor.Income = 6001.00;
            Assert.AreEqual(0.15, investor.TaxLiability, 2.0);
            investor.Income = 22258.32;
            Assert.AreEqual(2438.60, investor.TaxLiability, 2.0);
            investor.Income = 25000.99;
            Assert.AreEqual(2850.00, investor.TaxLiability, 2.0);

            investor.Income = 25001.00;
            Assert.AreEqual(2850.00, investor.TaxLiability, 2.0);
            investor.Income = 65000.00;
            Assert.AreEqual(14849.70, investor.TaxLiability, 2.0);
            investor.Income = 75000.99;
            Assert.AreEqual(17850.00, investor.TaxLiability, 2.0);

            investor.Income = 75001.00;
            Assert.AreEqual(17850.00, investor.TaxLiability, 2.0);
            investor.Income = 84526.00;
            Assert.AreEqual(21660.00, investor.TaxLiability, 2.0);
            investor.Income = 150000.99;
            Assert.AreEqual(47850.00, investor.TaxLiability, 2.0);

            investor.Income = 150001.00;
            Assert.AreEqual(47850.00, investor.TaxLiability, 2.0);
            investor.Income = 205456.44;
            Assert.AreEqual(72804.94, investor.TaxLiability, 2.0);
            investor.Income = double.MaxValue - 0.01;
            Assert.AreEqual(8.0896191068804208E+307, investor.TaxLiability, 2.0);
        }

        [Test]
        public void TaxRateCalculatedCorrectlyForNewZealand()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-NZ"));

            // Create the instance of the investor
            investor = Investor.CreateInvestor(currentCulture, 0.0);

            // Validate the calculations works as expected

            Assert.AreEqual(0.195, investor.TaxRate);
            investor.Income = 9500.00;
            Assert.AreEqual(0.195, investor.TaxRate);
            investor.Income = 19500.00;
            Assert.AreEqual(0.195, investor.TaxRate);
            investor.Income = 19500.99;
            Assert.AreEqual(0.195, investor.TaxRate);

            investor.Income = 19501.00;
            Assert.AreEqual(0.33, investor.TaxRate);
            investor.Income = 28000.00;
            Assert.AreEqual(0.33, investor.TaxRate);
            investor.Income = 60000.00;
            Assert.AreEqual(0.33, investor.TaxRate);
            investor.Income = 60000.99;
            Assert.AreEqual(0.33, investor.TaxRate);

            investor.Income = 60001.00;
            Assert.AreEqual(0.39, investor.TaxRate);
            investor.Income = 65000.00;
            Assert.AreEqual(0.39, investor.TaxRate);
            investor.Income = 999999999.00;
            Assert.AreEqual(0.39, investor.TaxRate);
        }

        [Test]
        public void TaxLiabilityCalculatedCorrectlyForNewZealand()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-NZ"));

            // Create the instance of the investor
            investor = Investor.CreateInvestor(currentCulture, 0.0);

            // Validate the calculations works as expected

            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 9500.00;
            Assert.AreEqual(1852.50, investor.TaxLiability, 2.0);
            investor.Income = 19500.00;
            Assert.AreEqual(3802.50, investor.TaxLiability, 2.0);
            investor.Income = 19500.99;
            Assert.AreEqual(3802.69, investor.TaxLiability, 2.0);

            investor.Income = 19501.00;
            Assert.AreEqual(3802.70, investor.TaxLiability, 2.0);
            investor.Income = 28000.00;
            Assert.AreEqual(6607.36, investor.TaxLiability, 2.0);
            investor.Income = 60000.00;
            Assert.AreEqual(17167.36, investor.TaxLiability, 2.0);
            investor.Income = 60000.99;
            Assert.AreEqual(17167.69, investor.TaxLiability, 2.0);

            investor.Income = 60001.00;
            Assert.AreEqual(17167.69, investor.TaxLiability, 2.0);
            investor.Income = 65000.00;
            Assert.AreEqual(19117.30, investor.TaxLiability, 2.0);
            investor.Income = 999999999.00;
            Assert.AreEqual(389993766.91, investor.TaxLiability, 2.0);
            investor.Income = double.MaxValue - 0.01;
            Assert.AreEqual(7.0110032259630313E+307, investor.TaxLiability, 2.0);
        }

        [Test]
        public void TaxRateCalculatedCorrectlyForNonExceptionalBehavior()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-ZA"));

            // Create the instance of the investor
            investor = Investor.CreateInvestor(currentCulture, 0.0);

            // Validate the calculations works as expected

            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 9500.00;
            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 19500.00;
            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 19500.99;
            Assert.AreEqual(0.0, investor.TaxRate);

            investor.Income = 19501.00;
            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 28000.00;
            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 60000.00;
            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 60000.99;
            Assert.AreEqual(0.0, investor.TaxRate);

            investor.Income = 60001.00;
            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 65000.00;
            Assert.AreEqual(0.0, investor.TaxRate);
            investor.Income = 999999999.00;
            Assert.AreEqual(0.0, investor.TaxRate);
        }

        [Test]
        public void TaxLiabilityCalculatedCorrectlyForNonExceptionalBehavior()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-ZA"));

            // Create the instance of the investor
            investor = Investor.CreateInvestor(currentCulture, 0.0);

            // Validate the calculations works as expected

            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 9500.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 19500.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 19500.99;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);

            investor.Income = 19501.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 28000.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 60000.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 60000.99;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);

            investor.Income = 60001.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 65000.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = 999999999.00;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
            investor.Income = double.MaxValue - 0.01;
            Assert.AreEqual(0.0, investor.TaxLiability, 2.0);
        }

        /// <summary>
        /// The Teardown method is called after each test method is called. 
        /// So we validate all the expectations have been met.
        /// </summary>
        [TearDown]
        public void Teardown()
        {
            mocks.VerifyAllExpectationsHaveBeenMet();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Web Developer
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions