Skip to content

Commit

Permalink
Merge pull request #13 from TripleDogDare/index-fix
Browse files Browse the repository at this point in the history
Index fix
  • Loading branch information
warpfork authored Nov 26, 2022
2 parents 9d18105 + 3893644 commit 99388b3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
7 changes: 4 additions & 3 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ func (dirent *DirEnt) fill(pathSegs []string, hunk Hunk) {
next.fill(pathSegs[1:], hunk)
} else {
l := len(dirent.ChildrenList)
dirent.ChildrenList = append(dirent.ChildrenList, DirEnt{
child := DirEnt{
Name: pathSegs[0],
})
dirent.Children[pathSegs[0]] = &dirent.ChildrenList[l]
}
dirent.ChildrenList = append(dirent.ChildrenList, &child)
dirent.Children[pathSegs[0]] = &child
dirent.ChildrenList[l].fill(pathSegs[1:], hunk)
}
}
104 changes: 100 additions & 4 deletions index_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package testmark
package testmark_test

import (
"fmt"
"path/filepath"
"strings"
"testing"

"github.com/warpfork/go-testmark"
)

func TestIndexingDirs(t *testing.T) {
testdata, err := filepath.Abs("testdata")
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)
}
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'")
Expand All @@ -37,3 +45,91 @@ 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")
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))
}
doc.BuildDirIndex()

for _, _hunk := range doc.DataHunks {
hunk := _hunk
t.Run("index:"+hunk.Name, func(t *testing.T) {
assertHunkReachable(t, doc, hunk)
})
}
assertChildren(t, doc.DirEnt)
}

func assertHunkReachable(t *testing.T, doc *testmark.Document, hunk testmark.DocHunk) {
splits := strings.Split(hunk.Name, "/")
dir := doc.DirEnt
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
}
assert(t, hunk.Hunk, fmt.Sprintf("%v", *dir.Hunk))
}

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]*testmark.DirEnt) []string {
names := make([]string, 0, len(dirs))
for k := range dirs {
names = append(names, k)
}
return names
}

func names(dirs []*testmark.DirEnt) []string {
names := make([]string, 0, len(dirs))
for _, d := range dirs {
names = append(names, d.Name)
}
return names
}
4 changes: 3 additions & 1 deletion read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions testexec/testexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,22 @@ func (tcfg *Tester) init() {
// Regen mode will only update hunks that already exist; it won't add them.
// As an edge case, note that if that an exitcode hunk is absent, but a nonzero exitcode is encountered,
// the test will still be failed, even though in patch regen mode most assertions are usually skipped.
func (tcfg Tester) TestSequence(t *testing.T, data testmark.DirEnt) {
func (tcfg Tester) TestSequence(t *testing.T, data *testmark.DirEnt) {
t.Helper()
tcfg.test(t, data, true, false, "")
}

func (tcfg Tester) TestScript(t *testing.T, data testmark.DirEnt) {
func (tcfg Tester) TestScript(t *testing.T, data *testmark.DirEnt) {
t.Helper()
tcfg.test(t, data, false, true, "")
}

func (tcfg Tester) Test(t *testing.T, data testmark.DirEnt) {
func (tcfg Tester) Test(t *testing.T, data *testmark.DirEnt) {
t.Helper()
tcfg.test(t, data, true, true, "")
}

func (tcfg Tester) test(t *testing.T, data testmark.DirEnt, allowExec, allowScript bool, parentTmpdir string) {
func (tcfg Tester) test(t *testing.T, data *testmark.DirEnt, allowExec, allowScript bool, parentTmpdir string) {
t.Helper()
tcfg.init()

Expand Down
2 changes: 1 addition & 1 deletion testmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ type DirEnt struct {

// Children, recursively.
Children map[string]*DirEnt
ChildrenList []DirEnt
ChildrenList []*DirEnt
}
1 change: 1 addition & 0 deletions testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 99388b3

Please sign in to comment.