def test_disjunction(self): self.check_parser(DISJUNCTION_STR, DISJUNCTION_AST) # self.check_parser('%s or %s' % (CONJUNCTION_STR, operator_str_gen('<=')), # AST.Disjunction(CONJUNCTION_AST, operator_ast_gen(AST.Lte))) self.check_parser('%s or %s' % (DATE_STR, DATE_STR), AST.Disjunction(DATE_AST, DATE_AST))
def test_comparison(self): self.check_operator_parser('=', AST.Eq) self.check_operator_parser('!=', AST.Neq) self.check_operator_parser('<', AST.Lt) self.check_operator_parser('<=', AST.Lte) self.check_operator_parser('>', AST.Gt) self.check_operator_parser('>=', AST.Gte) self.check_parser('%s between %s and %s' % (DATE_STR, CONTEXT_STR, LIST_STR), AST.Between(DATE_AST, CONTEXT_AST, LIST_AST)) # self.check_parser('%s between %s and %s' % (FILTER_EXPRESSION_STR, INSTANCE_OF_STR, LIST_STR), # AST.Between(FILTER_EXPRESSION_AST, INSTANCE_OF_AST, LIST_AST), # debug=True) self.check_parser('%s in null' % DATE_STR, AST.In(DATE_AST, AST.PositiveUnaryTests([AST.Null()]))) self.check_parser('%s in null' % FILTER_EXPRESSION_STR, AST.In(FILTER_EXPRESSION_AST, AST.PositiveUnaryTests([AST.Null()]))) # self.check_parser('%s in null' % FILTER_EXPRESSION_STR, AST.In(FILTER_EXPRESSION_AST, [AST.Null])) self.check_parser('%s in (null, null)' % FILTER_EXPRESSION_STR, AST.In(FILTER_EXPRESSION_AST, AST.PositiveUnaryTests([AST.Null()] * 2)))
def test_numeric_literal(self): self.check_parser('1', AST.Number(1)) self.check_parser('1.42', AST.Number(1.42)) self.check_parser('1 . 42', AST.Number(1.42)) self.check_parser('.42', AST.Number(.42)) self.check_parser('. 42', AST.Number(.42)) self.check_parser('42', AST.Number(42))
def test_named_parameters(self): self.check_parser('%s(ala: %s)' % (PATH_STR, INVOCATION_STR), AST.Invocation(PATH_AST, [(AST.Name('ala'), INVOCATION_AST)])) self.check_parser('%s(ala : %s, ola :%s)' % ((DATE_STR,) * 3), AST.Invocation(DATE_AST, [(AST.Name('ala'), DATE_AST), (AST.Name('ola'), DATE_AST)]))
def test_positional_parameters(self): self.check_parser('%s() ' % PATH_STR, AST.Invocation(PATH_AST, [])) self.check_parser('%s(%s) ' % (PATH_STR, FOR_STR), AST.Invocation(PATH_AST, [FOR_AST])) self.check_parser(INVOCATION_STR, INVOCATION_AST)
def p_interval(self, p): """interval : interval_starts endpoint DOTS endpoint interval_ends""" p[0] = AST.Interval(p[1], p[2], p[4], p[5])
def test_open_interval_start(self): self.check_parser('1 in (2..3]', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.Interval(AST.OpenIntervalStart(), AST.Endpoint(AST.Number(2)), AST.Endpoint(AST.Number(3)), AST.ClosedIntervalEnd())]))) self.check_parser('1 in ]2..3]', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.Interval(AST.OpenIntervalStart(), AST.Endpoint(AST.Number(2)), AST.Endpoint(AST.Number(3)), AST.ClosedIntervalEnd())])))
def test_unary_tests(self): self.check_table_parser('null', AST.PositiveUnaryTests([AST.Null()])) self.check_table_parser(NOT_STR, NOT_AST) self.check_table_parser('-', AST.NoTest())
def p_not_positive_tests(self, p): """not_positive_unary_tests : NOT '(' positive_unary_tests ')'""" p[0] = AST.Not(p[3])
def test_string_literal(self): self.check_parser('"ala"', AST.StringLiteral('ala'))
def test_instance_of(self): self.check_parser('%s instance of a.b.c' % DATE_STR, AST.InstanceOf(DATE_AST, AST.Type([AST.Name('a.b.c')])))
def test_list(self): self.check_parser(LIST_STR, LIST_AST) self.check_parser('[]', AST.List([]))
def test_function_definition(self): self.check_parser(FUNCTION_STR, FUNCTION_AST) self.check_parser('function(ola, iza) external %s' % CONTEXT_STR, AST.FunctionDefinition([AST.Name('ola'), AST.Name('iza')], True, CONTEXT_AST)) self.check_parser('function(ola, iza) external %s' % DATE_STR, AST.FunctionDefinition([AST.Name('ola'), AST.Name('iza')], True, DATE_AST))
def test_context(self): self.check_parser(CONTEXT_STR, CONTEXT_AST) self.check_parser('{{"ola":{ola_ctx}, "iza":{iza_ctx}}}'.format(ola_ctx=DATE_STR, iza_ctx=CONTEXT_STR), AST.Context([('ola', DATE_AST), ('iza', CONTEXT_AST)]))
def test_date_time_literal(self): self.check_parser(DATE_STR, DATE_AST) self.check_parser('time ("a")', AST.Time('a')) self.check_parser('date and time("a")', AST.DateAndTime('a')) self.check_parser('duration ("a")', AST.Duration('a')) self.check_parser_log('date and time("")', 'NAME{and time}', '6:1')
from feel.parser.parser.Parser import Parser from feel.parser.simple.SimpleParser import SimpleParser from feel.parser.table.TableParser import TableParser from utils.StoreLogger import StoreLogger def operator_ast_gen(operator_ast): return operator_ast(FILTER_EXPRESSION_AST, LIST_AST) def operator_str_gen(operator_str): return '%s %s %s' % (FILTER_EXPRESSION_STR, operator_str, LIST_STR) DATE_STR = 'date("1")' DATE_AST = AST.Date('1') NAME_STR = 'ala ma kota' NAME_AST = AST.Name(NAME_STR) CONTEXT_STR = '{%s:%s}' % (NAME_STR, DATE_STR) CONTEXT_AST = AST.Context([(NAME_AST, DATE_AST)]) FUNCTION_STR = 'function()%s' % CONTEXT_STR FUNCTION_AST = AST.FunctionDefinition([], False, CONTEXT_AST) LIST_STR = '[%s, %s]' % (FUNCTION_STR, CONTEXT_STR) LIST_AST = AST.List([FUNCTION_AST, CONTEXT_AST]) INSTANCE_OF_STR = '%s instance of ala.ma.kota' % LIST_STR # INSTANCE_OF_STR = '%s instance of ala.ma.kota' % LIST_STR
def p_no_tests(self, p): """no_tests : '-'""" p[0] = AST.NoTest()
def check_name(self, name): self.check_parser(name, AST.Name(name))
def test_boolean_literal(self): self.check_parser(' true ', AST.Boolean(True)) self.check_parser(' false ', AST.Boolean(False))
def test_arithmetic_expression(self): self.check_parser('%s**true' % DATE_STR, AST.Exp(DATE_AST, AST.Boolean(True))) self.check_parser('%s **true' % NEGATION_STR, AST.Exp(NEGATION_AST, AST.Boolean(True))) self.check_parser('%s /true **%s' % (NEGATION_STR, NAME_STR), AST.Div(NEGATION_AST, AST.Exp(AST.Boolean(True), NAME_AST))) self.check_parser('%s *true **%s' % (NEGATION_STR, NAME_STR), AST.Mul(NEGATION_AST, AST.Exp(AST.Boolean(True), NAME_AST))) self.check_parser('%s +1.2 /true **%s' % (NEGATION_STR, NAME_STR), AST.Sum(NEGATION_AST, AST.Div(AST.Number(1.2), AST.Exp(AST.Boolean(True), NAME_AST)))) self.check_parser(ARITHMETIC_EXPRESSION_STR, ARITHMETIC_EXPRESSION_AST)
def test_literal(self): self.check_parser('"ala"', AST.StringLiteral('ala')) self.check_parser('null', AST.Null())
def p_positive_unary_tests(self, p): """positive_unary_tests : many_positive_unary_tests""" p[0] = AST.PositiveUnaryTests(p[1])
def test_arithmetic_negation(self): self.check_parser(NEGATION_STR, NEGATION_AST) self.check_parser('--2', AST.Negation(AST.Negation(AST.Number(2)))) self.check_parser('- -2', AST.Negation(AST.Negation(AST.Number(2)))) self.check_parser('- -2', AST.Negation(AST.Negation(AST.Number(2))))
def test_conjunction(self): self.check_parser(CONJUNCTION_STR, CONJUNCTION_AST) self.check_parser('%s and %s' % (DATE_STR, DATE_STR), AST.Conjunction(DATE_AST, DATE_AST))
def test_simple_value(self): # self.check_simple_parser('ala.ma.kota', AST.QualifiedName(['ala', 'ma', 'kota'])) # self.check_simple_parser('ala.ma.kota', AST.QualifiedName(['ala', 'ma', 'kota'])) self.check_simple_parser(DATE_STR, AST.SimpleExpressions([DATE_AST]))
def test_if_expression(self): self.check_parser('if %s then %s else %s' % ((DATE_STR,) * 3), AST.If(*(DATE_AST,) * 3)) self.check_parser(IF_STR, IF_AST)
def test_simple_unary_tests(self): self._check_simple_unary_tests_parser(SIMPLE_POSITIVE_UNARY_TESTS_STR, SIMPLE_POSITIVE_UNARY_TESTS_AST) self._check_simple_unary_tests_parser('not (%s)' % SIMPLE_POSITIVE_UNARY_TESTS_STR, AST.Not(SIMPLE_POSITIVE_UNARY_TESTS_AST)) self._check_simple_unary_tests_parser('-', AST.NoTest())
def test_for_expression(self): self.check_parser(FOR_STR, FOR_AST) self.check_parser('for ala in %s ola in %s return %s' % (IF_STR, QUANTIFIED_STR, DISJUNCTION_STR), AST.For([(AST.Name('ala'), IF_AST), (AST.Name('ola'), QUANTIFIED_AST)], DISJUNCTION_AST))
def test_simple_positive_unary_test(self): self.check_parser('1 in <2', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.LtEp(AST.Endpoint(AST.Number(2)))]))) self.check_parser('1 in <=2', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.LteEp(AST.Endpoint(AST.Number(2)))]))) self.check_parser('1 in >2', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.GtEp(AST.Endpoint(AST.Number(2)))]))) self.check_parser('1 in >=2', AST.In(AST.Number(1), AST.PositiveUnaryTests([AST.GteEp(AST.Endpoint(AST.Number(2)))])))
def p_null(self, p): """null : NULL""" p[0] = AST.Null()