Skip to content

Instantly share code, notes, and snippets.

@vuon9
Created May 14, 2021 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vuon9/81249704f9f96ccb65ec20dbc60f5f44 to your computer and use it in GitHub Desktop.
Save vuon9/81249704f9f96ccb65ec20dbc60f5f44 to your computer and use it in GitHub Desktop.
GildedRose Refactoring Kata in Go
package main
func UpdateQualityOld(items ...*Item) {
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].quality > 0 {
if items[i].name != "Sulfuras, Hand of Ragnaros" {
items[i].quality = items[i].quality - 1
}
}
} else {
if items[i].quality < 50 {
items[i].quality = items[i].quality + 1
if items[i].name == "Backstage passes to a TAFKAL80ETC concert" {
if items[i].sellIn < 11 {
if items[i].quality < 50 {
items[i].quality = items[i].quality + 1
}
}
if items[i].sellIn < 6 {
if items[i].quality < 50 {
items[i].quality = items[i].quality + 1
}
}
}
}
}
if items[i].name != "Sulfuras, Hand of Ragnaros" {
items[i].sellIn = items[i].sellIn - 1
}
if items[i].sellIn < 0 {
if items[i].name != "Aged Brie" {
if items[i].name != "Backstage passes to a TAFKAL80ETC concert" {
if items[i].quality > 0 {
if items[i].name != "Sulfuras, Hand of Ragnaros" {
items[i].quality = items[i].quality - 1
}
}
} else {
items[i].quality = items[i].quality - items[i].quality
}
} else {
if items[i].quality < 50 {
items[i].quality = items[i].quality + 1
}
}
}
}
}
package main
type Item struct {
name string
sellIn, quality int
}
type Brie struct {
*Item
}
func (i *Brie) Update() *Item {
if i.quality < 50 {
i.quality++
}
i.sellIn = i.sellIn - 1
if i.sellIn < 0 && i.quality < 50 {
i.quality++
}
return i.Item
}
type Backstage struct {
*Item
}
func (i *Backstage) Update() *Item {
if i.quality < 50 {
i.quality++
}
if i.sellIn < 11 && i.quality < 50 {
i.quality++
}
if i.sellIn < 6 && i.quality < 50 {
i.quality++
}
i.sellIn = i.sellIn - 1
if i.sellIn < 0 {
i.quality = i.quality - i.quality
}
return i.Item
}
type Sulfuras struct {
*Item
}
func (i *Sulfuras) Update() *Item {
return i.Item
}
type Other struct {
*Item
}
func (i *Other) Update() *Item {
if i.quality > 0 {
i.quality = i.quality - 1
}
i.sellIn = i.sellIn - 1
if i.sellIn < 0 && i.quality > 0 {
i.quality = i.quality - 1
}
return i.Item
}
type Conjured struct {
*Item
}
func (i *Conjured) Update() *Item {
if i.quality > 0 {
i.quality = i.quality - 2
}
i.sellIn = i.sellIn - 1
if i.sellIn < 0 && i.quality > 0 {
i.quality = i.quality - 2
}
return i.Item
}
type updateable interface {
Update() *Item
}
func UpdateQuality(items ...*Item) {
for i := 0; i < len(items); i++ {
var ui updateable
switch items[i].name {
case "Sulfuras, Hand of Ragnaros":
ui = &Sulfuras{Item: items[i]}
case "Aged Brie":
ui = &Brie{Item: items[i]}
case "Backstage passes to a TAFKAL80ETC concert":
ui = &Backstage{Item: items[i]}
case "Conjured Mana Cake":
ui = &Conjured{Item: items[i]}
default:
ui = &Other{Item: items[i]}
}
items[i] = ui.Update()
}
}
package main
// This test was copied from https://github.com/robphoenix/gilded-rose/blob/10f9154a5b/gilded-rose/item_test.go
import "testing"
var tests = []struct {
name string
description string
days int
quality int
expectedDays int
expectedQuality int
}{
{"normal", "before sell date", 5, 10, 4, 9},
{"normal", "on sell date", 0, 10, -1, 8},
{"normal", "after sell date", -10, 10, -11, 8},
{"normal", "of zero quality", 5, 0, 4, 0},
{"Aged Brie", "before sell date with max quality", 5, 50, 4, 50},
{"Aged Brie", "on sell date", 0, 10, -1, 12},
{"Aged Brie", "on sell date near max quality", 0, 49, -1, 50},
{"Aged Brie", "on sell date with max quality", 0, 50, -1, 50},
{"Aged Brie", "after sell date", -10, 10, -11, 12},
{"Aged Brie", "after sell date with max quality", -10, 50, -11, 50},
{"Sulfuras, Hand of Ragnaros", "before sell date", 5, 80, 5, 80},
{"Sulfuras, Hand of Ragnaros", "on sell date", 0, 80, 0, 80},
{"Sulfuras, Hand of Ragnaros", "after sell date", -10, 80, -10, 80},
{"Backstage passes to a TAFKAL80ETC concert", "long before sell date", 11, 10, 10, 11},
{"Backstage passes to a TAFKAL80ETC concert", "long before sell date at max quality", 11, 50, 10, 50},
{"Backstage passes to a TAFKAL80ETC concert", "medium close to sell date (upper bound)", 10, 10, 9, 12},
{"Backstage passes to a TAFKAL80ETC concert", "medium close to sell date (upper bound) at max quality", 10, 50, 9, 50},
{"Backstage passes to a TAFKAL80ETC concert", "medium close to sell date (lower bound)", 6, 10, 5, 12},
{"Backstage passes to a TAFKAL80ETC concert", "medium close to sell date (lower bound) at max quality", 6, 50, 5, 50},
{"Backstage passes to a TAFKAL80ETC concert", "very close to sell date (upper bound)", 5, 10, 4, 13},
{"Backstage passes to a TAFKAL80ETC concert", "very close to sell date (upper bound) at max quality", 5, 50, 4, 50},
{"Backstage passes to a TAFKAL80ETC concert", "very close to sell date (lower bound)", 1, 10, 0, 13},
{"Backstage passes to a TAFKAL80ETC concert", "very close to sell date (lower bound) at max quality", 1, 50, 0, 50},
{"Backstage passes to a TAFKAL80ETC concert", "on sell date", 0, 10, -1, 0},
{"Backstage passes to a TAFKAL80ETC concert", "after sell date", -10, 10, -11, 0},
{"Conjured Mana Cake", "before sell date", 5, 10, 4, 8},
{"Conjured Mana Cake", "before sell date at zero quality", 5, 0, 4, 0},
{"Conjured Mana Cake", "on sell date", 0, 10, -1, 6},
{"Conjured Mana Cake", "on sell date at zero quality", 0, 0, -1, 0},
{"Conjured Mana Cake", "after sell date", -10, 10, -11, 6},
{"Conjured Mana Cake", "after sell date at zero quality", -10, 0, -11, 0},
}
func TestGildedRose(t *testing.T) {
for _, tt := range tests {
item := &Item{tt.name, tt.days, tt.quality}
UpdateQuality(item)
if item.quality != tt.expectedQuality {
t.Errorf("\n%s %s\nexpected quality: %d\nactual quality: %d", tt.name, tt.description, tt.expectedQuality, item.quality)
}
if item.sellIn != tt.expectedDays {
t.Errorf("\n%s %s\nexpected days left: %d\nactual days left: %d", tt.name, tt.description, tt.expectedDays, item.sellIn)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment