diff --git a/.github/workflows/solve.yaml b/.github/workflows/solve.yaml index 944d306..b3c62da 100644 --- a/.github/workflows/solve.yaml +++ b/.github/workflows/solve.yaml @@ -15,6 +15,8 @@ jobs: distribution: 'corretto' # See 'Supported distributions' for available options java-version: '21' - uses: https://github.com/fwilhe2/setup-kotlin@main + - name: Run tests + run: ./simpletest.sh - name: Solutions run: | set -e diff --git a/day01.kts b/day01.kts old mode 100644 new mode 100755 index 7cc4c52..6e0b093 --- a/day01.kts +++ b/day01.kts @@ -1,3 +1,4 @@ +#!/usr/bin/env kotlin import java.io.File import java.util.Scanner import kotlin.math.abs diff --git a/day01.kts.testvalue b/day01.kts.testvalue new file mode 100644 index 0000000..95be46d --- /dev/null +++ b/day01.kts.testvalue @@ -0,0 +1,2 @@ +Total distance: 1603498 +Similarity score: 25574739 diff --git a/day02.kts b/day02.kts old mode 100644 new mode 100755 index 36a0369..094a90d --- a/day02.kts +++ b/day02.kts @@ -1,3 +1,4 @@ +#!/usr/bin/env kotlin import java.io.File import java.util.Scanner import kotlin.math.abs diff --git a/day02.kts.testvalue b/day02.kts.testvalue new file mode 100644 index 0000000..e543821 --- /dev/null +++ b/day02.kts.testvalue @@ -0,0 +1,2 @@ +Safe lines: 472 +Safe lines with dampener: 520 diff --git a/day03.kts b/day03.kts old mode 100644 new mode 100755 index 5e51d18..d9252a5 --- a/day03.kts +++ b/day03.kts @@ -1,3 +1,4 @@ +#!/usr/bin/env kotlin import java.io.File import java.util.Scanner diff --git a/day03.kts.testvalue b/day03.kts.testvalue new file mode 100644 index 0000000..b5d5008 --- /dev/null +++ b/day03.kts.testvalue @@ -0,0 +1,2 @@ +Part One Sum: 187833789 +Part Two Sum: 94455185 diff --git a/simpletest.sh b/simpletest.sh new file mode 100755 index 0000000..ab7231b --- /dev/null +++ b/simpletest.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +# Function to show usage +usage() { + echo "Usage: $0 [write|validate]" + exit 1 +} + +# Ensure a mode is provided +if [[ $# -ne 1 ]]; then + usage +fi + +MODE=$1 + +# Write mode +if [[ "$MODE" == "write" ]]; then + echo "Running in write mode..." + + for script in *.kts; do + test_file="${script}.testvalue" + + echo "Processing $script..." + output=$(kotlin "$script" 2>&1) + + if [[ -f "$test_file" ]]; then + if [[ "$output" == "$(cat "$test_file")" ]]; then + echo "No change: $script" + else + echo "Output changed for $script" + diff -u "$test_file" <(echo "$output") + read -p "Update test value? (y/n) " update + if [[ "$update" == "y" ]]; then + echo "$output" > "$test_file" + echo "Updated $test_file" + fi + fi + else + echo "$output" > "$test_file" + echo "Created $test_file" + fi + done + + for test_file in *.kts.testvalue; do + script="${test_file%.testvalue}" + if [[ ! -f "$script" ]]; then + echo "Test file $test_file does not have a matching script." + read -p "Delete $test_file? (y/n) " delete + if [[ "$delete" == "y" ]]; then + rm "$test_file" + echo "Deleted $test_file" + fi + fi + done + +# Validate mode +elif [[ "$MODE" == "validate" ]]; then + echo "Running in validate mode..." + pass=0 + fail=0 + + for test_file in *.kts.testvalue; do + script="${test_file%.testvalue}" + if [[ -f "$script" ]]; then + output=$(kotlin "$script" 2>&1) + if [[ "$output" == "$(cat "$test_file")" ]]; then + echo "PASS: $script" + ((pass++)) + else + echo "FAIL: $script" + diff -u "$test_file" <(echo "$output") + ((fail++)) + fi + else + echo "Orphaned test file: $test_file (no matching script)" + ((fail++)) + fi + done + + echo "Summary: $pass pass, $fail fail" + + if [[ $fail -gt 0 ]]; then + exit 1 + else + exit 0 + fi + +else + usage +fi