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 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)