def setUp(self): self.input = "abcdefghi" self.bw = BufferWalker(self.input, None)
class TestBufferWalker(unittest.TestCase): """Checks all backend parser operations work as expected """ def setUp(self): self.input = "abcdefghi" self.bw = BufferWalker(self.input, None) def test_next(self): for c in self.input[1:]: self.assertEquals(self.bw.next(), c) def test_peek(self): for c in self.input: self.assertEquals(self.bw.peek(), c) self.bw.next() def test_current(self): for c, pos in izip(self.input, count(1)): self.assertEquals(self.bw.current(), (c, pos)) self.bw.next() def test_pos(self): for c, pos in izip(self.input, count(1)): self.assertEquals(self.bw.pos(), pos) self.bw.next() def test_fail(self): self.assertRaises(NoMatch, self.bw.fail) self.assertEquals(self.bw.peek(), 'a') def test_tri_accept(self): self.bw.tri(self.bw.next) self.assertEquals(self.bw.peek(), 'b') def test_tri_fail(self): def fun(): self.bw.next() self.bw.fail() self.assertRaises(NoMatch, p(self.bw.tri, fun)) def test_commit_multiple(self): def multiple_commits(): self.bw.commit() self.bw.commit() multiple_commits() self.bw.tri(multiple_commits) def test_commit_accept(self): def fun(): self.bw.next() self.bw.commit() self.bw.tri(fun) self.assertEquals(self.bw.peek(), 'b') def test_commit_fail(self): def fun(): self.bw.next() self.bw.commit() self.bw.fail() self.assertRaises(NoMatch, p(self.bw.tri, fun)) self.assertEquals(self.bw.peek(), 'b') def test_choice_null(self): self.bw.choice() def test_choice_accept(self): self.bw.choice(self.bw.fail, self.bw.next) self.bw.choice(self.bw.next, self.bw.fail) self.bw.choice(self.bw.next, self.bw.next) self.assertEquals(self.bw.peek(), 'd') def test_choice_fail(self): self.assertRaises(NoMatch, p(self.bw.choice, self.bw.fail, self.bw.fail)) def test_choice_commit_fail(self): def fun(): self.bw.next() self.bw.commit() self.bw.fail() self.assertRaises(NoMatch, p(self.bw.choice, fun)) self.assertEquals(self.bw.peek(), 'b')