-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_test.go
More file actions
155 lines (118 loc) · 3.14 KB
/
debug_test.go
File metadata and controls
155 lines (118 loc) · 3.14 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package needle_test
import (
"bytes"
"context"
"strings"
"testing"
"github.com/danpasecinic/needle"
)
func TestPrintGraphEmpty(t *testing.T) {
t.Parallel()
c := needle.New()
var buf bytes.Buffer
c.FprintGraph(&buf)
if !strings.Contains(buf.String(), "empty container") {
t.Errorf("expected empty container message, got: %s", buf.String())
}
}
func TestPrintGraph(t *testing.T) {
t.Parallel()
c := needle.New()
_ = needle.ProvideValue(c, &Config{Port: 8080})
_ = needle.Provide(
c, func(ctx context.Context, r needle.Resolver) (*Database, error) {
_ = needle.MustInvoke[*Config](c)
return &Database{}, nil
},
)
var buf bytes.Buffer
c.FprintGraph(&buf)
output := buf.String()
if !strings.Contains(output, "Config") {
t.Errorf("expected Config in output, got: %s", output)
}
if !strings.Contains(output, "Database") {
t.Errorf("expected Database in output, got: %s", output)
}
}
func TestPrintGraphWithInstantiated(t *testing.T) {
t.Parallel()
c := needle.New()
_ = needle.ProvideValue(c, &Config{Port: 8080})
_ = needle.MustInvoke[*Config](c)
var buf bytes.Buffer
c.FprintGraph(&buf)
output := buf.String()
if !strings.Contains(output, "●") {
t.Errorf("expected instantiated marker (●), got: %s", output)
}
}
func TestPrintGraphNotInstantiated(t *testing.T) {
t.Parallel()
c := needle.New()
_ = needle.Provide(
c, func(ctx context.Context, r needle.Resolver) (*Config, error) {
return &Config{Port: 8080}, nil
},
)
var buf bytes.Buffer
c.FprintGraph(&buf)
output := buf.String()
if !strings.Contains(output, "○") {
t.Errorf("expected not-instantiated marker (○), got: %s", output)
}
}
func TestSprintGraph(t *testing.T) {
t.Parallel()
c := needle.New()
_ = needle.ProvideValue(c, &Config{Port: 8080})
output := c.SprintGraph()
if output == "" {
t.Error("expected non-empty output")
}
}
func TestPrintGraphDOT(t *testing.T) {
t.Parallel()
c := needle.New()
_ = needle.ProvideValue(c, &Config{Port: 8080})
_ = needle.Provide(
c, func(ctx context.Context, r needle.Resolver) (*Database, error) {
return &Database{}, nil
}, needle.WithDependencies("*needle_test.Config"),
)
var buf bytes.Buffer
c.FprintGraphDOT(&buf)
output := buf.String()
if !strings.Contains(output, "digraph dependencies") {
t.Errorf("expected digraph header, got: %s", output)
}
if !strings.Contains(output, "rankdir=LR") {
t.Errorf("expected rankdir, got: %s", output)
}
if !strings.Contains(output, "->") {
t.Errorf("expected edge, got: %s", output)
}
}
func TestSprintGraphDOT(t *testing.T) {
t.Parallel()
c := needle.New()
_ = needle.ProvideValue(c, &Config{Port: 8080})
output := c.SprintGraphDOT()
if !strings.Contains(output, "digraph") {
t.Error("expected digraph in output")
}
}
func TestGraphInfo(t *testing.T) {
t.Parallel()
c := needle.New()
_ = needle.ProvideValue(c, &Config{Port: 8080})
_ = needle.Provide(
c, func(ctx context.Context, r needle.Resolver) (*Database, error) {
return &Database{}, nil
}, needle.WithDependencies("*needle_test.Config"),
)
info := c.Graph()
if len(info.Services) != 2 {
t.Errorf("expected 2 services, got %d", len(info.Services))
}
}