blob: fe74939026ca7d6d4a0d1489678ab82172ac947c [file] [log] [blame]
Ronald Oussoren2efd9242009-09-20 14:53:22 +00001"""
2Very minimal unittests for parts of the readline module.
3
4These tests were added to check that the libedit emulation on OSX and
5the "real" readline have the same interface for history manipulation. That's
6why the tests cover only a small subset of the interface.
7"""
8import unittest
Mark Dickinson2d7062e2009-10-26 12:01:06 +00009from test.support import run_unittest, import_module
Ronald Oussoren2efd9242009-09-20 14:53:22 +000010
Mark Dickinson2d7062e2009-10-26 12:01:06 +000011# Skip tests if there is no readline module
12readline = import_module('readline')
Ronald Oussoren2efd9242009-09-20 14:53:22 +000013
14class TestHistoryManipulation (unittest.TestCase):
R David Murrayf8b9dfd2011-03-14 17:10:22 -040015
16 @unittest.skipIf(not hasattr(readline, 'clear_history'),
17 "The history update test cannot be run because the "
18 "clear_history method is not available.")
Ronald Oussoren2efd9242009-09-20 14:53:22 +000019 def testHistoryUpdates(self):
Georg Brandl22bfa372012-08-11 10:59:23 +020020 # Some GNU versions of readline do not support clear_history
21 if hasattr('readline', 'clear_history'):
22 readline.clear_history()
Ronald Oussoren2efd9242009-09-20 14:53:22 +000023
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
45def test_main():
46 run_unittest(TestHistoryManipulation)
47
48if __name__ == "__main__":
49 test_main()