Merge branch 'main' into refactor-ruby

This commit is contained in:
Peter Kofler 2024-01-17 20:15:40 +01:00 committed by GitHub
commit 7e8becaa68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 221 additions and 187 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

37
bash/texttest_fixture.sh Executable file
View File

@ -0,0 +1,37 @@
#!/bin/bash
GILDED_ROSE_SCRIPT="./gilded_rose.sh"
# Define initial items as a multi-line string
initial_items="+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"
simulate_days() {
local days=$1
local items="$2"
for ((day = 0; day <= days; day++)); do
echo "-------- day $day --------"
echo "name, sellIn, quality"
echo "$items" | sed 's/|/, /g'
# Update the items for the next day
items=$(echo "$items" | bash "$GILDED_ROSE_SCRIPT")
echo ""
done
}
echo OMGHAI!
DAYS_TO_SIMULATE=${1:-2}
simulate_days $DAYS_TO_SIMULATE "$initial_items"

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)

View File

@ -1,6 +1,6 @@
# Installs
# Requirements
[install jaq](https://github.com/01mf02/jaq#installation) (latest development version)
jq
# Failing Unit Test
@ -11,11 +11,11 @@
# TextTest Fixture
```shell
jaq -nr "$(cat gilded-rose.jq) $(cat texttest_fixture.jq)"
jq -nr "$(cat gilded-rose.jq) $(cat texttest_fixture.jq)"
```
Specify days (e.g. 10 days):
```shell
jaq --arg days 10 -nr "$(cat gilded-rose.jq) $(cat texttest_fixture.jq)"
jq --arg days 10 -nr "$(cat gilded-rose.jq) $(cat texttest_fixture.jq)"
```

View File

@ -1,54 +1,52 @@
def update_quality:
[
foreach .[] as $item (
null;
$item |
if .name != "Aged Brie" and .name != "Backstage passes to a TAFKAL80ETC concert" then
if .quality > 0 then
if .name != "Sulfuras, Hand of Ragnaros" then
.quality = .quality - 1
else . end
else . end
.[]
|
if .name != "Aged Brie" and .name != "Backstage passes to a TAFKAL80ETC concert" then
if .quality > 0 then
if .name != "Sulfuras, Hand of Ragnaros" then
.quality = .quality - 1
end
end
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
end
end
|
if .sell_in < 6 then
if .quality < 50 then
.quality = .quality + 1
end
end
end
end
end
|
if .name != "Sulfuras, Hand of Ragnaros" then
.sell_in = .sell_in - 1
end
|
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
end
end
else
.quality = .quality - .quality
end
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
else . end
else . end
|
if .sell_in < 6 then
if .quality < 50 then
.quality = .quality + 1
else . end
else . end
else . end
else . end
end
|
if .name != "Sulfuras, Hand of Ragnaros" then
.sell_in = .sell_in - 1
else . end
|
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
else . end
else . end
else
.quality = .quality - .quality
end
else
if .quality < 50 then
.quality = .quality + 1
else . end
end
else . end
)
end
end
];

View File

@ -1,3 +1,3 @@
#!/usr/bin/env bash
jaq -en "$(cat gilded-rose.jq)"'[{name:"foo",sell_in:0,quality:0}] | update_quality | .[].name == "fixme"'
jq -en "$(cat gilded-rose.jq)"'[{name:"foo",sell_in:0,quality:0}] | update_quality | .[].name == "fixme"'

View File

@ -12,7 +12,7 @@
{ name: "Conjured Mana Cake", sell_in: 3, quality: 6} # <-- :O
] |
{ items: ., day: 0 } |
recurse(.day += 1 | .items = (.items | update_quality); .day < ($ARGS.named.days // 2 | tonumber)) |
recurse(.day += 1 | .items = (.items | update_quality); .day <= ($ARGS.named.days // 2 | tonumber)) |
(
(["-------- day ", (.day | tostring), " --------"] | add),
("name, sellIn, quality"),

View File

@ -3,77 +3,9 @@ require 'rspec'
require File.join(File.dirname(__FILE__), 'gilded_rose')
describe GildedRose do
describe "#update_quality" do
it "does not change the name" do
items = [Item.new("foo", 0, 0)]
GildedRose.new(items).update_quality()
expect(items[0].name).to eq "foo"
end
it 'decreases the sell_in and quality for regular items' do
item = Item.new('Regular Item', 5, 10)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(9)
expect(item.sell_in).to eq(4)
end
it 'decreases quality twice as fast when sell_in is negative for regular items' do
item = Item.new('Regular Item', 0, 10)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(8)
end
it 'increases the quality of Aged Brie' do
item = Item.new('Aged Brie', 5, 10)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(11)
end
it 'does not increase the quality of Aged Brie beyond 50' do
item = Item.new('Aged Brie', 5, 50)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(50)
end
it 'increases the quality of Backstage passes as sell_in approaches' do
item = Item.new('Backstage passes to a TAFKAL80ETC concert', 11, 20)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(21)
end
it 'increases the quality of Backstage passes by 2 when sell_in is 10 or less' do
item = Item.new('Backstage passes to a TAFKAL80ETC concert', 10, 20)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(22)
end
it 'increases the quality of Backstage passes by 3 when sell_in is 5 or less' do
item = Item.new('Backstage passes to a TAFKAL80ETC concert', 5, 20)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(23)
end
it 'sets the quality of Backstage passes to 0 after the concert' do
item = Item.new('Backstage passes to a TAFKAL80ETC concert', 0, 20)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(0)
end
it 'does not decrease the quality of Sulfuras' do
item = Item.new('Sulfuras, Hand of Ragnaros', 5, 80)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
expect(item.quality).to eq(80)
end
it "does not change the name" do
items = [Item.new("foo", 0, 0)]
GildedRose.new(items).update_quality()
expect(items[0].name).to eq "fixme"
end
end

View File

@ -2,67 +2,9 @@ require 'minitest/autorun'
require_relative 'gilded_rose'
class TestGildedRose < Minitest::Test
def test_update_quality_regular_item
item = Item.new('Regular Item', 5, 10)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 9, item.quality
assert_equal 4, item.sell_in
end
def test_update_quality_negative_sell_in_regular_item
item = Item.new('Regular Item', 0, 10)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 8, item.quality
end
def test_update_quality_aged_brie
item = Item.new('Aged Brie', 5, 10)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 11, item.quality
end
def test_update_quality_max_quality_aged_brie
item = Item.new('Aged Brie', 5, 50)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 50, item.quality
end
def test_update_quality_backstage_passes_approaching
item = Item.new('Backstage passes to a TAFKAL80ETC concert', 11, 20)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 21, item.quality
end
def test_update_quality_backstage_passes_sell_in_10_or_less
item = Item.new('Backstage passes to a TAFKAL80ETC concert', 10, 20)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 22, item.quality
end
def test_update_quality_backstage_passes_sell_in_5_or_less
item = Item.new('Backstage passes to a TAFKAL80ETC concert', 5, 20)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 23, item.quality
end
def test_update_quality_backstage_passes_after_concert
item = Item.new('Backstage passes to a TAFKAL80ETC concert', 0, 20)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 0, item.quality
end
def test_update_quality_sulfuras
item = Item.new('Sulfuras, Hand of Ragnaros', 5, 80)
gilded_rose = GildedRose.new([item])
gilded_rose.update_quality
assert_equal 80, item.quality
def test_foo
items = [Item.new("foo", 0, 0)]
GildedRose.new(items).update_quality()
assert_equal "fixme", items[0].name
end
end