123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // Copyright (c) 2019 Uber Technologies, Inc.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- package xreflect
- import (
- "errors"
- "log"
- "sync"
- "testing"
- "github.com/stretchr/testify/assert"
- "go.uber.org/dig"
- )
- type hollerer interface {
- Holler()
- }
- type impl struct{}
- func (impl) Holler() {}
- type result struct {
- dig.Out // referencing di introduces import cycles
- mu sync.Mutex // unexported
- Logger *log.Logger
- }
- func TestReturnTypes(t *testing.T) {
- t.Run("non-function", func(t *testing.T) {
- assert.Empty(t, ReturnTypes(42))
- })
- t.Run("primitive", func(t *testing.T) {
- fn := func() (int, string) {
- return 0, ""
- }
- assert.Equal(t, []string{"int", "string"}, ReturnTypes(fn))
- })
- t.Run("pointer", func(t *testing.T) {
- type s struct{}
- fn := func() *s {
- return &s{}
- }
- assert.Equal(t, []string{"*xreflect.s"}, ReturnTypes(fn))
- })
- t.Run("interface", func(t *testing.T) {
- fn := func() hollerer {
- return impl{}
- }
- assert.Equal(t, []string{"xreflect.hollerer"}, ReturnTypes(fn))
- })
- t.Run("result struct", func(t *testing.T) {
- fn := func() result {
- return result{}
- }
- assert.Equal(t, []string{"*log.Logger"}, ReturnTypes(fn))
- })
- t.Run("skips errors", func(t *testing.T) {
- fn := func() (string, error) {
- return "", errors.New("err")
- }
- assert.Equal(t, []string{"string"}, ReturnTypes(fn))
- })
- }
- func TestCaller(t *testing.T) {
- assert.Equal(t, "git.llsapp.com/zhenghe/pkg/di/xreflect.TestCaller", Caller())
- }
- func someFunc() {}
- func TestFuncName(t *testing.T) {
- tests := []struct {
- desc string
- give interface{}
- want string
- }{
- {
- desc: "function",
- give: someFunc,
- want: "git.llsapp.com/zhenghe/pkg/di/xreflect.someFunc()",
- },
- {
- desc: "not a function",
- give: 42,
- want: "42",
- },
- }
- for _, tt := range tests {
- t.Run(tt.desc, func(t *testing.T) {
- assert.Equal(t, tt.want, FuncName(tt.give))
- })
- }
- }
- func TestSanitizeFuncNames(t *testing.T) {
- cases := []struct {
- name string
- input string
- expected string
- }{
- {
- "url encoding",
- "go.uber.org/di/sample%2egit/someFunc",
- "go.uber.org/di/sample.git/someFunc",
- },
- {
- "vendor removal",
- "go.uber.org/di/vendor/github.com/some/lib.SomeFunc",
- "vendor/github.com/some/lib.SomeFunc",
- },
- {
- "package happens to be named vendor is untouched",
- "go.uber.org/di/foovendor/someFunc",
- "go.uber.org/di/foovendor/someFunc",
- },
- }
- for _, c := range cases {
- t.Run(c.name, func(t *testing.T) {
- assert.Equal(t, c.expected, sanitize(c.input))
- })
- }
- }
|