Fix bugs in func huaPaiShortSplit, and created unit test for it
testing / check and test (push) Successful in 42s
testing / check and test (push) Successful in 42s
This commit is contained in:
@@ -31,7 +31,7 @@ func huaPaiShortSplit(num int) (string, error) {
|
|||||||
if num < 1 || num > 8 {
|
if num < 1 || num > 8 {
|
||||||
return "", fmt.Errorf("invalid hua pai index %d", num)
|
return "", fmt.Errorf("invalid hua pai index %d", num)
|
||||||
}
|
}
|
||||||
return TileHuaTypes[num-1], fmt.Errorf("error calling huaPaiShortSplit()")
|
return TileHuaTypes[num-1], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func shortToArray(tiles string) ([]string, error) {
|
func shortToArray(tiles string) ([]string, error) {
|
||||||
|
|||||||
@@ -7,6 +7,41 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestHuaPaiShortSplit(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
input int
|
||||||
|
output string
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{0, "", fmt.Errorf("invalid hua pai index 0")},
|
||||||
|
{1, "ME", nil},
|
||||||
|
{2, "LA", nil},
|
||||||
|
{3, "ZU", nil},
|
||||||
|
{4, "JU", nil},
|
||||||
|
{5, "CH", nil},
|
||||||
|
{6, "XA", nil},
|
||||||
|
{7, "QI", nil},
|
||||||
|
{8, "DN", nil},
|
||||||
|
{9, "", fmt.Errorf("invalid hua pai index 9")},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(fmt.Sprintf("Test case %d", tc.input), func(t *testing.T) {
|
||||||
|
result, err := huaPaiShortSplit(tc.input)
|
||||||
|
if err != nil {
|
||||||
|
if tc.err != nil {
|
||||||
|
if err.Error() != tc.err.Error() {
|
||||||
|
t.Errorf("Expected error: %v, got: %v", tc.err, err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Errorf("not expect error, but got %v", err)
|
||||||
|
} else if result != tc.output {
|
||||||
|
t.Errorf("Expected result: %v, got: %v", tc.output, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
func TestShortToArray(t *testing.T) {
|
func TestShortToArray(t *testing.T) {
|
||||||
type testcase struct {
|
type testcase struct {
|
||||||
Name string
|
Name string
|
||||||
|
|||||||
Reference in New Issue
Block a user