remove quicktest dependency
commit 38936448f08cc63d006e15a8cfcf471756311767 · parent aa5901ac18
· browse this tree
Calvin Behling, 2022-11-26 18:46:13 UTC
diff --git a/go.mod b/go.mod index 1196f49..dc56ee0 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,3 @@ module github.com/warpfork/go-testmark go 1.16 - -require ( - github.com/frankban/quicktest v1.14.4 // indirect - golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect - gopkg.in/errgo.v2 v2.1.0 // indirect -) diff --git a/index_test.go b/index_test.go index 787b591..6c4fef8 100644 --- a/index_test.go +++ b/index_test.go @@ -1,11 +1,12 @@ -package testmark +package testmark_test import ( + "fmt" "path/filepath" "strings" "testing" - qt "github.com/frankban/quicktest" + "github.com/warpfork/go-testmark" ) func TestIndexingDirs(t *testing.T) { @@ -13,7 +14,7 @@ func TestIndexingDirs(t *testing.T) { if err != nil { panic(err) } - doc, err := ReadFile(filepath.Join(testdata, "exampleWithDirs.md")) + doc, err := testmark.ReadFile(filepath.Join(testdata, "exampleWithDirs.md")) if err != nil { panic(err) } @@ -47,18 +48,21 @@ func TestIndexingDirs(t *testing.T) { func TestIndexingTree(t *testing.T) { testdata, err := filepath.Abs("testdata") - qt.Assert(t, err, qt.IsNil) - doc, err := ReadFile(filepath.Join(testdata, "exampleWithDirs.md")) + if err != nil { + panic(err) + } + doc, err := testmark.ReadFile(filepath.Join(testdata, "exampleWithDirs.md")) if err != nil { panic(err) } if len(doc.DataHunks) != len(doc.HunksByName) { - t.Errorf("doc hunk list has different length than hunks-by-name: %d != %d", len(doc.DataHunks), len(doc.HunksByName)) + t.Errorf("doc hunk list has different length than hunks-by-name: %d != %d", + len(doc.DataHunks), len(doc.HunksByName)) } doc.BuildDirIndex() - qt.Assert(t, len(doc.DataHunks), qt.Equals, len(doc.HunksByName)) - for _, hunk := range doc.DataHunks { + for _, _hunk := range doc.DataHunks { + hunk := _hunk t.Run("index:"+hunk.Name, func(t *testing.T) { assertHunkReachable(t, doc, hunk) }) @@ -66,44 +70,55 @@ func TestIndexingTree(t *testing.T) { assertChildren(t, doc.DirEnt) } -func assertHunkReachable(t *testing.T, doc *Document, hunk DocHunk) { +func assertHunkReachable(t *testing.T, doc *testmark.Document, hunk testmark.DocHunk) { splits := strings.Split(hunk.Name, "/") dir := doc.DirEnt - for _, s := range splits { - qt.Assert(t, len(dir.Children), qt.Equals, len(dir.ChildrenList)) - d, ok := dir.Children[s] - qt.Assert(t, ok, qt.IsTrue) - dir = d + for _, split := range splits { + if len(dir.Children) != len(dir.ChildrenList) { + t.Errorf("expected dir to have equal number of children in both data structures") + } + child, ok := dir.Children[split] + if !ok { + t.Errorf("expected dir %q to have child named %q", dir.Name, split) + } + dir = child } - qt.Assert(t, &hunk.Hunk, qt.DeepEquals, dir.Hunk) + assert(t, hunk.Hunk, fmt.Sprintf("%v", *dir.Hunk)) } -func assertChildren(t *testing.T, dir *DirEnt) { - cm := make(map[string]struct{}) - for _, child := range dir.ChildrenList { - { - // Check that a child of this name has not been found already - _, exists := cm[child.Name] - qt.Check(t, exists, qt.IsFalse, qt.Commentf("duplicate child")) - cm[child.Name] = struct{}{} - } - { - // Check that the child in the list exists in the child map - _, exists := dir.Children[child.Name] - qt.Check(t, exists, qt.IsTrue) - } - assertChildren(t, child) - } - qt.Check(t, len(dir.Children), qt.Equals, len(dir.ChildrenList), - // If the lengths are equal then the map and list should contain entries with the same names. - // We don't know if the dir entries are _actually_ equivalent but the test recurses above so it should be fine. - qt.Commentf("%s", dir.Name), - qt.Commentf("list: %v", names(dir.ChildrenList)), - qt.Commentf("keys: %v", keys(dir.Children)), - ) +func assertChildren(t *testing.T, dir *testmark.DirEnt) { + foundChildren := make(map[string]struct{}) + for _, _child := range dir.ChildrenList { + child := _child + t.Run(child.Name, func(t *testing.T) { + _, exists := foundChildren[child.Name] + if exists { + t.Errorf("dir %q has duplicate child: %q", dir.Name, child.Name) + } + foundChildren[child.Name] = struct{}{} + + mapChild, exists := dir.Children[child.Name] + if !exists { + t.Errorf("dir %q missing child: %q", dir.Name, child.Name) + } + if child != mapChild { + t.Errorf("child %q should have equivalent pointers: %p %p", child.Name, child, mapChild) + } + + assertChildren(t, child) + }) + } + // If the lengths are equal then the map and list should contain entries with the same names. + // We don't know if the dir entries are _actually_ equivalent but the test recurses above so it should be fine. + if len(dir.Children) != len(dir.ChildrenList) { + t.Errorf( + "expected dir to have equal number of children in both data structures"+ + "\n\t%s:\n\tlist: %v\n\tkeys: %v", + dir.Name, names(dir.ChildrenList), keys(dir.Children)) + } } -func keys(dirs map[string]*DirEnt) []string { +func keys(dirs map[string]*testmark.DirEnt) []string { names := make([]string, 0, len(dirs)) for k := range dirs { names = append(names, k) @@ -111,7 +126,7 @@ func keys(dirs map[string]*DirEnt) []string { return names } -func names(dirs []*DirEnt) []string { +func names(dirs []*testmark.DirEnt) []string { names := make([]string, 0, len(dirs)) for _, d := range dirs { names = append(names, d.Name) diff --git a/testutil_test.go b/testutil_test.go index cea3512..8eddc7a 100644 --- a/testutil_test.go +++ b/testutil_test.go @@ -22,6 +22,7 @@ import ( // anything else relies on "%v". // It'll emit both the expected and actual values as strings if there's a mismatch. func assert(t *testing.T, actual interface{}, expect string) { + t.Helper() var actualStr string if s, ok := actual.(string); ok { actualStr = s