def test_arithmetic_expr(self): self.check_good_expression("10 * 20", AST.IntTypeNode()) self.check_good_expression("10.0 * 20", AST.DecimalTypeNode()) self.check_good_expression("10 + 20", AST.IntTypeNode()) self.check_good_expression("10.0 + 20", AST.DecimalTypeNode()) self.check_good_expression("\"abc\" + \"def\"", AST.StringTypeNode()) self.check_bad_expression("10 * \"abc\"", 0, 1) self.check_bad_expression("10 + true", 0, 1) self.check_bad_expression("10 + \"abc\"", 0, 1)
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)
def string_node(_): return AST.StringTypeNode()