Browse Source

test cases for common

pull/787/merge
Darien Raymond 7 years ago
parent
commit
c91112798c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 29
      common/common_test.go
  2. 38
      common/type_test.go

29
common/common_test.go

@ -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

@ -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)
}
Loading…
Cancel
Save