1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-15 19:33:17 +00:00
arikawa/bot/arguments_test.go

71 lines
1.4 KiB
Go
Raw Normal View History

2020-02-05 04:29:45 +00:00
package bot
import (
"reflect"
"strings"
"testing"
)
type mockParser string
func (m *mockParser) Parse(s string) error {
*m = mockParser(s)
return nil
}
func mockParse(str string) *mockParser {
return (*mockParser)(&str)
}
func TestArguments(t *testing.T) {
testArgs(t, "string", "string")
testArgs(t, true, "true")
testArgs(t, false, "n")
testArgs(t, int64(69420), "69420")
testArgs(t, uint64(1337), "1337")
testArgs(t, 69.420, "69.420")
testArgs(t, mockParse("testString"), "testString")
testArgs(t, *mockParse("testString"), "testString")
_, err := newArgument(reflect.TypeOf(struct{}{}), false)
2020-02-05 04:29:45 +00:00
if !strings.HasPrefix(err.Error(), "invalid type: ") {
t.Fatal("Unexpected error:", err)
}
}
func testArgs(t *testing.T, expect interface{}, input string) {
f, err := newArgument(reflect.TypeOf(expect), false)
2020-02-05 04:29:45 +00:00
if err != nil {
t.Fatal("Failed to get argument value function:", err)
}
v, err := f.fn(input)
if err != nil {
t.Fatal("avfs returned with error:", err)
}
if v := v.Interface(); !reflect.DeepEqual(v, expect) {
t.Fatal("Value :", v, "\nExpects:", expect)
}
}
2020-05-03 22:59:10 +00:00
// used for ctx_test.go
type customManualParsed struct {
args []string
}
func (c *customManualParsed) ParseContent(args []string) error {
c.args = args
return nil
}
type customParsed struct {
parsed bool
}
func (c *customParsed) Parse(string) error {
c.parsed = true
return nil
}