Merge pull request #509 from nitsanavni/bash

bash
This commit is contained in:
Emily Bache 2023-12-28 08:27:57 +00:00 committed by GitHub
commit a1e48be19f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 174 additions and 0 deletions

40
bash/README.md Normal file
View File

@ -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
```

58
bash/gilded_rose.sh Executable file
View File

@ -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

49
bash/texttest_fixture.sh Executable file
View File

@ -0,0 +1,49 @@
#!/bin/bash
GILDED_ROSE_SCRIPT="./gilded_rose.sh"
create_initial_items() {
local temp_file=$(mktemp)
cat <<EOF >"$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"

21
bash/unit_test.sh Executable file
View File

@ -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

6
bash/verify.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
./texttest_fixture.sh 30 |
diff -u - ../texttests/ThirtyDays/stdout.gr &&
echo "✅ looks good" ||
(echo "❌ failed" && exit 1)