def _helper_assert_about_apply_and_get_result( is_multiline, compile_method, sub_method ): """ Helper method to assert about the correct results of calls to apply_and_get_result to ensure the correct methods are called. """ pattern = 'pattern whoopty' repl = 'replacement whoopty' compiled_pattern = 'whoopty compiled' subbed_result = 'substituted' starting_string = 'the start' compile_method.return_value = compiled_pattern sub_method.return_value = subbed_result if is_multiline: sub = substitute.Substitution(pattern, repl, True) else: sub = substitute.Substitution(pattern, repl, False) actual = sub.apply_and_get_result(starting_string) if is_multiline: compile_method.assert_called_once_with(pattern, re.MULTILINE) else: compile_method.assert_called_once_with(pattern) sub_method.assert_called_once_with( compiled_pattern, repl, starting_string ) assert actual == subbed_result
def test_get_config_tuple_from_egrc_when_present(mock_expand): """ Make sure we extract values correctly from the egrc. """ # These are the values hardcoded into the files. egrc_examples_dir = 'test/example/dir/in/egrc_withdata' egrc_custom_dir = 'test/custom/dir/in/egrc_withdata' egrc_use_color = True egrc_pager_cmd = 'more egrc' egrc_editor_cmd = 'vim egrc' color_config_from_file = _get_color_config_from_egrc_withdata() egrc_squeeze = True # Order matters--we apply substitutions alphabetically. egrc_subs = [ substitute.Substitution(r' ', r'', False), substitute.Substitution('\n\n\n', '\n\n', True) ] def return_expanded_path(*args, **kwargs): if args[0] == egrc_examples_dir: return egrc_examples_dir elif args[0] == egrc_custom_dir: return egrc_custom_dir else: raise TypeError( args[0] + ' was an unexpected path--should be ' + egrc_examples_dir + ' or ' + egrc_custom_dir ) mock_expand.side_effect = return_expanded_path actual = config.get_config_tuple_from_egrc(PATH_EGRC_WITH_DATA) expected = config.Config( examples_dir=egrc_examples_dir, custom_dir=egrc_custom_dir, color_config=color_config_from_file, use_color=egrc_use_color, pager_cmd=egrc_pager_cmd, squeeze=egrc_squeeze, subs=egrc_subs, editor_cmd=egrc_editor_cmd, ) assert actual == expected mock_expand.assert_any_call(egrc_examples_dir) mock_expand.assert_any_call(egrc_custom_dir)
def test_get_substituted_contents_substitutes_correctly(): """ Basic test to make sure Substitutions can get applied correctly. """ sub_one = substitute.Substitution('foo', 'bar', False) sub_two = substitute.Substitution('bar\n\n', 'baz\n', True) start = 'foo\n\n something else\n\n bar\n\n' target = 'baz\n something else\n\n baz\n' subs = [sub_one, sub_two] actual = util.get_substituted_contents(start, subs) assert_equal(actual, target)
def test_get_substitution_from_config_finds_multiple_substitutions(): """ Retrieve multiple substitutions from a config in the appropriate order. Integration test--actually pulls from a file. """ # These are hardcoded matching the value in the file. They will be sorted # alphabetically by pattern name. first_sub = substitute.Substitution(r' ', r'', False) second_sub = substitute.Substitution('\n\n\n', '\n\n', True) target = [first_sub, second_sub] config_obj = _get_egrc_config(PATH_EGRC_WITH_DATA) actual = config.get_substitutions_from_config(config_obj) assert_equal(actual, target)
def get_squeezed_contents(contents): """ Squeeze the contents by removing blank lines between definition and example and remove duplicate blank lines except between sections. """ line_between_example_code = substitute.Substitution( '\n\n ', '\n ', True) lines_between_examples = substitute.Substitution('\n\n\n', '\n\n', True) lines_between_sections = substitute.Substitution('\n\n\n\n', '\n\n\n', True) result = contents result = line_between_example_code.apply_and_get_result(result) result = lines_between_examples.apply_and_get_result(result) result = lines_between_sections.apply_and_get_result(result) return result
def test_parse_substitution_from_list_with_is_multiline(): """ We should be able to parse a Subsitution if is_multiline is set. """ target = substitute.Substitution('patt', 'repl', True) list_rep = ['patt', 'repl', True] actual = config.parse_substitution_from_list(list_rep) assert_equal(actual, target)
def test_parse_substitution_from_list_without_is_multiline(): """ Make sure we can parse a list without the is_multiline option set, i.e. just a two element list. """ target = substitute.Substitution('foo', 'bar', False) list_rep = ['foo', 'bar'] actual = config.parse_substitution_from_list(list_rep) assert_equal(actual, target)
def test_applies_normal_mode_substitution(): """Correctly applies a substitution that is not multiline.""" raw = 'foo\n\n\n bar something\n\n\n the end' subbed = 'foo\n\n\nbar something\n\n\nthe end' pattern = ' ' replacement = '' sub = substitute.Substitution(pattern, replacement, False) actual = sub.apply_and_get_result(raw) assert actual == subbed
def test_applies_multiline_substitution(): """Correctly applies a substitution that is multiline.""" raw = 'foo\n\n\n bar something\n\n\n the end' subbed = 'foo\n bar something\n the end' pattern = '\n\n\n' replacement = '\n' sub = substitute.Substitution(pattern, replacement, True) actual = sub.apply_and_get_result(raw) assert_equal(actual, subbed)
def test_get_substitution_from_config_finds_single_substitution(): """ Retrieve a single substitution from the config. Integration test--actually pulls from a file. """ # This is hardcoded matching the value in the file. single_sub = substitute.Substitution('foo', 'bar', False) target = [single_sub] config_obj = _get_egrc_config(PATH_EGRC_SINGLE_SUB) actual = config.get_substitutions_from_config(config_obj) assert_equal(actual, target)
def parse_substitution_from_list(list_rep): """ Parse a substitution from the list representation in the config file. """ # We are expecting [pattern, replacement [, is_multiline]] if type(list_rep) is not list: raise SyntaxError('Substitution must be a list') if len(list_rep) < 2: raise SyntaxError('Substitution must be a list of size 2') pattern = list_rep[0] replacement = list_rep[1] # By default, substitutions are not multiline. is_multiline = False if (len(list_rep) > 2): is_multiline = list_rep[2] if type(is_multiline) is not bool: raise SyntaxError('is_multiline must be a boolean') result = substitute.Substitution(pattern, replacement, is_multiline) return result
def test_equality(): """== should work on Substitution objects.""" alpha = substitute.Substitution('foo', 'bar', False) beta = substitute.Substitution('foo', 'bar', False) assert alpha == beta
def test_not_equal(): """!= should work on Substitution objects.""" alpha = substitute.Substitution('foo', 'bar', True) beta = substitute.Substitution('foo', 'bar', False) assert alpha != beta