blob: aabbf54ec0a4155f99d38874c99133edadbe1ad2 [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
18 @unittest.skipIf(not 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):
46 def test_init(self):
47 # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
48 # written into stdout when the readline module is imported and stdout
49 # is redirected to a pipe.
50 rc, stdout, stderr = assert_python_ok('-c', 'import readline',
51 TERM='xterm-256color')
52 self.assertEqual(stdout, b'')
53
54
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000055def test_main():
Victor Stinner63a47472014-07-24 12:22:24 +020056 run_unittest(TestHistoryManipulation, TestReadline)
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000057
58if __name__ == "__main__":
59 test_main()