def test_question_parsing(self): def format_question(q, n, var_type): return "\"{}\" {}: {}".format(q, n, var_type) question = "Did you sell a house in 2010?" name = "hasSoldHouse" q_type = AST.BoolTypeNode() q_str = format_question(question, name, "boolean") test_node = AST.QuestionNode(question, name, q_type) self.validate_node(self.q_parser, q_str, test_node) question = "Did you sell a house in 2010?" q_type = AST.MoneyTypeNode() q_str = format_question(question, name, "money") test_node = AST.QuestionNode(question, name, q_type) self.validate_node(self.q_parser, q_str, test_node) question = "question?" name = "moneyVar" q_type = AST.DecimalTypeNode() q_str = format_question(question, name, "decimal") test_node = AST.QuestionNode(question, name, q_type) self.validate_node(self.q_parser, q_str, test_node) question_str = "name: \"question?\" boolean " self.check_parse_exception(self.q_parser, question_str, pp.ParseException) question_str = "\"question\" name boolean " self.check_parse_exception(self.q_parser, question_str, pp.ParseException) question_str = "\"question?\" boolean: name" self.check_parse_exception(self.q_parser, question_str, pp.ParseException)
def highest_number_type(left, right): """ :type left: AST.TypeNode """ if left.is_decimal() or right.is_decimal(): return AST.DecimalTypeNode() elif left.is_money() or right.is_money(): return AST.MoneyTypeNode() elif left.is_integer() and right.is_integer(): return AST.IntTypeNode() else: assert False, "Not both operands are of number type!"
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)