You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
9.0 KiB

import pytest
from collections import OrderedDict
from calculate.templates.format.compiz_format import CompizFormat
from pprint import pprint
@pytest.mark.compiz
class TestParsingMethods:
def test_if_input_document_contains_just_few_parameter_lines__the_initialised_object_contains_correct_dictionary(self):
document_text = '''
[Added Associations]
application/illustrator=zzz-gimp.desktop
application/pdf=evince.desktop;
application/rtf=libreoffice-writer.desktop;
application/vnd.oasis.opendocument.spreadsheet=calculate-calc.desktop;
'''
section = OrderedDict({('', 'application/illustrator'):
['zzz-gimp.desktop'],
('', 'application/pdf'):
['evince.desktop;'],
('', 'application/rtf'):
['libreoffice-writer.desktop;'],
('', 'application/vnd.oasis.opendocument.spreadsheet'):
['calculate-calc.desktop;']})
result = OrderedDict({('', 'Added Associations'): section})
compiz_object = CompizFormat(document_text)
assert compiz_object._document_dictionary == result
def test_if_input_document_contains_few_parameter_lines_and_some_empty_lines__the_initialized_object_contains_correct_dictionary(self):
document_text = '''
[Added Associations]
application/illustrator=zzz-gimp.desktop
application/pdf=evince.desktop;
application/rtf=libreoffice-writer.desktop;
application/vnd.oasis.opendocument.spreadsheet=calculate-calc.desktop;
'''
section = OrderedDict({('', 'application/illustrator'):
['zzz-gimp.desktop'],
('', 'application/pdf'):
['evince.desktop;'],
('', 'application/rtf'):
['libreoffice-writer.desktop;'],
('', 'application/vnd.oasis.opendocument.spreadsheet'):
["calculate-calc.desktop;"]})
result = OrderedDict({('', 'Added Associations'): section})
compiz_object = CompizFormat(document_text)
assert compiz_object._document_dictionary == result
def test_if_input_document_contains_sections_with_different_names_but_different_parameters__the_parameters_merged_in_one_section(self):
document_text = '''
[Added Associations]
application/illustrator=zzz-gimp.desktop
application/pdf=evince.desktop;
[Added Associations]
application/rtf=libreoffice-writer.desktop;
application/vnd.oasis.opendocument.spreadsheet=calculate-calc.desktop;
'''
section = OrderedDict({('', 'application/illustrator'):
['zzz-gimp.desktop'],
('', 'application/pdf'):
['evince.desktop;'],
('', 'application/rtf'):
['libreoffice-writer.desktop;'],
('', 'application/vnd.oasis.opendocument.spreadsheet'):
['calculate-calc.desktop;']})
result = OrderedDict({('', 'Added Associations'): section})
compiz_object = CompizFormat(document_text)
assert compiz_object._document_dictionary == result
def test_if_input_document_contains_sections_with_parameters_with_action_marks__the_key_tuples_of_object_s_dictionary_have_it_as_its_first_element(self):
document_text = '''
[Added Associations]
!application/illustrator=zzz-gimp.desktop
-application/pdf=evince.desktop;
!application/vnd.oasis.opendocument.spreadsheet=calculate-calc.desktop;
'''
section = OrderedDict({('!', 'application/illustrator'):
['zzz-gimp.desktop'],
('-', 'application/pdf'):
['evince.desktop;'],
('!', 'application/vnd.oasis.opendocument.spreadsheet'):
["calculate-calc.desktop;"]})
result = OrderedDict({('', 'Added Associations'): section})
compiz_object = CompizFormat(document_text)
assert compiz_object._document_dictionary == result
def test_if_parameter_in_input_document_has_some_comments__the_comments_will_be_collected_in_the_list_of_parameter_value(self):
document_text = '''
# Comment
[Added Associations]
# Comment1
application/illustrator=zzz-gimp.desktop
application/pdf=evince.desktop;
# Comment2
# Comment3
application/rtf=libreoffice-writer.desktop;
[Other Section]
#Comment
!application/vnd.oasis.opendocument.spreadsheet=calculate-calc.desktop;
'''
section_1 = OrderedDict({'#': ['# Comment'],
('', 'application/illustrator'):
['# Comment1',
'zzz-gimp.desktop'],
('', 'application/pdf'):
['evince.desktop;'],
('', 'application/rtf'):
['# Comment2',
'# Comment3',
'libreoffice-writer.desktop;']})
section_2 = OrderedDict({('!', 'application/vnd.oasis.opendocument.spreadsheet'):
['#Comment',
"calculate-calc.desktop;"]})
result = OrderedDict({('', 'Added Associations'): section_1,
('', 'Other Section'): section_2})
compiz_object = CompizFormat(document_text)
assert compiz_object._document_dictionary == result
def test_if_the_IgnoreComments_flag_is_set__the_parser_ignores_all_comments(self):
document_text = '''
# Comment
[Added Associations]
# Comment1
application/illustrator=zzz-gimp.desktop
application/pdf=evince.desktop;
# Comment2
# Comment3
application/rtf=libreoffice-writer.desktop;
[Other Section]
#Comment
!application/vnd.oasis.opendocument.spreadsheet=calculate-calc.desktop;
'''
section_1 = OrderedDict({('', 'application/illustrator'):
['zzz-gimp.desktop'],
('', 'application/pdf'):
['evince.desktop;'],
('', 'application/rtf'):
['libreoffice-writer.desktop;']})
section_2 = OrderedDict({('!', 'application/vnd.oasis.opendocument.spreadsheet'):
["calculate-calc.desktop;"]})
result = OrderedDict({('', 'Added Associations'): section_1,
('', 'Other Section'): section_2})
compiz_object = CompizFormat(document_text, ignore_comments=True)
assert compiz_object._document_dictionary == result
def test_if_input_document_contains_parameters_to_delete_without_assign_symbol_and_any_values_and_sections_to_delete__the_document_object_contains_dictionary_with_item_to_delete(self):
document_text = '''
[-Added Associations]
# Comment
!application/illustrator=zzz-gimp.desktop
!application/pdf=
!application/rtf
[!Other Section]
application/vnd.oasis.opendocument.spreadsheet=calculate-calc.desktop;
'''
section_1 = OrderedDict({('!', 'application/illustrator'):
['# Comment',
'zzz-gimp.desktop'],
('!', 'application/pdf'): [],
('!', 'application/rtf'): []})
section_2 = OrderedDict({('', 'application/vnd.oasis.opendocument.spreadsheet'):
["calculate-calc.desktop;"]})
result = OrderedDict({('-', 'Added Associations'): section_1,
('!', 'Other Section'): section_2})
compiz_object = CompizFormat(document_text)
assert compiz_object._document_dictionary == result
def test_joining_documents_1(self):
with open('./tests/format/testfiles/compiz_original', 'r') as original_file:
original_text = original_file.read()
compiz_original_object = CompizFormat(original_text)
with open('./tests/format/testfiles/compiz_template', 'r') as template_file:
template_text = template_file.read()
compiz_template_object = CompizFormat(template_text,
ignore_comments=True)
compiz_original_object.join_template(compiz_template_object)
with open('./tests/format/testfiles/compiz_result', 'r') as result_file:
result_text = result_file.read()
assert compiz_original_object.get_document_text() == result_text