|
|
|
import pytest
|
|
|
|
from collections import OrderedDict
|
|
|
|
from calculate.templates.format.contents_format import ContentsFormat
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.contents
|
|
|
|
class TestParsingMethods:
|
|
|
|
def test_if_input_document_contains_a_few_lines_without_any_action_symbols__the_initialised_object_contains_correct_dictionary(self):
|
|
|
|
document_text = '''
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
result = OrderedDict({('', '/usr/bin/vim'):
|
|
|
|
[('obj', '30acc0f256e11c1ecdb1bd80b688d238',
|
|
|
|
'1573538056')],
|
|
|
|
('', '/usr/share/bash-completion/completions/ex'):
|
|
|
|
[('sym', 'vim', '1573538054')],
|
|
|
|
('', '/usr/share/bash-completion/completions'):
|
|
|
|
[('dir',)]})
|
|
|
|
|
|
|
|
contents_object = ContentsFormat(document_text, '/path/to/template')
|
|
|
|
assert result == contents_object._document_dictionary
|
|
|
|
|
|
|
|
def test_if_input_document_contains_a_few_lines_with_some_action_symbols__the_initialised_object_contains_correct_dictionary(self):
|
|
|
|
document_text = '''
|
|
|
|
!obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
-sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
!dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
result = OrderedDict({('!', '/usr/bin/vim'):
|
|
|
|
[('obj', '30acc0f256e11c1ecdb1bd80b688d238',
|
|
|
|
'1573538056')],
|
|
|
|
('-', '/usr/share/bash-completion/completions/ex'):
|
|
|
|
[('sym', 'vim', '1573538054')],
|
|
|
|
('!', '/usr/share/bash-completion/completions'):
|
|
|
|
[('dir',)]})
|
|
|
|
|
|
|
|
contents_object = ContentsFormat(document_text, '/path/to/template')
|
|
|
|
assert result == contents_object._document_dictionary
|
|
|
|
|
|
|
|
def test_if_template_parser_flag_is_set_False__the_initialized_object_contains_correct_dictionary_for_contents_util_module(self):
|
|
|
|
document_text = '''
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
result = OrderedDict({'/usr/bin/vim':
|
|
|
|
OrderedDict(
|
|
|
|
{'type': 'obj',
|
|
|
|
'md5': '30acc0f256e11c1ecdb1bd80b688d238',
|
|
|
|
'mtime': '1573538056'}),
|
|
|
|
'/usr/share/bash-completion/completions/ex':
|
|
|
|
OrderedDict(
|
|
|
|
{'type': 'sym',
|
|
|
|
'target': 'vim',
|
|
|
|
'mtime': '1573538054'}),
|
|
|
|
'/usr/share/bash-completion/completions':
|
|
|
|
OrderedDict({'type': 'dir'})})
|
|
|
|
|
|
|
|
contents_object = ContentsFormat(document_text, '/path/to/template',
|
|
|
|
template_parser=False)
|
|
|
|
assert result == contents_object._document_dictionary
|
|
|
|
|
|
|
|
def test_joining_documents_1(self):
|
|
|
|
with open('./tests/templates/format/testfiles/contents_original',
|
|
|
|
'r') as original_file:
|
|
|
|
original_text = original_file.read()
|
|
|
|
contents_original_object = ContentsFormat(original_text, '/path/to/template')
|
|
|
|
|
|
|
|
with open('./tests/templates/format/testfiles/contents_template',
|
|
|
|
'r') as template_file:
|
|
|
|
template_text = template_file.read()
|
|
|
|
contents_template_object = ContentsFormat(template_text,
|
|
|
|
'/path/to/template',
|
|
|
|
ignore_comments=True)
|
|
|
|
|
|
|
|
contents_original_object.join_template(contents_template_object)
|
|
|
|
|
|
|
|
with open('./tests/templates/format/testfiles/contents_result',
|
|
|
|
'r') as result_file:
|
|
|
|
result_text = result_file.read()
|
|
|
|
|
|
|
|
assert contents_original_object.document_text == result_text
|
|
|
|
|
|
|
|
def test_if_input_documents_are_an_original_document_without_calculate_header_and_a_template_and_the_add_header_flag_is_set__the_format_object_joins_an_original_document_and_a_template_and_adds_the_calculate_header(self):
|
|
|
|
original_text = '''
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
template_text = '''
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
!sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
join_result = '''#-------------------------------------------------------------------------------
|
|
|
|
# Modified by Calculate Utilities 4.0
|
|
|
|
# Processed template files:
|
|
|
|
# /path/to/template
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
original_object = ContentsFormat(original_text, '/path/to/template',
|
|
|
|
add_header=True)
|
|
|
|
template_object = ContentsFormat(template_text, '/path/to/template')
|
|
|
|
original_object.join_template(template_object)
|
|
|
|
assert original_object.document_text == join_result
|
|
|
|
|
|
|
|
def test_if_input_documents_are_an_original_document_with_calculate_header_and_a_template_the_add_header_flag_is_set_and_the_already_changed_flag_is_not_set__the_format_object_removes_calculate_header_from_original_document__joins_an_original_document_and_a_template_and_adds_the_calculate_header(self):
|
|
|
|
original_text = '''#-------------------------------------------------------------------------------
|
|
|
|
# Modified by Calculate Utilities 4.0
|
|
|
|
# Processed template files:
|
|
|
|
# /path/to/ancient/template
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
template_text = '''
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
!sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
join_result = '''#-------------------------------------------------------------------------------
|
|
|
|
# Modified by Calculate Utilities 4.0
|
|
|
|
# Processed template files:
|
|
|
|
# /path/to/template
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
original_object = ContentsFormat(original_text, '/path/to/template',
|
|
|
|
add_header=True)
|
|
|
|
template_object = ContentsFormat(template_text, '/path/to/template')
|
|
|
|
original_object.join_template(template_object)
|
|
|
|
assert original_object.document_text == join_result
|
|
|
|
|
|
|
|
def test_if_input_documents_are_an_original_document_with_calculate_header_and_a_template_the_add_header_flag_is_set_and_the_already_changed_flag_is_set__the_format_object_joins_an_original_document_and_a_template_and_adds_the_calculate_header_with_a_template_paths_from_the_old_header_and_paths_to_a_current_template(self):
|
|
|
|
original_text = '''#-------------------------------------------------------------------------------
|
|
|
|
# Modified by Calculate Utilities 4.0
|
|
|
|
# Processed template files:
|
|
|
|
# /path/to/ancient/template
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
template_text = '''
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
!sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
join_result = '''#-------------------------------------------------------------------------------
|
|
|
|
# Modified by Calculate Utilities 4.0
|
|
|
|
# Processed template files:
|
|
|
|
# /path/to/ancient/template
|
|
|
|
# /path/to/template
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
|
|
|
dir /usr/share/bash-completion/completions
|
|
|
|
'''
|
|
|
|
|
|
|
|
original_object = ContentsFormat(original_text, '/path/to/template',
|
|
|
|
add_header=True, already_changed=True)
|
|
|
|
template_object = ContentsFormat(template_text, '/path/to/template')
|
|
|
|
original_object.join_template(template_object)
|
|
|
|
assert original_object.document_text == join_result
|