Created
August 14, 2022 15:45
-
-
Save ObsidianCat/86482af260a7bd71b47f417474084eb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package doughnuts_box | |
import ( | |
"testing" | |
"github.com/stretchr/testify/assert" | |
"github.com/stretchr/testify/require" | |
) | |
func TestPackDoughnutsBoxTableTests(t *testing.T) { | |
// Anonymous struct of test cases | |
tests := []struct { | |
name string | |
boxCapacity int | |
errorExpected bool | |
errorMessage string | |
items []string | |
expectedNumOfDoughnutsInTheBox int | |
}{ | |
{ | |
name: "Filling the box with tasty doughnuts", | |
boxCapacity: 4, | |
errorExpected: false, | |
errorMessage: "", | |
items: []string{"Sri Lankan Cinnamon Sugar", "Matcha Tea", "Home Made Raspberry Jam", "Lime & Coconut (ve)"}, | |
expectedNumOfDoughnutsInTheBox: 4, | |
}, | |
{ | |
name: "Attempt to fill the box with too many doughnuts", | |
boxCapacity: 4, | |
errorExpected: true, | |
errorMessage: "failed to put 5 doughnuts in the box, it's only has 4 doughnuts capacity", | |
items: []string{"Sri Lankan Cinnamon Sugar", "Matcha Tea", "Home Made Raspberry Jam", "Lime & Coconut (ve)", "Lime & Coconut (ve)"}, | |
expectedNumOfDoughnutsInTheBox: 0, | |
}, | |
{ | |
name: "Attempt to put a giant chocolate cookie into the box", | |
boxCapacity: 2, | |
errorExpected: true, | |
errorMessage: "the following items cannot be placed into the box: [Giant Chocolate Cookie]", | |
items: []string{"Sri Lankan Cinnamon Sugar", "Giant Chocolate Cookie"}, | |
expectedNumOfDoughnutsInTheBox: 1, | |
}, | |
} | |
for _, tc := range tests { | |
// each test case from table above run as a subtest | |
t.Run(tc.name, func(t *testing.T) { | |
// Arrange | |
box := newDoughnutsBox(tc.boxCapacity) | |
// Act | |
numOfDoughnutsInTheBox, err := box.pack(tc.items) | |
// Assert | |
if tc.errorExpected { | |
require.Error(t, err) | |
assert.Equal(t, tc.errorMessage, err.Error()) | |
} | |
assert.Equal(t, tc.expectedNumOfDoughnutsInTheBox, numOfDoughnutsInTheBox) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment