def test_simple_term(self): p = [Item(), Item(pos=4, size=3, head="\t", tail="\r")] self.headtail.simple_term(p) self.assertEqual(p[0].head, "\t") self.assertEqual(p[0].tail, "\r") self.assertEqual(p[0].pos, 4) self.assertEqual(p[0].size, 3)
def test_simple(self): self.assertEqual(Item(pos=0, size=3).span(), (0, 3)) self.assertEqual( Item(head="\r", tail="\t\t", pos=1, size=3).span(), (1, 4)) self.assertEqual( Item(head="\r", tail="\t\t", pos=1, size=3).span(head_tail=True), (0, 6))
def test_binary_operation(self): p = [ Item(), Item(pos=1, size=3, head="\t", tail="\n"), Item(pos=4, size=4, head="\r", tail=" "), ] self.headtail.binary_operation(p, op_tail="") self.assertEqual(p[0].pos, 0) self.assertEqual(p[0].size, 12) self.headtail.binary_operation(p, op_tail=" ") self.assertEqual(p[0].pos, 0) self.assertEqual(p[0].size, 10)
def test_post_unary(self): p = [ Item(), Item(head="\t", tail="\r", pos=3, size=3), Item(head="\n", tail=" ", pos=5, size=5), ] self.headtail.post_unary(p) self.assertEqual(p[0].head, "") self.assertEqual(p[0].tail, " ") self.assertEqual(p[0].pos, 2) self.assertEqual(p[0].size, 11) self.assertEqual(p[1].head, "\t") self.assertEqual(p[1].tail, "\r\n") self.assertEqual(p[1].pos, 3) self.assertEqual(p[1].pos, 3)
def test_search_field(self): p = [ Item(), # result Item(head="\t", tail="\r", pos=3, size=3), # name Item(head="\n", tail=" ", pos=5, size=1), # : Item(head="\n\n", tail="\t\t", pos=7, size=5), # expr ] self.headtail.search_field(p) # result self.assertEqual(p[0].head, "\t") self.assertEqual(p[0].tail, "") self.assertEqual(p[0].pos, 3) self.assertEqual(p[0].size, 17) # expr self.assertEqual(p[3].head, " \n\n") self.assertEqual(p[3].tail, "\t\t") self.assertEqual(p[3].pos, 7)
def test_token_first_pos(self): for value in (TokenValue("test"), Item()): create_token = token_factory() token = create_token("OTHER", value, 0) self.handle(token, "test") self.assertEqual(token.value.head, "") self.assertEqual(token.value.tail, "") self.assertEqual(token.value.pos, 0) self.assertEqual(token.value.size, 4)
def test_paren(self): p = [ Item(), # result Item(head="\t", tail="\r", pos=3, size=1), # ( Item(head="\n", tail=" ", pos=5, size=3), # expr Item(head="\n\n", tail="\t\t", pos=7, size=1), # ) ] self.headtail.paren(p) # result self.assertEqual(p[0].head, "\t") self.assertEqual(p[0].tail, "\t\t") self.assertEqual(p[0].pos, 3) self.assertEqual(p[0].size, 11) # expr self.assertEqual(p[2].head, "\r\n") self.assertEqual(p[2].tail, " \n\n") self.assertEqual(p[2].pos, 5) self.assertEqual(p[2].size, 3)
def test_separator_first_in_head(self): for value in (TokenValue("test"), Item()): create_token = token_factory() self.handle(create_token("SEPARATOR", "\t", 0), "\t") token = create_token("OTHER", value, 1) self.handle(token, "test") self.assertEqual(token.value.head, "\t") self.assertEqual(token.value.tail, "") self.assertEqual(token.value.pos, 1) self.assertEqual(token.value.size, 4)
def test_separator_last_elt_none(self): # this is a robustness test for value in (TokenValue("test"), Item()): create_token = token_factory() self.handle(create_token("SEPARATOR", "\t", 0), "\t") self.handle(create_token("SEPARATOR", "\n", 1), "\n") token = create_token("OTHER", value, 2) self.handle(token, "test") self.assertEqual(token.value.head, "\t") self.assertEqual(token.value.tail, "") self.assertEqual(token.value.pos, 2) self.assertEqual(token.value.size, 4)
def test_range(self): p = [ Item(), # result Item(head="\t", tail="\r", pos=3, size=1), # [ Item(head="\n", tail=" ", pos=5, size=3), # expr1 Item(head="\n\n", tail="\t\t", pos=7, size=2), # TO Item(head="\r\r", tail=" \t ", pos=9, size=5), # expr2 Item(head=" \r ", tail=" \n ", pos=12, size=1), # ] ] self.headtail.range(p) # result self.assertEqual(p[0].head, "\t") self.assertEqual(p[0].tail, " \n ") self.assertEqual(p[0].pos, 3) self.assertEqual(p[0].size, 28) # expr1 self.assertEqual(p[2].head, "\r\n") self.assertEqual(p[2].tail, " \n\n") self.assertEqual(p[2].pos, 5) self.assertEqual(p[2].size, 3) # expr2 self.assertEqual(p[4].head, "\t\t\r\r") self.assertEqual(p[4].tail, " \t \r ") self.assertEqual(p[4].pos, 9) self.assertEqual(p[4].size, 5)
def test_tail_at_end(self): create_token = token_factory() a = create_token("OTHER", TokenValue("a"), 0) b = create_token("OTHER", Item(), 3) self.handle(a, "a") self.handle(create_token("SEPARATOR", " \t", 1), "\t") self.handle(b, "") # finish with a separator self.handle(create_token("SEPARATOR", "\r", 4), "\r") self.assertEqual(a.value.head, "") self.assertEqual(a.value.tail, " \t") self.assertEqual(a.value.pos, 0) self.assertEqual(a.value.size, 1) self.assertEqual(b.value.head, "") self.assertEqual(b.value.tail, "\r") self.assertEqual(b.value.pos, 3) self.assertEqual(b.value.size, 0)
def test_simple_token_tail(self): create_token = token_factory() a = create_token("OTHER", TokenValue("a"), 0) b = create_token("OTHER", Item(), 3) c = create_token("OTHER", TokenValue("c"), 5) self.handle(a, "a") self.handle(create_token("SEPARATOR", " \t", 1), "\t") self.handle(b, "") self.handle(create_token("SEPARATOR", "\r", 4), "\r") self.handle(c, "c") self.assertEqual(a.value.head, "") self.assertEqual(a.value.tail, " \t") self.assertEqual(a.value.pos, 0) self.assertEqual(a.value.size, 1) self.assertEqual(b.value.head, "") self.assertEqual(b.value.tail, "\r") self.assertEqual(b.value.pos, 3) self.assertEqual(b.value.size, 0) self.assertEqual(c.value.head, "") self.assertEqual(c.value.tail, "") self.assertEqual(c.value.pos, 5) self.assertEqual(c.value.size, 1)
def test_none(self): self.assertEqual(Item(pos=None, size=3).span(), (None, None)) self.assertEqual( Item(pos=None, size=3).span(head_tail=True), (None, None))
def test_pos(self): p = [Item(), Item(pos=4, size=3)] self.headtail.pos(p) self.assertEqual(p[0].pos, 4) self.assertEqual(p[0].size, 3)
def test_pos_without_head_transfer(self): p = [Item(), Item(pos=4, size=3, head="\r\n")] self.headtail.pos(p, head_transfer=False) self.assertEqual(p[0].pos, 2) self.assertEqual(p[0].size, 5)
def test_pos_with_head_transfer(self): p = [Item(), Item(pos=4, size=3, head="\r\n")] self.headtail.pos(p, head_transfer=True) self.assertEqual(p[0].pos, 4) self.assertEqual(p[0].size, 3)
def test_pos_none(self): p = [Item(), Item(pos=None, size=None)] self.headtail.pos(p) self.assertEqual(p[0].pos, None) self.assertEqual(p[0].size, 0)