public void TestCaseConstruction3()
        {
            var @operator = new ArithmeticNeutralOperator("operator", TypeConverter);

            Assert.AreEqual("operator", @operator.Symbol);
            Assert.AreEqual(1, @operator.Precedence);
        }
Пример #2
0
        public void TestCaseOperatorRegistration()
        {
            var tokenizer = new Tokenizer("irrelevant");
            var lexer     = new Lexer(tokenizer, ExpressionFlowSymbols.Default, StringComparer.OrdinalIgnoreCase);

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

            ExpectArgumentNullException("operator", () => lexer.RegisterOperator(null));

            Assert.AreEqual(lexer, lexer.RegisterOperator(neutral));
            Assert.AreEqual(lexer, lexer.RegisterOperator(sum));

            ExpectOperatorAlreadyRegisteredException(neutral.ToString(), () => lexer.RegisterOperator(neutral));
            ExpectOperatorAlreadyRegisteredException(sum.ToString(), () => lexer.RegisterOperator(sum));

            /* Clashing with specials */
            lexer.RegisterSpecial("FALSE", false);

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

            ExpectOperatorAlreadyRegisteredException(clashingOp1.ToString(), () => lexer.RegisterOperator(clashingOp1));
            ExpectOperatorAlreadyRegisteredException(clashingOp2.ToString(), () => lexer.RegisterOperator(clashingOp2));

            /* Flow */
            ExpectOperatorAlreadyRegisteredException("(", () => lexer.RegisterOperator(new ArithmeticNeutralOperator("(", TypeConverter)));
            lexer.RegisterOperator(new ArithmeticNeutralOperator(")", TypeConverter));
            lexer.RegisterOperator(new ArithmeticNeutralOperator(".", TypeConverter));
            lexer.RegisterOperator(new ArithmeticNeutralOperator(",", TypeConverter));
            lexer.RegisterOperator(new ArithmeticSumOperator("(", TypeConverter));
            ExpectOperatorAlreadyRegisteredException(")", () => lexer.RegisterOperator(new ArithmeticSumOperator(")", TypeConverter)));
            ExpectOperatorAlreadyRegisteredException(".", () => lexer.RegisterOperator(new ArithmeticSumOperator(".", TypeConverter)));
            ExpectOperatorAlreadyRegisteredException(",", () => lexer.RegisterOperator(new ArithmeticSumOperator(",", TypeConverter)));
        }
        public void TestCaseEvaluation()
        {
            var @operator = new ArithmeticNeutralOperator(TypeConverter);

            AssertEvaluation <double>(@operator, 0, 0);
            AssertEvaluation <double>(@operator, double.NaN, double.NaN);
            AssertEvaluation <double>(@operator, -double.NaN, -double.NaN);
        }
Пример #4
0
        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));
        }
Пример #5
0
        public void TestCaseCaseSensitivity()
        {
            var expressionIns  = new Expression(ExpressionFlowSymbols.Default, StringComparer.OrdinalIgnoreCase);
            var expressionSens = new Expression(ExpressionFlowSymbols.Default, StringComparer.Ordinal);

            var testOperator = new ArithmeticNeutralOperator("lower_case_operator", TypeConverter);

            expressionIns.RegisterOperator(testOperator);
            expressionSens.RegisterOperator(testOperator);

            Assert.IsTrue(expressionIns.IsSupportedOperator(testOperator.Symbol.ToUpper()));
            Assert.IsTrue(expressionIns.IsSupportedOperator(testOperator.Symbol));
            Assert.IsFalse(expressionSens.IsSupportedOperator(testOperator.Symbol.ToUpper()));
            Assert.IsTrue(expressionSens.IsSupportedOperator(testOperator.Symbol));

            expressionIns.FeedSymbol(testOperator.Symbol.ToUpper());
            expressionSens.FeedSymbol(testOperator.Symbol.ToUpper());

            Assert.AreEqual("lower_case_operator{??}", expressionIns.ToString(ExpressionFormatStyle.Canonical));
            Assert.AreEqual("@LOWER_CASE_OPERATOR", expressionSens.ToString(ExpressionFormatStyle.Canonical));
        }
Пример #6
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));
        }
Пример #7
0
        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));
        }
Пример #8
0
        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));
        }
        public void TestCaseEvaluationExceptions()
        {
            var @operator = new ArithmeticNeutralOperator(TypeConverter);

            ExpectArgumentNullException("context", () => @operator.Evaluate(null, 1));
        }
        public void TestCaseConstruction2()
        {
            var @operator = new ArithmeticNeutralOperator(TypeConverter);

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