Exemplo n.º 1
0
        public void TestExpressionVariableProvider()
        {
            var input = new Dictionary <string, string>()
            {
                { "A", "B*C" },
                { "B", "C * 10" },
                { "C", "SQRT[4] * 5" }
            };

            var variableProvider = new ExpressionVariableProvider(input, DefaultFunctionProvider.Instance);

            var result = variableProvider.Lookup("A");

            Assert.AreEqual(1000, result);
        }
Exemplo n.º 2
0
        public void TestAllDataDrivenTestCases()
        {
            foreach (var sourceFile in Directory.EnumerateFiles(@".\Data", "*.csv"))
            {
                using (var sr = new StreamReader(sourceFile))
                {
                    using (var csvReader = new CsvReader(sr))
                    {
                        var assertions    = new List <Tuple <string, double> >();
                        var expressions   = new Dictionary <string, string>();
                        var dependencyMap = new Dictionary <string, HashSet <string> >();
                        foreach (var expressionEntry in csvReader.GetRecords <DataDrivenTestCase>())
                        {
                            expressions.Add(expressionEntry.Name, expressionEntry.Expression);
                            assertions.Add(new Tuple <string, double>(expressionEntry.Name, expressionEntry.Expected));
                            dependencyMap.Add(expressionEntry.Name, CsWrapper.ExtractExpressionDependencies(CsWrapper.ParseFormula(expressionEntry.Expression)));
                        }

                        var variableProvider = new ExpressionVariableProvider(expressions, DefaultFunctionProvider.Instance);

                        foreach (var assertion in assertions)
                        {
                            foreach (var dependency in dependencyMap[assertion.Item1])
                            {
                                Assert.IsTrue(dependencyMap.ContainsKey(dependency), $"Missing dependency {assertion.Item1}->{dependency}");
                            }

                            Assert.AreEqual(assertion.Item2, variableProvider.Lookup(assertion.Item1));
                            Assert.AreEqual(assertion.Item2, CsWrapper.InterpretFormula(expressions[assertion.Item1], variableProvider));
                        }

                        Console.WriteLine($"Validated {assertions.Count} entries from {sourceFile}");
                    }
                }
            }
        }