def test_var_lit_in_expr(self): name = "varName" var_node1 = AST.VarNode(name) self.validate_node(self.exp_parser, name, var_node1) name = "var_name_129" var_node2 = AST.VarNode(name) self.validate_node(self.exp_parser, name, var_node2) name = "v" var_node3 = AST.VarNode(name) self.validate_node(self.exp_parser, name, var_node3) self.check_parse_exception(self.exp_parser, "_hallo", pp.ParseException)
def test_binops(self): binop1 = ["1000", ">", "100.0"] binop_node1 = AST.GTNode(AST.IntNode(int(binop1[0])), AST.DecimalNode(Decimal(binop1[2]))) self.validate_node(self.exp_parser, "".join(binop1), binop_node1) binop2 = binop1 + ["==", "true"] binop_node2 = AST.EqNode(binop_node1, AST.BoolNode(True)) self.validate_node(self.exp_parser, "".join(binop2), binop_node2) # (100 + var) * (true) + "String" lit_bool = AST.BoolNode(True) lit_int = AST.IntNode(int("100")) lit_var = AST.VarNode("var") lit_string = AST.StringNode("String") binop_node3 = AST.AddNode(lit_int, lit_var) binop_node3 = AST.MulNode(binop_node3, lit_bool) binop_node3 = AST.AddNode(binop_node3, lit_string) self.validate_node(self.exp_parser, "(100 + var) * true + \"String\"", binop_node3) self.check_parse_exception(self.exp_parser, '20 +', pp.ParseException) self.check_parse_exception(self.exp_parser, '20 ** 30', pp.ParseException) self.check_parse_exception(self.exp_parser, '30 // 40', pp.ParseException)
def testUndefinedExpr(self): expr = "var * 10" self.env.add_var(AST.QuestionNode("q", AST.VarNode("var"), "money")) parse_res = self.exp_parser.parseString(expr, parseAll=True)[0] self.assertEqual(parse_res.accept(self.evaluator), Undefined) self.env.set_var_value("var", decimal.Decimal("10")) self.assertEqual(parse_res.accept(self.evaluator), decimal.Decimal("100")) self.env.clear_env()
def test_conditionalIfElse(self): if_expr = AST.VarNode("var") if_block_question = AST.QuestionNode("q?", "var", AST.BoolTypeNode()) if_block = AST.BlockNode([if_block_question]) if_else_cond = AST.IfElseNode(if_expr, if_block, if_block) parse_str = 'if (var) { "q?" var : boolean } else { "q?" var : boolean}' self.validate_node(self.cond_parser, parse_str, if_else_cond) self.check_parse_exception(self.cond_parser, 'if (var) {', pp.ParseException)
def test_computed_questions(self): def format_question(q, n, var_type, expr): return "\"{}\" {}: {} = ({})".format(q, n, var_type, expr) question = "Did you sell a house in 2010?" name = "hasSoldHouse" q_type = AST.BoolTypeNode() q_expr = "-var >= 600" node_expr = AST.GTENode(AST.MinNode(AST.VarNode("var")), AST.IntNode(int("600"))) q_str = format_question(question, name, "boolean", q_expr) test_node = AST.ComputedQuestionNode(question, name, q_type, node_expr) self.validate_node(self.q_parser, q_str, test_node)
def test_expression_combinations(self): # !(5.0 + +10 / var1 / 2 - !var2 && (19. * .12) || false) # = invalid eq but is parseable. int1 = AST.IntNode(int("10")) int2 = AST.IntNode(int("2")) dec1 = AST.DecimalNode(Decimal("5.0")) dec2 = AST.DecimalNode(Decimal("19.")) dec3 = AST.DecimalNode(Decimal(".12")) var1 = AST.VarNode("var1") var2 = AST.VarNode("var2") bool1 = AST.BoolNode(False) # First prefix +, then division expr = AST.DivNode(AST.PlusNode(int1), var1) expr = AST.DivNode(expr, int2) # Secondly + and - expr = AST.AddNode(dec1, expr) expr = AST.SubNode(expr, AST.NegNode(var2)) # Thirdly &&, last || and the very last ! expr = AST.AndNode(expr, AST.MulNode(dec2, dec3)) expr = AST.NegNode(AST.OrNode(expr, bool1)) string_expr = "!(5.0 + +10 / var1 / 2 - !var2 && (19. * .12) || false)" self.validate_node(self.exp_parser, string_expr, expr)
def test_parse_form(self): form1 = """ form TestForm { "question1?" var1 : money if (var1 > 200) { "question2?" var2 : string } } """ q1 = AST.QuestionNode("question1?", "var1", AST.MoneyTypeNode()) q2 = AST.QuestionNode("question2?", "var2", AST.StringTypeNode()) expr = AST.GTNode(AST.VarNode("var1"), AST.IntNode(int("200"))) if_block = AST.IfNode(expr, AST.BlockNode([q2])) form_block = AST.BlockNode([q1, if_block]) form_node = AST.FormNode("TestForm", form_block) form_node = AST.QuestionnaireAST(form_node) self.validate_node(self.parser.grammar, form1, form_node) form2 = """ form TestForm { "question1?" var1 : money if (var1 > 200) { "question2?" var2 : string } else { "question1?" var1 : money } "question2?" var2 : string } """ if_else_block = AST.IfElseNode(expr, AST.BlockNode([q2]), AST.BlockNode([q1])) form_block2 = AST.BlockNode([q1, if_else_block, q2]) form_node = AST.FormNode("TestForm", form_block2) form_node = AST.QuestionnaireAST(form_node) self.validate_node(self.parser.grammar, form2, form_node)