mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Merge branch 'main' into refactor-ruby
This commit is contained in:
commit
7e8becaa68
40
bash/README.md
Normal file
40
bash/README.md
Normal 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
58
bash/gilded_rose.sh
Executable 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
37
bash/texttest_fixture.sh
Executable 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
21
bash/unit_test.sh
Executable 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
6
bash/verify.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
./texttest_fixture.sh 30 |
|
||||||
|
diff -u - ../texttests/ThirtyDays/stdout.gr &&
|
||||||
|
echo "✅ looks good" ||
|
||||||
|
(echo "❌ failed" && exit 1)
|
||||||
@ -1,6 +1,6 @@
|
|||||||
# Installs
|
# Requirements
|
||||||
|
|
||||||
[install jaq](https://github.com/01mf02/jaq#installation) (latest development version)
|
jq
|
||||||
|
|
||||||
# Failing Unit Test
|
# Failing Unit Test
|
||||||
|
|
||||||
@ -11,11 +11,11 @@
|
|||||||
# TextTest Fixture
|
# TextTest Fixture
|
||||||
|
|
||||||
```shell
|
```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):
|
Specify days (e.g. 10 days):
|
||||||
|
|
||||||
```shell
|
```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)"
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,14 +1,13 @@
|
|||||||
def update_quality:
|
def update_quality:
|
||||||
[
|
[
|
||||||
foreach .[] as $item (
|
.[]
|
||||||
null;
|
|
|
||||||
$item |
|
|
||||||
if .name != "Aged Brie" and .name != "Backstage passes to a TAFKAL80ETC concert" then
|
if .name != "Aged Brie" and .name != "Backstage passes to a TAFKAL80ETC concert" then
|
||||||
if .quality > 0 then
|
if .quality > 0 then
|
||||||
if .name != "Sulfuras, Hand of Ragnaros" then
|
if .name != "Sulfuras, Hand of Ragnaros" then
|
||||||
.quality = .quality - 1
|
.quality = .quality - 1
|
||||||
else . end
|
end
|
||||||
else . end
|
end
|
||||||
else
|
else
|
||||||
if .quality < 50 then
|
if .quality < 50 then
|
||||||
.quality = .quality + 1
|
.quality = .quality + 1
|
||||||
@ -17,21 +16,21 @@ def update_quality:
|
|||||||
if .sell_in < 11 then
|
if .sell_in < 11 then
|
||||||
if .quality < 50 then
|
if .quality < 50 then
|
||||||
.quality = .quality + 1
|
.quality = .quality + 1
|
||||||
else . end
|
end
|
||||||
else . end
|
end
|
||||||
|
|
|
|
||||||
if .sell_in < 6 then
|
if .sell_in < 6 then
|
||||||
if .quality < 50 then
|
if .quality < 50 then
|
||||||
.quality = .quality + 1
|
.quality = .quality + 1
|
||||||
else . end
|
end
|
||||||
else . end
|
end
|
||||||
else . end
|
end
|
||||||
else . end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
|
||||||
if .name != "Sulfuras, Hand of Ragnaros" then
|
if .name != "Sulfuras, Hand of Ragnaros" then
|
||||||
.sell_in = .sell_in - 1
|
.sell_in = .sell_in - 1
|
||||||
else . end
|
end
|
||||||
|
|
|
|
||||||
if .sell_in < 0 then
|
if .sell_in < 0 then
|
||||||
if .name != "Aged Brie" then
|
if .name != "Aged Brie" then
|
||||||
@ -39,16 +38,15 @@ def update_quality:
|
|||||||
if .quality > 0 then
|
if .quality > 0 then
|
||||||
if .name != "Sulfuras, Hand of Ragnaros" then
|
if .name != "Sulfuras, Hand of Ragnaros" then
|
||||||
.quality = .quality - 1
|
.quality = .quality - 1
|
||||||
else . end
|
end
|
||||||
else . end
|
end
|
||||||
else
|
else
|
||||||
.quality = .quality - .quality
|
.quality = .quality - .quality
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if .quality < 50 then
|
if .quality < 50 then
|
||||||
.quality = .quality + 1
|
.quality = .quality + 1
|
||||||
else . end
|
|
||||||
end
|
end
|
||||||
else . end
|
end
|
||||||
)
|
end
|
||||||
];
|
];
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
#!/usr/bin/env bash
|
#!/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"'
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
{ name: "Conjured Mana Cake", sell_in: 3, quality: 6} # <-- :O
|
{ name: "Conjured Mana Cake", sell_in: 3, quality: 6} # <-- :O
|
||||||
] |
|
] |
|
||||||
{ items: ., day: 0 } |
|
{ 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),
|
(["-------- day ", (.day | tostring), " --------"] | add),
|
||||||
("name, sellIn, quality"),
|
("name, sellIn, quality"),
|
||||||
|
|||||||
@ -3,77 +3,9 @@ require 'rspec'
|
|||||||
require File.join(File.dirname(__FILE__), 'gilded_rose')
|
require File.join(File.dirname(__FILE__), 'gilded_rose')
|
||||||
|
|
||||||
describe GildedRose do
|
describe GildedRose do
|
||||||
|
|
||||||
describe "#update_quality" do
|
|
||||||
it "does not change the name" do
|
it "does not change the name" do
|
||||||
items = [Item.new("foo", 0, 0)]
|
items = [Item.new("foo", 0, 0)]
|
||||||
GildedRose.new(items).update_quality()
|
GildedRose.new(items).update_quality()
|
||||||
expect(items[0].name).to eq "foo"
|
expect(items[0].name).to eq "fixme"
|
||||||
end
|
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
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,67 +2,9 @@ require 'minitest/autorun'
|
|||||||
require_relative 'gilded_rose'
|
require_relative 'gilded_rose'
|
||||||
|
|
||||||
class TestGildedRose < Minitest::Test
|
class TestGildedRose < Minitest::Test
|
||||||
def test_update_quality_regular_item
|
def test_foo
|
||||||
item = Item.new('Regular Item', 5, 10)
|
items = [Item.new("foo", 0, 0)]
|
||||||
gilded_rose = GildedRose.new([item])
|
GildedRose.new(items).update_quality()
|
||||||
gilded_rose.update_quality
|
assert_equal "fixme", items[0].name
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user