Improve test result diffing

pull/6/head
Mickaël Menu 3 years ago
parent 9f1ec95194
commit 0e8ae93450
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -7,7 +7,7 @@ import (
"github.com/mickael-menu/zk/core/style"
"github.com/mickael-menu/zk/util"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
"github.com/mickael-menu/zk/util/fixtures"
)

@ -3,7 +3,7 @@ package sqlite
import (
"testing"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
"github.com/mickael-menu/zk/util/fixtures"
)

@ -7,8 +7,9 @@ import (
"github.com/mickael-menu/zk/core/note"
"github.com/mickael-menu/zk/util"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/paths"
"github.com/mickael-menu/zk/util/test"
"github.com/mickael-menu/zk/util/test/assert"
)
func TestNoteDAOIndexed(t *testing.T) {
@ -16,27 +17,27 @@ func TestNoteDAOIndexed(t *testing.T) {
expected := []paths.Metadata{
{
Path: "f39c8.md",
Modified: date("2020-01-20T08:52:42+01:00"),
Modified: test.Date("2020-01-20T08:52:42+01:00"),
},
{
Path: "index.md",
Modified: date("2019-12-04T12:17:21+01:00"),
Modified: test.Date("2019-12-04T12:17:21+01:00"),
},
{
Path: "log/2021-01-03.md",
Modified: date("2020-11-22T16:27:45+01:00"),
Modified: test.Date("2020-11-22T16:27:45+01:00"),
},
{
Path: "log/2021-01-04.md",
Modified: date("2020-11-29T08:20:18+01:00"),
Modified: test.Date("2020-11-29T08:20:18+01:00"),
},
{
Path: "ref/test/a.md",
Modified: date("2019-11-20T20:34:06+01:00"),
Modified: test.Date("2019-11-20T20:34:06+01:00"),
},
{
Path: "ref/test/b.md",
Modified: date("2019-11-20T20:34:06+01:00"),
Modified: test.Date("2019-11-20T20:34:06+01:00"),
},
}
@ -58,8 +59,8 @@ func TestNoteDAOAdd(t *testing.T) {
Title: "Added note",
Body: "Note body",
WordCount: 2,
Created: date("2019-11-19T15:33:31+01:00"),
Modified: date("2020-01-16T16:04:59+01:00"),
Created: test.Date("2019-11-19T15:33:31+01:00"),
Modified: test.Date("2020-01-16T16:04:59+01:00"),
Checksum: "check",
})
assert.Nil(t, err)
@ -72,8 +73,8 @@ func TestNoteDAOAdd(t *testing.T) {
Body: "Note body",
WordCount: 2,
Checksum: "check",
Created: date("2019-11-19T15:33:31+01:00"),
Modified: date("2020-01-16T16:04:59+01:00"),
Created: test.Date("2019-11-19T15:33:31+01:00"),
Modified: test.Date("2020-01-16T16:04:59+01:00"),
})
})
}
@ -93,8 +94,8 @@ func TestNoteDAOUpdate(t *testing.T) {
Title: "Updated note",
Body: "Updated body",
WordCount: 42,
Created: date("2020-11-22T16:49:47+01:00"),
Modified: date("2020-11-22T16:49:47+01:00"),
Created: test.Date("2020-11-22T16:49:47+01:00"),
Modified: test.Date("2020-11-22T16:49:47+01:00"),
Checksum: "updated checksum",
})
assert.Nil(t, err)
@ -107,8 +108,8 @@ func TestNoteDAOUpdate(t *testing.T) {
Body: "Updated body",
WordCount: 42,
Checksum: "updated checksum",
Created: date("2019-11-20T20:32:56+01:00"),
Modified: date("2020-11-22T16:49:47+01:00"),
Created: test.Date("2019-11-20T20:32:56+01:00"),
Modified: test.Date("2020-11-22T16:49:47+01:00"),
})
})
}
@ -138,75 +139,75 @@ func TestNoteDAORemoveUnknown(t *testing.T) {
func TestNoteDAOFindAll(t *testing.T) {
testNoteDAOFind(t, []note.Match{
note.Match{
{
Snippet: "",
Metadata: note.Metadata{
Path: "ref/test/b.md",
Title: "A nested note",
Body: "This one is in a sub sub directory",
WordCount: 8,
Created: date("2019-11-20T20:32:56+01:00"),
Modified: date("2019-11-20T20:34:06+01:00"),
Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.Local),
Modified: time.Date(2019, 11, 20, 20, 34, 6, 0, time.Local),
Checksum: "yvwbae",
},
},
note.Match{
{
Snippet: "",
Metadata: note.Metadata{
Path: "f39c8.md",
Title: "An interesting note",
Body: "Its content will surprise you",
WordCount: 5,
Created: date("2020-01-19T10:58:41+01:00"),
Modified: date("2020-01-20T08:52:42+01:00"),
Created: time.Date(2020, 1, 19, 10, 58, 41, 0, time.Local),
Modified: time.Date(2020, 1, 20, 8, 52, 42, 0, time.Local),
Checksum: "irkwyc",
},
},
note.Match{
{
Snippet: "",
Metadata: note.Metadata{
Path: "ref/test/a.md",
Title: "Another nested note",
Body: "It shall appear before b.md",
WordCount: 5,
Created: date("2019-11-20T20:32:56+01:00"),
Modified: date("2019-11-20T20:34:06+01:00"),
Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.Local),
Modified: time.Date(2019, 11, 20, 20, 34, 6, 0, time.Local),
Checksum: "iecywst",
},
},
note.Match{
{
Snippet: "",
Metadata: note.Metadata{
Path: "index.md",
Title: "Index",
Body: "Index of the Zettelkasten",
WordCount: 4,
Created: date("2019-12-04T11:59:11+01:00"),
Modified: date("2019-12-04T12:17:21+01:00"),
Created: time.Date(2019, 12, 4, 11, 59, 11, 0, time.Local),
Modified: time.Date(2019, 12, 4, 12, 17, 21, 0, time.Local),
Checksum: "iaefhv",
},
},
note.Match{
{
Snippet: "",
Metadata: note.Metadata{
Path: "log/2021-01-03.md",
Title: "January 3, 2021",
Body: "A daily note",
WordCount: 3,
Created: date("2020-11-22T16:27:45+01:00"),
Modified: date("2020-11-22T16:27:45+01:00"),
Created: time.Date(2020, 11, 22, 16, 27, 45, 0, time.Local),
Modified: time.Date(2020, 11, 22, 16, 27, 45, 0, time.Local),
Checksum: "qwfpgj",
},
},
note.Match{
{
Snippet: "",
Metadata: note.Metadata{
Path: "log/2021-01-04.md",
Title: "January 4, 2021",
Body: "A second daily note",
WordCount: 4,
Created: date("2020-11-29T08:20:18+01:00"),
Modified: date("2020-11-29T08:20:18+01:00"),
Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.Local),
Modified: time.Date(2020, 11, 29, 8, 20, 18, 0, time.Local),
Checksum: "arstde",
},
},
@ -221,28 +222,7 @@ func testNoteDAOFind(t *testing.T, expected []note.Match) {
return nil
})
assert.Nil(t, err)
popExpected := func() (note.Match, bool) {
if len(expected) == 0 {
return note.Match{}, false
}
item := expected[0]
expected = expected[1:]
return item, true
}
for _, act := range actual {
exp, ok := popExpected()
if !ok {
t.Errorf("More matches than expected: %v", actual)
return
}
assert.Equal(t, act, exp)
}
if len(expected) > 0 {
t.Errorf("Missing expected matches: %v", expected)
}
assert.Equal(t, actual, expected)
})
}
@ -267,8 +247,3 @@ func queryNoteRow(tx Transaction, where string) (noteRow, error) {
`, where)).Scan(&row.Path, &row.Title, &row.Body, &row.WordCount, &row.Checksum, &row.Created, &row.Modified)
return row, err
}
func date(s string) time.Time {
date, _ := time.Parse(time.RFC3339, s)
return date
}

@ -4,7 +4,7 @@ import (
"testing"
"github.com/go-testfixtures/testfixtures/v3"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
)
// testTransaction is an utility function used to test a SQLite transaction to

@ -5,7 +5,7 @@ import (
"github.com/fatih/color"
"github.com/mickael-menu/zk/core/style"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
)
func createStyler() *Styler {

@ -7,7 +7,7 @@ import (
"github.com/mickael-menu/zk/core/templ"
"github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
"github.com/mickael-menu/zk/util/opt"
)

@ -5,7 +5,7 @@ import (
"testing"
"github.com/mickael-menu/zk/core/zk"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
"github.com/mickael-menu/zk/util/opt"
)

@ -5,7 +5,7 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
"github.com/mickael-menu/zk/util/opt"
)

@ -5,7 +5,7 @@ import (
"path/filepath"
"testing"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
"github.com/mickael-menu/zk/util/opt"
)

@ -15,6 +15,8 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/lestrrat-go/strftime v1.0.3
github.com/mattn/go-sqlite3 v1.14.6
github.com/mickael-menu/pretty v0.2.3
github.com/rogpeppe/go-internal v1.6.2 // indirect
github.com/rvflash/elapsed v0.2.0
github.com/tebeka/strftime v0.1.5 // indirect
golang.org/x/sys v0.0.0-20210104204734-6f8348627aad // indirect

@ -15,6 +15,7 @@ github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMe
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -83,6 +84,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
@ -106,6 +109,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mickael-menu/pretty v0.2.3 h1:AXi5WcBuWxwQV6iY/GhmCFpaoboQO2SLtzfujrn7dv0=
github.com/mickael-menu/pretty v0.2.3/go.mod h1:gupeWUSWoo3KX7BItIuouLgTqQLlmRylpaPdIK6IqLk=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
@ -114,6 +119,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.6.2 h1:aIihoIOHCiLZHxyoNQ+ABL4NKhFTgKLBdMLyEAh98m0=
github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
@ -196,6 +205,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/djherbis/times.v1 v1.2.0 h1:UCvDKl1L/fmBygl2Y7hubXCnY7t4Yj46ZrBFNUipFbM=
gopkg.in/djherbis/times.v1 v1.2.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

@ -5,7 +5,7 @@ import (
"testing"
"time"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
)
var date1 = time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)

@ -4,14 +4,20 @@ import (
"testing"
"github.com/mickael-menu/zk/util"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/fixtures"
"github.com/mickael-menu/zk/util/test/assert"
)
func TestWalk(t *testing.T) {
var path = fixtures.Path("walk")
testEqual(t, Walk(path, "md", &util.NullLogger), []string{
actual := make([]string, 0)
for m := range Walk(path, "md", &util.NullLogger) {
assert.NotNil(t, m.Modified)
actual = append(actual, m.Path)
}
assert.Equal(t, actual, []string{
"a.md",
"b.md",
"dir1/a.md",
@ -20,28 +26,3 @@ func TestWalk(t *testing.T) {
"dir2/a.md",
})
}
func testEqual(t *testing.T, actual <-chan Metadata, expected []string) {
popExpected := func() (string, bool) {
if len(expected) == 0 {
return "", false
}
item := expected[0]
expected = expected[1:]
return item, true
}
for act := range actual {
exp, ok := popExpected()
if !ok {
t.Errorf("More paths available than expected")
return
}
assert.Equal(t, act.Path, exp)
assert.NotNil(t, act.Modified)
}
if len(expected) > 0 {
t.Errorf("Missing expected paths: %v", expected)
}
}

@ -3,7 +3,7 @@ package strings
import (
"testing"
"github.com/mickael-menu/zk/util/assert"
"github.com/mickael-menu/zk/util/test/assert"
)
func TestPrepend(t *testing.T) {

@ -7,6 +7,7 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mickael-menu/pretty"
)
func Nil(t *testing.T, value interface{}) {
@ -28,7 +29,14 @@ func isNil(value interface{}) bool {
func Equal(t *testing.T, actual, expected interface{}) {
if !(reflect.DeepEqual(actual, expected) || cmp.Equal(actual, expected)) {
t.Errorf("Received (type %v):\n%+v\n---\nBut expected (type %v):\n%+v", reflect.TypeOf(actual), toJSON(t, actual), reflect.TypeOf(expected), toJSON(t, expected))
t.Errorf("Received (type %v):\n% #v", reflect.TypeOf(actual), pretty.Formatter(actual))
t.Errorf("\n---\n")
t.Errorf("But expected (type %v):\n% #v", reflect.TypeOf(expected), pretty.Formatter(expected))
t.Errorf("\n---\n")
t.Errorf("Diff:\n")
for _, diff := range pretty.Diff(actual, expected) {
t.Errorf("\t% #v", diff)
}
}
}

@ -0,0 +1,8 @@
package test
import "time"
func Date(s string) time.Time {
date, _ := time.Parse(time.RFC3339, s)
return date
}
Loading…
Cancel
Save