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_int_lit_in_expr(self): integer = "1000" int_node1 = AST.IntNode(int(integer)) self.validate_node(self.exp_parser, integer, int_node1) integer = "10000000000000000" int_node2 = AST.IntNode(int(integer)) self.validate_node(self.exp_parser, integer, int_node2) self.check_parse_exception(self.exp_parser, "0x900", pp.ParseException)
def test_monops(self): monop_node1 = AST.NegNode(AST.BoolNode(True)) self.validate_node(self.exp_parser, "!true", monop_node1) monop_node2 = AST.NegNode(monop_node1) self.validate_node(self.exp_parser, "!!true", monop_node2) monop_node3 = AST.MinNode(AST.IntNode(int("30"))) self.validate_node(self.exp_parser, "-30", monop_node3) monop_node4 = AST.PlusNode(AST.IntNode(int("40"))) self.validate_node(self.exp_parser, "+40", monop_node4)
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)