Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1 | """ |
| 2 | Very minimal unittests for parts of the readline module. |
| 3 | |
| 4 | These tests were added to check that the libedit emulation on OSX and |
| 5 | the "real" readline have the same interface for history manipulation. That's |
| 6 | why the tests cover only a small subset of the interface. |
| 7 | """ |
| 8 | import unittest |
Mark Dickinson | 2d7062e | 2009-10-26 12:01:06 +0000 | [diff] [blame] | 9 | from test.support import run_unittest, import_module |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 10 | |
Mark Dickinson | 2d7062e | 2009-10-26 12:01:06 +0000 | [diff] [blame] | 11 | # Skip tests if there is no readline module |
| 12 | readline = import_module('readline') |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 13 | |
| 14 | class TestHistoryManipulation (unittest.TestCase): |
R David Murray | f8b9dfd | 2011-03-14 17:10:22 -0400 | [diff] [blame] | 15 | |
| 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 Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 19 | def testHistoryUpdates(self): |
Georg Brandl | 7b250a5 | 2012-08-11 11:02:14 +0200 | [diff] [blame] | 20 | readline.clear_history() |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 21 | |
| 22 | readline.add_history("first line") |
| 23 | readline.add_history("second line") |
| 24 | |
| 25 | self.assertEqual(readline.get_history_item(0), None) |
| 26 | self.assertEqual(readline.get_history_item(1), "first line") |
| 27 | self.assertEqual(readline.get_history_item(2), "second line") |
| 28 | |
| 29 | readline.replace_history_item(0, "replaced line") |
| 30 | self.assertEqual(readline.get_history_item(0), None) |
| 31 | self.assertEqual(readline.get_history_item(1), "replaced line") |
| 32 | self.assertEqual(readline.get_history_item(2), "second line") |
| 33 | |
| 34 | self.assertEqual(readline.get_current_history_length(), 2) |
| 35 | |
| 36 | readline.remove_history_item(0) |
| 37 | self.assertEqual(readline.get_history_item(0), None) |
| 38 | self.assertEqual(readline.get_history_item(1), "second line") |
| 39 | |
| 40 | self.assertEqual(readline.get_current_history_length(), 1) |
| 41 | |
| 42 | |
| 43 | def test_main(): |
| 44 | run_unittest(TestHistoryManipulation) |
| 45 | |
| 46 | if __name__ == "__main__": |
| 47 | test_main() |