Ejemplo n.º 1
0
        public void TestInvalidOptions()
        {
            var requests = new FooCalculatorRequest[]
            {
                new FooCalculatorRequest {
                    A = false, B = false, C = false
                },
                new FooCalculatorRequest {
                    A = true, B = false, C = false
                },
                new FooCalculatorRequest {
                    A = false, B = true, C = false
                },
                //new FooCalculatorRequest { A = true, B = true, C = false }, // valid
                //new FooCalculatorRequest { A = false, B = true, C = true }, // valid
                //new FooCalculatorRequest { A = true, B = true, C = true }, // valid
                new FooCalculatorRequest {
                    A = false, B = false, C = true
                },
                //new FooCalculatorRequest { A = true, B = false, C = true }, // valid
            };

            foreach (var request in requests)
            {
                Assert.Throws <InvalidOptionException>(() => target.Calculate(request));
            }
        }
Ejemplo n.º 2
0
        protected override XEnum GetX(FooCalculatorRequest request)
        {
            if (request.A && request.B && !request.C)
            {
                return(XEnum.T);
            }
            else if (request.A && !request.B && request.C)
            {
                return(XEnum.S);
            }

            return(base.GetX(request));
        }
Ejemplo n.º 3
0
        public void TestStrategy_Invalid()
        {
            var request = new FooCalculatorRequest
            {
                A = true,
                B = false,
                C = true,

                D = 75,
                E = 50,
                F = 25
            };

            Assert.Throws <ArgumentException>(() => target.Calculate(request, "foobar"));
        }
Ejemplo n.º 4
0
        public virtual FooCalculatorResult Calculate(FooCalculatorRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var x = GetX(request);

            return(new FooCalculatorResult
            {
                X = x,
                Y = GetY(x, request)
            });
        }
Ejemplo n.º 5
0
        protected virtual XEnum GetX(FooCalculatorRequest request)
        {
            if (request.A && request.B && !request.C)
            {
                return(XEnum.S);
            }
            else if (request.A && request.B && request.C)
            {
                return(XEnum.R);
            }
            else if (!request.A && request.B && request.C)
            {
                return(XEnum.T);
            }

            throw new InvalidOptionException();
        }
Ejemplo n.º 6
0
        public void TestCalculate_A_NotB_C()
        {
            var request = new FooCalculatorRequest
            {
                A = true,
                B = false,
                C = true,

                D = 75,
                E = 50,
                F = 25
            };

            var result = target.Calculate(request);

            Assert.AreEqual(XEnum.S, result.X);
            Assert.AreEqual(137.5, result.Y);
        }
Ejemplo n.º 7
0
        public void TestStrategy_Specialized1_Calculate_A_B_NotC()
        {
            var request = new FooCalculatorRequest
            {
                A = true,
                B = true,
                C = false,

                D = 75,
                E = 50,
                F = 25
            };

            var result = target.Calculate(request, "specialized1");

            Assert.AreEqual(XEnum.S, result.X);
            Assert.AreEqual(112.5, result.Y);
        }
Ejemplo n.º 8
0
        public void TestStrategy_Base_Calculate_NotA_B_C()
        {
            var request = new FooCalculatorRequest
            {
                A = false,
                B = true,
                C = true,

                D = 75,
                E = 50,
                F = 25
            };

            var result = target.Calculate(request, "base");

            Assert.AreEqual(XEnum.T, result.X);
            Assert.AreEqual(56.25, result.Y);
        }
Ejemplo n.º 9
0
        public void TestStrategy_Specialized2_Calculate_A_B_C()
        {
            var request = new FooCalculatorRequest
            {
                A = true,
                B = true,
                C = true,

                D = 75,
                E = 50,
                F = 25
            };

            var result = target.Calculate(request, "specialized2");

            Assert.AreEqual(XEnum.R, result.X);
            Assert.AreEqual(93.75, result.Y);
        }
Ejemplo n.º 10
0
        protected virtual decimal GetY(XEnum x, FooCalculatorRequest request)
        {
            // NOTE: We are purposely casting int > decimal in these formulas
            // so the correct results are calculated.
            switch (x)
            {
            case XEnum.S:
                return(sFormula(request.D, request.E, request.F));

            case XEnum.R:
                return(rFormula(request.D, request.E, request.F));

            case XEnum.T:
                return(tFormula(request.D, request.E, request.F));
            }

            throw new InvalidOptionException();
        }
Ejemplo n.º 11
0
        public FooCalculatorResult Calculate(FooCalculatorRequest request, string provider)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (string.IsNullOrWhiteSpace(provider))
            {
                throw new ArgumentNullException("provider");
            }

            var calculator = calculators.Where(x => x.AppliesTo(provider)).FirstOrDefault();

            if (calculator == null)
            {
                throw new ArgumentException("Provider '" + provider + "' is not valid or is not registered.");
            }

            return(calculator.Calculate(request));
        }