blob: 8b778186f1fe32a306585bfc6684b994e68ef9d2 [file] [log] [blame]
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001"""
2Very minimal unittests for parts of the readline module.
Ronald Oussoren2efd9242009-09-20 14:53:22 +00003"""
Victor Stinnera3c80ce2014-07-24 12:23:56 +02004import os
Ronald Oussoren2efd9242009-09-20 14:53:22 +00005import unittest
Mark Dickinson2d7062e2009-10-26 12:01:06 +00006from test.support import run_unittest, import_module
Victor Stinnera3c80ce2014-07-24 12:23:56 +02007from test.script_helper import assert_python_ok
Ronald Oussoren2efd9242009-09-20 14:53:22 +00008
Mark Dickinson2d7062e2009-10-26 12:01:06 +00009# Skip tests if there is no readline module
10readline = import_module('readline')
Ronald Oussoren2efd9242009-09-20 14:53:22 +000011
12class TestHistoryManipulation (unittest.TestCase):
Victor Stinnera3c80ce2014-07-24 12:23:56 +020013 """
14 These tests were added to check that the libedit emulation on OSX and the
15 "real" readline have the same interface for history manipulation. That's
16 why the tests cover only a small subset of the interface.
17 """
R David Murrayf8b9dfd2011-03-14 17:10:22 -040018
19 @unittest.skipIf(not hasattr(readline, 'clear_history'),
20 "The history update test cannot be run because the "
21 "clear_history method is not available.")
Ronald Oussoren2efd9242009-09-20 14:53:22 +000022 def testHistoryUpdates(self):
Georg Brandl7b250a52012-08-11 11:02:14 +020023 readline.clear_history()
Ronald Oussoren2efd9242009-09-20 14:53:22 +000024
25 readline.add_history("first line")
26 readline.add_history("second line")
27
28 self.assertEqual(readline.get_history_item(0), None)
29 self.assertEqual(readline.get_history_item(1), "first line")
30 self.assertEqual(readline.get_history_item(2), "second line")
31
32 readline.replace_history_item(0, "replaced line")
33 self.assertEqual(readline.get_history_item(0), None)
34 self.assertEqual(readline.get_history_item(1), "replaced line")
35 self.assertEqual(readline.get_history_item(2), "second line")
36
37 self.assertEqual(readline.get_current_history_length(), 2)
38
39 readline.remove_history_item(0)
40 self.assertEqual(readline.get_history_item(0), None)
41 self.assertEqual(readline.get_history_item(1), "second line")
42
43 self.assertEqual(readline.get_current_history_length(), 1)
44
45
Victor Stinnera3c80ce2014-07-24 12:23:56 +020046class TestReadline(unittest.TestCase):
47 def test_init(self):
48 # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
49 # written into stdout when the readline module is imported and stdout
50 # is redirected to a pipe.
51 rc, stdout, stderr = assert_python_ok('-c', 'import readline',
52 TERM='xterm-256color')
53 self.assertEqual(stdout, b'')
54
55
Ronald Oussoren2efd9242009-09-20 14:53:22 +000056def test_main():
Victor Stinnera3c80ce2014-07-24 12:23:56 +020057 run_unittest(TestHistoryManipulation, TestReadline)
Ronald Oussoren2efd9242009-09-20 14:53:22 +000058
59if __name__ == "__main__":
60 test_main()