Ejemplo n.º 1
0
    def test_typing_1(self):
        the_string = """
        ::.String: END
        ::.Number: END

        ::.a.test.type:
            .name.$x(::.String)
            .age.$y(::.Number)
        END

        ::.second.type:
            .name.$x(::.String)
            .age.$y(::.String)
        END

        .bob(::.a.test.type).age.$z
        .bill(::.second.type).age.$q
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        env.validate()
        self.assertTrue(
            env.query(
                p.safeParse(
                    p.MAIN,
                    ".bob.age.$z(::.Number)\n .bill.age.$q(::.String)")))
Ejemplo n.º 2
0
    def test_update_environment(self):
        the_string = """
        ::.String: END
        ::.Number: END
        ::.test.person:
        	.name.$x(::.String)
            .age.$y(::.Number)
        END

        .a.test.sentence.$x(::.test.person)

        ::.test.location:
            .name.$x(::.String)
            .size.$y(::.String)
        END
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        #check type equations are added
        self.assertEqual(len(env.type_equations), 4)
        #Check type assignments are added
        self.assertEqual(len(env.type_assignments), 1)
        result2 = p.safeParse(p.MAIN, ".a.second.test.$y(::.test.location)")[0]
        env.add(result2)
        self.assertEqual(len(env.type_assignments), 2)

        result3 = p.safeParse(p.MAIN,
                              "::.test.new:\n .name.$x(::.String)\nEND")[0]
        env.add(result3)
        self.assertEqual(len(env.type_equations), 5)
Ejemplo n.º 3
0
    def test_typing_polytype_nested_2(self):
        the_string = """
        ::.String: END
        ::.Number: END

        ::.ptypeOne[$x]:
            .place.$x
        END

        ::.ptypeTwo[$x]:
            .nested(::.$x)
        END

        .a(::.ptypeTwo(::.ptypeOne(::.Number))).nested.place.$x
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        env.validate()
        # self.assertTrue(env.query(p.safeParse(p.MAIN, ".a(::.ptypeTwo)")))
        self.assertTrue(
            env.query(
                p.safeParse(p.MAIN,
                            ".a(::.ptypeTwo(::.ptypeOne(::.Number)))")))
        self.assertTrue(
            env.query(p.safeParse(p.MAIN, ".a.nested.place.$x(::.Number)")))
Ejemplo n.º 4
0
    def test_typing_nested_types_fail(self):
        the_string = """
        ::.String: END
        ::.Number: END

        ::.small.type:
            .name.$x(::.String)
        END

        ::.large.type:
           .component.$x(::.small.type)
        END

        .a(::.large.type).component.$q.name.$w
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        env.validate()
        with self.assertRaises(te.TypeConflictException):
            self.assertTrue(
                env.query(
                    p.safeParse(
                        p.MAIN,
                        ".a(::.large.type)\n .a.component.$q(::.small.type)\n .a.component.$q.name.$w(::.Number)"
                    )))
Ejemplo n.º 5
0
 def test_infer_type(self):
     the_string = """
     ::.first: END
     .a.$y(::.first)
     .a.$x
     """
     result = p.safeParse(p.MAIN, the_string)
     env = Environment(result[:])
     env.validate()
     queried = env.type_assignments.query(p.safeParse(p.MAIN, ".a.$x")[0])
     self.assertEqual(queried._type, result[1][-1]._type)
Ejemplo n.º 6
0
    def test_infer_component(self):
        the_string = """
        ::.String: END
        ::.first:
            .name.$x(::.String)
        END

        .a.$x(::.first)
        .$x.name.$y
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        env.validate()
        env.query(p.safeParse(p.MAIN, ".$x (::.first)"))
        env.query(p.safeParse(p.MAIN, ".$x.name.$y(::.String)"))
Ejemplo n.º 7
0
 def test_parse_word_nested_types(self):
     result = p.safeParse(p.WORD, ".blah(::.First(::.Second))")[0]
     self.assertEqual(result.name, "blah")
     self.assertEqual(result.ex, ExOp.DOT)
     self.assertEqual(repr(result._type.name), ".First")
     self.assertEqual(len(result._type._args), 1)
     self.assertEqual(repr(result._type._args[0].name), ".Second")
Ejemplo n.º 8
0
    def test_parse_multiple_mixed_elements(self):
        the_string = """
        ::.test.person:
        	.name.$x(::.String)
            .age.$y(::.Number)
        END

        .a.test.sentence.$x(::.test.person)

        ::.test.location:
            .name.$x(::.String)
            .size.$y(::.String)
        END

        .a.test.rule(::.rule):
            .a.test.sentence.$q
            .$q.name.$y
        END
        """
        result = p.safeParse(p.MAIN, the_string)
        typedefs = [x for x in result if isinstance(x, TypeDefinition)]
        self.assertEqual(len(typedefs), 2)
        rules = [x for x in result if isinstance(x, Rule)]
        self.assertEqual(len(rules), 1)
        declarations = [x for x in result if isinstance(x, list)]
        self.assertEqual(len(declarations), 1)
Ejemplo n.º 9
0
 def test_update_environment_conflict_3(self):
     the_string = """
     .a.test(::.first)
     """
     result = p.safeParse(p.MAIN, the_string)
     with self.assertRaises(te.TypeUndefinedException):
         env = Environment(result[:])
         env.validate()
Ejemplo n.º 10
0
    def test_typing_polytype(self):
        the_string = """
        ::.String: END
        ::.Number: END

        ::.polytype[$x]:
            .name.$x
        END

        .a(::.polytype(::.String)).name.$q
        .b(::.polytype(::.Number)).name.$t
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        env.validate()
        self.assertTrue(env.query(p.safeParse(p.MAIN, ".a(::.polytype)")))
        self.assertTrue(
            env.query(p.safeParse(p.MAIN, ".a(::.polytype(::.String))")))
        self.assertTrue(env.query(p.safeParse(p.MAIN,
                                              ".a.name.$q(::.String)")))

        self.assertTrue(env.query(p.safeParse(p.MAIN, ".b(::.polytype)")))
        self.assertTrue(
            env.query(p.safeParse(p.MAIN, ".b(::.polytype(::.Number))")))
        self.assertTrue(env.query(p.safeParse(p.MAIN,
                                              ".b.name.$t(::.Number)")))
Ejemplo n.º 11
0
 def test_type_redefinition(self):
     the_string = """
     ::.String: END
     ::.String: END
     .a.$x(::.String)
     """
     result = p.safeParse(p.MAIN, the_string)
     with self.assertRaises(te.TypeRedefinitionException):
         env = Environment(result[:])
Ejemplo n.º 12
0
 def test_variable_conflict(self):
     the_string = """
     ::.String: END
     ::.Number: END
     .a.$x(::.String)
     .b.$x(::.Number)
     """
     result = p.safeParse(p.MAIN, the_string)
     with self.assertRaises(te.TypeConflictException):
         env = Environment(result[:])
Ejemplo n.º 13
0
 def test_type_undefined(self):
     the_string = """
     ::.String: END
     ::.Number: END
     .a.$x(::.String)
     .b.$y(::.Blah)
     """
     result = p.safeParse(p.MAIN, the_string)
     env = Environment(result[:])
     with self.assertRaises(te.TypeUndefinedException):
         env.validate()
Ejemplo n.º 14
0
    def test_update_environment_conflict_2(self):
        the_string = """
        ::.first: END
        ::.second: END

        .a.test(::.first)
        .a.test(::.second)
        """
        result = p.safeParse(p.MAIN, the_string)
        with self.assertRaises(te.TypeConflictException):
            env = Environment(result[:])
Ejemplo n.º 15
0
    def test_parse_typedef_with_vars(self):
        the_string = """
        ::.polytype[$x]:
            .name.$x
            .string.$x
        END

        """
        result = p.safeParse(p.MAIN, the_string)[0]
        self.assertEqual(repr(result.name), ".polytype")
        self.assertEqual(len(result.structure), 2)
        self.assertEqual(len(result._vars), 1)
Ejemplo n.º 16
0
    def test_structure_type_conflict(self):
        the_string = """
        ::.String: END
        ::.Number: END
        ::.first:
            .name.$x(::.String)
        END

        .a(::.first).name.$y(::.Number)
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        with self.assertRaises(te.TypeConflictException):
            env.validate()
Ejemplo n.º 17
0
    def test_structure_mismatch(self):
        the_string = """
        ::.String: END
        ::.Number: END
        ::.first:
            .name.$x(::.String)
        END

        .a(::.first).b
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        with self.assertRaises(te.TypeStructureMismatch):
            env.validate()
Ejemplo n.º 18
0
    def test_typing_polytype_nested_3(self):
        the_string = """
        ::.String: END
        ::.Number: END

        ::.ptypeOne[$x, $y]:
            .place.$x
            .age.$y
        END

        .a(::.ptypeOne(::.String, ::.Number)).place.$x
        .a.age.$y
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        env.validate()
        # self.assertTrue(env.query(p.safeParse(p.MAIN, ".a(::.ptypeTwo)")))
        self.assertTrue(
            env.query(
                p.safeParse(p.MAIN, ".a(::.ptypeOne(::.String, ::.Number))")))
        self.assertTrue(
            env.query(p.safeParse(p.MAIN, ".a.place.$x(::.String)")))
        self.assertTrue(env.query(p.safeParse(p.MAIN, ".a.age.$y(::.Number)")))
Ejemplo n.º 19
0
    def test_parse_multiple_type_definitions(self):
        the_string = """

    ::.test.person:
        .name.$x(::.String)
        .age.$y(::.Number)
    END

    ::.test.location:
        .name.$x(::.String)
        .size.$y(::.String)
    END
        """
        result = p.safeParse(p.MAIN, the_string)
        self.assertEqual(len(result), 2)
Ejemplo n.º 20
0
    def test_update_environment_conflict(self):
        the_string = """
        ::.test.person:
        	.name.$x(::.String)
            .age.$y(::.Number)
        END

        ::.test.person:
            .name.$x(::.String)
            .size.$y(::.String)
        END
        """
        result = p.safeParse(p.MAIN, the_string)
        with self.assertRaises(te.TypeRedefinitionException):
            env = Environment(result[:])
Ejemplo n.º 21
0
    def test_typing_polytype_nested_fail(self):
        the_string = """
        ::.String: END
        ::.Number: END

        ::.ptypeOne[$x, $y]:
            .place.$x
            .age.$y
        END

        .a(::.ptypeOne(::.String, ::.Number)).place.$x
        .a.age.$x
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        with self.assertRaises(te.TypeConflictException):
            env.validate()
Ejemplo n.º 22
0
    def test_construct_base_environment(self):
        the_string = """
        ::.String: END
        ::.Number: END
        ::.test.person:
        	.name.$x(::.String)
            .age.$y(::.Number)
        END

        .a.test.sentence.$x(::.test.person)

        ::.test.location:
            .name.$x(::.String)
            .size.$y(::.String)
        END
        """
        result = p.safeParse(p.MAIN, the_string)
        env = Environment(result[:])
        #check type equations are added
        self.assertEqual(len(env.type_equations), 4)
        #Check type assignments are added
        self.assertEqual(len(env.type_assignments), 1)
Ejemplo n.º 23
0
 def test_parse_word(self):
     result = p.safeParse(p.WORD, ".blah")[0]
     self.assertEqual(result.name, "blah")
     self.assertEqual(result.ex, ExOp.DOT)
     self.assertIsNone(result._type)
Ejemplo n.º 24
0
 def test_parse_simple_rule(self):
     the_string = " .a.rule:\n .a.b.$x(::.Something)\n .q.r.$x\n .q.w!$y\n END"
     result = p.safeParse(p.RULE, the_string)[0]
     self.assertEqual(repr(result), "Rule: '.a.rule'")
Ejemplo n.º 25
0
 def test_parse_type_definition(self):
     the_string = "::.MyType:\n .a.$x(::.Blah)\n .b!$y(::.Bloo)\nEND"
     result = p.safeParse(p.TYPEDEF, the_string)[0]
     self.assertEqual(repr(result.name), ".MyType")
     self.assertEqual(len(result.structure), 2)
Ejemplo n.º 26
0
 def test_parse_word_typed(self):
     result = p.safeParse(p.WORD, ".blah(::.Test)")[0]
     self.assertEqual(result.name, "blah")
     self.assertEqual(result.ex, ExOp.DOT)
     self.assertEqual(repr(result._type.name), ".Test")