testmark-go

add more indexing tests

commit 3cadbec7216b6278cf5493f8dee29a85d9ebb50f · parent 9d18105c14 · browse this tree
Calvin Behling, 2022-11-18 15:43:40 UTC

diff --git a/go.mod b/go.mod
index dc56ee0..1196f49 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,9 @@
 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 a4c9c19..e5c9c35 100644
--- a/index_test.go
+++ b/index_test.go
@@ -2,7 +2,10 @@ package testmark
 
 import (
 	"path/filepath"
+	"strings"
 	"testing"
+
+	qt "github.com/frankban/quicktest"
 )
 
 func TestIndexingDirs(t *testing.T) {
@@ -11,12 +14,16 @@ func TestIndexingDirs(t *testing.T) {
 		panic(err)
 	}
 	doc, err := ReadFile(filepath.Join(testdata, "exampleWithDirs.md"))
+	if err != nil {
+		panic(err)
+	}
 	doc.BuildDirIndex()
+
 	if len(doc.DirEnt.ChildrenList) != 2 {
-		t.Errorf("root dirent list should be length 2")
+		t.Errorf("root dirent list should be length 2 but was %d", len(doc.DirEnt.ChildrenList))
 	}
 	if len(doc.DirEnt.Children) != 2 {
-		t.Errorf("root dirent map should be length 2")
+		t.Errorf("root dirent map should be length 2 but was %d", len(doc.DirEnt.Children))
 	}
 	if doc.DirEnt.ChildrenList[0].Name != "one" {
 		t.Errorf("first child of root dirent should've been 'one'")
@@ -37,3 +44,77 @@ func TestIndexingDirs(t *testing.T) {
 		t.Errorf("hunk 'really/deep/dirs/wow' looked up through dir maps should have the right content")
 	}
 }
+
+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)
+	}
+	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))
+	}
+	doc.BuildDirIndex()
+
+	qt.Assert(t, len(doc.DataHunks), qt.Equals, len(doc.HunksByName))
+	for _, hunk := range doc.DataHunks {
+		t.Run("index:"+hunk.Name, func(t *testing.T) {
+			assertHunkReachable(t, doc, hunk)
+		})
+	}
+	assertChildren(t, *doc.DirEnt)
+}
+
+func assertHunkReachable(t *testing.T, doc *Document, hunk 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
+	}
+	qt.Assert(t, &hunk.Hunk, qt.DeepEquals, 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 keys(dirs map[string]*DirEnt) []string {
+	names := make([]string, 0, len(dirs))
+	for k := range dirs {
+		names = append(names, k)
+	}
+	return names
+}
+
+func names(dirs []DirEnt) []string {
+	names := make([]string, 0, len(dirs))
+	for _, d := range dirs {
+		names = append(names, d.Name)
+	}
+	return names
+}
diff --git a/read_test.go b/read_test.go
index 4ce03c8..41d1c12 100644
--- a/read_test.go
+++ b/read_test.go
@@ -18,7 +18,9 @@ func TestRead(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-
+	if len(doc.DataHunks) != len(doc.HunksByName) {
+		t.Errorf("document hunk list has different length than hunks-by-name: %d != %d", len(doc.DataHunks), len(doc.HunksByName))
+	}
 	readFixturesExample(t, doc)
 }