-
Notifications
You must be signed in to change notification settings - Fork 2
/
interactor_test.go
86 lines (70 loc) · 2.23 KB
/
interactor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package usecase_test
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
)
func TestInteract_Interact(t *testing.T) {
err := errors.New("failed")
inp := new(string)
out := new(int)
i := usecase.Interact(func(ctx context.Context, input, output interface{}) error {
assert.Equal(t, inp, input)
assert.Equal(t, out, output)
return err
})
assert.Equal(t, err, i.Interact(context.Background(), inp, out))
}
func TestInfo(t *testing.T) {
i := usecase.Info{}
i.SetName("name")
i.SetDescription("Description")
i.SetTitle("Title")
i.SetTags("tag1", "tag2")
i.SetIsDeprecated(true)
i.SetExpectedErrors(usecase.Error{StatusCode: status.InvalidArgument})
assert.Equal(t, "name", i.Name())
assert.Equal(t, "Description", i.Description())
assert.Equal(t, "Title", i.Title())
assert.Equal(t, []string{"tag1", "tag2"}, i.Tags())
assert.Equal(t, true, i.IsDeprecated())
assert.Equal(t, []error{usecase.Error{StatusCode: status.InvalidArgument}}, i.ExpectedErrors())
}
type Foo struct{}
func (f *Foo) Bar() usecase.IOInteractor {
return usecase.NewIOI(nil, nil, func(ctx context.Context, input, output interface{}) error {
return nil
})
}
func (f Foo) Baz() usecase.IOInteractor {
return usecase.NewIOI(nil, nil, func(ctx context.Context, input, output interface{}) error {
return nil
})
}
func TestNewIOI(t *testing.T) {
u := usecase.NewIOI(new(string), new(int), func(ctx context.Context, input, output interface{}) error {
return nil
}, func(i *usecase.IOInteractor) {
i.SetTags("foo")
})
assert.Equal(t, "swaggest/usecase_test.TestNewIOI", u.Name())
assert.Equal(t, "Test New IOI", u.Title())
assert.Equal(t, []string{"foo"}, u.Tags())
u = (&Foo{}).Bar()
assert.Equal(t, "swaggest/usecase_test.(*Foo).Bar", u.Name())
assert.Equal(t, "Foo Bar", u.Title())
u = Foo{}.Baz()
assert.Equal(t, "swaggest/usecase_test.Foo.Baz", u.Name())
assert.Equal(t, "Foo Baz", u.Title())
u = fooBar()
assert.Equal(t, "swaggest/usecase_test.fooBar", u.Name())
assert.Equal(t, "Foo Bar", u.Title())
}
func fooBar() usecase.IOInteractor {
return usecase.NewIOI(nil, nil, func(ctx context.Context, input, output interface{}) error {
return nil
})
}