From c91112798cd257adae5b6a5f6d701cec2a656606 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 20 Dec 2017 11:16:45 +0100 Subject: [PATCH] test cases for common --- common/common_test.go | 29 +++++++++++++++++++++++++++++ common/type_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 common/common_test.go create mode 100644 common/type_test.go diff --git a/common/common_test.go b/common/common_test.go new file mode 100644 index 00000000..9a11b47a --- /dev/null +++ b/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) +} diff --git a/common/type_test.go b/common/type_test.go new file mode 100644 index 00000000..099c4a04 --- /dev/null +++ b/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) +}