mtest/massert: rename a bunch of functions

This commit is contained in:
Brian Picciano 2019-03-10 19:23:37 -04:00
parent 2496b170d8
commit f5cea76cb7
15 changed files with 174 additions and 181 deletions

View File

@ -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 & yes | gcloud beta emulators bigtable start --host-port 127.0.0.1:8086 >/dev/null 2>&1 &
fi fi
$(gcloud beta emulators bigtable env-init) $(gcloud beta emulators bigtable env-init)
if ! (sudo systemctl status mysqld 1>/dev/null); then
echo "starting mysqld"
sudo systemctl start mysqld
fi

View File

@ -39,14 +39,14 @@ func TestServiceCtx(t *T) {
mlog.From(ctxA).Info("foo", ctxA) mlog.From(ctxA).Info("foo", ctxA)
mlog.From(ctxA).Debug("bar", ctxA) mlog.From(ctxA).Debug("bar", ctxA)
massert.Fatal(t, massert.All( massert.Require(t,
massert.Len(msgs, 2), massert.Length(msgs, 2),
massert.Equal(msgs[0].Level.String(), "INFO"), massert.Equal(msgs[0].Level.String(), "INFO"),
massert.Equal(msgs[0].Description, "foo"), massert.Equal(msgs[0].Description, "foo"),
massert.Equal(msgs[0].Contexts, []context.Context{ctxA}), massert.Equal(msgs[0].Contexts, []context.Context{ctxA}),
massert.Equal(msgs[1].Level.String(), "DEBUG"), massert.Equal(msgs[1].Level.String(), "DEBUG"),
massert.Equal(msgs[1].Description, "bar"), massert.Equal(msgs[1].Description, "bar"),
massert.Equal(msgs[1].Contexts, []context.Context{ctxA}), massert.Equal(msgs[1].Contexts, []context.Context{ctxA}),
)) )
}) })
} }

View File

@ -148,7 +148,7 @@ func (scs srcCommonState) assert(s Source) error {
return err return err
} }
return massert.All( return massert.All(
massert.Len(gotPVs, len(scs.expPVs)), massert.Length(gotPVs, len(scs.expPVs)),
massert.Subset(scs.expPVs, gotPVs), massert.Subset(scs.expPVs, gotPVs),
).Assert() ).Assert()
} }
@ -163,12 +163,12 @@ func TestSources(t *T) {
SourceCLI{Args: []string{"--a=1", "--b=666"}}, SourceCLI{Args: []string{"--a=1", "--b=666"}},
SourceEnv{Env: []string{"B=2", "C=3"}}, SourceEnv{Env: []string{"B=2", "C=3"}},
}) })
massert.Fatal(t, massert.All( massert.Require(t,
massert.Nil(err), massert.Nil(err),
massert.Equal(1, *a), massert.Equal(1, *a),
massert.Equal(2, *b), massert.Equal(2, *b),
massert.Equal(3, *c), massert.Equal(3, *c),
)) )
} }
func TestSourceParamValues(t *T) { 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: "b", Value: json.RawMessage(`"bbb"`)},
{Path: []string{"foo"}, Name: "c", Value: json.RawMessage("true")}, {Path: []string{"foo"}, Name: "c", Value: json.RawMessage("true")},
}) })
massert.Fatal(t, massert.All( massert.Require(t,
massert.Nil(err), massert.Nil(err),
massert.Equal(4, *a), massert.Equal(4, *a),
massert.Equal("bbb", *b), massert.Equal("bbb", *b),
massert.Equal(true, *c), massert.Equal(true, *c),
)) )
} }

View File

@ -20,19 +20,19 @@ func TestAnnotate(t *T) {
parentAnnotations := Annotations(parent) parentAnnotations := Annotations(parent)
childAnnotations := Annotations(child) childAnnotations := Annotations(child)
massert.Fatal(t, massert.All( massert.Require(t,
massert.Len(parentAnnotations, 2), massert.Length(parentAnnotations, 2),
massert.Has(parentAnnotations, Annotation{Key: "a", Value: "foo"}), massert.HasValue(parentAnnotations, Annotation{Key: "a", Value: "foo"}),
massert.Has(parentAnnotations, Annotation{Key: "b", Value: "bar"}), massert.HasValue(parentAnnotations, Annotation{Key: "b", Value: "bar"}),
massert.Len(childAnnotations, 4), massert.Length(childAnnotations, 4),
massert.Has(childAnnotations, Annotation{Key: "a", Value: "foo"}), massert.HasValue(childAnnotations, Annotation{Key: "a", Value: "foo"}),
massert.Has(childAnnotations, Annotation{Key: "b", Value: "bar"}), massert.HasValue(childAnnotations, Annotation{Key: "b", Value: "bar"}),
massert.Has(childAnnotations, massert.HasValue(childAnnotations,
Annotation{Key: "a", Path: []string{"child"}, Value: "FOO"}), Annotation{Key: "a", Path: []string{"child"}, Value: "FOO"}),
massert.Has(childAnnotations, massert.HasValue(childAnnotations,
Annotation{Key: "c", Path: []string{"child"}, Value: "BAZ"}), Annotation{Key: "c", Path: []string{"child"}, Value: "BAZ"}),
)) )
} }
func TestAnnotationsStringMap(t *T) { func TestAnnotationsStringMap(t *T) {
@ -46,7 +46,7 @@ func TestAnnotationsStringMap(t *T) {
{Key: B(2), Path: []string{"foo"}, Value: "TWO"}, {Key: B(2), Path: []string{"foo"}, Value: "TWO"},
} }
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal(map[string]string{ massert.Equal(map[string]string{
"0": "zero", "0": "zero",
"1(/)": "one", "1(/)": "one",
@ -65,7 +65,7 @@ func TestAnnotationsStringMap(t *T) {
"2(mctx.B)": "TWO", "2(mctx.B)": "TWO",
}, },
}, aa.StringMapByPath()), }, aa.StringMapByPath()),
)) )
} }
func TestMergeAnnotations(t *T) { func TestMergeAnnotations(t *T) {

View File

@ -18,19 +18,19 @@ func TestInheritance(t *T) {
ctx = WithChild(ctx, ctx1) ctx = WithChild(ctx, ctx1)
ctx = WithChild(ctx, ctx2) ctx = WithChild(ctx, ctx2)
massert.Fatal(t, massert.All( massert.Require(t,
massert.Len(Path(ctx), 0), massert.Length(Path(ctx), 0),
massert.Equal(Path(ctx1), []string{"1"}), massert.Equal(Path(ctx1), []string{"1"}),
massert.Equal(Path(ctx1a), []string{"1", "a"}), massert.Equal(Path(ctx1a), []string{"1", "a"}),
massert.Equal(Path(ctx1b), []string{"1", "b"}), massert.Equal(Path(ctx1b), []string{"1", "b"}),
massert.Equal(Path(ctx2), []string{"2"}), 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{ctx1, ctx2}, Children(ctx)),
massert.Equal([]context.Context{ctx1a, ctx1b}, Children(ctx1)), massert.Equal([]context.Context{ctx1a, ctx1b}, Children(ctx1)),
massert.Len(Children(ctx2), 0), massert.Length(Children(ctx2), 0),
)) )
} }
func TestBreadFirstVisit(t *T) { func TestBreadFirstVisit(t *T) {
@ -50,7 +50,7 @@ func TestBreadFirstVisit(t *T) {
got = append(got, ctx) got = append(got, ctx)
return true return true
}) })
massert.Fatal(t, massert.Require(t,
massert.Equal([]context.Context{ctx, ctx1, ctx2, ctx1a, ctx1b}, got), massert.Equal([]context.Context{ctx, ctx1, ctx2, ctx1a, ctx1b}, got),
) )
} }
@ -64,7 +64,7 @@ func TestBreadFirstVisit(t *T) {
got = append(got, ctx) got = append(got, ctx)
return true return true
}) })
massert.Fatal(t, massert.Require(t,
massert.Equal([]context.Context{ctx, ctx1, ctx2}, got), massert.Equal([]context.Context{ctx, ctx1, ctx2}, got),
) )
} }
@ -74,44 +74,44 @@ func TestLocalValues(t *T) {
// test with no value set // test with no value set
ctx := context.Background() ctx := context.Background()
massert.Fatal(t, massert.All( massert.Require(t,
massert.Nil(LocalValue(ctx, "foo")), massert.Nil(LocalValue(ctx, "foo")),
massert.Len(LocalValues(ctx), 0), massert.Length(LocalValues(ctx), 0),
)) )
// test basic value retrieval // test basic value retrieval
ctx = WithLocalValue(ctx, "foo", "bar") ctx = WithLocalValue(ctx, "foo", "bar")
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal("bar", LocalValue(ctx, "foo")), massert.Equal("bar", LocalValue(ctx, "foo")),
massert.Equal( massert.Equal(
map[interface{}]interface{}{"foo": "bar"}, map[interface{}]interface{}{"foo": "bar"},
LocalValues(ctx), LocalValues(ctx),
), ),
)) )
// test that doesn't conflict with WithValue // test that doesn't conflict with WithValue
ctx = context.WithValue(ctx, "foo", "WithValue bar") ctx = context.WithValue(ctx, "foo", "WithValue bar")
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal("bar", LocalValue(ctx, "foo")), massert.Equal("bar", LocalValue(ctx, "foo")),
massert.Equal("WithValue bar", ctx.Value("foo")), massert.Equal("WithValue bar", ctx.Value("foo")),
massert.Equal( massert.Equal(
map[interface{}]interface{}{"foo": "bar"}, map[interface{}]interface{}{"foo": "bar"},
LocalValues(ctx), LocalValues(ctx),
), ),
)) )
// test that child doesn't get values // test that child doesn't get values
child := NewChild(ctx, "child") child := NewChild(ctx, "child")
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal("bar", LocalValue(ctx, "foo")), massert.Equal("bar", LocalValue(ctx, "foo")),
massert.Nil(LocalValue(child, "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 // test that values on child don't affect parent values
child = WithLocalValue(child, "foo", "child bar") child = WithLocalValue(child, "foo", "child bar")
ctx = WithChild(ctx, child) ctx = WithChild(ctx, child)
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal("bar", LocalValue(ctx, "foo")), massert.Equal("bar", LocalValue(ctx, "foo")),
massert.Equal("child bar", LocalValue(child, "foo")), massert.Equal("child bar", LocalValue(child, "foo")),
massert.Equal( massert.Equal(
@ -122,12 +122,12 @@ func TestLocalValues(t *T) {
map[interface{}]interface{}{"foo": "child bar"}, map[interface{}]interface{}{"foo": "child bar"},
LocalValues(child), LocalValues(child),
), ),
)) )
// test that two With calls on the same context generate distinct contexts // test that two With calls on the same context generate distinct contexts
childA := WithLocalValue(child, "foo2", "baz") childA := WithLocalValue(child, "foo2", "baz")
childB := WithLocalValue(child, "foo2", "buz") childB := WithLocalValue(child, "foo2", "buz")
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal("bar", LocalValue(ctx, "foo")), massert.Equal("bar", LocalValue(ctx, "foo")),
massert.Equal("child bar", LocalValue(child, "foo")), massert.Equal("child bar", LocalValue(child, "foo")),
massert.Nil(LocalValue(child, "foo2")), massert.Nil(LocalValue(child, "foo2")),
@ -141,16 +141,16 @@ func TestLocalValues(t *T) {
map[interface{}]interface{}{"foo": "child bar", "foo2": "buz"}, map[interface{}]interface{}{"foo": "child bar", "foo2": "buz"},
LocalValues(childB), LocalValues(childB),
), ),
)) )
// if a value overwrites a previous one the newer one should show in // if a value overwrites a previous one the newer one should show in
// LocalValues // LocalValues
ctx = WithLocalValue(ctx, "foo", "barbar") ctx = WithLocalValue(ctx, "foo", "barbar")
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal("barbar", LocalValue(ctx, "foo")), massert.Equal("barbar", LocalValue(ctx, "foo")),
massert.Equal( massert.Equal(
map[interface{}]interface{}{"foo": "barbar"}, map[interface{}]interface{}{"foo": "barbar"},
LocalValues(ctx), LocalValues(ctx),
), ),
)) )
} }

View File

@ -35,10 +35,10 @@ func TestBasic(t *T) {
t.Fatal(err) t.Fatal(err)
} }
readColFam := readRow[colFam] readColFam := readRow[colFam]
massert.Fatal(t, massert.All( massert.Require(t,
massert.Len(readColFam, 1), massert.Length(readColFam, 1),
massert.Equal(colFam+":col", readColFam[0].Column), massert.Equal(colFam+":col", readColFam[0].Column),
massert.Equal([]byte("bar"), readColFam[0].Value), massert.Equal([]byte("bar"), readColFam[0].Value),
)) )
}) })
} }

View File

@ -35,6 +35,6 @@ func TestBasic(t *T) {
t.Fatal(err) t.Fatal(err)
} }
massert.Fatal(t, massert.Equal(val, val2)) massert.Require(t, massert.Equal(val, val2))
}) })
} }

View File

@ -10,7 +10,7 @@ import (
) )
func TestError(t *T) { func TestError(t *T) {
massert.Fatal(t, massert.Nil(Wrap(nil))) massert.Require(t, massert.Nil(Wrap(nil)))
ctx := mctx.Annotate(context.Background(), ctx := mctx.Annotate(context.Background(),
"a", "aaa aaa\n", "a", "aaa aaa\n",
@ -25,8 +25,8 @@ func TestError(t *T) {
ccc ccc
ccc ccc
* d: weird key but ok * d: weird key but ok
* errLoc: merr/merr_test.go:19` * errLoc: merr/merr_test.go:21`
massert.Fatal(t, massert.Equal(exp, e.Error())) massert.Require(t, massert.Equal(exp, e.Error()))
} }
{ {
@ -37,15 +37,15 @@ func TestError(t *T) {
ccc ccc
ccc ccc
* d: weird key but ok * d: weird key but ok
* errLoc: merr/merr_test.go:31` * errLoc: merr/merr_test.go:33`
massert.Fatal(t, massert.Equal(exp, e.Error())) massert.Require(t, massert.Equal(exp, e.Error()))
} }
} }
func TestBase(t *T) { func TestBase(t *T) {
errFoo, errBar := errors.New("foo"), errors.New("bar") errFoo, errBar := errors.New("foo"), errors.New("bar")
erFoo := Wrap(errFoo) erFoo := Wrap(errFoo)
massert.Fatal(t, massert.All( massert.Require(t,
massert.Nil(Base(nil)), massert.Nil(Base(nil)),
massert.Equal(errFoo, Base(erFoo)), massert.Equal(errFoo, Base(erFoo)),
massert.Equal(errBar, Base(errBar)), massert.Equal(errBar, Base(errBar)),
@ -53,29 +53,29 @@ func TestBase(t *T) {
massert.Not(massert.Equal(errBar, Base(erFoo))), massert.Not(massert.Equal(errBar, Base(erFoo))),
massert.Equal(true, Equal(errFoo, erFoo)), massert.Equal(true, Equal(errFoo, erFoo)),
massert.Equal(false, Equal(errBar, erFoo)), massert.Equal(false, Equal(errBar, erFoo)),
)) )
} }
func TestValue(t *T) { func TestValue(t *T) {
massert.Fatal(t, massert.All( massert.Require(t,
massert.Nil(WithValue(nil, "foo", "bar")), massert.Nil(WithValue(nil, "foo", "bar")),
massert.Nil(Value(nil, "foo")), massert.Nil(Value(nil, "foo")),
)) )
e1 := New("foo") e1 := New("foo")
e1 = WithValue(e1, "a", "A") e1 = WithValue(e1, "a", "A")
e2 := WithValue(errors.New("bar"), "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(e1, "a")),
massert.Equal("A", Value(e2, "a")), massert.Equal("A", Value(e2, "a")),
)) )
e3 := WithValue(e2, "a", "AAA") e3 := WithValue(e2, "a", "AAA")
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal("A", Value(e1, "a")), massert.Equal("A", Value(e1, "a")),
massert.Equal("A", Value(e2, "a")), massert.Equal("A", Value(e2, "a")),
massert.Equal("AAA", Value(e3, "a")), massert.Equal("AAA", Value(e3, "a")),
)) )
} }
func mkErr(ctx context.Context, err error) error { func mkErr(ctx context.Context, err error) error {
@ -96,7 +96,7 @@ func TestCtx(t *T) {
"1": "ONE", "1": "ONE",
"2": "TWO", "2": "TWO",
"err": "hello", "err": "hello",
"errLoc": "merr/merr_test.go:80", "errLoc": "merr/merr_test.go:82",
}, mctx.Annotations(Context(e)).StringMap()).Assert() }, mctx.Annotations(Context(e)).StringMap()).Assert()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -10,17 +10,17 @@ import (
func TestStack(t *T) { func TestStack(t *T) {
foo := New("test") foo := New("test")
fooStack, ok := Stack(foo) fooStack, ok := Stack(foo)
massert.Fatal(t, massert.Equal(true, ok)) massert.Require(t, massert.Equal(true, ok))
// test Frame // test Frame
frame := fooStack.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.File, "stack_test.go")),
massert.Equal(true, strings.Contains(frame.Function, "TestStack")), massert.Equal(true, strings.Contains(frame.Function, "TestStack")),
)) )
frames := fooStack.Frames() frames := fooStack.Frames()
massert.Fatal(t, massert.Comment( massert.Require(t, massert.Comment(
massert.All( massert.All(
massert.Equal(true, len(frames) >= 2), massert.Equal(true, len(frames) >= 2),
massert.Equal(true, strings.Contains(frames[0].File, "stack_test.go")), massert.Equal(true, strings.Contains(frames[0].File, "stack_test.go")),
@ -34,7 +34,7 @@ func TestStack(t *T) {
bar := WithStack(foo, 1) bar := WithStack(foo, 1)
barStack, _ := Stack(bar) barStack, _ := Stack(bar)
frames := barStack.Frames() frames := barStack.Frames()
massert.Fatal(t, massert.Comment( massert.Require(t, massert.Comment(
massert.All( massert.All(
massert.Equal(true, len(frames) >= 2), massert.Equal(true, len(frames) >= 2),
massert.Equal(true, strings.Contains(frames[0].File, "stack_test.go")), massert.Equal(true, strings.Contains(frames[0].File, "stack_test.go")),

View File

@ -45,17 +45,17 @@ func TestAddXForwardedFor(t *T) {
AddXForwardedFor(r, ipStr) AddXForwardedFor(r, ipStr)
var a massert.Assertion var a massert.Assertion
if expected == "" { if expected == "" {
a = massert.Len(r.Header["X-Forwarded-For"], 0) a = massert.Length(r.Header["X-Forwarded-For"], 0)
} else { } else {
a = massert.All( 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]), massert.Equal(expected, r.Header["X-Forwarded-For"][0]),
) )
} }
return massert.Comment(a, "prev:%#v ipStr:%q", prev, ipStr) return massert.Comment(a, "prev:%#v ipStr:%q", prev, ipStr)
} }
massert.Fatal(t, massert.All( massert.Require(t,
assertXFF(nil, "invalid", ""), assertXFF(nil, "invalid", ""),
assertXFF(nil, "::1", ""), assertXFF(nil, "::1", ""),
assertXFF([]string{"8.0.0.0"}, "invalid", "8.0.0.0"), 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.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", 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"), "8.0.0.0, 8.0.0.1, 8.0.0.2, 8.0.0.3"),
)) )
} }

View File

@ -12,11 +12,11 @@ import (
) )
func TestTruncate(t *T) { func TestTruncate(t *T) {
massert.Fatal(t, massert.All( massert.Require(t,
massert.Equal("abc", Truncate("abc", 4)), massert.Equal("abc", Truncate("abc", 4)),
massert.Equal("abc", Truncate("abc", 3)), massert.Equal("abc", Truncate("abc", 3)),
massert.Equal("ab...", Truncate("abc", 2)), massert.Equal("ab...", Truncate("abc", 2)),
)) )
} }
func TestLogger(t *T) { func TestLogger(t *T) {
@ -31,7 +31,7 @@ func TestLogger(t *T) {
select { select {
case <-l.testMsgWrittenCh: case <-l.testMsgWrittenCh:
case <-time.After(1 * time.Second): 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') out, err := buf.ReadString('\n')
return massert.All( return massert.All(
@ -45,11 +45,11 @@ func TestLogger(t *T) {
l.Info("bar") l.Info("bar")
l.Warn("baz") l.Warn("baz")
l.Error("buz") l.Error("buz")
massert.Fatal(t, massert.All( massert.Require(t,
assertOut(`{"level":"INFO","descr":"bar"}`), assertOut(`{"level":"INFO","descr":"bar"}`),
assertOut(`{"level":"WARN","descr":"baz"}`), assertOut(`{"level":"WARN","descr":"baz"}`),
assertOut(`{"level":"ERROR","descr":"buz"}`), assertOut(`{"level":"ERROR","descr":"buz"}`),
)) )
ctx := context.Background() ctx := context.Background()
@ -58,10 +58,10 @@ func TestLogger(t *T) {
l.Info("bar") l.Info("bar")
l.Warn("baz") l.Warn("baz")
l.Error("buz", mctx.Annotate(ctx, "a", "b", "c", "d")) 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":"WARN","descr":"baz"}`),
assertOut(`{"level":"ERROR","descr":"buz","annotations":{"/":{"a":"b","c":"d"}}}`), assertOut(`{"level":"ERROR","descr":"buz","annotations":{"/":{"a":"b","c":"d"}}}`),
)) )
l2 := l.Clone() l2 := l.Clone()
l2.SetMaxLevel(InfoLevel) l2.SetMaxLevel(InfoLevel)
@ -72,9 +72,9 @@ func TestLogger(t *T) {
l2.Info("bar") l2.Info("bar")
l2.Warn("baz") l2.Warn("baz")
l.Error("buz") l.Error("buz")
massert.Fatal(t, massert.All( massert.Require(t,
assertOut(`{"level":"INFO","descr":"BAR"}`), assertOut(`{"level":"INFO","descr":"BAR"}`),
assertOut(`{"level":"WARN","descr":"BAZ"}`), assertOut(`{"level":"WARN","descr":"BAZ"}`),
assertOut(`{"level":"ERROR","descr":"buz"}`), assertOut(`{"level":"ERROR","descr":"buz"}`),
)) )
} }

View File

@ -20,15 +20,15 @@ func TestIsReservedIP(t *T) {
"ip:%q", ipStr) "ip:%q", ipStr)
} }
massert.Fatal(t, massert.All( massert.Require(t,
assertReserved("127.0.0.1"), assertReserved("127.0.0.1"),
assertReserved("::ffff:127.0.0.1"), assertReserved("::ffff:127.0.0.1"),
assertReserved("192.168.40.50"), assertReserved("192.168.40.50"),
assertReserved("::1"), assertReserved("::1"),
assertReserved("100::1"), assertReserved("100::1"),
)) )
massert.Fatal(t, massert.None( massert.Require(t, massert.None(
assertReserved("8.8.8.8"), assertReserved("8.8.8.8"),
assertReserved("::ffff:8.8.8.8"), assertReserved("::ffff:8.8.8.8"),
assertReserved("2600:1700:7580:6e80:21c:25ff:fe97:44df"), assertReserved("2600:1700:7580:6e80:21c:25ff:fe97:44df"),

View File

@ -37,14 +37,14 @@ func TestHooks(t *T) {
ctx = WithHook(ctx, 0, mkHook(7)) ctx = WithHook(ctx, 0, mkHook(7))
massert.Fatal(t, massert.All( massert.Require(t,
massert.Nil(TriggerHooks(ctx, 0)), massert.Nil(TriggerHooks(ctx, 0)),
massert.Equal([]int{1, 2, 3, 4, 5, 6, 7}, out), massert.Equal([]int{1, 2, 3, 4, 5, 6, 7}, out),
)) )
out = nil out = nil
massert.Fatal(t, massert.All( massert.Require(t,
massert.Nil(TriggerHooksReverse(ctx, 0)), massert.Nil(TriggerHooksReverse(ctx, 0)),
massert.Equal([]int{7, 6, 5, 4, 3, 2, 1}, out), massert.Equal([]int{7, 6, 5, 4, 3, 2, 1}, out),
)) )
} }

View File

@ -151,25 +151,25 @@ func (a *assertion) Stack() []runtime.Frame {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Fatal is a convenience function which performs the Assertion and calls Fatal // Require is a convenience function which performs the Assertions and calls
// on the testing.T instance if the assertion fails. // Fatal on the testing.T instance for the first Assertion which fails.
// func Require(t *testing.T, aa ...Assertion) {
// TODO rename to Require for _, a := range aa {
func Fatal(t *testing.T, a Assertion) {
if err := a.Assert(); err != nil { if err := a.Assert(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
}
// Error is a convenience function which performs the Assertion and calls Error // Assert is a convenience function which performs the Assertion and calls Error
// on the testing.T instance if the assertion fails. // on the testing.T instance for the first Assertion which fails.
// func Assert(t *testing.T, aa ...Assertion) {
// TODO rename to Assert for _, a := range aa {
func Error(t *testing.T, a Assertion) {
if err := a.Assert(); err != nil { if err := a.Assert(); err != nil {
t.Error(err) t.Error(err)
} }
} }
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Assertion wrappers // Assertion wrappers
@ -291,18 +291,14 @@ func None(aa ...Assertion) Assertion {
return newAssertion(fn, fmtMultiDescr("None", aa...), 0) return newAssertion(fn, fmtMultiDescr("None", aa...), 0)
} }
// Err returns an Assertion which always fails with the given error. // Error returns an Assertion which always fails with the given error.
// func Error(err error) Assertion {
// TODO rename to Error
func Err(err error) Assertion {
return newAssertion(func() error { return err }, "", 0) return newAssertion(func() error { return err }, "", 0)
} }
// Errf is like Err but allows for a formatted string. // Errorf is like Err but allows for a formatted string.
// func Errorf(str string, args ...interface{}) Assertion {
// TODO rename to Errorf return Error(fmt.Errorf(str, args...))
func Errf(str string, args ...interface{}) Assertion {
return Err(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 // Equal asserts that the two values are exactly equal, and uses the
// reflect.DeepEqual function to determine if they are. // 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 { func Equal(a, b interface{}) Assertion {
return newAssertion(func() error { return newAssertion(func() error {
if !reflect.DeepEqual(a, b) { if !reflect.DeepEqual(a, b) {
@ -409,12 +401,10 @@ func Subset(set, subset interface{}) Assertion {
}, toStr(set)+" has subset "+toStr(subset), 0) }, toStr(set)+" has subset "+toStr(subset), 0)
} }
// Has asserts that the given set has the given element as a value in it. The // HasValue asserts that the given set has the given element as a value in it.
// set may be an array, a slice, or a map, and if it's a map then the elem will // The set may be an array, a slice, or a map, and if it's a map then the elem
// need to be a value in it. // will need to be a value in it.
// func HasValue(set, elem interface{}) Assertion {
// TODO rename to HasValue
func Has(set, elem interface{}) Assertion {
setVV, err := toSet(set, false) setVV, err := toSet(set, false)
if err != nil { if err != nil {
panic(err) panic(err)
@ -450,12 +440,10 @@ func HasKey(set, elem interface{}) Assertion {
}, toStr(set)+" has key "+toStr(elem), 0) }, 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 // set may be an array, a slice, or a map. A nil value'd set is considered to be
// a length of zero. // a length of zero.
// func Length(set interface{}, length int) Assertion {
// TODO rename to Length
func Len(set interface{}, length int) Assertion {
setVV, err := toSet(set, false) setVV, err := toSet(set, false)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -96,12 +96,12 @@ func TestNone(t *T) {
} }
func TestEqual(t *T) { func TestEqual(t *T) {
Fatal(t, All( Require(t,
Equal(1, 1), Equal(1, 1),
Equal("foo", "foo"), Equal("foo", "foo"),
)) )
Fatal(t, None( Require(t, None(
Equal(1, 2), Equal(1, 2),
Equal(1, int64(1)), Equal(1, int64(1)),
Equal(1, uint64(1)), Equal(1, uint64(1)),
@ -116,20 +116,20 @@ func TestEqual(t *T) {
aa = append(aa, Equal(1, m["foo"])) aa = append(aa, Equal(1, m["foo"]))
m["foo"] = 2 m["foo"] = 2
aa = append(aa, Equal(2, m["foo"])) aa = append(aa, Equal(2, m["foo"]))
Fatal(t, All(aa...)) Require(t, aa...)
} }
func TestNil(t *T) { func TestNil(t *T) {
Fatal(t, All( Require(t,
Nil(nil), Nil(nil),
Nil([]byte(nil)), Nil([]byte(nil)),
Nil(map[int]int(nil)), Nil(map[int]int(nil)),
Nil((*struct{})(nil)), Nil((*struct{})(nil)),
Nil(interface{}(nil)), Nil(interface{}(nil)),
Nil(error(nil)), Nil(error(nil)),
)) )
Fatal(t, None( Require(t, None(
Nil(1), Nil(1),
Nil([]byte("foo")), Nil([]byte("foo")),
Nil(map[int]int{1: 1}), Nil(map[int]int{1: 1}),
@ -140,7 +140,7 @@ func TestNil(t *T) {
} }
func TestSubset(t *T) { func TestSubset(t *T) {
Fatal(t, All( Require(t,
Subset([]int{1, 2, 3}, []int{}), Subset([]int{1, 2, 3}, []int{}),
Subset([]int{1, 2, 3}, []int{1}), Subset([]int{1, 2, 3}, []int{1}),
Subset([]int{1, 2, 3}, []int{2}), 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{}),
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}),
Subset(map[int]int{1: 1, 2: 2}, map[int]int{1: 1, 2: 2}), 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{}, []int{1, 2, 3}),
Subset([]int{1, 2, 3}, []int{4}), Subset([]int{1, 2, 3}, []int{4}),
Subset([]int{1, 2, 3}, []int{1, 3, 2, 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} m := map[int]int{1: 1, 2: 2}
a := Subset(m, map[int]int{1: 1}) a := Subset(m, map[int]int{1: 1})
m[1] = 2 m[1] = 2
Fatal(t, a) Require(t, a)
} }
func TestHas(t *T) { func TestHasValue(t *T) {
Fatal(t, All( Require(t,
Has([]int{1}, 1), HasValue([]int{1}, 1),
Has([]int{1, 2}, 1), HasValue([]int{1, 2}, 1),
Has([]int{2, 1}, 1), HasValue([]int{2, 1}, 1),
Has(map[int]int{1: 1}, 1), HasValue(map[int]int{1: 1}, 1),
Has(map[int]int{1: 2}, 2), HasValue(map[int]int{1: 2}, 2),
Has(map[int]int{1: 2, 2: 1}, 1), HasValue(map[int]int{1: 2, 2: 1}, 1),
Has(map[int]int{1: 2, 2: 2}, 2), HasValue(map[int]int{1: 2, 2: 2}, 2),
)) )
Fatal(t, None( Require(t, None(
Has([]int{}, 1), HasValue([]int{}, 1),
Has([]int{1}, 2), HasValue([]int{1}, 2),
Has([]int{2, 1}, 3), HasValue([]int{2, 1}, 3),
Has(map[int]int{}, 1), HasValue(map[int]int{}, 1),
Has(map[int]int{1: 1}, 2), HasValue(map[int]int{1: 1}, 2),
Has(map[int]int{1: 2}, 1), HasValue(map[int]int{1: 2}, 1),
Has(map[int]int{1: 2, 2: 1}, 3), HasValue(map[int]int{1: 2, 2: 1}, 3),
)) ))
// make sure changes don't retroactively fail the assertion // make sure changes don't retroactively fail the assertion
m := map[int]int{1: 1} m := map[int]int{1: 1}
a := Has(m, 1) a := HasValue(m, 1)
m[1] = 2 m[1] = 2
Fatal(t, a) Require(t, a)
} }
func TestHasKey(t *T) { func TestHasKey(t *T) {
Fatal(t, All( Require(t,
HasKey(map[int]int{1: 1}, 1), HasKey(map[int]int{1: 1}, 1),
HasKey(map[int]int{1: 1, 2: 2}, 1), HasKey(map[int]int{1: 1, 2: 2}, 1),
HasKey(map[int]int{1: 1, 2: 2}, 2), HasKey(map[int]int{1: 1, 2: 2}, 2),
)) )
Fatal(t, None( Require(t, None(
HasKey(map[int]int{}, 1), HasKey(map[int]int{}, 1),
HasKey(map[int]int{2: 2}, 1), HasKey(map[int]int{2: 2}, 1),
)) ))
@ -214,36 +214,36 @@ func TestHasKey(t *T) {
m := map[int]int{1: 1} m := map[int]int{1: 1}
a := HasKey(m, 1) a := HasKey(m, 1)
delete(m, 1) delete(m, 1)
Fatal(t, a) Require(t, a)
} }
func TestLen(t *T) { func TestLength(t *T) {
Fatal(t, All( Require(t,
Len([]int(nil), 0), Length([]int(nil), 0),
Len([]int{}, 0), Length([]int{}, 0),
Len([]int{1}, 1), Length([]int{1}, 1),
Len([]int{1, 2}, 2), Length([]int{1, 2}, 2),
Len(map[int]int(nil), 0), Length(map[int]int(nil), 0),
Len(map[int]int{}, 0), Length(map[int]int{}, 0),
Len(map[int]int{1: 1}, 1), Length(map[int]int{1: 1}, 1),
Len(map[int]int{1: 1, 2: 2}, 2), Length(map[int]int{1: 1, 2: 2}, 2),
)) )
Fatal(t, None( Require(t, None(
Len([]int(nil), 1), Length([]int(nil), 1),
Len([]int{}, 1), Length([]int{}, 1),
Len([]int{1}, 0), Length([]int{1}, 0),
Len([]int{1}, 2), Length([]int{1}, 2),
Len([]int{1, 2}, 1), Length([]int{1, 2}, 1),
Len([]int{1, 2}, 3), Length([]int{1, 2}, 3),
Len(map[int]int(nil), 1), Length(map[int]int(nil), 1),
Len(map[int]int{}, 1), Length(map[int]int{}, 1),
)) ))
// make sure changes don't retroactively fail the assertion // make sure changes don't retroactively fail the assertion
m := map[int]int{1: 1} m := map[int]int{1: 1}
a := Len(m, 1) a := Length(m, 1)
m[2] = 2 m[2] = 2
Fatal(t, a) Require(t, a)
} }