blob: 588043ee5e9cc9b2cbee0de1763ed969fb2fe838 [file] [log] [blame]
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00001"""
2Very minimal unittests for parts of the readline module.
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00003"""
Victor Stinner63a47472014-07-24 12:22:24 +02004import os
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00005import unittest
Mark Dickinson828b3982009-10-26 11:59:30 +00006from test.test_support import run_unittest, import_module
Victor Stinner63a47472014-07-24 12:22:24 +02007from test.script_helper import assert_python_ok
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +00008
Mark Dickinson828b3982009-10-26 11:59:30 +00009# Skip tests if there is no readline module
10readline = import_module('readline')
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000011
12class TestHistoryManipulation (unittest.TestCase):
Victor Stinner63a47472014-07-24 12:22:24 +020013 """These tests were added to check that the libedit emulation on OSX and
14 the "real" readline have the same interface for history manipulation.
15 That's why the tests cover only a small subset of the interface.
16 """
R David Murrayf8b9dfd2011-03-14 17:10:22 -040017
Benjamin Peterson2e26a622014-11-26 14:35:56 -060018 @unittest.skipUnless(hasattr(readline, "clear_history"),
19 "The history update test cannot be run because the "
20 "clear_history method is not available.")
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000021 def testHistoryUpdates(self):
22 readline.clear_history()
23
24 readline.add_history("first line")
25 readline.add_history("second line")
26
27 self.assertEqual(readline.get_history_item(0), None)
28 self.assertEqual(readline.get_history_item(1), "first line")
29 self.assertEqual(readline.get_history_item(2), "second line")
30
31 readline.replace_history_item(0, "replaced line")
32 self.assertEqual(readline.get_history_item(0), None)
33 self.assertEqual(readline.get_history_item(1), "replaced line")
34 self.assertEqual(readline.get_history_item(2), "second line")
35
36 self.assertEqual(readline.get_current_history_length(), 2)
37
38 readline.remove_history_item(0)
39 self.assertEqual(readline.get_history_item(0), None)
40 self.assertEqual(readline.get_history_item(1), "second line")
41
42 self.assertEqual(readline.get_current_history_length(), 1)
43
44
Victor Stinner63a47472014-07-24 12:22:24 +020045class TestReadline(unittest.TestCase):
Antoine Pitrou06c14972014-11-04 14:52:10 +010046
47 @unittest.skipIf(readline._READLINE_VERSION < 0x0600
48 and "libedit" not in readline.__doc__,
49 "not supported in this library version")
Victor Stinner63a47472014-07-24 12:22:24 +020050 def test_init(self):
51 # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
52 # written into stdout when the readline module is imported and stdout
53 # is redirected to a pipe.
54 rc, stdout, stderr = assert_python_ok('-c', 'import readline',
55 TERM='xterm-256color')
56 self.assertEqual(stdout, b'')
57
58
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000059def test_main():
Victor Stinner63a47472014-07-24 12:22:24 +020060 run_unittest(TestHistoryManipulation, TestReadline)
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000061
62if __name__ == "__main__":
63 test_main()