Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 1 | """ |
| 2 | Very minimal unittests for parts of the readline module. |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 3 | """ |
Victor Stinner | 63a4747 | 2014-07-24 12:22:24 +0200 | [diff] [blame] | 4 | import os |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 5 | import unittest |
Mark Dickinson | 828b398 | 2009-10-26 11:59:30 +0000 | [diff] [blame] | 6 | from test.test_support import run_unittest, import_module |
Victor Stinner | 63a4747 | 2014-07-24 12:22:24 +0200 | [diff] [blame] | 7 | from test.script_helper import assert_python_ok |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 8 | |
Mark Dickinson | 828b398 | 2009-10-26 11:59:30 +0000 | [diff] [blame] | 9 | # Skip tests if there is no readline module |
| 10 | readline = import_module('readline') |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 11 | |
| 12 | class TestHistoryManipulation (unittest.TestCase): |
Victor Stinner | 63a4747 | 2014-07-24 12:22:24 +0200 | [diff] [blame] | 13 | """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 Murray | f8b9dfd | 2011-03-14 17:10:22 -0400 | [diff] [blame] | 17 | |
Benjamin Peterson | 2e26a62 | 2014-11-26 14:35:56 -0600 | [diff] [blame] | 18 | @unittest.skipUnless(hasattr(readline, "clear_history"), |
| 19 | "The history update test cannot be run because the " |
| 20 | "clear_history method is not available.") |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 21 | 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 Stinner | 63a4747 | 2014-07-24 12:22:24 +0200 | [diff] [blame] | 45 | class TestReadline(unittest.TestCase): |
Antoine Pitrou | 06c1497 | 2014-11-04 14:52:10 +0100 | [diff] [blame] | 46 | |
| 47 | @unittest.skipIf(readline._READLINE_VERSION < 0x0600 |
| 48 | and "libedit" not in readline.__doc__, |
| 49 | "not supported in this library version") |
Victor Stinner | 63a4747 | 2014-07-24 12:22:24 +0200 | [diff] [blame] | 50 | 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 Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 59 | def test_main(): |
Victor Stinner | 63a4747 | 2014-07-24 12:22:24 +0200 | [diff] [blame] | 60 | run_unittest(TestHistoryManipulation, TestReadline) |
Ronald Oussoren | 9f20d9d | 2009-09-20 14:18:15 +0000 | [diff] [blame] | 61 | |
| 62 | if __name__ == "__main__": |
| 63 | test_main() |