Fix bugs in func huaPaiShortSplit, and created unit test for it
testing / check and test (push) Successful in 42s

This commit is contained in:
2024-08-23 04:24:38 +00:00
parent 79a4d94445
commit 6eff9005d6
2 changed files with 36 additions and 1 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ func huaPaiShortSplit(num int) (string, error) {
if num < 1 || num > 8 {
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) {
+35
View File
@@ -7,6 +7,41 @@ import (
"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) {
type testcase struct {
Name string