Go: Extracts texttest_feature.go from main Gilded Rose, write test similar to other languages.

This commit is contained in:
Peter Kofler 2018-12-03 00:12:00 +01:00
parent ab83717690
commit 60b2549066
4 changed files with 56 additions and 21 deletions

View File

@ -3,7 +3,7 @@
- Run :
```shell
go run gilded-rose.go
go run texttest_feature.go gilded-rose.go
```
- Run tests :

View File

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

View File

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

44
go/texttest_feature.go Normal file
View File

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