From 60b2549066d381adf32922e250bd1d9b7c63709b Mon Sep 17 00:00:00 2001 From: Peter Kofler Date: Mon, 3 Dec 2018 00:12:00 +0100 Subject: [PATCH] Go: Extracts texttest_feature.go from main Gilded Rose, write test similar to other languages. --- go/README.md | 2 +- go/gilded-rose.go | 19 +----------------- go/gilded-rose_test.go | 12 ++++++++++-- go/texttest_feature.go | 44 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 go/texttest_feature.go diff --git a/go/README.md b/go/README.md index 6b894ed4..98dfd387 100644 --- a/go/README.md +++ b/go/README.md @@ -3,7 +3,7 @@ - Run : ```shell -go run gilded-rose.go +go run texttest_feature.go gilded-rose.go ``` - Run tests : diff --git a/go/gilded-rose.go b/go/gilded-rose.go index c5f35822..57d2f189 100644 --- a/go/gilded-rose.go +++ b/go/gilded-rose.go @@ -1,28 +1,11 @@ package main -import "fmt" - type Item struct { name string sellIn, quality int } -var items = []Item{ - Item{"+5 Dexterity Vest", 10, 20}, - Item{"Aged Brie", 2, 0}, - Item{"Elixir of the Mongoose", 5, 7}, - Item{"Sulfuras, Hand of Ragnaros", 0, 80}, - Item{"Backstage passes to a TAFKAL80ETC concert", 15, 20}, - Item{"Conjured Mana Cake", 3, 6}, -} - -func main() { - fmt.Println("OMGHAI!") - // fmt.Print(items) - GildedRose() -} - -func GildedRose() { +func UpdateQuality(items []*Item) { for i := 0; i < len(items); i++ { if items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert" { diff --git a/go/gilded-rose_test.go b/go/gilded-rose_test.go index 6b03bf25..24d64bdd 100644 --- a/go/gilded-rose_test.go +++ b/go/gilded-rose_test.go @@ -2,6 +2,14 @@ package main import "testing" -func Test_GildedRose(t *testing.T) { - main() +func Test_Foo(t *testing.T) { + var items = []*Item{ + &Item{"foo", 0, 0}, + } + + UpdateQuality(items) + + if items[0].name != "fixme" { + t.Errorf("Name: Expected %s but got %s ", "fixme", items[0].name) + } } diff --git a/go/texttest_feature.go b/go/texttest_feature.go new file mode 100644 index 00000000..a5b47a77 --- /dev/null +++ b/go/texttest_feature.go @@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "os" + "strconv" +) + +func main() { + fmt.Println("OMGHAI!") + + var items = []*Item{ + &Item{"+5 Dexterity Vest", 10, 20}, + &Item{"Aged Brie", 2, 0}, + &Item{"Elixir of the Mongoose", 5, 7}, + &Item{"Sulfuras, Hand of Ragnaros", 0, 80}, + &Item{"Sulfuras, Hand of Ragnaros", -1, 80}, + &Item{"Backstage passes to a TAFKAL80ETC concert", 15, 20}, + &Item{"Backstage passes to a TAFKAL80ETC concert", 10, 49}, + &Item{"Backstage passes to a TAFKAL80ETC concert", 5, 49}, + &Item{"Conjured Mana Cake", 3, 6}, // <-- :O + } + + days := 2 + var err error + if len(os.Args) > 1 { + days, err = strconv.Atoi(os.Args[1]) + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + days++ + } + + for day := 0; day < days; day++ { + fmt.Printf("-------- day %d --------\n", day) + fmt.Println("name, sellIn, quality") + for i := 0; i < len(items); i++ { + fmt.Println(items[i]) + } + fmt.Println("") + UpdateQuality(items) + } +}