-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
324e105
commit 9cf42b5
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package template | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
) | ||
|
||
func random(list interface{}) (string, error) { | ||
v := reflect.ValueOf(list) | ||
|
||
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array { | ||
return "", errors.New("input must be a slice or array") | ||
} | ||
|
||
if v.Len() == 0 { | ||
return "", errors.New("input slice or array is empty") | ||
} | ||
|
||
return fmt.Sprintf("%v", v.Index(rand.Intn(v.Len()))), nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package template | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRandom(t *testing.T) { | ||
cases := []struct { | ||
Case string | ||
Input interface{} | ||
ShouldError bool | ||
}{ | ||
{ | ||
Case: "valid slice", | ||
Input: []int{1, 2, 3, 4, 5}, | ||
}, | ||
{ | ||
Case: "valid array", | ||
Input: [5]int{1, 2, 3, 4, 5}, | ||
}, | ||
{ | ||
Case: "empty slice", | ||
Input: []int{}, | ||
ShouldError: true, | ||
}, | ||
{ | ||
Case: "not a slice or array", | ||
Input: "not a slice", | ||
ShouldError: true, | ||
}, | ||
{ | ||
Case: "valid string slice", | ||
Input: []string{"a", "b", "c"}, | ||
}, | ||
{ | ||
Case: "valid float slice", | ||
Input: []float64{1.1, 2.2, 3.3}, | ||
}, | ||
{ | ||
Case: "interface with multiple types", | ||
Input: []interface{}{ | ||
"a", | ||
1, | ||
true, | ||
}, | ||
}, | ||
{ | ||
Case: "valid struct slice", | ||
Input: []struct{ Name string }{{Name: "Alice"}, {Name: "Bob"}}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
result, err := random(tc.Input) | ||
if tc.ShouldError { | ||
assert.Error(t, err, tc.Case) | ||
} else { | ||
assert.NoError(t, err, tc.Case) | ||
assert.Contains(t, fmt.Sprintf("%v", tc.Input), result, tc.Case) | ||
} | ||
} | ||
} |
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