From f7e9571ba57ea6d6e08d20b563d87ce89b10945c Mon Sep 17 00:00:00 2001 From: Isaac Good Date: Tue, 30 Jun 2026 16:05:28 -0700 Subject: [PATCH] [matrix] Add a Jinja test template --- exercises/practice/matrix/.meta/template.j2 | 54 +++++++++++++ exercises/practice/matrix/test-matrix.bats | 85 ++++++++------------- 2 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 exercises/practice/matrix/.meta/template.j2 diff --git a/exercises/practice/matrix/.meta/template.j2 b/exercises/practice/matrix/.meta/template.j2 new file mode 100644 index 00000000..e97b9d06 --- /dev/null +++ b/exercises/practice/matrix/.meta/template.j2 @@ -0,0 +1,54 @@ +{{ header }} +teardown() { + rm input.txt +} +{% for idx, case in cases %} +@test "{{ case["description"] }}" { + {% if idx == 0 %}# {% endif %}[[ $BATS_RUN_SKIPPED == "true" ]] || skip + printf "%s\n" ${{ case["input"]["string"] | repr }} > input.txt + + run gawk ' + @include "matrix" + BEGIN { + matrix::read("input.txt", mtx) + print matrix::{{ case["property"] }}(mtx, {{ case["input"]["index"] }}) + } + ' + assert_success + assert_output "{{ case["expected"] | join(" ") }}" +} +{% endfor %} +@test "can extract row from non-square matrix" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + + printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt + + run gawk ' + @include "matrix" + BEGIN { + matrix::read("input.txt", mtx) + print matrix::row(mtx, 3) + } + ' + + assert_success + assert_output "7 8 9" +} + +@test "can extract column from non-square matrix" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + + printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt + + run gawk ' + @include "matrix" + BEGIN { + matrix::read("input.txt", mtx) + print matrix::column(mtx, 3) + } + ' + + assert_success + assert_output "3 6 9 6" +} + diff --git a/exercises/practice/matrix/test-matrix.bats b/exercises/practice/matrix/test-matrix.bats index e71f379f..54c98670 100644 --- a/exercises/practice/matrix/test-matrix.bats +++ b/exercises/practice/matrix/test-matrix.bats @@ -1,16 +1,14 @@ #!/usr/bin/env bats load bats-extra +# generated on 2026-06-30T23:03:29+00:00 teardown() { rm input.txt } -# row tests - @test "extract row from one number matrix" { - #[[ $BATS_RUN_SKIPPED == "true" ]] || skip - - printf "%s\n" "1" > input.txt + # [[ $BATS_RUN_SKIPPED == "true" ]] || skip + printf "%s\n" $'1' > input.txt run gawk ' @include "matrix" @@ -19,15 +17,13 @@ teardown() { print matrix::row(mtx, 1) } ' - assert_success assert_output "1" } @test "can extract row" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - printf "%s\n" "1 2" "3 4" > input.txt + printf "%s\n" $'1 2\n3 4' > input.txt run gawk ' @include "matrix" @@ -36,15 +32,13 @@ teardown() { print matrix::row(mtx, 2) } ' - assert_success assert_output "3 4" } @test "extract row where numbers have different widths" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - printf "%s\n" "1 2" "10 20" > input.txt + printf "%s\n" $'1 2\n10 20' > input.txt run gawk ' @include "matrix" @@ -53,128 +47,115 @@ teardown() { print matrix::row(mtx, 2) } ' - assert_success assert_output "10 20" } -@test "can extract row from non-square matrix" { +@test "can extract row from non-square matrix with no corresponding column" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - printf "%s\n" "1 2 3" "4 5 6" "7 8 9" "8 7 6" > input.txt + printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt run gawk ' @include "matrix" BEGIN { matrix::read("input.txt", mtx) - print matrix::row(mtx, 3) + print matrix::row(mtx, 4) } ' - assert_success - assert_output "7 8 9" + assert_output "8 7 6" } -@test "can extract row from non-square matrix with no corresponding column" { +@test "extract column from one number matrix" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - printf "%s\n" "1 2 3" "4 5 6" "7 8 9" "8 7 6" > input.txt + printf "%s\n" $'1' > input.txt run gawk ' @include "matrix" BEGIN { matrix::read("input.txt", mtx) - print matrix::row(mtx, 4) + print matrix::column(mtx, 1) } ' - assert_success - assert_output "8 7 6" + assert_output "1" } -# column tests - -@test "extract column from one number matrix" { +@test "can extract column" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - printf "%s\n" "1" > input.txt + printf "%s\n" $'1 2 3\n4 5 6\n7 8 9' > input.txt run gawk ' @include "matrix" BEGIN { matrix::read("input.txt", mtx) - print matrix::column(mtx, 1) + print matrix::column(mtx, 3) } ' - assert_success - assert_output "1" + assert_output "3 6 9" } -@test "can extract column" { +@test "can extract column from non-square matrix with no corresponding row" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - printf "%s\n" "1 2 3" "4 5 6" "7 8 9" > input.txt + printf "%s\n" $'1 2 3 4\n5 6 7 8\n9 8 7 6' > input.txt run gawk ' @include "matrix" BEGIN { matrix::read("input.txt", mtx) - print matrix::column(mtx, 3) + print matrix::column(mtx, 4) } ' - assert_success - assert_output "3 6 9" + assert_output "4 8 6" } -@test "can extract column from non-square matrix" { +@test "extract column where numbers have different widths" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - - printf "%s\n" "1 2 3" "4 5 6" "7 8 9" "8 7 6" > input.txt + printf "%s\n" $'89 1903 3\n18 3 1\n9 4 800' > input.txt run gawk ' @include "matrix" BEGIN { matrix::read("input.txt", mtx) - print matrix::column(mtx, 3) + print matrix::column(mtx, 2) } ' - assert_success - assert_output "3 6 9 6" + assert_output "1903 3 4" } -@test "can extract column from non-square matrix with no corresponding row" { +@test "can extract row from non-square matrix" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - printf "%s\n" "1 2 3 4" "5 6 7 8" "9 8 7 6" > input.txt + printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt run gawk ' @include "matrix" BEGIN { matrix::read("input.txt", mtx) - print matrix::column(mtx, 4) + print matrix::row(mtx, 3) } ' assert_success - assert_output "4 8 6" + assert_output "7 8 9" } -@test "extract column where numbers have different widths" { +@test "can extract column from non-square matrix" { [[ $BATS_RUN_SKIPPED == "true" ]] || skip - printf "%s\n" "89 1903 3" "18 3 1" "9 4 800" > input.txt + printf "%s\n" $'1 2 3\n4 5 6\n7 8 9\n8 7 6' > input.txt run gawk ' @include "matrix" BEGIN { matrix::read("input.txt", mtx) - print matrix::column(mtx, 2) + print matrix::column(mtx, 3) } ' assert_success - assert_output "1903 3 4" + assert_output "3 6 9 6" }