From bee8f54440d588d452c9db888aa2f316104c5535 Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Sat, 23 Dec 2023 17:26:19 +0100 Subject: [PATCH 1/7] bash: initial impl --- bash/gilded_rose.sh | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 bash/gilded_rose.sh diff --git a/bash/gilded_rose.sh b/bash/gilded_rose.sh new file mode 100755 index 00000000..d1d32b34 --- /dev/null +++ b/bash/gilded_rose.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +update_quality() { + local IFS=',' + + while read -r name sell_in_str quality_str; do + declare -i sell_in="$sell_in_str" + declare -i quality="$quality_str" + + if [[ $name != "Aged Brie" && $name != "Backstage passes to a TAFKAL80ETC concert" ]]; then + if ((quality > 0)); then + if [[ $name != "Sulfuras, Hand of Ragnaros" ]]; then + ((quality--)) + fi + fi + else + if ((quality < 50)); then + ((quality++)) + + if [[ $name == "Backstage passes to a TAFKAL80ETC concert" ]]; then + if ((sell_in < 11)) && ((quality < 50)); then + ((quality++)) + fi + if ((sell_in < 6)) && ((quality < 50)); then + ((quality++)) + fi + fi + fi + fi + + if [[ $name != "Sulfuras, Hand of Ragnaros" ]]; then + ((sell_in--)) + + 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--)) + fi + fi + else + quality=0 + fi + else + if ((quality < 50)); then + ((quality++)) + fi + fi + fi + fi + + echo "$name,$sell_in,$quality" + done +} + +update_quality From 9039daa7be87e8238fb4528e3c860e151bb46304 Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Sat, 23 Dec 2023 17:29:46 +0100 Subject: [PATCH 2/7] . d README.md notes --- bash/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 bash/README.md diff --git a/bash/README.md b/bash/README.md new file mode 100644 index 00000000..de4024d7 --- /dev/null +++ b/bash/README.md @@ -0,0 +1,10 @@ +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 +``` \ No newline at end of file From 56ca5bf41148b63f1b6b6d2e47eff0f0c6da1766 Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Sat, 23 Dec 2023 17:56:14 +0100 Subject: [PATCH 3/7] - t verify results --- bash/gilded_rose.sh | 4 ++-- bash/texttest_fixture.sh | 49 ++++++++++++++++++++++++++++++++++++++++ bash/verify.sh | 3 +++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100755 bash/texttest_fixture.sh create mode 100755 bash/verify.sh diff --git a/bash/gilded_rose.sh b/bash/gilded_rose.sh index d1d32b34..cf4aef82 100755 --- a/bash/gilded_rose.sh +++ b/bash/gilded_rose.sh @@ -1,7 +1,7 @@ #!/bin/bash update_quality() { - local IFS=',' + local IFS='|' while read -r name sell_in_str quality_str; do declare -i sell_in="$sell_in_str" @@ -50,7 +50,7 @@ update_quality() { fi fi - echo "$name,$sell_in,$quality" + echo "$name|$sell_in|$quality" done } 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/verify.sh b/bash/verify.sh new file mode 100755 index 00000000..561af69d --- /dev/null +++ b/bash/verify.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +./texttest_fixture.sh 30 | diff -u - ../texttests/ThirtyDays/stdout.gr From 4490aaa5fd2cc878732ca12e860c2d60fd20fee7 Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Sat, 23 Dec 2023 18:18:38 +0100 Subject: [PATCH 4/7] - t failing unit test --- bash/README.md | 34 ++++++++++++++++++++++++++++++++-- bash/unit_test.sh | 21 +++++++++++++++++++++ bash/verify.sh | 5 ++++- 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100755 bash/unit_test.sh diff --git a/bash/README.md b/bash/README.md index de4024d7..07d0d6a7 100644 --- a/bash/README.md +++ b/bash/README.md @@ -1,4 +1,34 @@ -A pure "function", so this works: +# 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' |\ @@ -7,4 +37,4 @@ $ echo -e 'Aged Brie,3,5\nOther Item,4,5' |\ ./gilded_rose.sh Aged Brie,0,8 Other Item,1,2 -``` \ No newline at end of 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 index 561af69d..ad9d95e5 100755 --- a/bash/verify.sh +++ b/bash/verify.sh @@ -1,3 +1,6 @@ #!/bin/bash -./texttest_fixture.sh 30 | diff -u - ../texttests/ThirtyDays/stdout.gr +./texttest_fixture.sh 30 | + diff -u - ../texttests/ThirtyDays/stdout.gr && + echo "✅ looks good" || + (echo "❌ failed" && exit 1) From 887ad0b60da04e2d40b1ecb53a3c3fd525cb9abb Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Sat, 23 Dec 2023 19:46:03 +0100 Subject: [PATCH 5/7] . d README: update field delimiter --- bash/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bash/README.md b/bash/README.md index 07d0d6a7..825e530c 100644 --- a/bash/README.md +++ b/bash/README.md @@ -31,10 +31,10 @@ Verify againt `ThirtyDays/stdout.gr` 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 +$ 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 ``` From 7658124173cfa1236200aac26334e60943cc5fbe Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Tue, 26 Dec 2023 12:27:11 +0100 Subject: [PATCH 6/7] gilded_rose.sh: closer to original --- bash/gilded_rose.sh | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/bash/gilded_rose.sh b/bash/gilded_rose.sh index cf4aef82..e4ce5896 100755 --- a/bash/gilded_rose.sh +++ b/bash/gilded_rose.sh @@ -10,42 +10,46 @@ update_quality() { 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=$((quality - 1)) fi fi else if ((quality < 50)); then - ((quality++)) + quality=$((quality + 1)) if [[ $name == "Backstage passes to a TAFKAL80ETC concert" ]]; then - if ((sell_in < 11)) && ((quality < 50)); then - ((quality++)) + if ((sell_in < 11)); then + if ((quality < 50)); then + quality=$((quality + 1)) + fi fi - if ((sell_in < 6)) && ((quality < 50)); then - ((quality++)) + 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=$((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--)) - 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 - else - quality=0 fi else - if ((quality < 50)); then - ((quality++)) - fi + quality=$((quality - quality)) + fi + else + if ((quality < 50)); then + quality=$((quality + 1)) fi fi fi From 6223bf9919cb0a6794f34af5bb3266f5d4fb11f0 Mon Sep 17 00:00:00 2001 From: Nitsan Avni Date: Tue, 26 Dec 2023 18:09:52 +0100 Subject: [PATCH 7/7] - F more compact --- bash/gilded_rose.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bash/gilded_rose.sh b/bash/gilded_rose.sh index e4ce5896..4b723399 100755 --- a/bash/gilded_rose.sh +++ b/bash/gilded_rose.sh @@ -3,10 +3,7 @@ update_quality() { local IFS='|' - while read -r name sell_in_str quality_str; do - declare -i sell_in="$sell_in_str" - declare -i quality="$quality_str" - + 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