def test_parenthesis(self):
     try:
         tokenizer = Tokenizer('unimplementedfunction() || True || False')
         tree = BooleanExpressionTree(list(tokenizer), test_engine)
         tree.evaluate(None)
         self.fail()
     except Exception, e:
         pass
    def test_only_necessary_conditions_evaluated(self):
        tokenizer = Tokenizer('!( 5 == 6 ) || unimplementedfunction("shouldnt get run because first condition passes")')
        tree = BooleanExpressionTree(list(tokenizer), test_engine)
        self.assertTrue( tree.evaluate(None) )

        tokenizer = Tokenizer('( 5 == 6 ) && unimplementedfunction("shouldnt get run because first condition passes")')
        tree = BooleanExpressionTree(list(tokenizer), test_engine)
        self.assertFalse( tree.evaluate(None) )

        try:
            tokenizer = Tokenizer('(5 == 6) || unimplementedfunction("shouldnt get run because first condition passes")')
            tree = BooleanExpressionTree(list(tokenizer), test_engine)
            tree.evaluate(None)
            self.fail()
        except Exception, e:
            pass
    def test_process_expression_preceeded_by_unary_not(self):
        tokenizer = Tokenizer("!(5 == 6)")
        tree = BooleanExpressionTree(list(tokenizer), test_engine)

        self.assertEqual("!(5 == 6)", str(tree))
        self.assertTrue( tree.evaluate(None) )
    def test_process_two_expressions_and_binary_or(self):
        tokenizer = Tokenizer("!( 5 == 6 ) || abs(-1) == 1")
        tree = BooleanExpressionTree(list(tokenizer), test_engine)

        self.assertEqual("(!(5 == 6) || abs ( - 1 ) == 1)", str(tree))
        self.assertTrue( tree.evaluate(None) )
            pass

        try:
            tokenizer = Tokenizer('!(5 == 6) && unimplementedfunction("shouldnt get run because first condition passes")')
            tree = BooleanExpressionTree(list(tokenizer), test_engine)
            tree.evaluate(None)
            self.fail()
        except Exception, e:
            pass

    def test_parenthesis(self):
        try:
            tokenizer = Tokenizer('unimplementedfunction() || True || False')
            tree = BooleanExpressionTree(list(tokenizer), test_engine)
            tree.evaluate(None)
            self.fail()
        except Exception, e:
            pass

        tokenizer = Tokenizer('unimplementedfunction() || (True || False)')
        tree = BooleanExpressionTree(list(tokenizer), test_engine)
        self.assertTrue( tree.evaluate(None) )

    def test_complex_expression(self):
        tokenizer = Tokenizer('value == 2 && !(key in set or y == "some string" || myfunction(param1,"const param 2"))')
        tree = BooleanExpressionTree(list(tokenizer), test_engine)
        self.assertEqual('(value == 2 && !((key in set or y == "some string") || myfunction ( param1 , "const param 2" )))', str(tree))