Skip to content

Commit

Permalink
consolidate some assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmachine committed Jan 20, 2025
1 parent 2ae2639 commit dd67fe6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
19 changes: 3 additions & 16 deletions test/editor/chk/decoded_str_section_editor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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)
23 changes: 5 additions & 18 deletions test/editor/chk/decoded_strx_section_editor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
2 changes: 2 additions & 0 deletions test/editor/chk/decoded_strx_section_generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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):
Expand Down
31 changes: 31 additions & 0 deletions test/editor/chk/str_test_utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dd67fe6

Please sign in to comment.