Exemple #1
0
        public void TestCaseConstruction3()
        {
            var @operator = new ArithmeticSumOperator("operator", TypeConverter);

            Assert.AreEqual("operator", @operator.Symbol);
            Assert.AreEqual(4, @operator.Precedence);
            Assert.AreEqual(Associativity.LeftToRight, @operator.Associativity);
        }
Exemple #2
0
        public void TestCaseEvaluation()
        {
            var @operator = new ArithmeticSumOperator(TypeConverter);

            AssertEvaluation <double>(@operator, 0, 100, 100);
            AssertEvaluation <double>(@operator, 0, 0, 0);
            AssertEvaluation <double>(@operator, -1, -2, -3);

            AssertEvaluation <string>(@operator, "Hello ", "World", "Hello World");
        }
        public void TestCaseOperatorRegistration1()
        {
            var expression = new Expression();

            var unaryOpPlus  = new ArithmeticNeutralOperator("+", TypeConverter);
            var binaryOpPlus = new ArithmeticSumOperator("+", TypeConverter);

            expression.RegisterOperator(unaryOpPlus);
            ExpectOperatorAlreadyRegisteredException(unaryOpPlus.ToString(), () => expression.RegisterOperator(unaryOpPlus));
            expression.RegisterOperator(binaryOpPlus);
            ExpectOperatorAlreadyRegisteredException(binaryOpPlus.ToString(), () => expression.RegisterOperator(binaryOpPlus));
        }
Exemple #4
0
        public void TestCaseSpecialRegistration()
        {
            var tokenizer = new Tokenizer("irrelevant");
            var lexer     = new Lexer(tokenizer, ExpressionFlowSymbols.Default, StringComparer.OrdinalIgnoreCase);

            /* Exceptions */
            ExpectArgumentNullException("keyword", () => lexer.RegisterSpecial(null, null));
            ExpectArgumentEmptyException("keyword", () => lexer.RegisterSpecial(string.Empty, null));
            ExpectArgumentNotIdentifierException("keyword", () => lexer.RegisterSpecial("12ABC", null));

            var clashingOp1 = new ArithmeticNeutralOperator("T1", TypeConverter);
            var clashingOp2 = new ArithmeticSumOperator("T2", TypeConverter);

            lexer.RegisterOperator(clashingOp1);
            lexer.RegisterOperator(clashingOp2);

            ExpectSpecialCannotBeRegisteredException("T1", () => lexer.RegisterSpecial("T1", true));
            ExpectSpecialCannotBeRegisteredException("T2", () => lexer.RegisterSpecial("T2", true));

            Assert.AreEqual(lexer, lexer.RegisterSpecial("FALSE", false));
            Assert.AreEqual(lexer, lexer.RegisterSpecial("FALSE", false));
        }
        public void TestCaseSupportedOperators()
        {
            var expression = new Expression();

            var neutral = new ArithmeticNeutralOperator(TypeConverter);
            var sum     = new ArithmeticSumOperator(TypeConverter);

            /* Unary */
            expression.RegisterOperator(neutral);
            Assert.AreEqual(1, expression.SupportedOperators.Count);
            Assert.AreEqual(neutral, expression.SupportedOperators[0]);
            Assert.IsTrue(expression.IsSupportedOperator(neutral.Symbol));

            /* Binary */
            expression.RegisterOperator(sum);
            Assert.AreEqual(2, expression.SupportedOperators.Count);
            Assert.AreEqual(sum, expression.SupportedOperators[1]);
            Assert.IsTrue(expression.IsSupportedOperator(sum.Symbol));

            ExpectArgumentNullException("symbol", () => expression.IsSupportedOperator(null));
            ExpectArgumentEmptyException("symbol", () => expression.IsSupportedOperator(string.Empty));
        }
        public void TestCaseOperatorRegistration2()
        {
            var expression = new Expression();

            var unary1 = new ArithmeticNeutralOperator("(", TypeConverter);
            var unary2 = new ArithmeticNeutralOperator(")", TypeConverter);
            var unary3 = new ArithmeticNeutralOperator(".", TypeConverter);
            var unary4 = new ArithmeticNeutralOperator(",", TypeConverter);

            var binary1 = new ArithmeticSumOperator("(", TypeConverter);
            var binary2 = new ArithmeticSumOperator(")", TypeConverter);
            var binary3 = new ArithmeticSumOperator(".", TypeConverter);
            var binary4 = new ArithmeticSumOperator(",", TypeConverter);

            ExpectOperatorAlreadyRegisteredException(unary1.ToString(), () => expression.RegisterOperator(unary1));
            ExpectOperatorAlreadyRegisteredException(unary2.ToString(), () => expression.RegisterOperator(unary2));
            ExpectOperatorAlreadyRegisteredException(unary3.ToString(), () => expression.RegisterOperator(unary3));
            ExpectOperatorAlreadyRegisteredException(unary4.ToString(), () => expression.RegisterOperator(unary4));

            ExpectOperatorAlreadyRegisteredException(binary1.ToString(), () => expression.RegisterOperator(binary1));
            ExpectOperatorAlreadyRegisteredException(binary2.ToString(), () => expression.RegisterOperator(binary2));
            ExpectOperatorAlreadyRegisteredException(binary3.ToString(), () => expression.RegisterOperator(binary3));
            ExpectOperatorAlreadyRegisteredException(binary4.ToString(), () => expression.RegisterOperator(binary4));
        }
Exemple #7
0
        public void TestCaseConstruction2()
        {
            var @operator = new ArithmeticSumOperator(TypeConverter);

            Assert.AreEqual("+", @operator.Symbol);
        }