1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 09:50:43 +00:00

test cases for common

This commit is contained in:
Darien Raymond 2017-12-20 11:16:45 +01:00
parent 35545cb2db
commit c91112798c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 67 additions and 0 deletions

29
common/common_test.go Normal file
View File

@ -0,0 +1,29 @@
package common_test
import (
"errors"
"testing"
. "v2ray.com/core/common"
. "v2ray.com/ext/assert"
)
func TestMust(t *testing.T) {
assert := With(t)
f := func() error {
return errors.New("test error")
}
assert(func() { Must(f()) }, Panics)
}
func TestMust2(t *testing.T) {
assert := With(t)
f := func() (interface{}, error) {
return nil, errors.New("test error")
}
assert(func() { Must2(f()) }, Panics)
}

38
common/type_test.go Normal file
View File

@ -0,0 +1,38 @@
package common_test
import (
"context"
"testing"
. "v2ray.com/core/common"
. "v2ray.com/ext/assert"
)
type TConfig struct {
value int
}
type YConfig struct {
value string
}
func TestObjectCreation(t *testing.T) {
assert := With(t)
var f = func(ctx context.Context, t interface{}) (interface{}, error) {
return func() int {
return t.(*TConfig).value
}, nil
}
Must(RegisterConfig((*TConfig)(nil), f))
err := RegisterConfig((*TConfig)(nil), f)
assert(err, IsNotNil)
g, err := CreateObject(context.Background(), &TConfig{value: 2})
assert(err, IsNil)
assert(g.(func() int)(), Equals, 2)
_, err = CreateObject(context.Background(), &YConfig{value: "T"})
assert(err, IsNotNil)
}