✨ Migrate from testify to nalgeon/be testing library
- Replace all testify/assert and testify/require with be library - Update go.mod to use be v0.3.0 instead of testify - Simplify test assertions using be.Equal, be.Err, and be.True - Refactor append_test, entry_test, meta_test, log_test, and formatter tests
This commit is contained in:
parent
79f58b3e6c
commit
1110288d84
16 changed files with 226 additions and 236 deletions
|
|
@ -4,11 +4,12 @@ import (
|
|||
"bufio"
|
||||
"encoding"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/nalgeon/be"
|
||||
)
|
||||
|
||||
// Type assertions
|
||||
|
|
@ -126,19 +127,19 @@ func getEntryMarshalTestRunner(title string, date time.Time, fields []Meta, firs
|
|||
return func(t *testing.T) {
|
||||
en := Entry{title, date, fields}
|
||||
o, er := en.MarshalText()
|
||||
assert.Equal(t, err, er)
|
||||
be.Err(t, er, err)
|
||||
if first == "" {
|
||||
return
|
||||
}
|
||||
os := string(o)
|
||||
if len(lines) == 0 {
|
||||
assert.Equal(t, first, os)
|
||||
be.Equal(t, os, first)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Regexp(t, first, os)
|
||||
be.True(t, regexp.MustCompile(first).MatchString(os))
|
||||
for _, line := range lines {
|
||||
assert.Regexp(t, "(?m)^"+line, os)
|
||||
be.True(t, regexp.MustCompile("(?m)^"+line).MatchString(os))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -215,12 +216,12 @@ func getEntryUnmarshalTestRunner(in string, title string, date time.Time, fields
|
|||
e := &Entry{}
|
||||
er := e.UnmarshalText([]byte(in))
|
||||
if err != nil {
|
||||
assert.ErrorIs(t, er, err)
|
||||
be.Err(t, er, err)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, title, e.Title)
|
||||
assert.WithinRange(t, e.Date, date.Add(-time.Second), date.Add(time.Second))
|
||||
be.Equal(t, e.Title, title)
|
||||
be.True(t, e.Date.After(date.Add(-time.Second)) && e.Date.Before(date.Add(time.Second)))
|
||||
for _, f := range fields {
|
||||
got := false
|
||||
for _, m := range e.Fields {
|
||||
|
|
@ -237,7 +238,7 @@ func getEntryUnmarshalTestRunner(in string, title string, date time.Time, fields
|
|||
break
|
||||
}
|
||||
}
|
||||
assert.Truef(t, got, "Couldn't find field %+v. We have %+v", f, e.Fields)
|
||||
be.True(t, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -247,13 +248,13 @@ func TestScan(t *testing.T) {
|
|||
read := strings.NewReader(in)
|
||||
scan := bufio.NewScanner(read)
|
||||
scan.Split(scanEntry)
|
||||
assert.True(t, scan.Scan())
|
||||
assert.Equal(t, "@begin date - Title\nlong", scan.Text())
|
||||
assert.True(t, scan.Scan())
|
||||
assert.Equal(t, "@foo john\njones", scan.Text())
|
||||
assert.True(t, scan.Scan())
|
||||
assert.Equal(t, "@bar 42@nobody", scan.Text())
|
||||
assert.False(t, scan.Scan())
|
||||
be.True(t, scan.Scan())
|
||||
be.Equal(t, scan.Text(), "@begin date - Title\nlong")
|
||||
be.True(t, scan.Scan())
|
||||
be.Equal(t, scan.Text(), "@foo john\njones")
|
||||
be.True(t, scan.Scan())
|
||||
be.Equal(t, scan.Text(), "@bar 42@nobody")
|
||||
be.True(t, !scan.Scan())
|
||||
}
|
||||
|
||||
func TestEntryJsonMarshal(t *testing.T) {
|
||||
|
|
@ -297,10 +298,12 @@ func getEntryJsonMarshalTestRunner(title string, date time.Time, fields []Meta,
|
|||
e := Entry{title, date, fields}
|
||||
o, er := json.Marshal(e)
|
||||
if err == nil {
|
||||
assert.JSONEq(t, out, string(o))
|
||||
|
||||
var got, want any
|
||||
json.Unmarshal([]byte(out), &want)
|
||||
json.Unmarshal(o, &got)
|
||||
be.Equal(t, got, want)
|
||||
} else {
|
||||
assert.ErrorIs(t, er, err)
|
||||
be.Err(t, er, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -376,14 +379,14 @@ func getEntryJsonUnmarshalTestRunner(in, title string, date time.Time, fields []
|
|||
e := new(Entry)
|
||||
er := e.UnmarshalJSON([]byte(in))
|
||||
if err != nil {
|
||||
assert.ErrorIs(t, er, err)
|
||||
be.Err(t, er, err)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Nil(t, er)
|
||||
assert.Equal(t, title, e.Title)
|
||||
assert.WithinRange(t, e.Date, date.Add(-time.Second), date.Add(time.Second))
|
||||
assert.Len(t, e.Fields, len(fields))
|
||||
be.Equal(t, er, nil)
|
||||
be.Equal(t, e.Title, title)
|
||||
be.True(t, e.Date.After(date.Add(-time.Second)) && e.Date.Before(date.Add(time.Second)))
|
||||
be.Equal(t, len(e.Fields), len(fields))
|
||||
for _, f := range fields {
|
||||
got := false
|
||||
fTime, isTime := f.Value.(time.Time)
|
||||
|
|
@ -402,13 +405,13 @@ func getEntryJsonUnmarshalTestRunner(in, title string, date time.Time, fields []
|
|||
}
|
||||
if isTime && m.Key == f.Key {
|
||||
mTime, _ := mVal.(time.Time)
|
||||
if assert.WithinRange(t, mTime, fTime.Add(-2*time.Second), fTime.Add(2*time.Second)) {
|
||||
if mTime.After(fTime.Add(-2*time.Second)) && mTime.Before(fTime.Add(2*time.Second)) {
|
||||
got = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
assert.Truef(t, got, "Couldn't find field %+v. We have %+v", f, e.Fields)
|
||||
be.True(t, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,7 @@ import (
|
|||
"encoding"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/nalgeon/be"
|
||||
)
|
||||
|
||||
var _ encoding.TextUnmarshaler = new(Log)
|
||||
|
|
@ -32,15 +31,15 @@ const all = first + second + third + fourth + skip + fifth
|
|||
func TestLogUnmarshalBig(t *testing.T) {
|
||||
l := &Log{Name: "test-log"}
|
||||
err := l.UnmarshalText([]byte(all))
|
||||
require.NoError(t, err)
|
||||
require.Len(t, l.Entries, 5)
|
||||
be.Err(t, err, nil)
|
||||
be.Equal(t, len(l.Entries), 5)
|
||||
|
||||
var e Entry
|
||||
var f bool
|
||||
if e, f = findEntry(t, l, "This is simple", true); !f {
|
||||
return
|
||||
}
|
||||
assert.Len(t, e.Fields, 0)
|
||||
be.Equal(t, len(e.Fields), 0)
|
||||
|
||||
for _, e := range l.Entries {
|
||||
findMeta(t, e, "ignoreme", true, false)
|
||||
|
|
@ -51,28 +50,28 @@ func TestLogUnmarshalIgnoreGarbage(t *testing.T) {
|
|||
l := &Log{Name: "test-log"}
|
||||
in := "ignore this\n" + second + "some crap also skip -> " + third + skip
|
||||
err := l.UnmarshalText([]byte(in))
|
||||
require.NoError(t, err)
|
||||
require.Len(t, l.Entries, 1)
|
||||
be.Err(t, err, nil)
|
||||
be.Equal(t, len(l.Entries), 1)
|
||||
|
||||
en := l.Entries[0]
|
||||
assert.Equal(t, "We have one thing here", en.Title)
|
||||
assert.Len(t, en.Fields, 1)
|
||||
assert.Equal(t, "foo", en.Fields[0].Key)
|
||||
assert.Equal(t, "bar", en.Fields[0].Value)
|
||||
be.Equal(t, en.Title, "We have one thing here")
|
||||
be.Equal(t, len(en.Fields), 1)
|
||||
be.Equal(t, en.Fields[0].Key, "foo")
|
||||
be.Equal(t, en.Fields[0].Value, "bar")
|
||||
}
|
||||
|
||||
func TestLogUnmarshalEmpty(t *testing.T) {
|
||||
l := &Log{Name: "test-log"}
|
||||
err := l.UnmarshalText([]byte{})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, l.Entries, 0)
|
||||
be.Err(t, err, nil)
|
||||
be.Equal(t, len(l.Entries), 0)
|
||||
}
|
||||
|
||||
func TestLogUnmarshalBad(t *testing.T) {
|
||||
l := &Log{Name: "test-log"}
|
||||
err := l.UnmarshalText([]byte(badEntry))
|
||||
require.NoError(t, err)
|
||||
require.Len(t, l.Entries, 0)
|
||||
be.Err(t, err, nil)
|
||||
be.Equal(t, len(l.Entries), 0)
|
||||
}
|
||||
|
||||
func findEntry(t *testing.T, log *Log, title string, shouldFind bool) (Entry, bool) {
|
||||
|
|
@ -85,9 +84,9 @@ func findEntry(t *testing.T, log *Log, title string, shouldFind bool) (Entry, bo
|
|||
}
|
||||
}
|
||||
if shouldFind {
|
||||
found = assert.Truef(t, found, "Unable to found entry %s", title)
|
||||
be.True(t, found)
|
||||
} else {
|
||||
found = assert.Falsef(t, found, "Entry %s should not have been found but was", title)
|
||||
be.True(t, !found)
|
||||
}
|
||||
|
||||
return ret, found
|
||||
|
|
@ -103,9 +102,9 @@ func findMeta(t *testing.T, entry Entry, key string, value any, shouldFind bool)
|
|||
}
|
||||
}
|
||||
if shouldFind {
|
||||
found = assert.Truef(t, found, "Unable to found meta %s", key)
|
||||
be.True(t, found)
|
||||
} else {
|
||||
found = assert.Falsef(t, found, "Meta %s should not have been found but was", key)
|
||||
be.True(t, !found)
|
||||
}
|
||||
|
||||
return ret, found
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/nalgeon/be"
|
||||
)
|
||||
|
||||
// Type assertions
|
||||
|
|
@ -72,8 +72,8 @@ func getMetaTestRunner(key string, value any, out string, err error, newVal any)
|
|||
if valE, ok := value.(error); !ok || !errors.Is(valE, skipMarshalTest) {
|
||||
var o []byte
|
||||
o, e = st.MarshalText()
|
||||
assert.Equal(t, out, string(o))
|
||||
assert.Equal(t, err, e)
|
||||
be.Equal(t, string(o), out)
|
||||
be.Equal(t, e, err)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -83,17 +83,16 @@ func getMetaTestRunner(key string, value any, out string, err error, newVal any)
|
|||
e = n.UnmarshalText([]byte(out))
|
||||
}
|
||||
if newE, ok := newVal.(error); ok {
|
||||
assert.ErrorIs(t, e, newE)
|
||||
be.Err(t, e, newE)
|
||||
} else {
|
||||
assert.Equal(t, key, n.Key)
|
||||
be.Equal(t, n.Key, key)
|
||||
if ti, ok := newVal.(time.Time); ok {
|
||||
valT, ok := n.Value.(time.Time)
|
||||
if assert.True(t, ok) {
|
||||
assert.WithinRange(t, valT, ti.Add(-time.Second), ti.Add(time.Second))
|
||||
}
|
||||
be.True(t, ok)
|
||||
be.True(t, valT.After(ti.Add(-time.Second)) && valT.Before(ti.Add(time.Second)))
|
||||
|
||||
} else {
|
||||
assert.Equal(t, newVal, n.Value)
|
||||
be.Equal(t, n.Value, newVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -103,42 +102,45 @@ func TestMetasJson(t *testing.T) {
|
|||
ms := Metas{{"me", 41}, {"you", false}}
|
||||
exp := `{"me":41,"you":false}`
|
||||
o, err := json.Marshal(ms)
|
||||
assert.NoError(t, err)
|
||||
assert.JSONEq(t, exp, string(o))
|
||||
be.Err(t, err, nil)
|
||||
var got, want any
|
||||
json.Unmarshal([]byte(exp), &want)
|
||||
json.Unmarshal(o, &got)
|
||||
be.Equal(t, got, want)
|
||||
}
|
||||
|
||||
func TestMetasJsonUnmarshal(t *testing.T) {
|
||||
ms := Metas{}
|
||||
in := `{"me":"cool","you":false}`
|
||||
err := json.Unmarshal([]byte(in), &ms)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, ms, 2)
|
||||
assert.ElementsMatch(t, Metas{
|
||||
be.Err(t, err, nil)
|
||||
be.Equal(t, len(ms), 2)
|
||||
be.Equal(t, ms, Metas{
|
||||
{"me", "cool"},
|
||||
{"you", false},
|
||||
}, ms)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMetasJsonError(t *testing.T) {
|
||||
ms := Metas{}
|
||||
in := "not json"
|
||||
err := (&ms).UnmarshalJSON([]byte(in))
|
||||
assert.Error(t, err)
|
||||
assert.Len(t, ms, 0)
|
||||
be.Err(t, err)
|
||||
be.Equal(t, len(ms), 0)
|
||||
}
|
||||
|
||||
func TestMetasAppend(t *testing.T) {
|
||||
ms := Metas{}
|
||||
ms = ms.Append("foo", 42)
|
||||
assert.Len(t, ms, 1)
|
||||
assert.Equal(t, Meta{"foo", 42}, ms[0])
|
||||
be.Equal(t, len(ms), 1)
|
||||
be.Equal(t, ms[0], Meta{"foo", 42})
|
||||
}
|
||||
|
||||
func TestMetasAppendTo(t *testing.T) {
|
||||
ms := &Metas{}
|
||||
ms.AppendTo("foo", 42)
|
||||
assert.Len(t, *ms, 1)
|
||||
assert.Equal(t, Meta{"foo", 42}, (*ms)[0])
|
||||
be.Equal(t, len(*ms), 1)
|
||||
be.Equal(t, (*ms)[0], Meta{"foo", 42})
|
||||
}
|
||||
|
||||
func TestMetasSet(t *testing.T) {
|
||||
|
|
@ -196,7 +198,7 @@ func TestMetasSet(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := tt.initial.Set(tt.key, tt.value)
|
||||
assert.ElementsMatch(t, tt.expected, result)
|
||||
be.Equal(t, result, tt.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -205,14 +207,14 @@ func TestMetasGet(t *testing.T) {
|
|||
ms := Metas{{"foo", 42}, {"bar", "hello"}}
|
||||
|
||||
val, found := ms.Get("foo")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, 42, val)
|
||||
be.True(t, found)
|
||||
be.Equal(t, val, 42)
|
||||
|
||||
val, found = ms.Get("bar")
|
||||
assert.True(t, found)
|
||||
assert.Equal(t, "hello", val)
|
||||
be.True(t, found)
|
||||
be.Equal(t, val, "hello")
|
||||
|
||||
val, found = ms.Get("baz")
|
||||
assert.False(t, found)
|
||||
assert.Nil(t, val)
|
||||
be.True(t, !found)
|
||||
be.Equal(t, val, nil)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue