add comments

This commit is contained in:
dreddick.home2@gmail.com 2020-11-05 17:26:20 +00:00
parent 0d24f3ed98
commit c63614cc15
2 changed files with 19 additions and 0 deletions

View File

@ -1,10 +1,13 @@
package main
// Item represents an item for sale at the inn
type Item struct {
name string
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 {
if x == y {
return x
@ -15,6 +18,8 @@ func min(x int, y int) int {
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 {
if x == y {
return x
@ -25,6 +30,7 @@ func max(x int, y int) int {
return y
}
// UpdateQualityBrie handles updates for aged brie
func UpdateQualityBrie(item *Item) {
defer func() {
item.sellIn--
@ -43,6 +49,7 @@ func UpdateQualityBrie(item *Item) {
}
// UpdateQualityBackstagePasses handles updates for backstage passes
func UpdateQualityBackstagePasses(item *Item) {
defer func() {
item.sellIn--
@ -67,6 +74,7 @@ func UpdateQualityBackstagePasses(item *Item) {
}
// UpdateQualityStandard handles updates for standard items
func UpdateQualityStandard(item *Item) {
defer func() {
item.sellIn--
@ -81,6 +89,7 @@ func UpdateQualityStandard(item *Item) {
}
// UpdateQualityConjured handles updates for conjured items
func UpdateQualityConjured(item *Item) {
defer func() {
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) {
for i := 0; i < len(items); i++ {

View File

@ -2,13 +2,20 @@ package main
import "testing"
// Expected is an array of integers representing expected sellIn and quality
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 {
*Item
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) {
var items []*Item
var expected []Expected
@ -38,6 +45,7 @@ func Test_Items(t *testing.T) {
{&Item{"Acme Dynamite", 0, 0}, Expected{-1, 0}},
}
// get the items and expected componenets
items, expected := GetItemTestsComponents(itemTests)
UpdateQuality(items)