Пример #1
0
 def test_two_chars(self):
     obj = GWord("aB")
     text = "aBBa"
     text_iterator = TextIterator(text)
     self.assertTrue(obj.match(text_iterator))
     self.assertEqual((0, 0), obj.get_start_pos().tuple())
     self.assertEqual((0, 1), obj.get_last_pos().tuple())
Пример #2
0
 def test_underscore_name(self):
     obj = GName()
     text = "\naBBa"
     text_iterator = TextIterator(text)
     self.assertTrue(obj.match(text_iterator))
     self.assertEqual("aBBa", obj.get_name())
     self.assertEqual((1, 3), obj.get_last_pos().tuple())
Пример #3
0
 def test_single_line(self):
     text = "asdf"
     text_iterator = TextIterator(text)
     gany = GAny()
     self.assertTrue(gany.match(text_iterator))
     pos = gany.get_last_pos()
     self.assertEqual("asdf", gany.get_text())
     self.assertEqual((0, 3), pos.tuple())
Пример #4
0
 def test_one_letter_name(self):
     obj = GName()
     text = "a"
     text_iterator = TextIterator(text)
     self.assertTrue(obj.match(text_iterator))
     self.assertEqual("a", obj.get_name())
     self.assertEqual((0, 0), obj.get_start_pos().tuple())
     self.assertEqual((0, 0), obj.get_last_pos().tuple())
Пример #5
0
 def test_def_name(self):
     defs = GSequence([GWord("def"), GName()])
     lines = ["def Test_Fun123", ""]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertTrue(defs.match(text_iterator))
     pos = defs.get_last_pos()
     self.assertEqual((0, 14), pos.tuple())
     self.assertEqual("Test_Fun123", defs.get_definitions()[1].get_name())
Пример #6
0
 def test_3_words(self):
     defs = GSequence([GWord("def"), GWord("def"), GWord("def")])
     lines = ["def def", "   def"]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertTrue(defs.match(text_iterator))
     self.assertEqual((1, len(lines[1]) - 1),
                      defs.get_last_pos().tuple(),
                      "Position should be in last line")
Пример #7
0
 def test_empty_braces(self):
     exp = BracketExp("(", ")")
     text = "()"
     text_iterator = TextIterator(text)
     self.assertTrue(exp.match(text_iterator))
     pos = exp.get_last_pos()
     self.assertEqual((1, 0), pos.tuple())
     body = exp.get_body().get_text()
     self.assertEqual("", body)
Пример #8
0
 def test_fun_arguments(self):
     exp = BracketExp("(", ")")
     exp.set_recursive_definitions([exp, GAny()])
     text = "(a,b,c)"
     text_iterator = TextIterator(text)
     self.assertTrue(exp.match(text_iterator))
     pos = exp.get_last_pos()
     self.assertEqual((1, 0), pos.tuple())
     body = exp.get_body().get_text()
     self.assertEqual("a,b,c", body)
Пример #9
0
 def test_def_fun_incomplete(self):
     defs = GSequence([GWord("def"), GName()])
     lines = ["def Test_Fun123(a: Integer)", ""]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertTrue(defs.match(text_iterator))
     pos = defs.get_last_pos().tuple()
     self.assertEqual(0, pos[0])
     self.assertEqual(len("def Test_Fun123") - 1, pos[1])
     self.assertEqual("Test_Fun123", defs.get_definitions()[1].get_name())
Пример #10
0
 def test_single_word(self):
     a = GWord("A")
     b = GWord("B")
     obj = GChoice([a, b])
     text = "B"
     text_iterator = TextIterator(text)
     self.assertTrue(obj.match(text_iterator))
     pos = obj.get_last_pos()
     self.assertEqual(1, pos[0])
     self.assertEqual(b, obj.get_chosen())
Пример #11
0
 def test_bracket_exp_def(self):
     bracket_exp = BracketExp("(", ")")
     defs = GSequence([GWord("def"), bracket_exp])
     # defs.set_recursive_definitions([defs])
     lines = ["def(a, b, c) "]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertTrue(defs.match(text_iterator))
     pos = defs.get_last_pos().tuple()
     self.assertEqual((0, len(lines[0]) - 2), pos)
     self.assertEqual("a, b, c", bracket_exp.get_body().get_text())
Пример #12
0
 def test_bracket_exp3(self):
     defs = GSequence(
         [GWord("def"),
          GName(), BracketExp("(", ")"),
          GType()])
     lines = ["def TestFun_123(a, b, c) :Unit"]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertTrue(defs.match(text_iterator))
     pos = defs.get_last_pos()
     self.assertEqual(len(lines), pos[0])
Пример #13
0
 def test_optional_invalid(self):
     defs = GSequence([
         GWord("def"),
         GName(),
         GOptional(GWord(":")),
         GName(),
         GWord("{")
     ])
     lines = ["def test {"]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertFalse(defs.match(text_iterator))
Пример #14
0
    def test_sequence(self):
        int_choose = GWord("int")
        float_choose = GWord("float")
        tchoice = GChoice([int_choose, float_choose])

        def_choice = GChoice([GWord(";"), GWord("= 1;")])
        name = GName()
        seq = GSequence([tchoice, name, def_choice])
        text = "int num;"
        text_iterator = TextIterator(text)
        self.assertTrue(seq.match(text_iterator))
        self.assertEqual("num", name.get_name())
        self.assertEqual(int_choose, tchoice.get_chosen())
Пример #15
0
 def test_nested_bracket_exps(self):
     defs = GSequence([
         GWord("def"),
         GName(),
         BracketExp("(", ")"),
         BracketExp("{", "}")
     ])
     nestedFun = "def hello() { print(\"hello\") }"
     lines = ["def TestFun_123(a, b, c) {", nestedFun, "}"]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertTrue(defs.match(text_iterator))
     pos = defs.get_last_pos()
     self.assertEqual(len(lines), pos[0])
Пример #16
0
 def test_optional_type(self):
     defs = GSequence([
         GWord("def"),
         GName(),
         GOptional(GWord(":")),
         GName(),
         GWord("{")
     ])
     lines = ["def test: test_name {"]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertTrue(defs.match(text_iterator))
     pos = defs.get_last_pos().tuple()
     self.assertEqual((0, 20), pos)
Пример #17
0
 def test_bracket_exp2(self):
     defs = GSequence([
         GWord("def"),
         GName(),
         BracketExp("(", ")"),
         BracketExp("{", "}")
     ])
     defs.set_recursive_definitions([defs])
     lines = [
         "def TestFun_123(a, b, c) {", " print(123) ", "print(456)", "}"
     ]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertTrue(defs.match(text_iterator))
     pos = defs.get_last_pos()
     self.assertEqual(len(lines), pos[0])
Пример #18
0
 def test_spaces(self):
     obj = GName()
     text = " \t test_name"
     text_iterator = TextIterator(text)
     self.assertTrue(obj.match(text_iterator))
     self.assertEqual((0, len(text) - 1), obj.get_last_pos().tuple())
Пример #19
0
 def __init__(self, text):
     self._text_iterator = TextIterator(text)
     self._definitions = [ClassDefinition(), GSequence([GAny()])]
     for df in self._definitions:
         df.set_recursive_definitions(self._definitions)
Пример #20
0
 def test_3_words_invalid(self):
     defs = GSequence([GWord("def"), GWord("def"), GWord("def")])
     lines = ["def def", "   abc"]
     text = "\n".join(lines)
     text_iterator = TextIterator(text)
     self.assertFalse(defs.match(text_iterator))
Пример #21
0
 def test_empty_text(self):
     text = ""
     text_iterator = TextIterator(text)
     gany = GAny()
     self.assertFalse(gany.match(text_iterator))
     self.assertEqual("", gany.get_text())
Пример #22
0
 def test_one_char(self):
     obj = GWord("a")
     text = "a"
     text_iterator = TextIterator(text)
     self.assertTrue(obj.match(text_iterator))
     self.assertEqual((0, 0), obj.get_last_pos().tuple())