Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 1 | """ |
| 2 | Very minimal unittests for parts of the readline module. |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 3 | """ |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 4 | import os |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 5 | import unittest |
Mark Dickinson | 2d7062e | 2009-10-26 12:01:06 +0000 | [diff] [blame] | 6 | from test.support import run_unittest, import_module |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 7 | from test.script_helper import assert_python_ok |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 8 | |
Mark Dickinson | 2d7062e | 2009-10-26 12:01:06 +0000 | [diff] [blame] | 9 | # Skip tests if there is no readline module |
| 10 | readline = import_module('readline') |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 11 | |
| 12 | class TestHistoryManipulation (unittest.TestCase): |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 13 | """ |
| 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 Murray | f8b9dfd | 2011-03-14 17:10:22 -0400 | [diff] [blame] | 18 | |
| 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 Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 22 | def testHistoryUpdates(self): |
Georg Brandl | 7b250a5 | 2012-08-11 11:02:14 +0200 | [diff] [blame] | 23 | readline.clear_history() |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 24 | |
| 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 Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 46 | class 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 Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 56 | def test_main(): |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 57 | run_unittest(TestHistoryManipulation, TestReadline) |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 58 | |
| 59 | if __name__ == "__main__": |
| 60 | test_main() |