Exemple #1
0
        public void ThenItShouldCorrectlyParseComparizonStatements()
        {
            foreach (var comparisons in new[]
            {
                new { FormulaText = "|1|<|2|", ExpectedResult = (object)(1 < 2) },
                new { FormulaText = "|8|-|3|<=|9|-|5|", ExpectedResult = (object)(8 - 3 <= 9 - 5) },
                new { FormulaText = "|-|6.6666|>|3.0|", ExpectedResult = (object)(-6.6666M > 3.0M) },
                new { FormulaText = "|6.6666|>=|3|", ExpectedResult = (object)(6.6666M >= 3) },
                new { FormulaText = "|10|-|3.3333|<|25|*|-5|", ExpectedResult = (object)(10 - 3.3333M < 25 * -5) },
                new { FormulaText = "|2|+|3|+|6|>=|10|-|12|", ExpectedResult = (object)(2 + 3 + 6 >= 10 - 12) },

                new { FormulaText = "|2|+|3|*|6|<|10|/|2|", ExpectedResult = (object)(2 + 3 * 6 < 10 / 2) },
                new { FormulaText = "|2|*|3|>|60|/|12|*|3|-|100|", ExpectedResult = (object)(2 * 3 > 60 / 12 * 3 - 100) },

                new { FormulaText = "|f|(|)|>=|3|", ExpectedResult = (object)(StockFunctions.f() >= 3) },
                new { FormulaText = "|5.456|<|foo|(|1|)|+|3|", ExpectedResult = (object)(5.456M < StockFunctions.foo(1) + 3) }
            })
            {
                foreach (var formulaTextWithSpaces in InsertSpacesIntoFormula(comparisons.FormulaText))
                {
                    var formula = _testee.Build(formulaTextWithSpaces);
                    Assert.AreEqual(
                        comparisons.ExpectedResult,
                        formula.CalculateCellValue(DefaultPropertyHolder, formula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })),
                        $"Formula text: {formulaTextWithSpaces}");
                }
            }
        }
Exemple #2
0
        public void ThenItShouldCorrectlyParseMultiplyingStatements()
        {
            foreach (var invocation in new[]
            {
                new { FormulaText = "|2|*|3|", ExpectedResult = (object)(2 * 3) },
                new { FormulaText = "|8|/|3|", ExpectedResult = (object)(8 / 3) },
                new { FormulaText = "|6.6666|*|3.0|", ExpectedResult = (object)(6.6666M * 3.0M) },
                new { FormulaText = "|6.6666|*|3|", ExpectedResult = (object)(6.6666M * 3) },
                new { FormulaText = "|10|/|3.3333|", ExpectedResult = (object)(10 / 3.3333M) },
                new { FormulaText = "|2|*|3|*|6|*|10|/|12|", ExpectedResult = (object)(2 * 3 * 6 * 10 / 12) },

                new { FormulaText = "|f|(|)|*|3|", ExpectedResult = (object)(StockFunctions.f() * 3) },
                new { FormulaText = "|5.456|/|foo|(|1|)|*|3|", ExpectedResult = (object)(5.456M / StockFunctions.foo(1) * 3) }
            })
            {
                foreach (var formulaTextWithSpaces in InsertSpacesIntoFormula(invocation.FormulaText))
                {
                    var formula = _testee.Build(formulaTextWithSpaces);
                    Assert.AreEqual(
                        invocation.ExpectedResult,
                        formula.CalculateCellValue(DefaultPropertyHolder, formula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })),
                        $"Formula text: {formulaTextWithSpaces}");
                }
            }
        }
Exemple #3
0
        public void ThenItShouldCorrectlyParseAddingStatements()
        {
            foreach (var invocation in new[]
            {
                new { FormulaText = "|2|+|3|", ExpectedResult = (object)(2 + 3) },
                new { FormulaText = "|8|-|3|", ExpectedResult = (object)(8 - 3) },
                new { FormulaText = "|-|6.6666|+|3.0|", ExpectedResult = (object)(-6.6666M + 3.0M) },
                new { FormulaText = "|6.6666|+|3|", ExpectedResult = (object)(6.6666M + 3) },
                new { FormulaText = "|10|-|3.3333|", ExpectedResult = (object)(10 - 3.3333M) },
                new { FormulaText = "|2|+|3|+|6|+|10|-|12|", ExpectedResult = (object)(2 + 3 + 6 + 10 - 12) },

                new { FormulaText = "|2|+|3|*|6|+|10|/|2|", ExpectedResult = (object)(2 + 3 * 6 + 10 / 2) },
                new { FormulaText = "|2|*|3|+|60|/|12|*|3|-|100|", ExpectedResult = (object)(2 * 3 + 60 / 12 * 3 - 100) },

                new { FormulaText = "|f|(|)|+|3|", ExpectedResult = (object)(StockFunctions.f() + 3) },
                new { FormulaText = "|5.456|-|foo|(|1|)|+|3|", ExpectedResult = (object)(5.456M - StockFunctions.foo(1) + 3) }
            })
            {
                foreach (var formulaTextWithSpaces in InsertSpacesIntoFormula(invocation.FormulaText))
                {
                    var formula = _testee.Build(formulaTextWithSpaces);
                    Assert.AreEqual(
                        invocation.ExpectedResult,
                        formula.CalculateCellValue(DefaultPropertyHolder, formula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })),
                        $"Formula text: {formulaTextWithSpaces}");
                }
            }
        }
Exemple #4
0
        private void subtractStock(string orderRef)
        {
            List <CartProductViewModel>         cartProducts = GetCartProductsFromOrderRef(orderRef);
            IEnumerable <AdminProductViewModel> stock        = new StockFunctions(ConnectionString, null, StockFunctions).RunGetStock();

            foreach (var product in cartProducts)
            {
                StockViewModel stockvm = new StockViewModel();
                foreach (var s in stock)
                {
                    if (stockvm.Id == 0)
                    {
                        stockvm = s.Stock.FirstOrDefault(a => a.Id == product.StockId);
                    }
                }
                new Stock.Stock(new StockViewModel()
                {
                    Id          = product.StockId,
                    Quantity    = stockvm.Quantity - product.Quantity,
                    Description = stockvm.Description,
                    ProductId   = stockvm.ProductId
                }
                                , Stock).RunUpdate();
            }
        }
Exemple #5
0
        private static void DOD(string header, int noOfTrades)
        {
            StockTradeData stockTradeData = new StockTradeData();
            StockFunctions F = new StockFunctions();

            stockTradeData = F.StockTradeInit(noOfTrades);
            F.GenerateTrades(noOfTrades, stockTradeData);

            watch.Restart();

            F.CalcAverages(noOfTrades, stockTradeData);

            long mSecs = watch.ElapsedMilliseconds;

            Console.WriteLine($"{header} took {mSecs} msecs.");
        }
Exemple #6
0
        private CartProductViewModel AdditionChecks(CartProductViewModel product)
        {
            IEnumerable <AdminProductViewModel> products = new StockFunctions(_constring, null, stockFunctions).RunGetStock();

            foreach (var p in products)
            {
                foreach (var s in p.Stock)
                {
                    if (s.Id == product.StockId && product.Quantity > s.Quantity)
                    {
                        product.Quantity = s.Quantity;
                    }
                }
            }
            if (product.Quantity < 1)
            {
                product.Quantity = 1;
            }
            return(product);
        }
Exemple #7
0
 public void ThenItShouldCorrectlyParseStatementsWithNegation()
 {
     foreach (var formula in new[]
     {
         new { Text = "|-|1|", ExpectedResult = (object)-1 },
         new { Text = "|-|[I]|", ExpectedResult = (object)-DefaultPropertyHolder.I },
         new { Text = "|-|f(|)|", ExpectedResult = (object)-StockFunctions.f() },
         new { Text = "|-|10|-|5| + 6", ExpectedResult = (object)(-10 - 5 + 6) },
         new { Text = "|-|(|10|+|f|(|)|)|/|3|", ExpectedResult = (object)(-(10 + StockFunctions.f()) / 3) },
         new { Text = "|If|(|-|foo|(|1|)|<|0|,|f|(|)|,|-|f|(|)|)|", ExpectedResult = (object)(-StockFunctions.foo(1) < 0 ? StockFunctions.f() : -StockFunctions.f()) }
     })
     {
         foreach (var formulaText in InsertSpacesIntoFormula(formula.Text))
         {
             var builtFormula = _testee.Build(formulaText);
             Assert.AreEqual(
                 formula.ExpectedResult,
                 builtFormula.CalculateCellValue(DefaultPropertyHolder, builtFormula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })),
                 $"Formula text: {formulaText}, Expression built: {builtFormula}");
         }
     }
 }
Exemple #8
0
 public void ThenItShouldCorrectlyParseFunctionCalls()
 {
     foreach (var functionCall in new[]
     {
         // exact parameter types match
         new { FormulaText = "|f|(|)|", ExpectedResult = (object)StockFunctions.f() },
         new { FormulaText = "|foo|(|1|)|", ExpectedResult = (object)StockFunctions.foo(1) },
         new { FormulaText = "|bar|(|'qwerty'|,|[Book]|)|", ExpectedResult = (object)StockFunctions.bar("qwerty", DefaultPropertyHolder.Book) },
         new { FormulaText = "|MakeFoo|(|bar|(|'asdf'|,|[Book]|)|,|foo|(|100|)|,|'2018-12-10 10:47:03Z'|)|", ExpectedResult = (object)StockFunctions.MakeFoo(StockFunctions.bar("asdf", DefaultPropertyHolder.Book), StockFunctions.foo(100), DateTime.Parse("2018-12-10 10:47:03Z")) },
         // implicit conversions
         new { FormulaText = "|foo|(|1.567|)|", ExpectedResult = (object)StockFunctions.foo((int)1.567M) },
         new { FormulaText = "|MakeFoo|(|bar|(|'asdf'|,|[Book]|)|,|f|(|)|,|'2018-12-10 10:47:03Z'|)|", ExpectedResult = (object)StockFunctions.MakeFoo(StockFunctions.bar("asdf", DefaultPropertyHolder.Book), StockFunctions.f(), DateTime.Parse("2018-12-10 10:47:03Z")) },
     })
     {
         foreach (var formulaTextWithSpaces in InsertSpacesIntoFormula(functionCall.FormulaText))
         {
             var formula = _testee.Build(formulaTextWithSpaces);
             Assert.AreEqual(
                 functionCall.ExpectedResult,
                 formula.CalculateCellValue(DefaultPropertyHolder, formula.CalculateAggregatedValues(new[] { DefaultPropertyHolder })),
                 $"Formula text: [{formulaTextWithSpaces}]");
         }
     }
 }
 public StockTests()
 {
     stockFunctions = new StockFunctions(null, iStock.Object, iStockFunctions.Object);
 }