diff --git a/bash/README.md b/bash/README.md new file mode 100644 index 00000000..825e530c --- /dev/null +++ b/bash/README.md @@ -0,0 +1,40 @@ +# Requirements + +`bash` and friends (`diff`, `grep`, `cat`) + +# (Failing) Unit Test + +```shell +./unit_test.sh +``` + +# Texttest Fixture + +```shell +./texttest_fixture.sh +``` + +Specify days: + +```shell +./texttest_fixture.sh 30 +``` + +Verify againt `ThirtyDays/stdout.gr` + +```shell +./verify.sh +``` + +## BTW + +BTW, the script is a pure "function", so this works: + +```shell +$ echo -e 'Aged Brie|3|5\nOther Item|4|5' | +> ./gilded_rose.sh | +> ./gilded_rose.sh | +> ./gilded_rose.sh +Aged Brie|0|8 +Other Item|1|2 +``` diff --git a/bash/gilded_rose.sh b/bash/gilded_rose.sh new file mode 100755 index 00000000..4b723399 --- /dev/null +++ b/bash/gilded_rose.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +update_quality() { + local IFS='|' + + while read -r name sell_in quality; do + if [[ $name != "Aged Brie" && $name != "Backstage passes to a TAFKAL80ETC concert" ]]; then + if ((quality > 0)); then + if [[ $name != "Sulfuras, Hand of Ragnaros" ]]; then + quality=$((quality - 1)) + fi + fi + else + if ((quality < 50)); then + quality=$((quality + 1)) + + if [[ $name == "Backstage passes to a TAFKAL80ETC concert" ]]; then + if ((sell_in < 11)); then + if ((quality < 50)); then + quality=$((quality + 1)) + fi + fi + if ((sell_in < 6)); then + if ((quality < 50)); then + quality=$((quality + 1)) + fi + fi + fi + fi + fi + + if [[ $name != "Sulfuras, Hand of Ragnaros" ]]; then + sell_in=$((sell_in - 1)) + fi + + if ((sell_in < 0)); then + if [[ $name != "Aged Brie" ]]; then + if [[ $name != "Backstage passes to a TAFKAL80ETC concert" ]]; then + if ((quality > 0)); then + if [[ $name != "Sulfuras, Hand of Ragnaros" ]]; then + quality=$((quality - 1)) + fi + fi + else + quality=$((quality - quality)) + fi + else + if ((quality < 50)); then + quality=$((quality + 1)) + fi + fi + fi + + echo "$name|$sell_in|$quality" + done +} + +update_quality diff --git a/bash/texttest_fixture.sh b/bash/texttest_fixture.sh new file mode 100755 index 00000000..690c9833 --- /dev/null +++ b/bash/texttest_fixture.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +GILDED_ROSE_SCRIPT="./gilded_rose.sh" + +create_initial_items() { + local temp_file=$(mktemp) + + cat <"$temp_file" ++5 Dexterity Vest|10|20 +Aged Brie|2|0 +Elixir of the Mongoose|5|7 +Sulfuras, Hand of Ragnaros|0|80 +Sulfuras, Hand of Ragnaros|-1|80 +Backstage passes to a TAFKAL80ETC concert|15|20 +Backstage passes to a TAFKAL80ETC concert|10|49 +Backstage passes to a TAFKAL80ETC concert|5|49 +Conjured Mana Cake|3|6 +EOF + + echo "$temp_file" +} + +simulate_days() { + local days=$1 + local items_file=$2 + + for ((day = 0; day <= days; day++)); do + echo "-------- day $day --------" + echo "name, sellIn, quality" + + cat "$items_file" | sed 's/|/, /g' + + local temp_output=$(mktemp) + cat "$items_file" | bash "$GILDED_ROSE_SCRIPT" >"$temp_output" + mv "$temp_output" "$items_file" + + echo "" + done +} + +echo OMGHAI! + +ITEMS_FILE=$(create_initial_items) + +DAYS_TO_SIMULATE=${1:-2} + +simulate_days $DAYS_TO_SIMULATE $ITEMS_FILE + +rm "$ITEMS_FILE" diff --git a/bash/unit_test.sh b/bash/unit_test.sh new file mode 100755 index 00000000..34cf29d8 --- /dev/null +++ b/bash/unit_test.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +GILDED_ROSE_SCRIPT="./gilded_rose.sh" + +get_name() { + grep -o '^[^|]*' +} + +assert_equals() { + local expected="$1" + diff -u - <(echo "$expected") +} + +test_foo() { + echo "foo|0|0" | + bash "$GILDED_ROSE_SCRIPT" | + get_name | + assert_equals "fixme" +} + +test_foo diff --git a/bash/verify.sh b/bash/verify.sh new file mode 100755 index 00000000..ad9d95e5 --- /dev/null +++ b/bash/verify.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +./texttest_fixture.sh 30 | + diff -u - ../texttests/ThirtyDays/stdout.gr && + echo "✅ looks good" || + (echo "❌ failed" && exit 1)