Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmachine committed Jan 20, 2025
1 parent db5358c commit 2ae2639
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
69 changes: 48 additions & 21 deletions test/editor/chk/decoded_str_section_editor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

import uuid

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

Expand All @@ -25,31 +28,31 @@ def _read_chk_section() -> bytes:
return chk_binary_data


def test_it_does_not_modify_the_str_section_if_adding_already_existing_string():
@pytest.fixture(scope="function")
def str_section() -> DecodedStrSection:
return ChkStrTranscoder().decode(_read_chk_section())


def test_it_does_not_modify_the_str_section_if_adding_already_existing_string(
str_section,
):
editor: DecodedStrSectionEditor = DecodedStrSectionEditor()
transcoder: ChkStrTranscoder = ChkStrTranscoder()
chk_binary_data = _read_chk_section()
str_section: DecodedStrSection = transcoder.decode(chk_binary_data)
new_str_section = editor.add_strings_to_str_section(
[_EXPECTED_STRINGS[0]], str_section
)
assert new_str_section == str_section


def test_it_does_not_modify_the_str_section_if_adding_already_existing_strings():
def test_it_does_not_modify_the_str_section_if_adding_already_existing_strings(
str_section,
):
editor: DecodedStrSectionEditor = DecodedStrSectionEditor()
transcoder: ChkStrTranscoder = ChkStrTranscoder()
chk_binary_data = _read_chk_section()
str_section: DecodedStrSection = transcoder.decode(chk_binary_data)
new_str_section = editor.add_strings_to_str_section(_EXPECTED_STRINGS, str_section)
assert new_str_section == str_section


def test_it_adds_a_new_string_and_modifies_str_as_expected():
def test_it_adds_a_new_string_and_modifies_str_as_expected(str_section):
editor: DecodedStrSectionEditor = DecodedStrSectionEditor()
transcoder: ChkStrTranscoder = ChkStrTranscoder()
chk_binary_data = _read_chk_section()
str_section: DecodedStrSection = transcoder.decode(chk_binary_data)
string_to_add = str(uuid.uuid4())
new_str_section = editor.add_strings_to_str_section([string_to_add], str_section)
assert new_str_section != str_section
Expand All @@ -60,16 +63,40 @@ def test_it_adds_a_new_string_and_modifies_str_as_expected():
assert new_str_section.strings[:-1] == str_section.strings


def test_adding_a_string_creates_a_valid_str_section():
editor: DecodedStrSectionEditor = DecodedStrSectionEditor()
transcoder: ChkStrTranscoder = ChkStrTranscoder()
chk_binary_data = _read_chk_section()
str_section: DecodedStrSection = transcoder.decode(chk_binary_data)
def test_adding_a_string_creates_a_valid_str_section(str_section):
string_to_add = str(uuid.uuid4())
new_str_section: DecodedStrSection = editor.add_strings_to_str_section(
[string_to_add], str_section
new_str_section: DecodedStrSection = (
DecodedStrSectionEditor().add_strings_to_str_section(
[string_to_add], str_section
)
)
decoded_new_str_section: DecodedStrSection = transcoder.decode(
transcoder.encode(new_str_section, include_header=False)
decoded_new_str_section: DecodedStrSection = ChkStrTranscoder().decode(
ChkStrTranscoder().encode(new_str_section, include_header=False)
)
assert decoded_new_str_section == new_str_section


def test_added_strings_have_correct_offsets(str_section):
first_strings_to_add = [str(uuid.uuid4())]
new_str = DecodedStrSectionEditor().add_strings_to_str_section(
first_strings_to_add, str_section
)
_assert_string_offsets_are_valid(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
18 changes: 18 additions & 0 deletions test/editor/chk/decoded_strx_section_editor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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

Expand Down Expand Up @@ -40,6 +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)


def test_it_adds_a_new_string_to_strx(decoded_strx):
Expand All @@ -48,6 +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)


def test_it_adds_multiple_strings_to_strx(decoded_strx):
Expand All @@ -56,6 +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)


def test_it_only_adds_unique_strings_to_strx(decoded_strx):
Expand All @@ -77,6 +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)


def _assert_it_encodes_and_decodes_new_strx_without_changes(strx_section):
Expand All @@ -96,3 +101,16 @@ 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

0 comments on commit 2ae2639

Please sign in to comment.