From dd67fe6eb1d4f141eb2f1474aea70088939688bb Mon Sep 17 00:00:00 2001 From: sethmachine Date: Mon, 20 Jan 2025 09:23:31 -0500 Subject: [PATCH] consolidate some assertions --- .../chk/decoded_str_section_editor_test.py | 19 ++---------- .../chk/decoded_strx_section_editor_test.py | 23 +++----------- .../decoded_strx_section_generator_test.py | 2 ++ test/editor/chk/str_test_utils.py | 31 +++++++++++++++++++ 4 files changed, 41 insertions(+), 34 deletions(-) create mode 100644 test/editor/chk/str_test_utils.py diff --git a/test/editor/chk/decoded_str_section_editor_test.py b/test/editor/chk/decoded_str_section_editor_test.py index 86146b8..09517e1 100644 --- a/test/editor/chk/decoded_str_section_editor_test.py +++ b/test/editor/chk/decoded_str_section_editor_test.py @@ -6,11 +6,11 @@ import pytest from richchk.editor.chk.decoded_str_section_editor import DecodedStrSectionEditor -from richchk.io.richchk.rich_str_lookup_builder import RichStrLookupBuilder from richchk.model.chk.str.decoded_str_section import DecodedStrSection from richchk.transcoder.chk.transcoders.chk_str_transcoder import ChkStrTranscoder from ...chk_resources import CHK_SECTION_FILE_PATHS +from .str_test_utils import assert_string_offsets_are_valid_for_str # these strings were added into the CHK section by using a GUI map editor _EXPECTED_STRINGS = [ @@ -81,22 +81,9 @@ def test_added_strings_have_correct_offsets(str_section): new_str = DecodedStrSectionEditor().add_strings_to_str_section( first_strings_to_add, str_section ) - _assert_string_offsets_are_valid(new_str) + assert_string_offsets_are_valid_for_str(new_str) next_strings_to_add = [str(uuid.uuid4()), str(uuid.uuid4())] next_new_str = DecodedStrSectionEditor().add_strings_to_str_section( next_strings_to_add, str_section ) - _assert_string_offsets_are_valid(next_new_str) - - -def _assert_string_offsets_are_valid(str_section: DecodedStrSection): - expected_strings = set(str_section.strings) - found_strings = set() - str_binary_data = ChkStrTranscoder().encode(str_section, include_header=False) - for offset in str_section.strings_offsets: - found_strings.add( - RichStrLookupBuilder.get_rich_string_by_offset( - offset=offset, str_binary_data=str_binary_data - ).value - ) - assert expected_strings == found_strings + assert_string_offsets_are_valid_for_str(next_new_str) diff --git a/test/editor/chk/decoded_strx_section_editor_test.py b/test/editor/chk/decoded_strx_section_editor_test.py index 81f0df5..977efe1 100644 --- a/test/editor/chk/decoded_strx_section_editor_test.py +++ b/test/editor/chk/decoded_strx_section_editor_test.py @@ -6,11 +6,11 @@ import pytest from richchk.editor.chk.decoded_strx_section_editor import DecodedStrxSectionEditor -from richchk.io.richchk.rich_str_lookup_builder import RichStrLookupBuilder from richchk.model.chk.strx.decoded_strx_section import DecodedStrxSection from richchk.transcoder.chk.transcoders.chk_strx_transcoder import ChkStrxTranscoder from ...chk_resources import CHK_SECTION_FILE_PATHS +from .str_test_utils import assert_string_offsets_are_valid_for_strx # these strings were added into the CHK section by using a GUI map editor _EXPECTED_STRINGS = [ @@ -41,7 +41,7 @@ def test_it_does_not_modify_the_str_section_if_adding_only_already_existing_stri _EXPECTED_STRINGS, decoded_strx ) assert new_str_section == decoded_strx - _assert_string_offsets_are_valid(decoded_strx) + assert_string_offsets_are_valid_for_strx(decoded_strx) def test_it_adds_a_new_string_to_strx(decoded_strx): @@ -50,7 +50,7 @@ def test_it_adds_a_new_string_to_strx(decoded_strx): new_strx_section = editor.add_strings_to_strx_section([string_to_add], decoded_strx) _assert_it_encodes_and_decodes_new_strx_without_changes(new_strx_section) _assert_new_strings_are_added(new_strx_section, decoded_strx, [string_to_add]) - _assert_string_offsets_are_valid(decoded_strx) + assert_string_offsets_are_valid_for_strx(decoded_strx) def test_it_adds_multiple_strings_to_strx(decoded_strx): @@ -59,7 +59,7 @@ def test_it_adds_multiple_strings_to_strx(decoded_strx): new_strx_section = editor.add_strings_to_strx_section(strings_to_add, decoded_strx) _assert_it_encodes_and_decodes_new_strx_without_changes(new_strx_section) _assert_new_strings_are_added(new_strx_section, decoded_strx, strings_to_add) - _assert_string_offsets_are_valid(decoded_strx) + assert_string_offsets_are_valid_for_strx(decoded_strx) def test_it_only_adds_unique_strings_to_strx(decoded_strx): @@ -81,7 +81,7 @@ def test_it_adds_more_than_u16_strings(decoded_strx): editor = DecodedStrxSectionEditor() new_strx = editor.add_strings_to_strx_section(strings_to_add, decoded_strx) _assert_new_strings_are_added(new_strx, decoded_strx, strings_to_add) - _assert_string_offsets_are_valid(decoded_strx) + assert_string_offsets_are_valid_for_strx(decoded_strx) def _assert_it_encodes_and_decodes_new_strx_without_changes(strx_section): @@ -101,16 +101,3 @@ def _assert_new_strings_are_added(new_strx, old_strx, strings_added): ) assert len(new_strx.strings) == (len(old_strx.strings) + len(strings_added)) assert new_strx.strings[:index_for_newly_added_strings] == old_strx.strings - - -def _assert_string_offsets_are_valid(str_section: DecodedStrxSection): - expected_strings = set(str_section.strings) - found_strings = set() - str_binary_data = ChkStrxTranscoder().encode(str_section, include_header=False) - for offset in str_section.strings_offsets: - found_strings.add( - RichStrLookupBuilder.get_rich_string_by_offset( - offset=offset, str_binary_data=str_binary_data - ).value - ) - assert expected_strings == found_strings diff --git a/test/editor/chk/decoded_strx_section_generator_test.py b/test/editor/chk/decoded_strx_section_generator_test.py index 2ac64fe..50857bf 100644 --- a/test/editor/chk/decoded_strx_section_generator_test.py +++ b/test/editor/chk/decoded_strx_section_generator_test.py @@ -9,6 +9,7 @@ from richchk.transcoder.chk.transcoders.chk_strx_transcoder import ChkStrxTranscoder from ...chk_resources import CHK_SECTION_FILE_PATHS +from .str_test_utils import assert_string_offsets_are_valid_for_strx # these strings were added into the CHK section by using a GUI map editor _EXPECTED_STRINGS = [ @@ -36,6 +37,7 @@ def test_it_generates_strx_from_str(): assert strx_again == strx_section _assert_strx_equals_str(strx_section, str_section) _assert_strx_equals_str(strx_again, str_section) + assert_string_offsets_are_valid_for_strx(strx_section) def _assert_strx_equals_str(strx: DecodedStrxSection, str_: DecodedStrSection): diff --git a/test/editor/chk/str_test_utils.py b/test/editor/chk/str_test_utils.py new file mode 100644 index 0000000..a906eef --- /dev/null +++ b/test/editor/chk/str_test_utils.py @@ -0,0 +1,31 @@ +from richchk.io.richchk.rich_str_lookup_builder import RichStrLookupBuilder +from richchk.model.chk.str.decoded_str_section import DecodedStrSection +from richchk.model.chk.strx.decoded_strx_section import DecodedStrxSection +from richchk.transcoder.chk.transcoders.chk_str_transcoder import ChkStrTranscoder +from richchk.transcoder.chk.transcoders.chk_strx_transcoder import ChkStrxTranscoder + + +def assert_string_offsets_are_valid_for_str(str_section: DecodedStrSection): + expected_strings = set(str_section.strings) + found_strings = set() + str_binary_data = ChkStrTranscoder().encode(str_section, include_header=False) + for offset in str_section.strings_offsets: + found_strings.add( + RichStrLookupBuilder.get_rich_string_by_offset( + offset=offset, str_binary_data=str_binary_data + ).value + ) + assert expected_strings == found_strings + + +def assert_string_offsets_are_valid_for_strx(str_section: DecodedStrxSection): + expected_strings = set(str_section.strings) + found_strings = set() + str_binary_data = ChkStrxTranscoder().encode(str_section, include_header=False) + for offset in str_section.strings_offsets: + found_strings.add( + RichStrLookupBuilder.get_rich_string_by_offset( + offset=offset, str_binary_data=str_binary_data + ).value + ) + assert expected_strings == found_strings