Parcourir la source

test cases for common

Darien Raymond il y a 7 ans
Parent
commit
c91112798c
2 fichiers modifiés avec 67 ajouts et 0 suppressions
  1. 29 0
      common/common_test.go
  2. 38 0
      common/type_test.go

+ 29 - 0
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 - 0
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)
+}