Initial implementation with unit and integration tests
Reads value1=value2 pairs from a configuration file and replaces every non-overlapping occurrence of value1 with value2 in a text file, printing the resulting lines sorted by the number of symbols replaced. Overlapping patterns resolve to the longest match so the result does not depend on configuration file ordering. Ties keep their original file order.
This commit is contained in:
0
tests/unit/__init__.py
Normal file
0
tests/unit/__init__.py
Normal file
58
tests/unit/test_config_loader.py
Normal file
58
tests/unit/test_config_loader.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""Unit tests for config_loader.py"""
|
||||
|
||||
import pytest
|
||||
from config_loader import load_config
|
||||
|
||||
|
||||
def test_valid_config(tmp_path):
|
||||
config_file = tmp_path / "config.txt"
|
||||
config_file.write_text("a=z\nbb=y\nc=x\n")
|
||||
pairs = load_config(str(config_file))
|
||||
assert pairs == [("a", "z"), ("bb", "y"), ("c", "x")]
|
||||
|
||||
|
||||
def test_skips_blank_lines(tmp_path):
|
||||
config_file = tmp_path / "config.txt"
|
||||
config_file.write_text("a=z\n\nbb=y\n")
|
||||
pairs = load_config(str(config_file))
|
||||
assert pairs == [("a", "z"), ("bb", "y")]
|
||||
|
||||
|
||||
def test_invalid_line_raises_value_error(tmp_path):
|
||||
config_file = tmp_path / "config.txt"
|
||||
config_file.write_text("a=z\nnoequalsign\n")
|
||||
with pytest.raises(ValueError):
|
||||
load_config(str(config_file))
|
||||
|
||||
|
||||
def test_value_can_contain_equals_sign(tmp_path):
|
||||
"""split('=', 1) should only split on the FIRST '=', so value2
|
||||
is allowed to contain additional '=' characters."""
|
||||
config_file = tmp_path / "config.txt"
|
||||
config_file.write_text("a=x=y\n")
|
||||
pairs = load_config(str(config_file))
|
||||
assert pairs == [("a", "x=y")]
|
||||
|
||||
|
||||
def test_trailing_whitespace_in_value_is_preserved(tmp_path):
|
||||
"""Whitespace inside value2 is significant and deliberately kept.
|
||||
|
||||
The configuration format has no quoting or escaping, so stripping
|
||||
would make a replacement value ending in a space impossible to
|
||||
express. Guarding against accidental trailing spaces is therefore
|
||||
left to the caller.
|
||||
"""
|
||||
config_file = tmp_path / "config.txt"
|
||||
config_file.write_text("a=z \n") # trailing space before newline
|
||||
|
||||
pairs = load_config(str(config_file))
|
||||
|
||||
assert pairs == [("a", "z ")]
|
||||
|
||||
|
||||
def test_empty_config_file(tmp_path):
|
||||
"""An empty configuration is valid and simply replaces nothing."""
|
||||
config_file = tmp_path / "config.txt"
|
||||
config_file.write_text("", encoding="utf-8")
|
||||
|
||||
assert load_config(str(config_file)) == []
|
||||
61
tests/unit/test_replacer.py
Normal file
61
tests/unit/test_replacer.py
Normal file
@@ -0,0 +1,61 @@
|
||||
"""Unit tests for replacer.py"""
|
||||
|
||||
from replacer import prepare_pairs, replace_line
|
||||
|
||||
|
||||
def test_sample_line_from_assignment():
|
||||
"""First sample line from the assignment: 'jgrebbk6hnae'."""
|
||||
pairs = prepare_pairs([("a", "z"), ("bb", "y"), ("c", "x")])
|
||||
result, replaced = replace_line("jgrebbk6hnae", pairs)
|
||||
assert result == "jgreyk6hnze"
|
||||
assert replaced == 3 # "bb" (2) + "a" (1)
|
||||
|
||||
|
||||
def test_no_matches():
|
||||
pairs = prepare_pairs([("a", "z"), ("bb", "y")])
|
||||
result, replaced = replace_line("xyz123", pairs)
|
||||
assert result == "xyz123"
|
||||
assert replaced == 0
|
||||
|
||||
|
||||
def test_empty_line():
|
||||
pairs = prepare_pairs([("a", "z")])
|
||||
result, replaced = replace_line("", pairs)
|
||||
assert result == ""
|
||||
assert replaced == 0
|
||||
|
||||
|
||||
def test_overlapping_prefix_patterns_longest_wins():
|
||||
"""
|
||||
Config has both 'b' and 'bb'. Input 'bbbb' should be read as two
|
||||
'bb' matches (longest-match-first), not four 'b' matches.
|
||||
This is the key edge case the greedy algorithm is designed for.
|
||||
"""
|
||||
pairs = prepare_pairs([("b", "1"), ("bb", "2")])
|
||||
result, replaced = replace_line("bbbb", pairs)
|
||||
assert result == "22"
|
||||
assert replaced == 4 # 2 matches of "bb", 2 symbols each
|
||||
|
||||
|
||||
def test_adjacent_matches_non_overlapping():
|
||||
"""Consecutive matches of the same pattern shouldn't overlap."""
|
||||
pairs = prepare_pairs([("bb", "y")])
|
||||
# Five b's -> "bb", "bb", and a leftover "b".
|
||||
result, replaced = replace_line("bbbbb", pairs)
|
||||
assert result == "yyb"
|
||||
assert replaced == 4
|
||||
|
||||
|
||||
def test_full_line_replacement():
|
||||
pairs = prepare_pairs([("abc", "X")])
|
||||
result, replaced = replace_line("abc", pairs)
|
||||
assert result == "X"
|
||||
assert replaced == 3
|
||||
|
||||
|
||||
def test_single_character_values():
|
||||
# Replacement with an empty string is a valid edge case.
|
||||
pairs = prepare_pairs([("a", "")])
|
||||
result, replaced = replace_line("banana", pairs)
|
||||
assert result == "bnn"
|
||||
assert replaced == 3
|
||||
29
tests/unit/test_sample_data.py
Normal file
29
tests/unit/test_sample_data.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""
|
||||
Verifies the worked example from instructions.md line by line.
|
||||
|
||||
The end-to-end behaviour of that same example, including the sort order
|
||||
and the printed output, is covered by tests/integration/test_cli.py.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from replacer import prepare_pairs, replace_line
|
||||
|
||||
SAMPLE_PAIRS = prepare_pairs([("a", "z"), ("bb", "y"), ("c", "x")])
|
||||
|
||||
# Each sample line mapped to its expected result and replacement count.
|
||||
EXPECTED_RESULTS = {
|
||||
"jgrebbk6hnae": ("jgreyk6hnze", 3),
|
||||
"cnhjrfyjvth3nxr": ("xnhjrfyjvth3nxr", 1),
|
||||
"b#sjcf_ansbbbbbvo!": ("b#sjxf_znsyybvo!", 6),
|
||||
"dajf#aemfaocfna%": ("dzjf#zemfzoxfnz%", 5),
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("line", list(EXPECTED_RESULTS))
|
||||
def test_each_sample_line(line):
|
||||
expected_result, expected_replaced = EXPECTED_RESULTS[line]
|
||||
|
||||
result, replaced = replace_line(line, SAMPLE_PAIRS)
|
||||
|
||||
assert result == expected_result
|
||||
assert replaced == expected_replaced
|
||||
Reference in New Issue
Block a user