Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Who cares about Domain Rules?

, 20 Nov 2006
With this article, I am evolving a domain problem towards the best possible solution.
domainrules_src.zip
BusinessRules
0 - Procedural_Way
0 - Procedural_Way.smp
ProceduralWay.cd
Tests
1 - Evolution One
1 - Evolution One.smp
EvolutionOne.cd
Tests
2 - Evolution Two
2 - Evolution Two.smp
EvolutionTwo.cd
Tests
3 - Evolution Three
3 - Evolution Three.smp
EvolutionThree.cd
IncomeTaxEngine.cd
Tests
4 - Evolution Four
4 - Evolution Four.smp
EvolutionFour.cd
Tests
5 - Evolution Five
5 - Evolution Five.smp
EvolutionFive.cd
TaxBandGenerator.cd
Tests
bin
Debug
BusinessRules.dll
BusinessRules.dll.lastcodeanalysissucceeded
NMock2.dll
BusinessRules.csproj.user
obj
Debug
BusinessRules.dll
TempPE
Properties
Tools
NMock2.dll
nunit.framework.dll
/*
 * Created by: Maruis Marais
 * Created: Saturday, 11 November 2006
 */

using NMock2;

using NUnit.Framework;

namespace BusinessRules.Evolution_Five.Tests
{
    [TestFixture]
    public class IncomeTaxEngineFixture
    {
        private Mockery mocks = null;
        private ICurrentCultureInfo currentCulture = null;
        private IIncomeTaxEngine incomeTaxEngine = 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 TaxRateCalculatedCorrectlyForUSABands()
        {
            // Setup expectations for our mock.
            Expect.Once.On(currentCulture).GetProperty("CurrentCultureName").Will(Return.Value("en-US"));

            // Create the instance of the income tax engine
            incomeTaxEngine = new IncomeTaxEngine(currentCulture);

            // Validate the income tax engine works as expected

            Assert.AreEqual(0.10, incomeTaxEngine.CalculateTaxRate(0.0));
            Assert.AreEqual(0.10, incomeTaxEngine.CalculateTaxRate(1500.00));
            Assert.AreEqual(0.10, incomeTaxEngine.CalculateTaxRate(7550.99));

            Assert.AreEqual(0.15, incomeTaxEngine.CalculateTaxRate(7551.00));
            Assert.AreEqual(0.15, incomeTaxEngine.CalculateTaxRate(28000.00));
            Assert.AreEqual(0.15, incomeTaxEngine.CalculateTaxRate(30650.99));

            Assert.AreEqual(0.25, incomeTaxEngine.CalculateTaxRate(30651.00));
            Assert.AreEqual(0.25, incomeTaxEngine.CalculateTaxRate(65000.00));
            Assert.AreEqual(0.25, incomeTaxEngine.CalculateTaxRate(74200.99));

            Assert.AreEqual(0.28, incomeTaxEngine.CalculateTaxRate(74201.00));
            Assert.AreEqual(0.28, incomeTaxEngine.CalculateTaxRate(84526.00));
            Assert.AreEqual(0.28, incomeTaxEngine.CalculateTaxRate(154800.99));

            Assert.AreEqual(0.33, incomeTaxEngine.CalculateTaxRate(154801.00));
            Assert.AreEqual(0.33, incomeTaxEngine.CalculateTaxRate(205456.44));
            Assert.AreEqual(0.33, incomeTaxEngine.CalculateTaxRate(336550.99));

            Assert.AreEqual(0.35, incomeTaxEngine.CalculateTaxRate(336551.00));
            Assert.AreEqual(0.35, incomeTaxEngine.CalculateTaxRate(536551.00));
            Assert.AreEqual(0.35, incomeTaxEngine.CalculateTaxRate(double.MaxValue - 0.01));
        }

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

            // Create the instance of the income tax engine
            incomeTaxEngine = new IncomeTaxEngine(currentCulture);

            // Validate the income tax engine works as expected

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(0.0), 2.0);
            Assert.AreEqual(150.00, incomeTaxEngine.CalculateTaxLiability(1500.00), 2.0);
            Assert.AreEqual(755.10, incomeTaxEngine.CalculateTaxLiability(7550.99), 2.0);

            Assert.AreEqual(755.10, incomeTaxEngine.CalculateTaxLiability(7551.00), 2.0);
            Assert.AreEqual(3822.45, incomeTaxEngine.CalculateTaxLiability(28000.00), 2.0);
            Assert.AreEqual(4220.10, incomeTaxEngine.CalculateTaxLiability(30650.99), 2.0);

            Assert.AreEqual(4220.10, incomeTaxEngine.CalculateTaxLiability(30651.00), 2.0);
            Assert.AreEqual(12807.35, incomeTaxEngine.CalculateTaxLiability(65000.00), 2.0);
            Assert.AreEqual(15107.60, incomeTaxEngine.CalculateTaxLiability(74200.99), 2.0);

            Assert.AreEqual(15107.60, incomeTaxEngine.CalculateTaxLiability(74201.00), 2.0);
            Assert.AreEqual(17998.60, incomeTaxEngine.CalculateTaxLiability(84526.00), 2.0);
            Assert.AreEqual(37675.60, incomeTaxEngine.CalculateTaxLiability(154800.99), 2.0);

            Assert.AreEqual(37675.60, incomeTaxEngine.CalculateTaxLiability(154801.00), 2.0);
            Assert.AreEqual(54391.89, incomeTaxEngine.CalculateTaxLiability(205456.44), 2.0);
            Assert.AreEqual(97653.09, incomeTaxEngine.CalculateTaxLiability(336550.99), 2.0);

            Assert.AreEqual(97653.09, incomeTaxEngine.CalculateTaxLiability(336551.00), 2.0);
            Assert.AreEqual(167653.09, incomeTaxEngine.CalculateTaxLiability(536551.00), 2.0);
            Assert.AreEqual(6.2919259720181043E+307, incomeTaxEngine.CalculateTaxLiability(double.MaxValue - 0.01), 2.0);
        }

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

            // Create the instance of the income tax engine
            incomeTaxEngine = new IncomeTaxEngine(currentCulture);

            // Validate the income tax engine works as expected

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(0.0));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(1500.00));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(6000.99));

            Assert.AreEqual(0.15, incomeTaxEngine.CalculateTaxRate(6001.00));
            Assert.AreEqual(0.15, incomeTaxEngine.CalculateTaxRate(22258.32));
            Assert.AreEqual(0.15, incomeTaxEngine.CalculateTaxRate(25000.99));

            Assert.AreEqual(0.30, incomeTaxEngine.CalculateTaxRate(25001.00));
            Assert.AreEqual(0.30, incomeTaxEngine.CalculateTaxRate(65000.00));
            Assert.AreEqual(0.30, incomeTaxEngine.CalculateTaxRate(75000.99));

            Assert.AreEqual(0.40, incomeTaxEngine.CalculateTaxRate(75001.00));
            Assert.AreEqual(0.40, incomeTaxEngine.CalculateTaxRate(84526.00));
            Assert.AreEqual(0.40, incomeTaxEngine.CalculateTaxRate(150000.99));

            Assert.AreEqual(0.45, incomeTaxEngine.CalculateTaxRate(150001.00));
            Assert.AreEqual(0.45, incomeTaxEngine.CalculateTaxRate(205456.44));
            Assert.AreEqual(0.45, incomeTaxEngine.CalculateTaxRate(double.MaxValue - 0.01));
        }

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

            // Create the instance of the income tax engine
            incomeTaxEngine = new IncomeTaxEngine(currentCulture);

            // Validate the income tax engine works as expected

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(0.0), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(1500.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(6000.99), 2.0);

            Assert.AreEqual(0.15, incomeTaxEngine.CalculateTaxLiability(6001.00), 2.0);
            Assert.AreEqual(2438.60, incomeTaxEngine.CalculateTaxLiability(22258.32), 2.0);
            Assert.AreEqual(2850.00, incomeTaxEngine.CalculateTaxLiability(25000.99), 2.0);

            Assert.AreEqual(2850.00, incomeTaxEngine.CalculateTaxLiability(25001.00), 2.0);
            Assert.AreEqual(14849.70, incomeTaxEngine.CalculateTaxLiability(65000.00), 2.0);
            Assert.AreEqual(17850.00, incomeTaxEngine.CalculateTaxLiability(75000.99), 2.0);

            Assert.AreEqual(17850.00, incomeTaxEngine.CalculateTaxLiability(75001.00), 2.0);
            Assert.AreEqual(21660.00, incomeTaxEngine.CalculateTaxLiability(84526.00), 2.0);
            Assert.AreEqual(47850.00, incomeTaxEngine.CalculateTaxLiability(150000.99), 2.0);

            Assert.AreEqual(47850.00, incomeTaxEngine.CalculateTaxLiability(150001.00), 2.0);
            Assert.AreEqual(72804.94, incomeTaxEngine.CalculateTaxLiability(205456.44), 2.0);
            Assert.AreEqual(8.0896191068804208E+307, incomeTaxEngine.CalculateTaxLiability(double.MaxValue - 0.01), 2.0);
        }

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

            // Create the instance of the income tax engine
            incomeTaxEngine = new IncomeTaxEngine(currentCulture);

            // Validate the income tax engine works as expected

            Assert.AreEqual(0.195, incomeTaxEngine.CalculateTaxRate(0.0));
            Assert.AreEqual(0.195, incomeTaxEngine.CalculateTaxRate(9500.00));
            Assert.AreEqual(0.195, incomeTaxEngine.CalculateTaxRate(19500.00));
            Assert.AreEqual(0.195, incomeTaxEngine.CalculateTaxRate(19500.99));

            Assert.AreEqual(0.33, incomeTaxEngine.CalculateTaxRate(19501.00));
            Assert.AreEqual(0.33, incomeTaxEngine.CalculateTaxRate(28000.00));
            Assert.AreEqual(0.33, incomeTaxEngine.CalculateTaxRate(60000.00));
            Assert.AreEqual(0.33, incomeTaxEngine.CalculateTaxRate(60000.99));

            Assert.AreEqual(0.39, incomeTaxEngine.CalculateTaxRate(60001.00));
            Assert.AreEqual(0.39, incomeTaxEngine.CalculateTaxRate(65000.00));
            Assert.AreEqual(0.39, incomeTaxEngine.CalculateTaxRate(999999999.00));
        }

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

            // Create the instance of the income tax engine
            incomeTaxEngine = new IncomeTaxEngine(currentCulture);

            // Validate the income tax engine works as expected

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(0.0), 2.0);
            Assert.AreEqual(1852.50, incomeTaxEngine.CalculateTaxLiability(9500.00), 2.0);
            Assert.AreEqual(3802.50, incomeTaxEngine.CalculateTaxLiability(19500.00), 2.0);
            Assert.AreEqual(3802.69, incomeTaxEngine.CalculateTaxLiability(19500.99), 2.0);

            Assert.AreEqual(3802.70, incomeTaxEngine.CalculateTaxLiability(19501.00), 2.0);
            Assert.AreEqual(6607.36, incomeTaxEngine.CalculateTaxLiability(28000.00), 2.0);
            Assert.AreEqual(17167.36, incomeTaxEngine.CalculateTaxLiability(60000.00), 2.0);
            Assert.AreEqual(17167.69, incomeTaxEngine.CalculateTaxLiability(60000.99), 2.0);

            Assert.AreEqual(17167.69, incomeTaxEngine.CalculateTaxLiability(60001.00), 2.0);
            Assert.AreEqual(19117.30, incomeTaxEngine.CalculateTaxLiability(65000.00), 2.0);
            Assert.AreEqual(389993766.91, incomeTaxEngine.CalculateTaxLiability(999999999.00), 2.0);
            Assert.AreEqual(7.0110032259630313E+307, incomeTaxEngine.CalculateTaxLiability(double.MaxValue - 0.01), 2.0);
        }

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

            // Create the instance of the income tax engine
            incomeTaxEngine = new IncomeTaxEngine(currentCulture);

            // Validate the income tax engine works as expected

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(0.0));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(9500.00));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(19500.00));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(19500.99));

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(19501.00));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(28000.00));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(60000.00));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(60000.99));

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(60001.00));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(65000.00));
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxRate(999999999.00));
        }

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

            // Create the instance of the income tax engine
            incomeTaxEngine = new IncomeTaxEngine(currentCulture);

            // Validate the income tax engine works as expected

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(0.0), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(9500.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(19500.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(19500.99), 2.0);

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(19501.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(28000.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(60000.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(60000.99), 2.0);

            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(60001.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(65000.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(999999999.00), 2.0);
            Assert.AreEqual(0.0, incomeTaxEngine.CalculateTaxLiability(double.MaxValue - 0.01), 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

About the Author

Maruis Marais
Web Developer
New Zealand New Zealand
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 21 Nov 2006
Article Copyright 2006 by Maruis Marais
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid