2017-12-20 05:16:45 -05:00
|
|
|
package common_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v5/common"
|
2017-12-20 05:16:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type TConfig struct {
|
|
|
|
value int
|
|
|
|
}
|
|
|
|
|
|
|
|
type YConfig struct {
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestObjectCreation(t *testing.T) {
|
2021-05-19 17:28:52 -04:00
|
|
|
f := func(ctx context.Context, t interface{}) (interface{}, error) {
|
2017-12-20 05:16:45 -05:00
|
|
|
return func() int {
|
|
|
|
return t.(*TConfig).value
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
Must(RegisterConfig((*TConfig)(nil), f))
|
|
|
|
err := RegisterConfig((*TConfig)(nil), f)
|
2019-02-02 16:19:30 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Error("expect non-nil error, but got nil")
|
|
|
|
}
|
2017-12-20 05:16:45 -05:00
|
|
|
|
|
|
|
g, err := CreateObject(context.Background(), &TConfig{value: 2})
|
2019-02-02 16:19:30 -05:00
|
|
|
Must(err)
|
|
|
|
if v := g.(func() int)(); v != 2 {
|
|
|
|
t.Error("expect return value 2, but got ", v)
|
|
|
|
}
|
2017-12-20 05:16:45 -05:00
|
|
|
|
|
|
|
_, err = CreateObject(context.Background(), &YConfig{value: "T"})
|
2019-02-02 16:19:30 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Error("expect non-nil error, but got nil")
|
|
|
|
}
|
2017-12-20 05:16:45 -05:00
|
|
|
}
|