示例#1
0
    def test_safe_eval_error(self):
        test_cases = [TCData("__import__('os')", "_value_")]

        for test_case in test_cases:
            with self.assertRaises(NameError):
                ConditionalAnswer.safe_eval(test_case.input[0],
                                            test_case.input[1])
示例#2
0
    def test_safe_eval(self):
        test_cases = [
            TCData((Decimal(12), '_value_ < 15'), True),
            TCData((Decimal('1.05'), '_value_ - 1 == Decimal(str(0.05))'),
                   True),
            TCData(('foobar', 'len(_value_) > 0'), True),
            TCData(('foobar', '_value_.isupper()'), False),
            TCData((Decimal('1.666'), '_value_ > 1.5 and _value_ < 1.6'),
                   False),
            TCData(('banana', "_value_ in set(['watermelon', 'orange'])"),
                   False)
        ]

        for test_case in test_cases:
            self.assertEqual(
                ConditionalAnswer.safe_eval(test_case.input[0],
                                            test_case.input[1]),
                test_case.expected_output)