public void TestFromFile()
 {
     TokenGenerator tokenGenerator = new TokenGenerator();
     DataRow row = TestContext.DataRow;
     string input = row[0].ToString().Trim();
     string output = row[1].ToString().Trim();
     Assert.AreEqual(output, tokenGenerator.Parse(input).Simplify()[0].Text);
 }
        public void TestFloatMath()
        {
            Dictionary<string, double> tests = new Dictionary<string, double>
            {
                ["6.1 + 2.1"] = 8.2,
                ["6.1 * 2.0"] = 12.2,
                ["6.4 - 2.3"] = 4.1,
                ["6.1 / 2.0"] = 3.05,
                ["5.4 / -1.8"] = -3,
                ["-5.4 / 1.8"] = -3
            };

            TokenGenerator tokenGenerator = new TokenGenerator();
            foreach (var test in tests)
            {
                IToken token = tokenGenerator.Parse(test.Key).Simplify()[0];
                Assert.IsTrue(token is TypeToken<double>, test.Key + ": " + token.Text);
                Assert.AreEqual(test.Value, token.Convert<double>(), 0.001, test.Key);
            }
        }
        public void TestIntegerMath()
        {
            Dictionary<string, int> tests = new Dictionary<string, int>
            {
                ["6 + -2"] = 4,
                ["8 / -(2 + 2)"] = -2,
                ["6 + 2"] = 8,
                ["6 * 2"] = 12,
                ["6 - 2"] = 4,
                ["6 / 2"] = 3,
                ["2 + 6 + 2"] = 10,
                ["2 + 6 * 2"] = 14,
                ["2 + 6 - 2"] = 6,
                ["2 + 6 / 2"] = 5,
                ["6 + 2 + 2"] = 10,
                ["6 * 2 + 2"] = 14,
                ["6 - 2 + 2"] = 6,
                ["6 / 2 + 2"] = 5,
                ["6 + (2 + 2)"] = 10,
                ["6 * (2 + 2)"] = 24,
                ["6 - (2 + 2)"] = 2,
                ["6 - (-2 + 2)"] = 6,
                ["(-2 + 2) - 6"] = -6,
                ["8 / (2 + 2)"] = 2,
                ["10 + (12 - 10) / 2"] = 11
            };

            TokenGenerator tokenGenerator = new TokenGenerator();
            foreach (var test in tests)
            {
                IToken token = tokenGenerator.Parse(test.Key);
                token = token.Simplify()[0];
                Assert.IsTrue(token is TypeToken<int>, test.Key + ": " + token.Text);
                Assert.AreEqual(test.Value, token.Convert<int>(), test.Key);
            }
        }
        public void TestSubstitution()
        {
            Dictionary<string, string> tests = new Dictionary<string, string>
            {
                ["$($e + '.c')"] = "d",
                ["$$d"] = "4",
                ["{b}"] = "4",
                ["${d}"] = "4",
                ["$($d)"] = "4",
                ["'a' $b + $d 'd'"] = "a4bd",
                ["{$e.$f}"] = "d",
                ["$a"] = "3",
                ["$a + $b"] = "7",
                ["$($e + '.c') + $b"] = "d4",
            //                ["$($e).c + $b"] = "d4", These to be added later possibly
            //                ["${e}.c + $b"] = "d4",
                ["$a.c + $b"] = "d4",
                ["{'a' + 'b'}"] = "5",
                ["{$d + $e}"] = "12"
            };

            TokenTree parameters = new TokenTree
            {
                Children =
                {
                    new TokenTree("a", "3")
                    {
                        Children = {new TokenTree("c", "d")}
                    },
                    new TokenTree("b", "4"),
                    new TokenTree("ab", "5"),
                    new TokenTree("ba", "12"),
                    new TokenTree("d", "b"),
                    new TokenTree("e", "a"),
                    new TokenTree("f", "c")
                }
            };

            TokenGenerator tokenGenerator = new TokenGenerator();
            int passedCount = 0;
            foreach (var test in tests)
            {
                IToken token = tokenGenerator.Parse(test.Key);
                token = token.Evaluate(new TokenTreeList(parameters))[0];
                bool passed = test.Value == token.Text;
                string result = passed ? "passed" : "failed";
                if (passed)
                    ++passedCount;
                Console.WriteLine($"'{test.Key}' {result}.");
            }
            Assert.AreEqual(tests.Count, passedCount);
        }
        public void TestStringMath()
        {
            Dictionary<string, string> tests = new Dictionary<string, string>
            {
                ["'+-' + '-+'"] = "+--+",
                ["'123456' - '123'"] = "456",
                ["'1232323456' / '23'"] = "3"
            };

            TokenGenerator tokenGenerator = new TokenGenerator();
            foreach (var test in tests)
            {
                IToken token = tokenGenerator.Parse(test.Key).Simplify()[0];
                Assert.AreEqual(test.Value, token.Text, test.Key);
            }
        }
 public void TestSimple()
 {
     TokenGenerator tokenGenerator = new TokenGenerator();
     IToken token = tokenGenerator.Parse("word");
     Assert.IsTrue(token is StringToken);
     Assert.AreEqual("word", token.Text);
     Assert.AreEqual(int.MinValue, token.Convert<int>());
 }
        public void TestPlus()
        {
            TokenGenerator tokenGenerator = new TokenGenerator();
            IToken token = tokenGenerator.Parse("2 + 2");
            Assert.IsTrue(token is ExpressionToken, token.GetType().Name);
            Assert.AreEqual("(2+2)", token.Text);

            token = token.Simplify()[0];
            Assert.IsTrue(token is TypeToken<int>, token.GetType().Name);
            Assert.AreEqual("4", token.Text);
            Assert.AreEqual(4, token.Convert<int>());

            token = tokenGenerator.Parse("\"2+2\"");
            Assert.IsTrue(token is StringToken);
            Assert.AreEqual("2+2", token.Text);
        }
        public void TestNumber()
        {
            TokenGenerator tokenGenerator = new TokenGenerator();
            IToken token = tokenGenerator.Parse("4");
            Assert.IsTrue(token is TypeToken<int>);
            Assert.AreEqual("4", token.Text);
            Assert.AreEqual(4, token.Convert<int>());

            token = tokenGenerator.Parse("4.5");
            Assert.IsTrue(token is TypeToken<double>);
            Assert.AreEqual("4.5", token.Text);
            Assert.AreEqual(4.5, token.Convert<double>());
        }
        public void TestMixedStringMath()
        {
            Dictionary<string, string> tests = new Dictionary<string, string>
            {
                ["'+-' * 2"] = "+-+-",
                ["'+-' * 2.5"] = "+-+-+",
                ["'+-' + 2"] = "+-2",
                ["'+-' + 2.3"] = "+-2.3",
                ["2 * '+-'"] = "+-+-",
                ["2.5 * '+-'"] = "+-+-+",
                ["2 + '+-'"] = "2+-",
                ["2.3 + '+-'"] = "2.3+-",
                ["'123' 2 * 2 '567'"] = "1234567"
            };

            TokenGenerator tokenGenerator = new TokenGenerator();
            foreach (var test in tests)
            {
                IToken token = tokenGenerator.Parse(test.Key).Simplify()[0];
                Assert.IsTrue(token is TypeToken<string>, test.Key + ": " + token.Text);
                Assert.AreEqual(test.Value, token.Text, test.Key);
            }
        }