mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +00:00
add comments
This commit is contained in:
parent
0d24f3ed98
commit
c63614cc15
@ -1,10 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
// Item represents an item for sale at the inn
|
||||||
type Item struct {
|
type Item struct {
|
||||||
name string
|
name string
|
||||||
sellIn, quality int
|
sellIn, quality int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// simple func to return a min of 2 integers
|
||||||
|
// useful in this context to stop items value going over 50
|
||||||
func min(x int, y int) int {
|
func min(x int, y int) int {
|
||||||
if x == y {
|
if x == y {
|
||||||
return x
|
return x
|
||||||
@ -15,6 +18,8 @@ func min(x int, y int) int {
|
|||||||
return y
|
return y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// simple func to return a max of 2 integers
|
||||||
|
// useful in this context to stop items value going under 0
|
||||||
func max(x int, y int) int {
|
func max(x int, y int) int {
|
||||||
if x == y {
|
if x == y {
|
||||||
return x
|
return x
|
||||||
@ -25,6 +30,7 @@ func max(x int, y int) int {
|
|||||||
return y
|
return y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateQualityBrie handles updates for aged brie
|
||||||
func UpdateQualityBrie(item *Item) {
|
func UpdateQualityBrie(item *Item) {
|
||||||
defer func() {
|
defer func() {
|
||||||
item.sellIn--
|
item.sellIn--
|
||||||
@ -43,6 +49,7 @@ func UpdateQualityBrie(item *Item) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateQualityBackstagePasses handles updates for backstage passes
|
||||||
func UpdateQualityBackstagePasses(item *Item) {
|
func UpdateQualityBackstagePasses(item *Item) {
|
||||||
defer func() {
|
defer func() {
|
||||||
item.sellIn--
|
item.sellIn--
|
||||||
@ -67,6 +74,7 @@ func UpdateQualityBackstagePasses(item *Item) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateQualityStandard handles updates for standard items
|
||||||
func UpdateQualityStandard(item *Item) {
|
func UpdateQualityStandard(item *Item) {
|
||||||
defer func() {
|
defer func() {
|
||||||
item.sellIn--
|
item.sellIn--
|
||||||
@ -81,6 +89,7 @@ func UpdateQualityStandard(item *Item) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateQualityConjured handles updates for conjured items
|
||||||
func UpdateQualityConjured(item *Item) {
|
func UpdateQualityConjured(item *Item) {
|
||||||
defer func() {
|
defer func() {
|
||||||
item.sellIn--
|
item.sellIn--
|
||||||
@ -95,6 +104,8 @@ func UpdateQualityConjured(item *Item) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateQuality handles the update of a slice of items. The individual items are handled
|
||||||
|
// by separate functions based on the name of the item.
|
||||||
func UpdateQuality(items []*Item) {
|
func UpdateQuality(items []*Item) {
|
||||||
for i := 0; i < len(items); i++ {
|
for i := 0; i < len(items); i++ {
|
||||||
|
|
||||||
|
|||||||
@ -2,13 +2,20 @@ package main
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
|
// Expected is an array of integers representing expected sellIn and quality
|
||||||
type Expected [2]int
|
type Expected [2]int
|
||||||
|
|
||||||
|
// An ItemTest is a struct which includes an Item along with the expected values
|
||||||
|
// for sellIn and quality after the quality is updated by the UpdateQuality function
|
||||||
type ItemTest struct {
|
type ItemTest struct {
|
||||||
*Item
|
*Item
|
||||||
Expected
|
Expected
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetItemTestsComponents returns an slice of Items and Expecteds from a slice of
|
||||||
|
// ItemTests. This is useful so that we can have our tests as ItemTests to keep the expected
|
||||||
|
// results along with the Item definitions. This function can be used to extract the slice of Items
|
||||||
|
// which can then be passed to the UpdateQuality function
|
||||||
func GetItemTestsComponents(itemTests []ItemTest) ([]*Item, []Expected) {
|
func GetItemTestsComponents(itemTests []ItemTest) ([]*Item, []Expected) {
|
||||||
var items []*Item
|
var items []*Item
|
||||||
var expected []Expected
|
var expected []Expected
|
||||||
@ -38,6 +45,7 @@ func Test_Items(t *testing.T) {
|
|||||||
{&Item{"Acme Dynamite", 0, 0}, Expected{-1, 0}},
|
{&Item{"Acme Dynamite", 0, 0}, Expected{-1, 0}},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get the items and expected componenets
|
||||||
items, expected := GetItemTestsComponents(itemTests)
|
items, expected := GetItemTestsComponents(itemTests)
|
||||||
|
|
||||||
UpdateQuality(items)
|
UpdateQuality(items)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user