mtest/massert: rename a bunch of functions
This commit is contained in:
parent
2496b170d8
commit
f5cea76cb7
5
env.test
5
env.test
@ -17,3 +17,8 @@ if [ "$(ps aux | grep '[b]igtable-emulator')" = "" ]; then
|
||||
yes | gcloud beta emulators bigtable start --host-port 127.0.0.1:8086 >/dev/null 2>&1 &
|
||||
fi
|
||||
$(gcloud beta emulators bigtable env-init)
|
||||
|
||||
if ! (sudo systemctl status mysqld 1>/dev/null); then
|
||||
echo "starting mysqld"
|
||||
sudo systemctl start mysqld
|
||||
fi
|
||||
|
@ -39,14 +39,14 @@ func TestServiceCtx(t *T) {
|
||||
|
||||
mlog.From(ctxA).Info("foo", ctxA)
|
||||
mlog.From(ctxA).Debug("bar", ctxA)
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Len(msgs, 2),
|
||||
massert.Require(t,
|
||||
massert.Length(msgs, 2),
|
||||
massert.Equal(msgs[0].Level.String(), "INFO"),
|
||||
massert.Equal(msgs[0].Description, "foo"),
|
||||
massert.Equal(msgs[0].Contexts, []context.Context{ctxA}),
|
||||
massert.Equal(msgs[1].Level.String(), "DEBUG"),
|
||||
massert.Equal(msgs[1].Description, "bar"),
|
||||
massert.Equal(msgs[1].Contexts, []context.Context{ctxA}),
|
||||
))
|
||||
)
|
||||
})
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ func (scs srcCommonState) assert(s Source) error {
|
||||
return err
|
||||
}
|
||||
return massert.All(
|
||||
massert.Len(gotPVs, len(scs.expPVs)),
|
||||
massert.Length(gotPVs, len(scs.expPVs)),
|
||||
massert.Subset(scs.expPVs, gotPVs),
|
||||
).Assert()
|
||||
}
|
||||
@ -163,12 +163,12 @@ func TestSources(t *T) {
|
||||
SourceCLI{Args: []string{"--a=1", "--b=666"}},
|
||||
SourceEnv{Env: []string{"B=2", "C=3"}},
|
||||
})
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Nil(err),
|
||||
massert.Equal(1, *a),
|
||||
massert.Equal(2, *b),
|
||||
massert.Equal(3, *c),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
func TestSourceParamValues(t *T) {
|
||||
@ -184,10 +184,10 @@ func TestSourceParamValues(t *T) {
|
||||
{Path: []string{"foo"}, Name: "b", Value: json.RawMessage(`"bbb"`)},
|
||||
{Path: []string{"foo"}, Name: "c", Value: json.RawMessage("true")},
|
||||
})
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Nil(err),
|
||||
massert.Equal(4, *a),
|
||||
massert.Equal("bbb", *b),
|
||||
massert.Equal(true, *c),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
@ -20,19 +20,19 @@ func TestAnnotate(t *T) {
|
||||
|
||||
parentAnnotations := Annotations(parent)
|
||||
childAnnotations := Annotations(child)
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Len(parentAnnotations, 2),
|
||||
massert.Has(parentAnnotations, Annotation{Key: "a", Value: "foo"}),
|
||||
massert.Has(parentAnnotations, Annotation{Key: "b", Value: "bar"}),
|
||||
massert.Require(t,
|
||||
massert.Length(parentAnnotations, 2),
|
||||
massert.HasValue(parentAnnotations, Annotation{Key: "a", Value: "foo"}),
|
||||
massert.HasValue(parentAnnotations, Annotation{Key: "b", Value: "bar"}),
|
||||
|
||||
massert.Len(childAnnotations, 4),
|
||||
massert.Has(childAnnotations, Annotation{Key: "a", Value: "foo"}),
|
||||
massert.Has(childAnnotations, Annotation{Key: "b", Value: "bar"}),
|
||||
massert.Has(childAnnotations,
|
||||
massert.Length(childAnnotations, 4),
|
||||
massert.HasValue(childAnnotations, Annotation{Key: "a", Value: "foo"}),
|
||||
massert.HasValue(childAnnotations, Annotation{Key: "b", Value: "bar"}),
|
||||
massert.HasValue(childAnnotations,
|
||||
Annotation{Key: "a", Path: []string{"child"}, Value: "FOO"}),
|
||||
massert.Has(childAnnotations,
|
||||
massert.HasValue(childAnnotations,
|
||||
Annotation{Key: "c", Path: []string{"child"}, Value: "BAZ"}),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
func TestAnnotationsStringMap(t *T) {
|
||||
@ -46,7 +46,7 @@ func TestAnnotationsStringMap(t *T) {
|
||||
{Key: B(2), Path: []string{"foo"}, Value: "TWO"},
|
||||
}
|
||||
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal(map[string]string{
|
||||
"0": "zero",
|
||||
"1(/)": "one",
|
||||
@ -65,7 +65,7 @@ func TestAnnotationsStringMap(t *T) {
|
||||
"2(mctx.B)": "TWO",
|
||||
},
|
||||
}, aa.StringMapByPath()),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
func TestMergeAnnotations(t *T) {
|
||||
|
@ -18,19 +18,19 @@ func TestInheritance(t *T) {
|
||||
ctx = WithChild(ctx, ctx1)
|
||||
ctx = WithChild(ctx, ctx2)
|
||||
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Len(Path(ctx), 0),
|
||||
massert.Require(t,
|
||||
massert.Length(Path(ctx), 0),
|
||||
massert.Equal(Path(ctx1), []string{"1"}),
|
||||
massert.Equal(Path(ctx1a), []string{"1", "a"}),
|
||||
massert.Equal(Path(ctx1b), []string{"1", "b"}),
|
||||
massert.Equal(Path(ctx2), []string{"2"}),
|
||||
))
|
||||
)
|
||||
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal([]context.Context{ctx1, ctx2}, Children(ctx)),
|
||||
massert.Equal([]context.Context{ctx1a, ctx1b}, Children(ctx1)),
|
||||
massert.Len(Children(ctx2), 0),
|
||||
))
|
||||
massert.Length(Children(ctx2), 0),
|
||||
)
|
||||
}
|
||||
|
||||
func TestBreadFirstVisit(t *T) {
|
||||
@ -50,7 +50,7 @@ func TestBreadFirstVisit(t *T) {
|
||||
got = append(got, ctx)
|
||||
return true
|
||||
})
|
||||
massert.Fatal(t,
|
||||
massert.Require(t,
|
||||
massert.Equal([]context.Context{ctx, ctx1, ctx2, ctx1a, ctx1b}, got),
|
||||
)
|
||||
}
|
||||
@ -64,7 +64,7 @@ func TestBreadFirstVisit(t *T) {
|
||||
got = append(got, ctx)
|
||||
return true
|
||||
})
|
||||
massert.Fatal(t,
|
||||
massert.Require(t,
|
||||
massert.Equal([]context.Context{ctx, ctx1, ctx2}, got),
|
||||
)
|
||||
}
|
||||
@ -74,44 +74,44 @@ func TestLocalValues(t *T) {
|
||||
|
||||
// test with no value set
|
||||
ctx := context.Background()
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Nil(LocalValue(ctx, "foo")),
|
||||
massert.Len(LocalValues(ctx), 0),
|
||||
))
|
||||
massert.Length(LocalValues(ctx), 0),
|
||||
)
|
||||
|
||||
// test basic value retrieval
|
||||
ctx = WithLocalValue(ctx, "foo", "bar")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("bar", LocalValue(ctx, "foo")),
|
||||
massert.Equal(
|
||||
map[interface{}]interface{}{"foo": "bar"},
|
||||
LocalValues(ctx),
|
||||
),
|
||||
))
|
||||
)
|
||||
|
||||
// test that doesn't conflict with WithValue
|
||||
ctx = context.WithValue(ctx, "foo", "WithValue bar")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("bar", LocalValue(ctx, "foo")),
|
||||
massert.Equal("WithValue bar", ctx.Value("foo")),
|
||||
massert.Equal(
|
||||
map[interface{}]interface{}{"foo": "bar"},
|
||||
LocalValues(ctx),
|
||||
),
|
||||
))
|
||||
)
|
||||
|
||||
// test that child doesn't get values
|
||||
child := NewChild(ctx, "child")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("bar", LocalValue(ctx, "foo")),
|
||||
massert.Nil(LocalValue(child, "foo")),
|
||||
massert.Len(LocalValues(child), 0),
|
||||
))
|
||||
massert.Length(LocalValues(child), 0),
|
||||
)
|
||||
|
||||
// test that values on child don't affect parent values
|
||||
child = WithLocalValue(child, "foo", "child bar")
|
||||
ctx = WithChild(ctx, child)
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("bar", LocalValue(ctx, "foo")),
|
||||
massert.Equal("child bar", LocalValue(child, "foo")),
|
||||
massert.Equal(
|
||||
@ -122,12 +122,12 @@ func TestLocalValues(t *T) {
|
||||
map[interface{}]interface{}{"foo": "child bar"},
|
||||
LocalValues(child),
|
||||
),
|
||||
))
|
||||
)
|
||||
|
||||
// test that two With calls on the same context generate distinct contexts
|
||||
childA := WithLocalValue(child, "foo2", "baz")
|
||||
childB := WithLocalValue(child, "foo2", "buz")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("bar", LocalValue(ctx, "foo")),
|
||||
massert.Equal("child bar", LocalValue(child, "foo")),
|
||||
massert.Nil(LocalValue(child, "foo2")),
|
||||
@ -141,16 +141,16 @@ func TestLocalValues(t *T) {
|
||||
map[interface{}]interface{}{"foo": "child bar", "foo2": "buz"},
|
||||
LocalValues(childB),
|
||||
),
|
||||
))
|
||||
)
|
||||
|
||||
// if a value overwrites a previous one the newer one should show in
|
||||
// LocalValues
|
||||
ctx = WithLocalValue(ctx, "foo", "barbar")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("barbar", LocalValue(ctx, "foo")),
|
||||
massert.Equal(
|
||||
map[interface{}]interface{}{"foo": "barbar"},
|
||||
LocalValues(ctx),
|
||||
),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
@ -35,10 +35,10 @@ func TestBasic(t *T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readColFam := readRow[colFam]
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Len(readColFam, 1),
|
||||
massert.Require(t,
|
||||
massert.Length(readColFam, 1),
|
||||
massert.Equal(colFam+":col", readColFam[0].Column),
|
||||
massert.Equal([]byte("bar"), readColFam[0].Value),
|
||||
))
|
||||
)
|
||||
})
|
||||
}
|
||||
|
@ -35,6 +35,6 @@ func TestBasic(t *T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
massert.Fatal(t, massert.Equal(val, val2))
|
||||
massert.Require(t, massert.Equal(val, val2))
|
||||
})
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestError(t *T) {
|
||||
massert.Fatal(t, massert.Nil(Wrap(nil)))
|
||||
massert.Require(t, massert.Nil(Wrap(nil)))
|
||||
|
||||
ctx := mctx.Annotate(context.Background(),
|
||||
"a", "aaa aaa\n",
|
||||
@ -25,8 +25,8 @@ func TestError(t *T) {
|
||||
ccc
|
||||
ccc
|
||||
* d: weird key but ok
|
||||
* errLoc: merr/merr_test.go:19`
|
||||
massert.Fatal(t, massert.Equal(exp, e.Error()))
|
||||
* errLoc: merr/merr_test.go:21`
|
||||
massert.Require(t, massert.Equal(exp, e.Error()))
|
||||
}
|
||||
|
||||
{
|
||||
@ -37,15 +37,15 @@ func TestError(t *T) {
|
||||
ccc
|
||||
ccc
|
||||
* d: weird key but ok
|
||||
* errLoc: merr/merr_test.go:31`
|
||||
massert.Fatal(t, massert.Equal(exp, e.Error()))
|
||||
* errLoc: merr/merr_test.go:33`
|
||||
massert.Require(t, massert.Equal(exp, e.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBase(t *T) {
|
||||
errFoo, errBar := errors.New("foo"), errors.New("bar")
|
||||
erFoo := Wrap(errFoo)
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Nil(Base(nil)),
|
||||
massert.Equal(errFoo, Base(erFoo)),
|
||||
massert.Equal(errBar, Base(errBar)),
|
||||
@ -53,29 +53,29 @@ func TestBase(t *T) {
|
||||
massert.Not(massert.Equal(errBar, Base(erFoo))),
|
||||
massert.Equal(true, Equal(errFoo, erFoo)),
|
||||
massert.Equal(false, Equal(errBar, erFoo)),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
func TestValue(t *T) {
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Nil(WithValue(nil, "foo", "bar")),
|
||||
massert.Nil(Value(nil, "foo")),
|
||||
))
|
||||
)
|
||||
|
||||
e1 := New("foo")
|
||||
e1 = WithValue(e1, "a", "A")
|
||||
e2 := WithValue(errors.New("bar"), "a", "A")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("A", Value(e1, "a")),
|
||||
massert.Equal("A", Value(e2, "a")),
|
||||
))
|
||||
)
|
||||
|
||||
e3 := WithValue(e2, "a", "AAA")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("A", Value(e1, "a")),
|
||||
massert.Equal("A", Value(e2, "a")),
|
||||
massert.Equal("AAA", Value(e3, "a")),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
func mkErr(ctx context.Context, err error) error {
|
||||
@ -96,7 +96,7 @@ func TestCtx(t *T) {
|
||||
"1": "ONE",
|
||||
"2": "TWO",
|
||||
"err": "hello",
|
||||
"errLoc": "merr/merr_test.go:80",
|
||||
"errLoc": "merr/merr_test.go:82",
|
||||
}, mctx.Annotations(Context(e)).StringMap()).Assert()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -10,17 +10,17 @@ import (
|
||||
func TestStack(t *T) {
|
||||
foo := New("test")
|
||||
fooStack, ok := Stack(foo)
|
||||
massert.Fatal(t, massert.Equal(true, ok))
|
||||
massert.Require(t, massert.Equal(true, ok))
|
||||
|
||||
// test Frame
|
||||
frame := fooStack.Frame()
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal(true, strings.Contains(frame.File, "stack_test.go")),
|
||||
massert.Equal(true, strings.Contains(frame.Function, "TestStack")),
|
||||
))
|
||||
)
|
||||
|
||||
frames := fooStack.Frames()
|
||||
massert.Fatal(t, massert.Comment(
|
||||
massert.Require(t, massert.Comment(
|
||||
massert.All(
|
||||
massert.Equal(true, len(frames) >= 2),
|
||||
massert.Equal(true, strings.Contains(frames[0].File, "stack_test.go")),
|
||||
@ -34,7 +34,7 @@ func TestStack(t *T) {
|
||||
bar := WithStack(foo, 1)
|
||||
barStack, _ := Stack(bar)
|
||||
frames := barStack.Frames()
|
||||
massert.Fatal(t, massert.Comment(
|
||||
massert.Require(t, massert.Comment(
|
||||
massert.All(
|
||||
massert.Equal(true, len(frames) >= 2),
|
||||
massert.Equal(true, strings.Contains(frames[0].File, "stack_test.go")),
|
||||
|
@ -45,17 +45,17 @@ func TestAddXForwardedFor(t *T) {
|
||||
AddXForwardedFor(r, ipStr)
|
||||
var a massert.Assertion
|
||||
if expected == "" {
|
||||
a = massert.Len(r.Header["X-Forwarded-For"], 0)
|
||||
a = massert.Length(r.Header["X-Forwarded-For"], 0)
|
||||
} else {
|
||||
a = massert.All(
|
||||
massert.Len(r.Header["X-Forwarded-For"], 1),
|
||||
massert.Length(r.Header["X-Forwarded-For"], 1),
|
||||
massert.Equal(expected, r.Header["X-Forwarded-For"][0]),
|
||||
)
|
||||
}
|
||||
return massert.Comment(a, "prev:%#v ipStr:%q", prev, ipStr)
|
||||
}
|
||||
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
assertXFF(nil, "invalid", ""),
|
||||
assertXFF(nil, "::1", ""),
|
||||
assertXFF([]string{"8.0.0.0"}, "invalid", "8.0.0.0"),
|
||||
@ -66,5 +66,5 @@ func TestAddXForwardedFor(t *T) {
|
||||
assertXFF([]string{"8.0.0.0, 8.0.0.1"}, "8.0.0.2", "8.0.0.0, 8.0.0.1, 8.0.0.2"),
|
||||
assertXFF([]string{"8.0.0.0, 8.0.0.1", "8.0.0.2"}, "8.0.0.3",
|
||||
"8.0.0.0, 8.0.0.1, 8.0.0.2, 8.0.0.3"),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
@ -12,11 +12,11 @@ import (
|
||||
)
|
||||
|
||||
func TestTruncate(t *T) {
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Equal("abc", Truncate("abc", 4)),
|
||||
massert.Equal("abc", Truncate("abc", 3)),
|
||||
massert.Equal("ab...", Truncate("abc", 2)),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
func TestLogger(t *T) {
|
||||
@ -31,7 +31,7 @@ func TestLogger(t *T) {
|
||||
select {
|
||||
case <-l.testMsgWrittenCh:
|
||||
case <-time.After(1 * time.Second):
|
||||
return massert.Errf("waited too long for msg to write")
|
||||
return massert.Errorf("waited too long for msg to write")
|
||||
}
|
||||
out, err := buf.ReadString('\n')
|
||||
return massert.All(
|
||||
@ -45,11 +45,11 @@ func TestLogger(t *T) {
|
||||
l.Info("bar")
|
||||
l.Warn("baz")
|
||||
l.Error("buz")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
assertOut(`{"level":"INFO","descr":"bar"}`),
|
||||
assertOut(`{"level":"WARN","descr":"baz"}`),
|
||||
assertOut(`{"level":"ERROR","descr":"buz"}`),
|
||||
))
|
||||
)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
@ -58,10 +58,10 @@ func TestLogger(t *T) {
|
||||
l.Info("bar")
|
||||
l.Warn("baz")
|
||||
l.Error("buz", mctx.Annotate(ctx, "a", "b", "c", "d"))
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
assertOut(`{"level":"WARN","descr":"baz"}`),
|
||||
assertOut(`{"level":"ERROR","descr":"buz","annotations":{"/":{"a":"b","c":"d"}}}`),
|
||||
))
|
||||
)
|
||||
|
||||
l2 := l.Clone()
|
||||
l2.SetMaxLevel(InfoLevel)
|
||||
@ -72,9 +72,9 @@ func TestLogger(t *T) {
|
||||
l2.Info("bar")
|
||||
l2.Warn("baz")
|
||||
l.Error("buz")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
assertOut(`{"level":"INFO","descr":"BAR"}`),
|
||||
assertOut(`{"level":"WARN","descr":"BAZ"}`),
|
||||
assertOut(`{"level":"ERROR","descr":"buz"}`),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
@ -20,15 +20,15 @@ func TestIsReservedIP(t *T) {
|
||||
"ip:%q", ipStr)
|
||||
}
|
||||
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
assertReserved("127.0.0.1"),
|
||||
assertReserved("::ffff:127.0.0.1"),
|
||||
assertReserved("192.168.40.50"),
|
||||
assertReserved("::1"),
|
||||
assertReserved("100::1"),
|
||||
))
|
||||
)
|
||||
|
||||
massert.Fatal(t, massert.None(
|
||||
massert.Require(t, massert.None(
|
||||
assertReserved("8.8.8.8"),
|
||||
assertReserved("::ffff:8.8.8.8"),
|
||||
assertReserved("2600:1700:7580:6e80:21c:25ff:fe97:44df"),
|
||||
|
@ -37,14 +37,14 @@ func TestHooks(t *T) {
|
||||
|
||||
ctx = WithHook(ctx, 0, mkHook(7))
|
||||
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Nil(TriggerHooks(ctx, 0)),
|
||||
massert.Equal([]int{1, 2, 3, 4, 5, 6, 7}, out),
|
||||
))
|
||||
)
|
||||
|
||||
out = nil
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Require(t,
|
||||
massert.Nil(TriggerHooksReverse(ctx, 0)),
|
||||
massert.Equal([]int{7, 6, 5, 4, 3, 2, 1}, out),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
@ -151,23 +151,23 @@ func (a *assertion) Stack() []runtime.Frame {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Fatal is a convenience function which performs the Assertion and calls Fatal
|
||||
// on the testing.T instance if the assertion fails.
|
||||
//
|
||||
// TODO rename to Require
|
||||
func Fatal(t *testing.T, a Assertion) {
|
||||
if err := a.Assert(); err != nil {
|
||||
t.Fatal(err)
|
||||
// Require is a convenience function which performs the Assertions and calls
|
||||
// Fatal on the testing.T instance for the first Assertion which fails.
|
||||
func Require(t *testing.T, aa ...Assertion) {
|
||||
for _, a := range aa {
|
||||
if err := a.Assert(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Error is a convenience function which performs the Assertion and calls Error
|
||||
// on the testing.T instance if the assertion fails.
|
||||
//
|
||||
// TODO rename to Assert
|
||||
func Error(t *testing.T, a Assertion) {
|
||||
if err := a.Assert(); err != nil {
|
||||
t.Error(err)
|
||||
// Assert is a convenience function which performs the Assertion and calls Error
|
||||
// on the testing.T instance for the first Assertion which fails.
|
||||
func Assert(t *testing.T, aa ...Assertion) {
|
||||
for _, a := range aa {
|
||||
if err := a.Assert(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -291,18 +291,14 @@ func None(aa ...Assertion) Assertion {
|
||||
return newAssertion(fn, fmtMultiDescr("None", aa...), 0)
|
||||
}
|
||||
|
||||
// Err returns an Assertion which always fails with the given error.
|
||||
//
|
||||
// TODO rename to Error
|
||||
func Err(err error) Assertion {
|
||||
// Error returns an Assertion which always fails with the given error.
|
||||
func Error(err error) Assertion {
|
||||
return newAssertion(func() error { return err }, "", 0)
|
||||
}
|
||||
|
||||
// Errf is like Err but allows for a formatted string.
|
||||
//
|
||||
// TODO rename to Errorf
|
||||
func Errf(str string, args ...interface{}) Assertion {
|
||||
return Err(fmt.Errorf(str, args...))
|
||||
// Errorf is like Err but allows for a formatted string.
|
||||
func Errorf(str string, args ...interface{}) Assertion {
|
||||
return Error(fmt.Errorf(str, args...))
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@ -313,10 +309,6 @@ func toStr(i interface{}) string {
|
||||
|
||||
// Equal asserts that the two values are exactly equal, and uses the
|
||||
// reflect.DeepEqual function to determine if they are.
|
||||
//
|
||||
// TODO this does not currently handle the case of creating the Assertion using
|
||||
// a reference type (like a map), changing one of the map's keys, and then
|
||||
// calling Assert.
|
||||
func Equal(a, b interface{}) Assertion {
|
||||
return newAssertion(func() error {
|
||||
if !reflect.DeepEqual(a, b) {
|
||||
@ -409,12 +401,10 @@ func Subset(set, subset interface{}) Assertion {
|
||||
}, toStr(set)+" has subset "+toStr(subset), 0)
|
||||
}
|
||||
|
||||
// Has asserts that the given set has the given element as a value in it. The
|
||||
// set may be an array, a slice, or a map, and if it's a map then the elem will
|
||||
// need to be a value in it.
|
||||
//
|
||||
// TODO rename to HasValue
|
||||
func Has(set, elem interface{}) Assertion {
|
||||
// HasValue asserts that the given set has the given element as a value in it.
|
||||
// The set may be an array, a slice, or a map, and if it's a map then the elem
|
||||
// will need to be a value in it.
|
||||
func HasValue(set, elem interface{}) Assertion {
|
||||
setVV, err := toSet(set, false)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -450,12 +440,10 @@ func HasKey(set, elem interface{}) Assertion {
|
||||
}, toStr(set)+" has key "+toStr(elem), 0)
|
||||
}
|
||||
|
||||
// Len asserts that the given set has the given number of elements in it. The
|
||||
// Length asserts that the given set has the given number of elements in it. The
|
||||
// set may be an array, a slice, or a map. A nil value'd set is considered to be
|
||||
// a length of zero.
|
||||
//
|
||||
// TODO rename to Length
|
||||
func Len(set interface{}, length int) Assertion {
|
||||
func Length(set interface{}, length int) Assertion {
|
||||
setVV, err := toSet(set, false)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -96,12 +96,12 @@ func TestNone(t *T) {
|
||||
}
|
||||
|
||||
func TestEqual(t *T) {
|
||||
Fatal(t, All(
|
||||
Require(t,
|
||||
Equal(1, 1),
|
||||
Equal("foo", "foo"),
|
||||
))
|
||||
)
|
||||
|
||||
Fatal(t, None(
|
||||
Require(t, None(
|
||||
Equal(1, 2),
|
||||
Equal(1, int64(1)),
|
||||
Equal(1, uint64(1)),
|
||||
@ -116,20 +116,20 @@ func TestEqual(t *T) {
|
||||
aa = append(aa, Equal(1, m["foo"]))
|
||||
m["foo"] = 2
|
||||
aa = append(aa, Equal(2, m["foo"]))
|
||||
Fatal(t, All(aa...))
|
||||
Require(t, aa...)
|
||||
}
|
||||
|
||||
func TestNil(t *T) {
|
||||
Fatal(t, All(
|
||||
Require(t,
|
||||
Nil(nil),
|
||||
Nil([]byte(nil)),
|
||||
Nil(map[int]int(nil)),
|
||||
Nil((*struct{})(nil)),
|
||||
Nil(interface{}(nil)),
|
||||
Nil(error(nil)),
|
||||
))
|
||||
)
|
||||
|
||||
Fatal(t, None(
|
||||
Require(t, None(
|
||||
Nil(1),
|
||||
Nil([]byte("foo")),
|
||||
Nil(map[int]int{1: 1}),
|
||||
@ -140,7 +140,7 @@ func TestNil(t *T) {
|
||||
}
|
||||
|
||||
func TestSubset(t *T) {
|
||||
Fatal(t, All(
|
||||
Require(t,
|
||||
Subset([]int{1, 2, 3}, []int{}),
|
||||
Subset([]int{1, 2, 3}, []int{1}),
|
||||
Subset([]int{1, 2, 3}, []int{2}),
|
||||
@ -152,9 +152,9 @@ func TestSubset(t *T) {
|
||||
Subset(map[int]int{1: 1, 2: 2}, map[int]int{}),
|
||||
Subset(map[int]int{1: 1, 2: 2}, map[int]int{1: 1}),
|
||||
Subset(map[int]int{1: 1, 2: 2}, map[int]int{1: 1, 2: 2}),
|
||||
))
|
||||
)
|
||||
|
||||
Fatal(t, None(
|
||||
Require(t, None(
|
||||
Subset([]int{}, []int{1, 2, 3}),
|
||||
Subset([]int{1, 2, 3}, []int{4}),
|
||||
Subset([]int{1, 2, 3}, []int{1, 3, 2, 4}),
|
||||
@ -167,45 +167,45 @@ func TestSubset(t *T) {
|
||||
m := map[int]int{1: 1, 2: 2}
|
||||
a := Subset(m, map[int]int{1: 1})
|
||||
m[1] = 2
|
||||
Fatal(t, a)
|
||||
Require(t, a)
|
||||
}
|
||||
|
||||
func TestHas(t *T) {
|
||||
Fatal(t, All(
|
||||
Has([]int{1}, 1),
|
||||
Has([]int{1, 2}, 1),
|
||||
Has([]int{2, 1}, 1),
|
||||
Has(map[int]int{1: 1}, 1),
|
||||
Has(map[int]int{1: 2}, 2),
|
||||
Has(map[int]int{1: 2, 2: 1}, 1),
|
||||
Has(map[int]int{1: 2, 2: 2}, 2),
|
||||
))
|
||||
func TestHasValue(t *T) {
|
||||
Require(t,
|
||||
HasValue([]int{1}, 1),
|
||||
HasValue([]int{1, 2}, 1),
|
||||
HasValue([]int{2, 1}, 1),
|
||||
HasValue(map[int]int{1: 1}, 1),
|
||||
HasValue(map[int]int{1: 2}, 2),
|
||||
HasValue(map[int]int{1: 2, 2: 1}, 1),
|
||||
HasValue(map[int]int{1: 2, 2: 2}, 2),
|
||||
)
|
||||
|
||||
Fatal(t, None(
|
||||
Has([]int{}, 1),
|
||||
Has([]int{1}, 2),
|
||||
Has([]int{2, 1}, 3),
|
||||
Has(map[int]int{}, 1),
|
||||
Has(map[int]int{1: 1}, 2),
|
||||
Has(map[int]int{1: 2}, 1),
|
||||
Has(map[int]int{1: 2, 2: 1}, 3),
|
||||
Require(t, None(
|
||||
HasValue([]int{}, 1),
|
||||
HasValue([]int{1}, 2),
|
||||
HasValue([]int{2, 1}, 3),
|
||||
HasValue(map[int]int{}, 1),
|
||||
HasValue(map[int]int{1: 1}, 2),
|
||||
HasValue(map[int]int{1: 2}, 1),
|
||||
HasValue(map[int]int{1: 2, 2: 1}, 3),
|
||||
))
|
||||
|
||||
// make sure changes don't retroactively fail the assertion
|
||||
m := map[int]int{1: 1}
|
||||
a := Has(m, 1)
|
||||
a := HasValue(m, 1)
|
||||
m[1] = 2
|
||||
Fatal(t, a)
|
||||
Require(t, a)
|
||||
}
|
||||
|
||||
func TestHasKey(t *T) {
|
||||
Fatal(t, All(
|
||||
Require(t,
|
||||
HasKey(map[int]int{1: 1}, 1),
|
||||
HasKey(map[int]int{1: 1, 2: 2}, 1),
|
||||
HasKey(map[int]int{1: 1, 2: 2}, 2),
|
||||
))
|
||||
)
|
||||
|
||||
Fatal(t, None(
|
||||
Require(t, None(
|
||||
HasKey(map[int]int{}, 1),
|
||||
HasKey(map[int]int{2: 2}, 1),
|
||||
))
|
||||
@ -214,36 +214,36 @@ func TestHasKey(t *T) {
|
||||
m := map[int]int{1: 1}
|
||||
a := HasKey(m, 1)
|
||||
delete(m, 1)
|
||||
Fatal(t, a)
|
||||
Require(t, a)
|
||||
|
||||
}
|
||||
|
||||
func TestLen(t *T) {
|
||||
Fatal(t, All(
|
||||
Len([]int(nil), 0),
|
||||
Len([]int{}, 0),
|
||||
Len([]int{1}, 1),
|
||||
Len([]int{1, 2}, 2),
|
||||
Len(map[int]int(nil), 0),
|
||||
Len(map[int]int{}, 0),
|
||||
Len(map[int]int{1: 1}, 1),
|
||||
Len(map[int]int{1: 1, 2: 2}, 2),
|
||||
))
|
||||
func TestLength(t *T) {
|
||||
Require(t,
|
||||
Length([]int(nil), 0),
|
||||
Length([]int{}, 0),
|
||||
Length([]int{1}, 1),
|
||||
Length([]int{1, 2}, 2),
|
||||
Length(map[int]int(nil), 0),
|
||||
Length(map[int]int{}, 0),
|
||||
Length(map[int]int{1: 1}, 1),
|
||||
Length(map[int]int{1: 1, 2: 2}, 2),
|
||||
)
|
||||
|
||||
Fatal(t, None(
|
||||
Len([]int(nil), 1),
|
||||
Len([]int{}, 1),
|
||||
Len([]int{1}, 0),
|
||||
Len([]int{1}, 2),
|
||||
Len([]int{1, 2}, 1),
|
||||
Len([]int{1, 2}, 3),
|
||||
Len(map[int]int(nil), 1),
|
||||
Len(map[int]int{}, 1),
|
||||
Require(t, None(
|
||||
Length([]int(nil), 1),
|
||||
Length([]int{}, 1),
|
||||
Length([]int{1}, 0),
|
||||
Length([]int{1}, 2),
|
||||
Length([]int{1, 2}, 1),
|
||||
Length([]int{1, 2}, 3),
|
||||
Length(map[int]int(nil), 1),
|
||||
Length(map[int]int{}, 1),
|
||||
))
|
||||
|
||||
// make sure changes don't retroactively fail the assertion
|
||||
m := map[int]int{1: 1}
|
||||
a := Len(m, 1)
|
||||
a := Length(m, 1)
|
||||
m[2] = 2
|
||||
Fatal(t, a)
|
||||
Require(t, a)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user