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 |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 5 | import tempfile |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 6 | import unittest |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 7 | from test.support import import_module, unlink |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 8 | from test.support.script_helper import assert_python_ok |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 9 | |
Mark Dickinson | 2d7062e | 2009-10-26 12:01:06 +0000 | [diff] [blame] | 10 | # Skip tests if there is no readline module |
| 11 | readline = import_module('readline') |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 12 | |
| 13 | class TestHistoryManipulation (unittest.TestCase): |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 14 | """ |
| 15 | These tests were added to check that the libedit emulation on OSX and the |
| 16 | "real" readline have the same interface for history manipulation. That's |
| 17 | why the tests cover only a small subset of the interface. |
| 18 | """ |
R David Murray | f8b9dfd | 2011-03-14 17:10:22 -0400 | [diff] [blame] | 19 | |
Benjamin Peterson | 5b53507 | 2014-11-26 14:35:56 -0600 | [diff] [blame] | 20 | @unittest.skipUnless(hasattr(readline, "clear_history"), |
| 21 | "The history update test cannot be run because the " |
| 22 | "clear_history method is not available.") |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 23 | def testHistoryUpdates(self): |
Georg Brandl | 7b250a5 | 2012-08-11 11:02:14 +0200 | [diff] [blame] | 24 | readline.clear_history() |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 25 | |
| 26 | readline.add_history("first line") |
| 27 | readline.add_history("second line") |
| 28 | |
| 29 | self.assertEqual(readline.get_history_item(0), None) |
| 30 | self.assertEqual(readline.get_history_item(1), "first line") |
| 31 | self.assertEqual(readline.get_history_item(2), "second line") |
| 32 | |
| 33 | readline.replace_history_item(0, "replaced line") |
| 34 | self.assertEqual(readline.get_history_item(0), None) |
| 35 | self.assertEqual(readline.get_history_item(1), "replaced line") |
| 36 | self.assertEqual(readline.get_history_item(2), "second line") |
| 37 | |
| 38 | self.assertEqual(readline.get_current_history_length(), 2) |
| 39 | |
| 40 | readline.remove_history_item(0) |
| 41 | self.assertEqual(readline.get_history_item(0), None) |
| 42 | self.assertEqual(readline.get_history_item(1), "second line") |
| 43 | |
| 44 | self.assertEqual(readline.get_current_history_length(), 1) |
| 45 | |
Ned Deily | 8007cbc | 2014-11-26 13:02:33 -0800 | [diff] [blame] | 46 | @unittest.skipUnless(hasattr(readline, "append_history_file"), |
Benjamin Peterson | d1e22ba | 2014-11-26 14:35:12 -0600 | [diff] [blame] | 47 | "append_history not available") |
Benjamin Peterson | 33f8f15 | 2014-11-26 13:58:16 -0600 | [diff] [blame] | 48 | def test_write_read_append(self): |
| 49 | hfile = tempfile.NamedTemporaryFile(delete=False) |
| 50 | hfile.close() |
| 51 | hfilename = hfile.name |
| 52 | self.addCleanup(unlink, hfilename) |
| 53 | |
| 54 | # test write-clear-read == nop |
| 55 | readline.clear_history() |
| 56 | readline.add_history("first line") |
| 57 | readline.add_history("second line") |
| 58 | readline.write_history_file(hfilename) |
| 59 | |
| 60 | readline.clear_history() |
| 61 | self.assertEqual(readline.get_current_history_length(), 0) |
| 62 | |
| 63 | readline.read_history_file(hfilename) |
| 64 | self.assertEqual(readline.get_current_history_length(), 2) |
| 65 | self.assertEqual(readline.get_history_item(1), "first line") |
| 66 | self.assertEqual(readline.get_history_item(2), "second line") |
| 67 | |
| 68 | # test append |
| 69 | readline.append_history_file(1, hfilename) |
| 70 | readline.clear_history() |
| 71 | readline.read_history_file(hfilename) |
| 72 | self.assertEqual(readline.get_current_history_length(), 3) |
| 73 | self.assertEqual(readline.get_history_item(1), "first line") |
| 74 | self.assertEqual(readline.get_history_item(2), "second line") |
| 75 | self.assertEqual(readline.get_history_item(3), "second line") |
| 76 | |
| 77 | # test 'no such file' behaviour |
| 78 | os.unlink(hfilename) |
| 79 | with self.assertRaises(FileNotFoundError): |
| 80 | readline.append_history_file(1, hfilename) |
| 81 | |
| 82 | # write_history_file can create the target |
| 83 | readline.write_history_file(hfilename) |
| 84 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 85 | |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 86 | class TestReadline(unittest.TestCase): |
Antoine Pitrou | 7e8b867 | 2014-11-04 14:52:10 +0100 | [diff] [blame] | 87 | |
| 88 | @unittest.skipIf(readline._READLINE_VERSION < 0x0600 |
| 89 | and "libedit" not in readline.__doc__, |
| 90 | "not supported in this library version") |
Victor Stinner | a3c80ce | 2014-07-24 12:23:56 +0200 | [diff] [blame] | 91 | def test_init(self): |
| 92 | # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not |
| 93 | # written into stdout when the readline module is imported and stdout |
| 94 | # is redirected to a pipe. |
| 95 | rc, stdout, stderr = assert_python_ok('-c', 'import readline', |
| 96 | TERM='xterm-256color') |
| 97 | self.assertEqual(stdout, b'') |
| 98 | |
| 99 | |
Ronald Oussoren | 2efd924 | 2009-09-20 14:53:22 +0000 | [diff] [blame] | 100 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 101 | unittest.main() |