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..7018f127 --- /dev/null +++ b/bash/texttest_fixture.sh @@ -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" 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) diff --git a/jq/README.md b/jq/README.md index a4dd4d82..1e823968 100644 --- a/jq/README.md +++ b/jq/README.md @@ -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)" ``` diff --git a/jq/gilded-rose.jq b/jq/gilded-rose.jq index e4096d57..e9c7d941 100644 --- a/jq/gilded-rose.jq +++ b/jq/gilded-rose.jq @@ -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 ]; diff --git a/jq/test-gilded-rose.sh b/jq/test-gilded-rose.sh index 62d91240..b667d26f 100755 --- a/jq/test-gilded-rose.sh +++ b/jq/test-gilded-rose.sh @@ -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"' diff --git a/jq/texttest_fixture.jq b/jq/texttest_fixture.jq index 12c08dc3..9f153790 100644 --- a/jq/texttest_fixture.jq +++ b/jq/texttest_fixture.jq @@ -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"), diff --git a/ruby/gilded_rose_spec.rb b/ruby/gilded_rose_spec.rb index 1ad468f8..015a759f 100644 --- a/ruby/gilded_rose_spec.rb +++ b/ruby/gilded_rose_spec.rb @@ -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 diff --git a/ruby/gilded_rose_tests.rb b/ruby/gilded_rose_tests.rb index b7643d3e..4e2ec30d 100644 --- a/ruby/gilded_rose_tests.rb +++ b/ruby/gilded_rose_tests.rb @@ -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