diff --git a/bin/generate_tests b/bin/generate_tests index 3c1c2fa4..c5ccbf36 100755 --- a/bin/generate_tests +++ b/bin/generate_tests @@ -23,28 +23,22 @@ def problem_spec_dir() -> pathlib.Path: specs = pathlib.Path(cache_dir) / "exercism/configlet/problem-specifications" if specs.exists(): return specs - cur = pathlib.Path(os.getcwd()) - for i in cur.parents: - if i.name == "problem-specifications": - return i raise LookupError("Could not find problem specs") -def flatten_cases(cases: list[dict]) -> list[tuple[list[str], dict]]: +def flatten_cases(cases: list[dict], included: list[str]) -> list[tuple[list[str], dict]]: """Recursive flatten test cases, returning individual cases with parent descriptions.""" for case_or_group in cases: if "cases" in case_or_group: - for groups, child_case in flatten_cases(case_or_group["cases"]): + for groups, child_case in flatten_cases(case_or_group["cases"], included): yield ([case_or_group["description"]] + groups, child_case) else: - yield ([], case_or_group) + if case_or_group["uuid"] in included: + yield ([], case_or_group) -def get_cases(specs: pathlib.Path, exercise: pathlib.Path) -> list[dict]: - """Return flattened, filtered cases with additional metadata attached.""" - canonical_path = specs / "exercises" / exercise.name / "canonical-data.json" - with open(canonical_path, "r", encoding="utf-8") as f: - canonical = json.load(f) +def get_test_uuids(exercise: pathlib.Path) -> list[str]: + """Return UUIDs of included test cases from tests.toml.""" with open(exercise / ".meta" / "tests.toml", "rb") as f: tests = tomllib.load(f) @@ -53,34 +47,70 @@ def get_cases(specs: pathlib.Path, exercise: pathlib.Path) -> list[dict]: for test in tests.values() if test.get("include", True) and "reimplements" in test } + + return [ + uuid for uuid, details in tests.items() + if details.get("include", True) and uuid not in reimplemented + ] + + +def update_test_case(case: dict, groups: list[str]) -> None: + """Update test case data in place for use by the templates.""" + case["descriptions"] = groups + [case["description"]] + case["expect_error"] = isinstance(case["expected"], dict) and "error" in case["expected"] + if case["expect_error"]: + case["expect_error_msg"] = case["expected"]["error"] + + +def get_cases(specs: pathlib.Path, exercise: pathlib.Path) -> list[dict]: + """Return flattened, filtered cases with additional metadata attached.""" + canonical_path = specs / "exercises" / exercise.name / "canonical-data.json" + with open(canonical_path, "r", encoding="utf-8") as f: + canonical = json.load(f) + cases = [] - for groups, case in flatten_cases(canonical["cases"]): - # Filter out test cases with include=false or not listed. - if case["uuid"] not in tests or case["uuid"] in reimplemented: - continue - if not tests[case["uuid"]].get("include", True): - continue - # Add metadata. - case["descriptions"] = groups + [case["description"]] - case["expect_error"] = isinstance(case["expected"], dict) and "error" in case["expected"] - if case["expect_error"]: - case["expect_error_msg"] = case["expected"]["error"] + for groups, case in flatten_cases(canonical["cases"], get_test_uuids(exercise)): + update_test_case(case, groups) cases.append(case) return cases -def filter_tojson(data, separators=(',', ':'), indent=None) -> str: +def get_grouped_cases(specs: pathlib.Path, exercise: pathlib.Path) -> list[dict]: + """Return filtered cases, in the original nested hierarchy, with additional metadata attached.""" + canonical_path = specs / "exercises" / exercise.name / "canonical-data.json" + with open(canonical_path, "r", encoding="utf-8") as f: + canonical = json.load(f) + + cases = canonical["cases"] + for groups, case in flatten_cases(cases, get_test_uuids(exercise)): + update_test_case(case, groups) + return cases + + +def quote(obj) -> str: + """Convert Python objects to native objects.""" + if obj is True: + return '"true"' + if obj is False: + return '"false"' + if isinstance(obj, str): + return shlex.quote(str) + if isinstance(obj, list): + return [quote(val) for val in obj] + raise ValueError(f"quote() does not support {type(obj)}") + +def tojson(data, separators=(',', ':'), indent=None) -> str: """Filter `tojson` that JSON encodes a string with flexible settings.""" return json.dumps(data, separators=separators, indent=indent) def jinja_env(exercise: pathlib.Path) -> jinja2.Environment: """Return a configured Jinja env with filters added.""" - env = jinja2.Environment(loader=jinja2.FileSystemLoader(exercise / ".meta")) + env = jinja2.Environment(loader=jinja2.FileSystemLoader(["bin", exercise / ".meta"])) # Shell quoting - env.filters["quote"] = shlex.quote + env.filters["quote"] = quote # JSON formatting, default to compact form (`jq -c`). - env.filters["tojson"] = filter_tojson + env.filters["tojson"] = tojson # String escaping, ANSI-C style. env.filters["repr"] = repr # Return a dict with only specified keys kepts. @@ -91,25 +121,9 @@ def jinja_env(exercise: pathlib.Path) -> jinja2.Environment: return env -def bool_to_str(obj): - """Recursively convert boolean values to strings in collections.""" - if isinstance(obj, dict): - return {key: bool_to_str(val) for key, val in obj.items()} - if isinstance(obj, list): - return [bool_to_str(val) for val in obj] - if obj is True: - return "true" - if obj is False: - return "false" - return obj - - def generate(specs: pathlib.Path, exercise: pathlib.Path) -> None: """Generate and write test file for a given spec and exercise.""" - cases = get_cases(specs, exercise) - for case in cases: - case["expected"] = bool_to_str(case["expected"]) - + config = json.loads((exercise / ".meta/config.json").read_text()) timestamp = datetime.datetime.now(tz=datetime.UTC).replace(microsecond=0).isoformat() header = textwrap.dedent(f"""\ #!/usr/bin/env bats @@ -119,21 +133,21 @@ def generate(specs: pathlib.Path, exercise: pathlib.Path) -> None: """ ).strip() data = { - "cases": cases, + "cases": get_cases(specs, exercise), "header": header, - "solution": json.loads((exercise / ".meta/config.json").read_text())["files"]["solution"][0] + "solution": config["files"]["solution"][0], } # Render the template. try: template = jinja_env(exercise).get_template("template.j2") - out = template.render(data) + out = template.render(data).strip() except jinja2.exceptions.TemplateAssertionError as e: e.add_note(f"Error rendering template for {exercise.name}") raise # Check for changes or the lack thereof. - test_file = exercise / json.loads((exercise / ".meta/config.json").read_text())["files"]["test"][0] + test_file = exercise / config["files"]["test"][0] if test_file.exists(): old_content = [i for i in test_file.read_text().strip().splitlines() if "generated on" not in i] new_content = [i for i in out.splitlines() if "generated on" not in i] @@ -147,10 +161,10 @@ def generate(specs: pathlib.Path, exercise: pathlib.Path) -> None: def argparser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser() parser.add_argument( - "--no-pull", - action="store_false", + "--pull", + action="store_true", dest="pull", - help="Do not run `git pull` on the problem specs repo", + help="Run `git pull` on the problem specs repo", ) parser.add_argument( "exercises", @@ -169,10 +183,12 @@ def main(): exercises = args.exercises # Generate all exercises with templates if none are specified as args. if not exercises: - exercises = [ - i.parent.parent - for i in pathlib.Path("exercises/practice").glob("*/.meta/template.j2") - ] + exercises = sorted( + { + i.parent.parent + for i in pathlib.Path("exercises/practice").glob("*/.meta/template.j2") + } + ) else: # Turn strings to paths and make them relative to the practice exercises. out = [] diff --git a/exercises/practice/etl/test-etl.bats b/exercises/practice/etl/test-etl.bats index e0044739..71992153 100644 --- a/exercises/practice/etl/test-etl.bats +++ b/exercises/practice/etl/test-etl.bats @@ -1,114 +1,33 @@ #!/usr/bin/env bats load bats-extra -@test 'single letter' { - #[[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f etl.awk << END_INPUT -1: A -END_INPUT +# generated on 2026-11-04T19:33:58+00:00 +@test "single letter" { + # [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run gawk -f etl.awk <<< "{'1': ['A']}" assert_success - assert_output "a,1" + assert_output "{'a': 1}" } -@test 'single score with multiple letters' { +@test "single score with multiple letters" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f etl.awk << END_INPUT -1: "A", "E", "I", "O", "U" -END_INPUT - + run gawk -f etl.awk <<< "{'1': ['A', 'E', 'I', 'O', 'U']}" assert_success - assert_line --index 0 -- "a,1" - assert_line --index 1 -- "e,1" - assert_line --index 2 -- "i,1" - assert_line --index 3 -- "o,1" - assert_line --index 4 -- "u,1" - assert_equal "${#lines[@]}" 5 + assert_output "{'a': 1, 'e': 1, 'i': 1, 'o': 1, 'u': 1}" } -@test 'a score with no letters' { +@test "multiple scores with multiple letters" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f etl.awk << END_INPUT -1: A -7: -END_INPUT - + run gawk -f etl.awk <<< "{'1': ['A', 'E'], '2': ['D', 'G']}" assert_success - assert_output "a,1" + assert_output "{'a': 1, 'd': 2, 'e': 1, 'g': 2}" } -@test 'multiple scores with multiple letters' { +@test "multiple scores with differing numbers of letters" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f etl.awk << END_INPUT -1: "E", "A" -2:"D","G" -END_INPUT - + run gawk -f etl.awk <<< "{'1': ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], '2': ['D', 'G'], '3': ['B', 'C', 'M', 'P'], '4': ['F', 'H', 'V', 'W', 'Y'], '5': ['K'], '8': ['J', 'X'], '10': ['Q', 'Z']}" assert_success - assert_line --index 0 -- "a,1" - assert_line --index 1 -- "d,2" - assert_line --index 2 -- "e,1" - assert_line --index 3 -- "g,2" - assert_equal "${#lines[@]}" 4 + assert_output "{'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10}" } -@test 'multiple scores with multiple letters, blank lines' { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f etl.awk << END_INPUT - -2:"D","G" - -1: "E", "A" -END_INPUT - - assert_success - assert_line --index 0 -- "a,1" - assert_line --index 1 -- "d,2" - assert_line --index 2 -- "e,1" - assert_line --index 3 -- "g,2" - assert_equal "${#lines[@]}" 4 -} - -@test 'multiple scores with differing numbers of letters' { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - # there are tab characters below - run gawk -f etl.awk << END_INPUT -1: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" -2: "D", "G" -4: "F", "H", "V", "W", "Y" -3: "m", "B", "C", "P" -5: "K" -8: "J", "X" -10: "Q", "Z" -END_INPUT - - assert_success - assert_line --index 0 -- a,1 - assert_line --index 1 -- b,3 - assert_line --index 2 -- c,3 - assert_line --index 3 -- d,2 - assert_line --index 4 -- e,1 - assert_line --index 5 -- f,4 - assert_line --index 6 -- g,2 - assert_line --index 7 -- h,4 - assert_line --index 8 -- i,1 - assert_line --index 9 -- j,8 - assert_line --index 10 -- k,5 - assert_line --index 11 -- l,1 - assert_line --index 12 -- m,3 - assert_line --index 13 -- n,1 - assert_line --index 14 -- o,1 - assert_line --index 15 -- p,3 - assert_line --index 16 -- q,10 - assert_line --index 17 -- r,1 - assert_line --index 18 -- s,1 - assert_line --index 19 -- t,1 - assert_line --index 20 -- u,1 - assert_line --index 21 -- v,4 - assert_line --index 22 -- w,4 - assert_line --index 23 -- x,8 - assert_line --index 24 -- y,4 - assert_line --index 25 -- z,10 - assert_equal "${#lines[@]}" 26 -} diff --git a/exercises/practice/grep/test-grep.bats b/exercises/practice/grep/test-grep.bats index 83ed44cc..5ce8da98 100644 --- a/exercises/practice/grep/test-grep.bats +++ b/exercises/practice/grep/test-grep.bats @@ -1,365 +1,180 @@ #!/usr/bin/env bats load bats-extra -setup() { - - cat > iliad.txt < midsummer-night.txt < paradise-lost.txt < instructions.txt -} -teardown() { - rm -f instructions.txt -} - -# A robot is created with a position and a direction. -# The awk program will output the robot's variables: x, y, dir +# generated on 2026-11-04T19:33:58+00:00 -@test "Robots are created with a position and direction" { - #[[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f robot-simulator.awk -v x=0 -v y=0 -v dir=north /dev/null - assert_success - assert_output "0 0 north" -} -@test "Robots are created with a default position and direction" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f robot-simulator.awk /dev/null +@test "at origin facing north" { + # [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run gawk -f robot-simulator.awk <<< "north" assert_success - assert_output "0 0 north" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'north'}" } -@test "Negative positions are allowed" { +@test "at negative position facing south" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f robot-simulator.awk -v x=-1 -v y=-1 -v dir=south /dev/null + run gawk -f robot-simulator.awk <<< "south" assert_success - assert_output "-1 -1 south" + assert_output "{'position': {'x': -1, 'y': -1}, 'direction': 'south'}" } - -# rotates the robot's direction 90 degrees clockwise - -@test "changes the direction from north to east" { +@test "changes north to east" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions R - run gawk -f robot-simulator.awk -v dir=north instructions.txt + run gawk -f robot-simulator.awk <<< "north" assert_success - assert_output "0 0 east" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'east'}" } -@test "changes the direction from east to south" { +@test "changes east to south" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions R - run gawk -f robot-simulator.awk -v dir=east instructions.txt + run gawk -f robot-simulator.awk <<< "east" assert_success - assert_output "0 0 south" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'south'}" } -@test "changes the direction from south to west" { +@test "changes south to west" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions R - run gawk -f robot-simulator.awk -v dir=south instructions.txt + run gawk -f robot-simulator.awk <<< "south" assert_success - assert_output "0 0 west" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'west'}" } -@test "changes the direction from west to north" { +@test "changes west to north" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions R - run gawk -f robot-simulator.awk -v dir=west instructions.txt + run gawk -f robot-simulator.awk <<< "west" assert_success - assert_output "0 0 north" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'north'}" } - -# rotates the robot's direction 90 degrees counter-clockwise - -@test "changes the direction from north to west" { +@test "changes north to west" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions L - run gawk -f robot-simulator.awk -v dir=north instructions.txt + run gawk -f robot-simulator.awk <<< "north" assert_success - assert_output "0 0 west" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'west'}" } -@test "changes the direction from west to south" { +@test "changes west to south" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions L - run gawk -f robot-simulator.awk -v dir=west instructions.txt + run gawk -f robot-simulator.awk <<< "west" assert_success - assert_output "0 0 south" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'south'}" } -@test "changes the direction from south to east" { +@test "changes south to east" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions L - run gawk -f robot-simulator.awk -v dir=south instructions.txt + run gawk -f robot-simulator.awk <<< "south" assert_success - assert_output "0 0 east" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'east'}" } -@test "changes the direction from east to north" { +@test "changes east to north" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions L - run gawk -f robot-simulator.awk -v dir=east instructions.txt + run gawk -f robot-simulator.awk <<< "east" assert_success - assert_output "0 0 north" + assert_output "{'position': {'x': 0, 'y': 0}, 'direction': 'north'}" } - -# moves the robot forward 1 space in the direction it is pointing - -@test "increases the y coordinate one when facing north" { +@test "facing north increments Y" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions A - run gawk -f robot-simulator.awk -v x=0 -v y=0 -v dir=north instructions.txt + run gawk -f robot-simulator.awk <<< "north" assert_success - assert_output "0 1 north" + assert_output "{'position': {'x': 0, 'y': 1}, 'direction': 'north'}" } -@test "decreases the y coordinate by one when facing south" { +@test "facing south decrements Y" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions A - run gawk -f robot-simulator.awk -v x=0 -v y=0 -v dir=south instructions.txt + run gawk -f robot-simulator.awk <<< "south" assert_success - assert_output "0 -1 south" + assert_output "{'position': {'x': 0, 'y': -1}, 'direction': 'south'}" } -@test "increases the x coordinate by one when facing east" { +@test "facing east increments X" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions A - run gawk -f robot-simulator.awk -v x=0 -v y=0 -v dir=east instructions.txt + run gawk -f robot-simulator.awk <<< "east" assert_success - assert_output "1 0 east" + assert_output "{'position': {'x': 1, 'y': 0}, 'direction': 'east'}" } -@test "decreases the x coordinate by one when facing west" { +@test "facing west decrements X" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions A - run gawk -f robot-simulator.awk -v x=0 -v y=0 -v dir=west instructions.txt + run gawk -f robot-simulator.awk <<< "west" assert_success - assert_output "-1 0 west" + assert_output "{'position': {'x': -1, 'y': 0}, 'direction': 'west'}" } - -# Where R = Turn Right, L = Turn Left and A = Advance, the -# robot can follow a series of instructions and end up with -# the correct position and direction - -@test "instructions to move east and north from README" { +@test "moving east and north from README" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions R A A L A L - run gawk -f robot-simulator.awk -v x=7 -v y=3 -v dir=north instructions.txt + run gawk -f robot-simulator.awk <<< "north" assert_success - assert_output "9 4 west" + assert_output "{'position': {'x': 9, 'y': 4}, 'direction': 'west'}" } -@test "instructions to move west and north" { +@test "moving west and north" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions L A A A R A L A - run gawk -f robot-simulator.awk -v x=0 -v y=0 -v dir=north instructions.txt + run gawk -f robot-simulator.awk <<< "north" assert_success - assert_output "-4 1 west" + assert_output "{'position': {'x': -4, 'y': 1}, 'direction': 'west'}" } -@test "instructions to move west and south" { +@test "moving west and south" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions R R A A A A A L A - run gawk -f robot-simulator.awk -v x=2 -v y=-7 -v dir=east instructions.txt + run gawk -f robot-simulator.awk <<< "east" assert_success - assert_output "-3 -8 south" + assert_output "{'position': {'x': -3, 'y': -8}, 'direction': 'south'}" } -@test "instructions to move east and north" { +@test "moving east and north" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions L A A A R R R A L L L L - run gawk -f robot-simulator.awk -v x=8 -v y=4 -v dir=south instructions.txt + run gawk -f robot-simulator.awk <<< "south" assert_success - assert_output "11 5 north" -} - - -# error conditions - -@test "invalid direction" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f robot-simulator.awk -v dir=foo /dev/null - assert_failure - assert_output "invalid direction" + assert_output "{'position': {'x': 11, 'y': 5}, 'direction': 'north'}" } -@test "invalid instructions" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - write_instructions L R A X - run gawk -f robot-simulator.awk instructions.txt - assert_failure - assert_output "invalid instruction" -} diff --git a/exercises/practice/simple-cipher/test-simple-cipher.bats b/exercises/practice/simple-cipher/test-simple-cipher.bats index 4265f19b..d59de5b1 100644 --- a/exercises/practice/simple-cipher/test-simple-cipher.bats +++ b/exercises/practice/simple-cipher/test-simple-cipher.bats @@ -1,172 +1,89 @@ #!/usr/bin/env bats load bats-extra -# local version: 2.0.0.1 -# bash-specific test: Input validation, lower-casing +# generated on 2026-11-04T19:33:58+00:00 -# Random key cipher" - -@test "Can generate a random key" { - #[[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f simple-cipher.awk +@test "Can encode" { + # [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run gawk -f simple-cipher.awk <<< "aaaaaaaaaa" assert_success - key=$output - assert [ "${#key}" -ge 100 ] # at least 100 chars - [[ "$key" =~ ^[[:lower:]]+$ ]] # only lowercase letters + assert_output "cipher.key.substring(0, plaintext.length)" } -@test "Can encode random" { +@test "Can decode" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f simple-cipher.awk - assert_success - key=$output - - plaintext="aaaaaaaaaa" - run gawk -v key="$key" -v type=encode -f simple-cipher.awk <<< "$plaintext" + run gawk -f simple-cipher.awk <<< "" assert_success - assert_equal "${#output}" 10 - assert_output "${key:0:10}" + assert_output "aaaaaaaaaa" } -@test "Can decode random" { +@test "Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f simple-cipher.awk + run gawk -f simple-cipher.awk <<< "abcdefghij" assert_success - key=$output - - plaintext="aaaaaaaaaa" - run gawk -v key="$key" -v type=decode -f simple-cipher.awk <<< "${key:0:10}" - assert_success - assert_equal "${#output}" 10 - assert_output "$plaintext" + assert_output "abcdefghij" } -@test "Is reversible random" { +@test "Key is made only of lowercase letters" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f simple-cipher.awk - assert_success - key=$output - - plaintext="abcdefghij" - run gawk -v key="$key" -v type=encode -f simple-cipher.awk <<< "$plaintext" + run gawk -f simple-cipher.awk <<< "" assert_success - encoded=$output - - run gawk -v key="$key" -v type=decode -f simple-cipher.awk <<< "$encoded" - assert_success - assert_output "$plaintext" + assert_output "{'match': '^[a-z]+$'}" } -# Substitution cipher - -@test "Can encode" { +@test "Can encode" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - key=abcdefghij - txt=aaaaaaaaaa - run gawk -v key="$key" -v type=encode -f simple-cipher.awk <<< "$txt" + run gawk -f simple-cipher.awk <<< "aaaaaaaaaa" assert_success - assert_output "$key" + assert_output "abcdefghij" } -@test "Can decode" { +@test "Can decode" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - key=abcdefghij - txt=abcdefghij - exp=aaaaaaaaaa - run gawk -v key="$key" -v type=decode -f simple-cipher.awk <<< "$txt" + run gawk -f simple-cipher.awk <<< "" assert_success - assert_output "$exp" + assert_output "aaaaaaaaaa" } -@test "Is reversible" { +@test "Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - key=abcdefghij - txt=abcdefghij - exp=abcdefghij - run gawk -v key="$key" -v type=encode -f simple-cipher.awk <<< "$txt" + run gawk -f simple-cipher.awk <<< "abcdefghij" assert_success - encoded=$output - run gawk -v key="$key" -v type=decode -f simple-cipher.awk <<< "$encoded" - assert_success - assert_output "$exp" + assert_output "abcdefghij" } -@test "Can double shift encode" { +@test "Can double shift encode" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - key=iamapandabear - txt=iamapandabear - exp=qayaeaagaciai - run gawk -v key="$key" -v type=encode -f simple-cipher.awk <<< "$txt" + run gawk -f simple-cipher.awk <<< "iamapandabear" assert_success - assert_output "$exp" + assert_output "qayaeaagaciai" } -@test "Can wrap on encode" { +@test "Can wrap on encode" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - key=abcdefghij - txt=zzzzzzzzzz - exp=zabcdefghi - run gawk -v key="$key" -v type=encode -f simple-cipher.awk <<< "$txt" + run gawk -f simple-cipher.awk <<< "zzzzzzzzzz" assert_success - assert_output "$exp" + assert_output "zabcdefghi" } -@test "Can wrap on decode" { +@test "Can wrap on decode" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - key=abcdefghij - txt=zabcdefghi - exp=zzzzzzzzzz - run gawk -v key="$key" -v type=decode -f simple-cipher.awk <<< "$txt" + run gawk -f simple-cipher.awk <<< "" assert_success - assert_output "$exp" + assert_output "zzzzzzzzzz" } -@test "Can encode messages longer than the key" { +@test "Can encode messages longer than the key" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - key=abc - txt=iamapandabear - exp=iboaqcnecbfcr - run gawk -v key="$key" -v type=encode -f simple-cipher.awk <<< "$txt" + run gawk -f simple-cipher.awk <<< "iamapandabear" assert_success - assert_output "$exp" + assert_output "iboaqcnecbfcr" } -@test "Can decode messages longer than the key" { +@test "Can decode messages longer than the key" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - key=abc - txt=iboaqcnecbfcr - exp=iamapandabear - run gawk -v key="$key" -v type=decode -f simple-cipher.awk <<< "$txt" + run gawk -f simple-cipher.awk <<< "" assert_success - assert_output "$exp" + assert_output "iamapandabear" } -@test "plaintext is lowercased" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -v key=b -v type=encode -f simple-cipher.awk <<< FOOBAR - assert_success - assert_output "gppcbs" -} - -@test "ciphertext is lowercased" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -v key=b -v type=decode -f simple-cipher.awk <<< GPPCBS - assert_success - assert_output "foobar" -} - -# errors - -@test "key must be lowercase" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -v key=ABC -v type=encode -f simple-cipher.awk <<< foo - assert_failure - assert_output --partial "invalid key" -} - -@test "key must be letters" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -v key=123 -v type=encode -f simple-cipher.awk <<< foo - assert_failure - assert_output --partial "invalid key" -} diff --git a/exercises/practice/tournament/test-tournament.bats b/exercises/practice/tournament/test-tournament.bats index 59a68e2c..8b765951 100644 --- a/exercises/practice/tournament/test-tournament.bats +++ b/exercises/practice/tournament/test-tournament.bats @@ -1,198 +1,89 @@ #!/usr/bin/env bats load bats-extra -# Assert that running the program with input $1 generates output $2. -assert_in_to_out() { - run gawk -f tournament.awk <<< "$1" - assert_success - assert_output "$2" -} +# generated on 2026-11-04T19:33:58+00:00 @test "just the header if no input" { - #[[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="" - - expected="Team | MP | W | D | L | P" - - assert_in_to_out "$input" "$expected" + # [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run gawk -f tournament.awk <<< "[]" + assert_success + assert_output "['Team | MP | W | D | L | P']" } @test "a win is three points, a loss is zero points" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="Allegoric Alaskans;Blithering Badgers;win" - - expected="\ -Team | MP | W | D | L | P -Allegoric Alaskans | 1 | 1 | 0 | 0 | 3 -Blithering Badgers | 1 | 0 | 0 | 1 | 0" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Allegoric Alaskans;Blithering Badgers;win']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Allegoric Alaskans | 1 | 1 | 0 | 0 | 3', 'Blithering Badgers | 1 | 0 | 0 | 1 | 0']" } @test "a win can also be expressed as a loss" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="Blithering Badgers;Allegoric Alaskans;loss" - - expected="\ -Team | MP | W | D | L | P -Allegoric Alaskans | 1 | 1 | 0 | 0 | 3 -Blithering Badgers | 1 | 0 | 0 | 1 | 0" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Blithering Badgers;Allegoric Alaskans;loss']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Allegoric Alaskans | 1 | 1 | 0 | 0 | 3', 'Blithering Badgers | 1 | 0 | 0 | 1 | 0']" } @test "a different team can win" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="Blithering Badgers;Allegoric Alaskans;win" - - expected="\ -Team | MP | W | D | L | P -Blithering Badgers | 1 | 1 | 0 | 0 | 3 -Allegoric Alaskans | 1 | 0 | 0 | 1 | 0" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Blithering Badgers;Allegoric Alaskans;win']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Blithering Badgers | 1 | 1 | 0 | 0 | 3', 'Allegoric Alaskans | 1 | 0 | 0 | 1 | 0']" } @test "a draw is one point each" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="Allegoric Alaskans;Blithering Badgers;draw" - - expected="\ -Team | MP | W | D | L | P -Allegoric Alaskans | 1 | 0 | 1 | 0 | 1 -Blithering Badgers | 1 | 0 | 1 | 0 | 1" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Allegoric Alaskans;Blithering Badgers;draw']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Allegoric Alaskans | 1 | 0 | 1 | 0 | 1', 'Blithering Badgers | 1 | 0 | 1 | 0 | 1']" } @test "There can be more than one match" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="\ -Allegoric Alaskans;Blithering Badgers;win -Allegoric Alaskans;Blithering Badgers;win" - - expected="\ -Team | MP | W | D | L | P -Allegoric Alaskans | 2 | 2 | 0 | 0 | 6 -Blithering Badgers | 2 | 0 | 0 | 2 | 0" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Allegoric Alaskans;Blithering Badgers;win', 'Allegoric Alaskans;Blithering Badgers;win']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Allegoric Alaskans | 2 | 2 | 0 | 0 | 6', 'Blithering Badgers | 2 | 0 | 0 | 2 | 0']" } @test "There can be more than one winner" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="\ -Allegoric Alaskans;Blithering Badgers;loss -Allegoric Alaskans;Blithering Badgers;win" - - expected="\ -Team | MP | W | D | L | P -Allegoric Alaskans | 2 | 1 | 0 | 1 | 3 -Blithering Badgers | 2 | 1 | 0 | 1 | 3" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Allegoric Alaskans;Blithering Badgers;loss', 'Allegoric Alaskans;Blithering Badgers;win']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Allegoric Alaskans | 2 | 1 | 0 | 1 | 3', 'Blithering Badgers | 2 | 1 | 0 | 1 | 3']" } @test "There can be more than two teams" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="\ -Allegoric Alaskans;Blithering Badgers;win -Blithering Badgers;Courageous Californians;win -Courageous Californians;Allegoric Alaskans;loss" - - expected="\ -Team | MP | W | D | L | P -Allegoric Alaskans | 2 | 2 | 0 | 0 | 6 -Blithering Badgers | 2 | 1 | 0 | 1 | 3 -Courageous Californians | 2 | 0 | 0 | 2 | 0" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Allegoric Alaskans;Blithering Badgers;win', 'Blithering Badgers;Courageous Californians;win', 'Courageous Californians;Allegoric Alaskans;loss']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Allegoric Alaskans | 2 | 2 | 0 | 0 | 6', 'Blithering Badgers | 2 | 1 | 0 | 1 | 3', 'Courageous Californians | 2 | 0 | 0 | 2 | 0']" } @test "typical input" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="\ -Allegoric Alaskans;Blithering Badgers;win -Devastating Donkeys;Courageous Californians;draw -Devastating Donkeys;Allegoric Alaskans;win -Courageous Californians;Blithering Badgers;loss -Blithering Badgers;Devastating Donkeys;loss -Allegoric Alaskans;Courageous Californians;win" - - expected="\ -Team | MP | W | D | L | P -Devastating Donkeys | 3 | 2 | 1 | 0 | 7 -Allegoric Alaskans | 3 | 2 | 0 | 1 | 6 -Blithering Badgers | 3 | 1 | 0 | 2 | 3 -Courageous Californians | 3 | 0 | 1 | 2 | 1" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Allegoric Alaskans;Blithering Badgers;win', 'Devastating Donkeys;Courageous Californians;draw', 'Devastating Donkeys;Allegoric Alaskans;win', 'Courageous Californians;Blithering Badgers;loss', 'Blithering Badgers;Devastating Donkeys;loss', 'Allegoric Alaskans;Courageous Californians;win']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Devastating Donkeys | 3 | 2 | 1 | 0 | 7', 'Allegoric Alaskans | 3 | 2 | 0 | 1 | 6', 'Blithering Badgers | 3 | 1 | 0 | 2 | 3', 'Courageous Californians | 3 | 0 | 1 | 2 | 1']" } @test "incomplete competition (not all pairs have played)" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="\ -Allegoric Alaskans;Blithering Badgers;loss -Devastating Donkeys;Allegoric Alaskans;loss -Courageous Californians;Blithering Badgers;draw -Allegoric Alaskans;Courageous Californians;win" - - expected="\ -Team | MP | W | D | L | P -Allegoric Alaskans | 3 | 2 | 0 | 1 | 6 -Blithering Badgers | 2 | 1 | 1 | 0 | 4 -Courageous Californians | 2 | 0 | 1 | 1 | 1 -Devastating Donkeys | 1 | 0 | 0 | 1 | 0" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Allegoric Alaskans;Blithering Badgers;loss', 'Devastating Donkeys;Allegoric Alaskans;loss', 'Courageous Californians;Blithering Badgers;draw', 'Allegoric Alaskans;Courageous Californians;win']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Allegoric Alaskans | 3 | 2 | 0 | 1 | 6', 'Blithering Badgers | 2 | 1 | 1 | 0 | 4', 'Courageous Californians | 2 | 0 | 1 | 1 | 1', 'Devastating Donkeys | 1 | 0 | 0 | 1 | 0']" } @test "ties broken alphabetically" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="\ -Courageous Californians;Devastating Donkeys;win -Allegoric Alaskans;Blithering Badgers;win -Devastating Donkeys;Allegoric Alaskans;loss -Courageous Californians;Blithering Badgers;win -Blithering Badgers;Devastating Donkeys;draw -Allegoric Alaskans;Courageous Californians;draw" - - expected="\ -Team | MP | W | D | L | P -Allegoric Alaskans | 3 | 2 | 1 | 0 | 7 -Courageous Californians | 3 | 2 | 1 | 0 | 7 -Blithering Badgers | 3 | 0 | 1 | 2 | 1 -Devastating Donkeys | 3 | 0 | 1 | 2 | 1" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Courageous Californians;Devastating Donkeys;win', 'Allegoric Alaskans;Blithering Badgers;win', 'Devastating Donkeys;Allegoric Alaskans;loss', 'Courageous Californians;Blithering Badgers;win', 'Blithering Badgers;Devastating Donkeys;draw', 'Allegoric Alaskans;Courageous Californians;draw']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Allegoric Alaskans | 3 | 2 | 1 | 0 | 7', 'Courageous Californians | 3 | 2 | 1 | 0 | 7', 'Blithering Badgers | 3 | 0 | 1 | 2 | 1', 'Devastating Donkeys | 3 | 0 | 1 | 2 | 1']" } @test "ensure points sorted numerically" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - input="\ -Devastating Donkeys;Blithering Badgers;win -Devastating Donkeys;Blithering Badgers;win -Devastating Donkeys;Blithering Badgers;win -Devastating Donkeys;Blithering Badgers;win -Blithering Badgers;Devastating Donkeys;win" - - expected="\ -Team | MP | W | D | L | P -Devastating Donkeys | 5 | 4 | 0 | 1 | 12 -Blithering Badgers | 5 | 1 | 0 | 4 | 3" - - assert_in_to_out "$input" "$expected" + run gawk -f tournament.awk <<< "['Devastating Donkeys;Blithering Badgers;win', 'Devastating Donkeys;Blithering Badgers;win', 'Devastating Donkeys;Blithering Badgers;win', 'Devastating Donkeys;Blithering Badgers;win', 'Blithering Badgers;Devastating Donkeys;win']" + assert_success + assert_output "['Team | MP | W | D | L | P', 'Devastating Donkeys | 5 | 4 | 0 | 1 | 12', 'Blithering Badgers | 5 | 1 | 0 | 4 | 3']" } + diff --git a/exercises/practice/triangle/.meta/template.j2 b/exercises/practice/triangle/.meta/template.j2 index e6a55625..ed2f090b 100644 --- a/exercises/practice/triangle/.meta/template.j2 +++ b/exercises/practice/triangle/.meta/template.j2 @@ -4,6 +4,6 @@ {% if loop.first %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip run gawk -f {{ solution }} -v type={{ case["property"] }} <<< "{{ case["input"]["sides"] | join(" ") }}" assert_success - assert_output "{{ case["expected"] }}" + assert_output {{ case["expected"] | quote }} } {% endfor %} diff --git a/exercises/practice/two-fer/test-two-fer.bats b/exercises/practice/two-fer/test-two-fer.bats index 00398778..64882bbe 100644 --- a/exercises/practice/two-fer/test-two-fer.bats +++ b/exercises/practice/two-fer/test-two-fer.bats @@ -1,54 +1,26 @@ #!/usr/bin/env bats load bats-extra -setup() { - echo > empty.txt - echo "Alice" > one.txt -} +# generated on 2026-11-04T19:33:58+00:00 -teardown() { - rm empty.txt one.txt -} - - -@test "no file given" { - #[[ $BATS_RUN_SKIPPED == "true" ]] || skip - - # The above line controls whether to skip the test. - # Normally, we skip every test except for the first one - # (the first one is always commented out). This allows for - # a person to focus on solving a test at a time: you can - # comment out or delete the - # `[[ $BATS_RUN_SKIPPED == "true" ]] || skip` - # line to run the test when you are ready. - # - # You can also run all the tests by setting the - # `$BATS_RUN_SKIPPED` environment variable, like this: - # - # $ BATS_RUN_SKIPPED=true bats test-two-fer.bats - - run gawk -f two-fer.awk < /dev/null - assert_success - assert_output "One for you, one for me." -} - -@test "empty file given" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f two-fer.awk empty.txt - assert_success - assert_output "One for you, one for me." +@test "no name given" { + # [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run gawk -f two-fer.awk <<< "None" + assert_success + assert_output "One for you, one for me." } @test "a name given" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f two-fer.awk one.txt - assert_success - assert_output "One for Alice, one for me." + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run gawk -f two-fer.awk <<< "Alice" + assert_success + assert_output "One for Alice, one for me." } -@test "name with a space" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f two-fer.awk <<< "Mary Ann" - assert_success - assert_output "One for Mary Ann, one for me." +@test "another name given" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run gawk -f two-fer.awk <<< "Bob" + assert_success + assert_output "One for Bob, one for me." } + diff --git a/exercises/practice/variable-length-quantity/test-variable-length-quantity.bats b/exercises/practice/variable-length-quantity/test-variable-length-quantity.bats index dd4ed13e..e308e22e 100644 --- a/exercises/practice/variable-length-quantity/test-variable-length-quantity.bats +++ b/exercises/practice/variable-length-quantity/test-variable-length-quantity.bats @@ -1,236 +1,222 @@ #!/usr/bin/env bats load bats-extra -# -# *** Input and Output numbers are expressed in hexadecimal. -# +# generated on 2026-11-04T19:33:58+00:00 -# Encode a series of integers, producing a series of bytes. - -@test "encode zero" { - #[[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "00" - assert_success - assert_output "00" -} - -@test "encode arbitrary single byte" { - [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "40" +@test "zero" { + # [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run gawk -f variable-length-quantity.awk <<< "[0]" assert_success - assert_output "40" + assert_output "[0]" } -@test "encode asymmetric single byte" { +@test "arbitrary single byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "53" + run gawk -f variable-length-quantity.awk <<< "[64]" assert_success - assert_output "53" + assert_output "[64]" } -@test "encode largest single byte" { +@test "asymmetric single byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "7F" + run gawk -f variable-length-quantity.awk <<< "[83]" assert_success - assert_output "7F" + assert_output "[83]" } -@test "encode smallest double byte" { +@test "largest single byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "80" + run gawk -f variable-length-quantity.awk <<< "[127]" assert_success - assert_output "81 00" + assert_output "[127]" } -@test "encode arbitrary double byte" { +@test "smallest double byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "2000" + run gawk -f variable-length-quantity.awk <<< "[128]" assert_success - assert_output "C0 00" + assert_output "[129, 0]" } -@test "encode asymmetric double byte" { +@test "arbitrary double byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "AD" + run gawk -f variable-length-quantity.awk <<< "[8192]" assert_success - assert_output "81 2D" + assert_output "[192, 0]" } -@test "encode largest double byte" { +@test "asymmetric double byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "3FFF" + run gawk -f variable-length-quantity.awk <<< "[173]" assert_success - assert_output "FF 7F" + assert_output "[129, 45]" } -@test "encode smallest triple byte" { +@test "largest double byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "4000" + run gawk -f variable-length-quantity.awk <<< "[16383]" assert_success - assert_output "81 80 00" + assert_output "[255, 127]" } -@test "encode arbitrary triple byte" { +@test "smallest triple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "100000" + run gawk -f variable-length-quantity.awk <<< "[16384]" assert_success - assert_output "C0 80 00" + assert_output "[129, 128, 0]" } -@test "encode asymmetric triple byte" { +@test "arbitrary triple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "1D59C" + run gawk -f variable-length-quantity.awk <<< "[1048576]" assert_success - assert_output "87 AB 1C" + assert_output "[192, 128, 0]" } -@test "encode largest triple byte" { +@test "asymmetric triple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "1FFFFF" + run gawk -f variable-length-quantity.awk <<< "[120220]" assert_success - assert_output "FF FF 7F" + assert_output "[135, 171, 28]" } -@test "encode smallest quadruple byte" { +@test "largest triple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "200000" + run gawk -f variable-length-quantity.awk <<< "[2097151]" assert_success - assert_output "81 80 80 00" + assert_output "[255, 255, 127]" } -@test "encode arbitrary quadruple byte" { +@test "smallest quadruple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "8000000" + run gawk -f variable-length-quantity.awk <<< "[2097152]" assert_success - assert_output "C0 80 80 00" + assert_output "[129, 128, 128, 0]" } -@test "encode asymmetric quadruple byte" { +@test "arbitrary quadruple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "357704" + run gawk -f variable-length-quantity.awk <<< "[134217728]" assert_success - assert_output "81 D5 EE 04" + assert_output "[192, 128, 128, 0]" } -@test "encode largest quadruple byte" { +@test "asymmetric quadruple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "FFFFFFF" + run gawk -f variable-length-quantity.awk <<< "[3503876]" assert_success - assert_output "FF FF FF 7F" + assert_output "[129, 213, 238, 4]" } -@test "encode smallest quintuple byte" { +@test "largest quadruple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "10000000" + run gawk -f variable-length-quantity.awk <<< "[268435455]" assert_success - assert_output "81 80 80 80 00" + assert_output "[255, 255, 255, 127]" } -@test "encode arbitrary quintuple byte" { +@test "smallest quintuple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "FF000000" + run gawk -f variable-length-quantity.awk <<< "[268435456]" assert_success - assert_output "8F F8 80 80 00" + assert_output "[129, 128, 128, 128, 0]" } -@test "encode asymmetric quintuple byte" { +@test "arbitrary quintuple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "86656105" + run gawk -f variable-length-quantity.awk <<< "[4278190080]" assert_success - assert_output "88 B3 95 C2 05" + assert_output "[143, 248, 128, 128, 0]" } -@test "encode maximum 32-bit integer input" { +@test "asymmetric quintuple byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "FFFFFFFF" + run gawk -f variable-length-quantity.awk <<< "[2254790917]" assert_success - assert_output "8F FF FF FF 7F" + assert_output "[136, 179, 149, 194, 5]" } -@test "encode two single-byte values" { +@test "maximum 32-bit integer input" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "40 7F" + run gawk -f variable-length-quantity.awk <<< "[4294967295]" assert_success - assert_output "40 7F" + assert_output "[143, 255, 255, 255, 127]" } -@test "encode two multi-byte values" { +@test "two single-byte values" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "4000 123456" + run gawk -f variable-length-quantity.awk <<< "[64, 127]" assert_success - assert_output "81 80 00 C8 E8 56" + assert_output "[64, 127]" } -@test "encode many multi-byte values" { +@test "two multi-byte values" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=encode <<< "2000 123456 FFFFFFF 00 3FFF 4000" + run gawk -f variable-length-quantity.awk <<< "[16384, 1193046]" assert_success - assert_output "C0 00 C8 E8 56 FF FF FF 7F 00 FF 7F 81 80 00" + assert_output "[129, 128, 0, 200, 232, 86]" } -# Decode a series of bytes, producing a series of integers. - -@test "decode one byte" { +@test "many multi-byte values" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=decode <<< "7F" + run gawk -f variable-length-quantity.awk <<< "[8192, 1193046, 268435455, 0, 16383, 16384]" assert_success - assert_output "7F" + assert_output "[192, 0, 200, 232, 86, 255, 255, 255, 127, 0, 255, 127, 129, 128, 0]" } -@test "decode two bytes" { +@test "one byte" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=decode <<< "C0 00" + run gawk -f variable-length-quantity.awk <<< "[127]" assert_success - assert_output "2000" + assert_output "[127]" } -@test "decode three bytes" { +@test "two bytes" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=decode <<< "FF FF 7F" + run gawk -f variable-length-quantity.awk <<< "[192, 0]" assert_success - assert_output "1FFFFF" + assert_output "[8192]" } -@test "decode four bytes" { +@test "three bytes" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=decode <<< "81 80 80 00" + run gawk -f variable-length-quantity.awk <<< "[255, 255, 127]" assert_success - assert_output "200000" + assert_output "[2097151]" } -@test "decode maximum 32-bit integer" { +@test "four bytes" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=decode <<< "8F FF FF FF 7F" + run gawk -f variable-length-quantity.awk <<< "[129, 128, 128, 0]" assert_success - assert_output "FFFFFFFF" + assert_output "[2097152]" } -@test "decode multiple values" { +@test "maximum 32-bit integer" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=decode <<< "C0 00 C8 E8 56 FF FF FF 7F 00 FF 7F 81 80 00" + run gawk -f variable-length-quantity.awk <<< "[143, 255, 255, 255, 127]" assert_success - assert_output "2000 123456 FFFFFFF 00 3FFF 4000" + assert_output "[4294967295]" } -# Some error conditions - -@test "decode incomplete sequence causes error" { +@test "incomplete sequence causes error" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=decode <<< "FF" + run gawk -f variable-length-quantity.awk <<< "[255]" assert_failure - assert_output --partial "incomplete byte sequence" + assert_output "incomplete sequence" } -@test "decode incomplete sequence causes error, even if value is zero" { +@test "incomplete sequence causes error, even if value is zero" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=decode <<< "80" + run gawk -f variable-length-quantity.awk <<< "[128]" assert_failure - assert_output --partial "incomplete byte sequence" + assert_output "incomplete sequence" } -@test "invalid subcommand" { +@test "multiple values" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - run gawk -f variable-length-quantity.awk -v action=hello <<< "80" - assert_failure - assert_output --partial "unknown action" + run gawk -f variable-length-quantity.awk <<< "[192, 0, 200, 232, 86, 255, 255, 255, 127, 0, 255, 127, 129, 128, 0]" + assert_success + assert_output "[8192, 1193046, 268435455, 0, 16383, 16384]" } +