OILS / builtin / readline_osh_test.py View on Github | oilshell.org

100 lines, 46 significant
1#!/usr/bin/env python2
2"""
3builtin_lib_test.py: Tests for readline_osh.py
4"""
5from __future__ import print_function
6
7import cStringIO
8import unittest
9# We use native/line_input.c, a fork of readline.c, but this is good enough for
10# unit testing
11import readline
12
13from builtin import readline_osh # module under test
14from core import test_lib
15from core import state
16from core import alloc
17from core import ui
18from frontend import flag_def # side effect: flags are defined!
19
20_ = flag_def
21
22
23class BuiltinTest(unittest.TestCase):
24
25 def testHistoryBuiltin(self):
26 test_path = '_tmp/builtin_test_history.txt'
27 with open(test_path, 'w') as f:
28 f.write("""\
29echo hello
30ls one/
31ls two/
32echo bye
33""")
34 readline.read_history_file(test_path)
35
36 # Show all
37 f = cStringIO.StringIO()
38 out = _TestHistory(['history'])
39
40 self.assertEqual(
41 out, """\
42 1 echo hello
43 2 ls one/
44 3 ls two/
45 4 echo bye
46""")
47
48 # Show two history items
49 out = _TestHistory(['history', '2'])
50 # Note: whitespace here is exact
51 self.assertEqual(out, """\
52 3 ls two/
53 4 echo bye
54""")
55
56 # Delete single history item.
57 # This functionality is *silent*
58 # so call history again after
59 # this to feed the test assertion
60
61 _TestHistory(['history', '-d', '4'])
62
63 # Call history
64 out = _TestHistory(['history'])
65
66 # Note: whitespace here is exact
67 self.assertEqual(
68 out, """\
69 1 echo hello
70 2 ls one/
71 3 ls two/
72""")
73
74 # Clear history
75 # This functionality is *silent*
76 # so call history again after
77 # this to feed the test assertion
78
79 _TestHistory(['history', '-c'])
80
81 # Call history
82 out = _TestHistory(['history'])
83
84 self.assertEqual(out, """\
85""")
86
87
88def _TestHistory(argv):
89 f = cStringIO.StringIO()
90 arena = alloc.Arena()
91 mem = state.Mem('', [], arena, [])
92 errfmt = ui.ErrorFormatter()
93 b = readline_osh.History(readline, mem, errfmt, f)
94 cmd_val = test_lib.MakeBuiltinArgv(argv)
95 b.Run(cmd_val)
96 return f.getvalue()
97
98
99if __name__ == '__main__':
100 unittest.main()