From 5cc1c4a9a454866bae943a5b679875fb0bf6dd53 Mon Sep 17 00:00:00 2001 From: chhsiao90 Date: Mon, 21 Nov 2016 15:08:25 +0800 Subject: [PATCH] Impelment test_sheets.py --- cheat/test/test_sheets.py | 61 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/cheat/test/test_sheets.py b/cheat/test/test_sheets.py index f38fa68..b6f67ed 100644 --- a/cheat/test/test_sheets.py +++ b/cheat/test/test_sheets.py @@ -5,8 +5,9 @@ from unittest import TestCase from cheat import cheatsheets from cheat.sheets import ( - default_path, paths, get, + default_path, paths, get, search, ) +from cheat.sheets import list as sheets_list class TestSheets(TestCase): @@ -187,3 +188,61 @@ class TestSheets(TestCase): 'zip': os.path.join(self.tmp_dir, 'cheatdir2', 'zip'), } ) + + @patch('cheat.sheets.get') + def test_list(self, mock_get): + mock_get.return_value = { + 'curl': os.path.join(self.tmp_dir, 'cheatdir1', 'curl'), + 'http': os.path.join(self.tmp_dir, 'cheatdir1', 'http'), + 'tar': os.path.join(self.tmp_dir, 'cheatdir2', 'tar'), + 'zip': os.path.join(self.tmp_dir, 'cheatdir2', 'zip'), + 'telnet': os.path.join(self.tmp_dir, 'cheatdir2', 'telnet'), + } + + sheet_list = sheets_list() + self.assertEqual( + sheet_list, + '\n'.join([ + 'curl'.ljust(10) + os.path.join( + self.tmp_dir, 'cheatdir1', 'curl'), + 'http'.ljust(10) + os.path.join( + self.tmp_dir, 'cheatdir1', 'http'), + 'tar'.ljust(10) + os.path.join( + self.tmp_dir, 'cheatdir2', 'tar'), + 'telnet'.ljust(10) + os.path.join( + self.tmp_dir, 'cheatdir2', 'telnet'), + 'zip'.ljust(10) + os.path.join( + self.tmp_dir, 'cheatdir2', 'zip'), + ]) + '\n' + ) + + @patch('cheat.sheets.get') + def test_search(self, mock_get): + cheat_curl = os.path.join(self.tmp_dir, 'cheatdir1', 'curl') + cheat_http = os.path.join(self.tmp_dir, 'cheatdir1', 'http') + self._make_sheets([ + cheat_curl, cheat_http, + ]) + with open(cheat_curl, 'w') as f: + f.write(( + '# This line match the KEYWORD\n' + '# This line not\n' + 'cheat KEYWORD\n' + )) + with open(cheat_http, 'w') as f: + f.write(( + '# This sheet not match anything\n' + 'cheat sorry\n' + )) + mock_get.return_value = { + 'curl': cheat_curl, + 'http': cheat_http + } + + search_result = search('KEYWORD') + self.assertEqual( + search_result, + ('curl:\n' + ' # This line match the KEYWORD\n' + ' cheat KEYWORD\n\n') + )