1 | #!/usr/bin/env python2
|
2 | """
|
3 | reader_test.py: Tests for reader.py
|
4 | """
|
5 |
|
6 | import cStringIO
|
7 | import unittest
|
8 |
|
9 | from _devbuild.gen.syntax_asdl import source, SourceLine
|
10 | from core import alloc
|
11 | from core import test_lib
|
12 | from frontend import reader # module under test
|
13 |
|
14 |
|
15 | class ReaderTest(unittest.TestCase):
|
16 |
|
17 | def testStringLineReader(self):
|
18 | arena = test_lib.MakeArena('<reader_test.py>')
|
19 |
|
20 | r = reader.StringLineReader('one\ntwo', arena)
|
21 |
|
22 | src_line, offset = r.GetLine()
|
23 | self.assertEqual('one\n', src_line.content),
|
24 | self.assertEqual(1, src_line.line_num)
|
25 | self.assertEqual(0, offset)
|
26 |
|
27 | src_line, offset = r.GetLine()
|
28 | self.assertEqual('two', src_line.content),
|
29 | self.assertEqual(2, src_line.line_num)
|
30 | self.assertEqual(0, offset)
|
31 |
|
32 | src_line, offset = r.GetLine()
|
33 | self.assertEqual(None, src_line)
|
34 | self.assertEqual(0, offset)
|
35 |
|
36 | def testLineReadersAreEquivalent(self):
|
37 | a1 = alloc.Arena()
|
38 | r1 = reader.StringLineReader('one\ntwo', a1)
|
39 |
|
40 | a2 = alloc.Arena()
|
41 | f = cStringIO.StringIO('one\ntwo')
|
42 | r2 = reader.FileLineReader(f, a2)
|
43 |
|
44 | a3 = alloc.Arena()
|
45 |
|
46 | line1 = SourceLine(1, 'one\n', None)
|
47 | line2 = SourceLine(2, 'two', None)
|
48 |
|
49 | lines = [(line1, 0), (line2, 0)]
|
50 | r3 = reader.VirtualLineReader(a3, lines, False)
|
51 |
|
52 | for a in [a1, a2, a3]:
|
53 | a.PushSource(source.MainFile('reader_test.py'))
|
54 |
|
55 | for r in [r1, r2, r3]:
|
56 | print(r)
|
57 |
|
58 | src_line, offset = r.GetLine()
|
59 | self.assertEqual('one\n', src_line.content),
|
60 | self.assertEqual(1, src_line.line_num)
|
61 | self.assertEqual(0, offset)
|
62 |
|
63 | src_line, offset = r.GetLine()
|
64 | self.assertEqual('two', src_line.content),
|
65 | self.assertEqual(2, src_line.line_num)
|
66 | self.assertEqual(0, offset)
|
67 |
|
68 | src_line, offset = r.GetLine()
|
69 | self.assertEqual(None, src_line)
|
70 | self.assertEqual(0, offset)
|
71 |
|
72 |
|
73 | if __name__ == '__main__':
|
74 | unittest.main()
|