OILS / core / alloc_test.py View on Github | oilshell.org

54 lines, 35 significant
1#!/usr/bin/env python2
2"""alloc_test.py: Tests for alloc.py."""
3
4import unittest
5
6from _devbuild.gen.id_kind_asdl import Id
7from _devbuild.gen.syntax_asdl import source
8from core import alloc # module under test
9
10
11class AllocTest(unittest.TestCase):
12
13 def setUp(self):
14 self.arena = alloc.Arena()
15
16 def testArena(self):
17 arena = self.arena
18 arena.SaveTokens()
19 arena.PushSource(source.MainFile('one.ysh'))
20
21 line = arena.AddLine('line 1', 1)
22 self.assertEqual(1, line.line_num)
23 line = arena.AddLine('line 2', 2)
24 self.assertEqual(2, line.line_num)
25
26 tok = arena.NewToken(Id.Undefined_Tok, -1, -1, -1)
27 self.assertEqual(0, arena.GetSpanId(tok))
28
29 arena.PopSource()
30
31 def testPushSource(self):
32 arena = self.arena
33
34 arena.PushSource(source.MainFile('one.ysh'))
35 arena.AddLine('echo 1a', 1)
36 arena.AddLine('source two.ysh', 2)
37
38 arena.PushSource(source.MainFile('two.ysh'))
39 arena.AddLine('echo 2a', 1)
40 line2 = arena.AddLine('echo 2b', 2) # line 2 of two.ysh
41 arena.PopSource()
42
43 line3 = arena.AddLine('echo 1c', 3) # line 3 of one.ysh
44 arena.PopSource()
45
46 self.assertEqual('two.ysh', line2.src.path)
47 self.assertEqual(2, line2.line_num)
48
49 self.assertEqual('one.ysh', line3.src.path)
50 self.assertEqual(3, line3.line_num)
51
52
53if __name__ == '__main__':
54 unittest.main()