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 def testStringLineReader(self):
17 arena = test_lib.MakeArena('<reader_test.py>')
18
19 r = reader.StringLineReader('one\ntwo', arena)
20
21 src_line, offset = r.GetLine()
22 self.assertEqual('one\n', src_line.content),
23 self.assertEqual(1, src_line.line_num)
24 self.assertEqual(0, offset)
25
26 src_line, offset = r.GetLine()
27 self.assertEqual('two', src_line.content),
28 self.assertEqual(2, src_line.line_num)
29 self.assertEqual(0, offset)
30
31 src_line, offset = r.GetLine()
32 self.assertEqual(None, src_line)
33 self.assertEqual(0, offset)
34
35 def testLineReadersAreEquivalent(self):
36 a1 = alloc.Arena()
37 r1 = reader.StringLineReader('one\ntwo', a1)
38
39 a2 = alloc.Arena()
40 f = cStringIO.StringIO('one\ntwo')
41 r2 = reader.FileLineReader(f, a2)
42
43 a3 = alloc.Arena()
44
45 line1 = SourceLine(1, 'one\n', None)
46 line2 = SourceLine(2, 'two', None)
47
48 lines = [(line1, 0), (line2, 0)]
49 r3 = reader.VirtualLineReader(lines, a3)
50
51 for a in [a1, a2, a3]:
52 a.PushSource(source.MainFile('reader_test.py'))
53
54 for r in [r1, r2, r3]:
55 print(r)
56
57 src_line, offset = r.GetLine()
58 self.assertEqual('one\n', src_line.content),
59 self.assertEqual(1, src_line.line_num)
60 self.assertEqual(0, offset)
61
62 src_line, offset = r.GetLine()
63 self.assertEqual('two', src_line.content),
64 self.assertEqual(2, src_line.line_num)
65 self.assertEqual(0, offset)
66
67 src_line, offset = r.GetLine()
68 self.assertEqual(None, src_line)
69 self.assertEqual(0, offset)
70
71
72 if __name__ == '__main__':
73 unittest.main()