separate brie update

This commit is contained in:
dreddick.home2@gmail.com 2020-11-05 16:31:48 +00:00
parent 3d90e599d7
commit afa3d43717
3 changed files with 47 additions and 12 deletions

View File

@ -18,4 +18,16 @@ go test
go test -coverprofile=coverage.out go test -coverprofile=coverage.out
go tool cover -html=coverage.out go tool cover -html=coverage.out
``` ```
- Create golden test file using textest (*nix):
```bash
go run texttest_fixture.go gilded-rose.go > golden_test.txt
```
- Testing changes against the golden test file (*nix):
```bash
diff <(go run texttest_fixture.go gilded-rose.go) golden_test.txt
```

View File

@ -5,14 +5,41 @@ type Item struct {
sellIn, quality int sellIn, quality int
} }
func UpdateQualityBrie(item *Item) {
defer func() {
item.sellIn = item.sellIn - 1
}()
if item.quality == 50 {
return
}
if item.quality == 49 && item.sellIn <= 0 {
item.quality = 50
}
if item.sellIn > 0 {
item.quality++
}
if item.sellIn <= 0 {
item.quality += 2
}
}
func UpdateQuality(items []*Item) { func UpdateQuality(items []*Item) {
for i := 0; i < len(items); i++ { for i := 0; i < len(items); i++ {
if items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert" { if items[i].name == "Sulfuras, Hand of Ragnaros" {
continue
}
if items[i].name == "Aged Brie" {
UpdateQualityBrie(items[i])
continue
}
if items[i].name != "Backstage passes to a TAFKAL80ETC concert" {
if items[i].quality > 0 { if items[i].quality > 0 {
if items[i].name != "Sulfuras, Hand of Ragnaros" { items[i].quality = items[i].quality - 1
items[i].quality = items[i].quality - 1
}
} }
} else { } else {
if items[i].quality < 50 { if items[i].quality < 50 {
@ -32,17 +59,13 @@ func UpdateQuality(items []*Item) {
} }
} }
if items[i].name != "Sulfuras, Hand of Ragnaros" { items[i].sellIn = items[i].sellIn - 1
items[i].sellIn = items[i].sellIn - 1
}
if items[i].sellIn < 0 { if items[i].sellIn < 0 {
if items[i].name != "Aged Brie" { if items[i].name != "Aged Brie" {
if items[i].name != "Backstage passes to a TAFKAL80ETC concert" { if items[i].name != "Backstage passes to a TAFKAL80ETC concert" {
if items[i].quality > 0 { if items[i].quality > 0 {
if items[i].name != "Sulfuras, Hand of Ragnaros" { items[i].quality = items[i].quality - 1
items[i].quality = items[i].quality - 1
}
} }
} else { } else {
items[i].quality = items[i].quality - items[i].quality items[i].quality = items[i].quality - items[i].quality

View File

@ -43,7 +43,7 @@ func Test_Items(t *testing.T) {
for i, item := range items { for i, item := range items {
actual := [2]int{item.sellIn, item.quality} actual := [2]int{item.sellIn, item.quality}
if actual != expected[i] { if actual != expected[i] {
t.Errorf("Expected %+v but got %+v", expected[i], actual) t.Errorf("%s: Expected %+v but got %+v", item.name, expected[i], actual)
} }
} }