Example #1
0
	def test_ast_type_hints(self):
		parser_ = parser.Parser()
		cases = (
			# type,             type_is,             type_is_not
			('symbol << 1',     ast.DataType.FLOAT,  ast.DataType.STRING),
			('symbol + 1',      ast.DataType.FLOAT,  ast.DataType.STRING),
			('symbol > 1',      ast.DataType.FLOAT,  ast.DataType.STRING),
			('symbol =~ "foo"', ast.DataType.STRING, ast.DataType.FLOAT),
		)
		for case, type_is, type_is_not in cases:
			parser_.parse(case, self.context)
			context = engine.Context(type_resolver=engine.type_resolver_from_dict({'symbol': type_is}))
			parser_.parse(case, context)
			context = engine.Context(type_resolver=engine.type_resolver_from_dict({'symbol': type_is_not}))
			with self.assertRaises(errors.EvaluationError):
				parser_.parse(case, context)
Example #2
0
 def test_number_14(self):
     context = engine.Context(type_resolver=engine.type_resolver_from_dict({
         'TEST_FLOAT':
         ast.DataType.FLOAT,
     }))
     rule = engine.Rule('(TEST_FLOAT == null ? 0 : TEST_FLOAT) < 42',
                        context=context)
     rule.matches({'TEST_FLOAT': None})
Example #3
0
 def test_number_19(self):
     context = engine.Context(type_resolver=engine.type_resolver_from_dict({
         'facts':
         ast.DataType.MAPPING(key_type=ast.DataType.STRING,
                              value_type=ast.DataType.STRING)
     }))
     rule = engine.Rule('facts.abc == "def"', context=context)
     self.assertTrue(rule.matches({'facts': {'abc': 'def'}}))
Example #4
0
	def test_engine_type_resolver_from_dict(self):
		type_resolver = engine.type_resolver_from_dict({
			'string': ast.DataType.STRING,
			'float': ast.DataType.FLOAT
		})
		self.assertTrue(callable(type_resolver))
		self.assertEqual(type_resolver('string'), ast.DataType.STRING)
		self.assertEqual(type_resolver('float'), ast.DataType.FLOAT)
		with self.assertRaises(errors.SymbolResolutionError):
			type_resolver('doesnotexist')
Example #5
0
    def test_parser_comprehension_expressions_errors(self):
        # test non-iterables raise an exception
        with self.assertRaises(errors.EvaluationError):
            self._parse('[null for something in null]', self.context)

        # test invalid assignments raise an exception
        with self.assertRaises(errors.SyntaxError):
            self._parse('[null for null in something]', self.context)

        # test that data types are propagated...
        typed_context = engine.Context(
            type_resolver=engine.type_resolver_from_dict(
                {'words': types.DataType.ARRAY(types.DataType.STRING)}))
        # ... the result expression
        self._parse('[word =~ ".*" for word in words]', typed_context)
        with self.assertRaises(errors.EvaluationError):
            self._parse('[word % 2 for word in words]', typed_context)
        # ... and the condition expression
        self._parse('[null for word in words if word =~ ".*"]', typed_context)
        with self.assertRaises(errors.EvaluationError):
            self._parse('[null for word in words if word % 2]', typed_context)