Skip to content

Commit 7cc4257

Browse files
committed
Add assertive lib
Signed-off-by: apostasie <[email protected]>
1 parent 3a6ceb2 commit 7cc4257

File tree

3 files changed

+239
-0
lines changed

3 files changed

+239
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package assertive
18+
19+
import (
20+
"errors"
21+
"strings"
22+
"time"
23+
)
24+
25+
type testingT interface {
26+
Helper()
27+
FailNow()
28+
Fail()
29+
Log(args ...interface{})
30+
}
31+
32+
// ErrorIsNil immediately fails a test if err is not nil.
33+
func ErrorIsNil(t testingT, err error, msg ...string) {
34+
t.Helper()
35+
36+
if err != nil {
37+
t.Log("expecting nil error, but got:", err)
38+
failNow(t, msg...)
39+
}
40+
}
41+
42+
// ErrorIs immediately fails a test if err is not the comparison error.
43+
func ErrorIs(t testingT, err, compErr error, msg ...string) {
44+
t.Helper()
45+
46+
if !errors.Is(err, compErr) {
47+
t.Log("expected error to be:", compErr, "- instead it is:", err)
48+
failNow(t, msg...)
49+
}
50+
}
51+
52+
// IsEqual immediately fails a test if the two interfaces are not equal.
53+
func IsEqual(t testingT, actual, expected interface{}, msg ...string) {
54+
t.Helper()
55+
56+
if !isEqual(t, actual, expected) {
57+
t.Log("expected:", actual, " - to be equal to:", expected)
58+
failNow(t, msg...)
59+
}
60+
}
61+
62+
// IsNotEqual immediately fails a test if the two interfaces are equal.
63+
func IsNotEqual(t testingT, actual, expected interface{}, msg ...string) {
64+
t.Helper()
65+
66+
if isEqual(t, actual, expected) {
67+
t.Log("expected:", actual, " - to be equal to:", expected)
68+
failNow(t, msg...)
69+
}
70+
}
71+
72+
// StringContains immediately fails a test if the actual string does not contain the other string.
73+
func StringContains(t testingT, actual, contains string, msg ...string) {
74+
t.Helper()
75+
76+
if !strings.Contains(actual, contains) {
77+
t.Log("expected:", actual, " - to contain:", contains)
78+
failNow(t, msg...)
79+
}
80+
}
81+
82+
// StringDoesNotContain immediately fails a test if the actual string contains the other string.
83+
func StringDoesNotContain(t testingT, actual, contains string, msg ...string) {
84+
t.Helper()
85+
86+
if strings.Contains(actual, contains) {
87+
t.Log("expected:", actual, " - to NOT contain:", contains)
88+
failNow(t, msg...)
89+
}
90+
}
91+
92+
// StringHasSuffix immediately fails a test if the string does not end with suffix.
93+
func StringHasSuffix(t testingT, actual, suffix string, msg ...string) {
94+
t.Helper()
95+
96+
if !strings.HasSuffix(actual, suffix) {
97+
t.Log("expected:", actual, " - to end with:", suffix)
98+
failNow(t, msg...)
99+
}
100+
}
101+
102+
// StringHasPrefix immediately fails a test if the string does not start with prefix.
103+
func StringHasPrefix(t testingT, actual, prefix string, msg ...string) {
104+
t.Helper()
105+
106+
if !strings.HasPrefix(actual, prefix) {
107+
t.Log("expected:", actual, " - to start with:", prefix)
108+
failNow(t, msg...)
109+
}
110+
}
111+
112+
// DurationIsLessThan immediately fails a test if the duration is more than the reference.
113+
func DurationIsLessThan(t testingT, actual, expected time.Duration, msg ...string) {
114+
t.Helper()
115+
116+
if actual >= expected {
117+
t.Log("expected:", actual, " - to be less than:", expected)
118+
failNow(t, msg...)
119+
}
120+
}
121+
122+
// True immediately fails a test if the boolean is not true...
123+
func True(t testingT, comp bool, msg ...string) bool {
124+
t.Helper()
125+
126+
if !comp {
127+
failNow(t, msg...)
128+
}
129+
130+
return comp
131+
}
132+
133+
// Check marks a test as failed if the boolean is not true (safe in go routines)
134+
//
135+
//nolint:varnamelen
136+
func Check(t testingT, comp bool, msg ...string) bool {
137+
t.Helper()
138+
139+
if !comp {
140+
for _, m := range msg {
141+
t.Log(m)
142+
}
143+
144+
t.Fail()
145+
}
146+
147+
return comp
148+
}
149+
150+
//nolint:varnamelen
151+
func failNow(t testingT, msg ...string) {
152+
t.Helper()
153+
154+
if len(msg) > 0 {
155+
for _, m := range msg {
156+
t.Log(m)
157+
}
158+
}
159+
160+
t.FailNow()
161+
}
162+
163+
func isEqual(t testingT, actual, expected interface{}) bool {
164+
t.Helper()
165+
166+
// FIXME: this is risky and limited. Right now this is fine internally, but do better if this
167+
// becomes public.
168+
return actual == expected
169+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package assertive_test
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/containerd/nerdctl/mod/tigron/internal/assertive"
25+
)
26+
27+
func TestY(t *testing.T) {
28+
t.Parallel()
29+
30+
var err error
31+
32+
assertive.ErrorIsNil(t, err)
33+
34+
//nolint:err113
35+
someErr := errors.New("test error")
36+
37+
err = fmt.Errorf("wrap: %w", someErr)
38+
assertive.ErrorIs(t, err, someErr)
39+
40+
foo := "foo"
41+
assertive.IsEqual(t, foo, "foo")
42+
43+
bar := 10
44+
assertive.IsEqual(t, bar, 10)
45+
46+
baz := true
47+
assertive.IsEqual(t, baz, true)
48+
}

mod/tigron/internal/assertive/doc.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package assertive is an experimental, zero-dependencies assert library.
18+
// Right now, it is not public and meant to be used only inside tigron.
19+
// Consumers of tigron are free to use whatever assert library they want.
20+
// In the future, this may become public for peeps who want `assert` to be
21+
// bundled in.
22+
package assertive

0 commit comments

Comments
 (0)