From d35b26b754bb4db982b437421972c24d9306ac28 Mon Sep 17 00:00:00 2001 From: bth Date: Tue, 20 Jan 2026 14:57:11 -0800 Subject: [PATCH] tag : import tests from gag --- go.mod | 7 +++ go.sum | 9 +++ tag/parse.go | 4 +- tag/tag.go | 2 +- tag/tag_test.go | 139 +++++++++++++++++++++++++++++++++++++++++ tag/testdata/01.foo.md | 6 ++ tag/testdata/02.foo.md | 6 ++ tag/testdata/03.bar.md | 6 ++ tag/testdata/04.baz.md | 5 ++ tag/testdata/05.quz.md | 5 ++ tag/testdata/06.quz.md | 4 ++ 11 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 go.sum create mode 100644 tag/tag_test.go create mode 100644 tag/testdata/01.foo.md create mode 100644 tag/testdata/02.foo.md create mode 100644 tag/testdata/03.bar.md create mode 100644 tag/testdata/04.baz.md create mode 100644 tag/testdata/05.quz.md create mode 100644 tag/testdata/06.quz.md diff --git a/go.mod b/go.mod index aa7a135..6d602bf 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module github.com/brtholomy/um/go go 1.25.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.11.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cc8b3f4 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +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= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tag/parse.go b/tag/parse.go index 2f0dc00..0d309ce 100644 --- a/tag/parse.go +++ b/tag/parse.go @@ -91,8 +91,8 @@ func getFilelist(glob string) []string { } // create []Entry representing qualifying files in current directory or from stdin -func entriesGlobOrStdin() []Entry { - filelist := getFilelist(GLOB) +func entriesGlobOrStdin(glob string) []Entry { + filelist := getFilelist(glob) // NOTE: size 0, capacity specified: entries := make([]Entry, 0, len(filelist)) diff --git a/tag/tag.go b/tag/tag.go index 346940f..2ddc313 100644 --- a/tag/tag.go +++ b/tag/tag.go @@ -54,7 +54,7 @@ func parseArgs(args []string) options { func Tag(args []string) { opts := parseArgs(args) queries := parseQuery(opts.Query.Val) - entries := entriesGlobOrStdin() + entries := entriesGlobOrStdin(GLOB) // we shrink the entries list immediately if we want a date range: if opts.Date.IsSet() { diff --git a/tag/tag_test.go b/tag/tag_test.go new file mode 100644 index 0000000..afbf86a --- /dev/null +++ b/tag/tag_test.go @@ -0,0 +1,139 @@ +package tag + +import ( + "bytes" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +const TEST_PATTERN string = "./testdata/*.md" + +func TestParseHeader(t *testing.T) { + entries := entriesGlobOrStdin(TEST_PATTERN) + header := parseHeader(&entries[0].content) + expected := "# 01.foo.md\n: 2024.09.25\n+ bar\n+ foo" + assert.Equal(t, expected, header) +} + +func TestEntriesLen(t *testing.T) { + entries := entriesGlobOrStdin(TEST_PATTERN) + expected := 6 + if len(entries) != expected { + t.Errorf("entries should be len == %v, got %v", expected, len(entries)) + } +} + +func TestEntries(t *testing.T) { + entries := entriesGlobOrStdin(TEST_PATTERN) + d, _ := time.Parse("2006.01.02", "2024.09.25") + expected := Entry{filename: "01.foo.md", date: d, content: "# 01.foo.md\n: 2024.09.25\n+ bar\n+ foo\n\nFoo bar.\n", tags: []string{"bar", "foo"}} + assert.Equal(t, expected, entries[0]) +} + +func TestTagmap(t *testing.T) { + entries := entriesGlobOrStdin(TEST_PATTERN) + tagmap := makeTagmap(entries) + expected := Set{"01.foo.md": true, "02.foo.md": true, "03.bar.md": true} + assert.Equal(t, expected, tagmap["bar"]) +} + +func TestAdjacencies(t *testing.T) { + entries := entriesGlobOrStdin(TEST_PATTERN) + tagmap := makeTagmap(entries) + queries := parseQuery("bar") + fs := processQueries(tagmap, queries) + adjacencies := makeAdjacencies(entries, fs) + expected := map[string]Set{ + "foo": Set{"01.foo.md": true}, + "science": Set{"02.foo.md": true, "03.bar.md": true}, + } + assert.Equal(t, expected, adjacencies["bar"]) +} + +func TestPrint(t *testing.T) { + entries := entriesGlobOrStdin(TEST_PATTERN) + tagmap := makeTagmap(entries) + query := parseQuery("bar") + fs := processQueries(tagmap, query) + adjacencies := reduceAdjacencies(makeAdjacencies(entries, fs), query, false) + buf := bytes.Buffer{} + printFiles(&buf, entries, tagmap, fs, adjacencies, query, true) + expected := `[files] +01.foo.md +02.foo.md +03.bar.md + +[tags] +bar = 3 + +[adjacencies] +foo = 1 : 1 +science = 2 : 3 + +[sums] +files = 3 : 6 +adjacencies = 2 : 4 + +` + assert.Equal(t, expected, buf.String()) +} + +func TestBadTag(t *testing.T) { + entries := entriesGlobOrStdin(TEST_PATTERN) + tagmap := makeTagmap(entries) + + _, ok := tagmap["qaz"] + assert.False(t, ok) +} + +// Since entriesGlobOrStdin() involves filesystem reads, we test the underlying logic. +func BenchmarkParseContent(b *testing.B) { + e := entriesGlobOrStdin(TEST_PATTERN)[0] + for b.Loop() { + parseContent(e.filename, &e.content) + } +} + +func BenchmarkTagmap(b *testing.B) { + entries := entriesGlobOrStdin(TEST_PATTERN) + for b.Loop() { + makeTagmap(entries) + } +} + +func BenchmarkAdjacencies(b *testing.B) { + entries := entriesGlobOrStdin(TEST_PATTERN) + tagmap := makeTagmap(entries) + queries := parseQuery("foo") + fs := processQueries(tagmap, queries) + for b.Loop() { + makeAdjacencies(entries, fs) + } +} + +func BenchmarkPrint(b *testing.B) { + entries := entriesGlobOrStdin(TEST_PATTERN) + tagmap := makeTagmap(entries) + query := parseQuery("bar") + fs := processQueries(tagmap, query) + adjacencies := reduceAdjacencies(makeAdjacencies(entries, fs), query, false) + buf := bytes.Buffer{} + for b.Loop() { + printFiles(&buf, entries, tagmap, fs, adjacencies, query, true) + } +} + +func FuzzParseContent(f *testing.F) { + entries := entriesGlobOrStdin(TEST_PATTERN) + for _, e := range entries { + f.Add(e.filename, e.content) + } + f.Fuzz(func(t *testing.T, filename, content string) { + e := parseContent(filename, &content) + if e.date.IsZero() { + t.Fatalf("failed to read date: %v", filename) + } + }) +} diff --git a/tag/testdata/01.foo.md b/tag/testdata/01.foo.md new file mode 100644 index 0000000..b877295 --- /dev/null +++ b/tag/testdata/01.foo.md @@ -0,0 +1,6 @@ +# 01.foo.md +: 2024.09.25 ++ bar ++ foo + +Foo bar. diff --git a/tag/testdata/02.foo.md b/tag/testdata/02.foo.md new file mode 100644 index 0000000..09c624e --- /dev/null +++ b/tag/testdata/02.foo.md @@ -0,0 +1,6 @@ +# 02.foo.md +: 2024.09.25 ++ bar ++ science + +Foo. diff --git a/tag/testdata/03.bar.md b/tag/testdata/03.bar.md new file mode 100644 index 0000000..e4ca707 --- /dev/null +++ b/tag/testdata/03.bar.md @@ -0,0 +1,6 @@ +# 03.bar.md +: 2024.09.25 ++ bar ++ science + +Bar. diff --git a/tag/testdata/04.baz.md b/tag/testdata/04.baz.md new file mode 100644 index 0000000..9bf4407 --- /dev/null +++ b/tag/testdata/04.baz.md @@ -0,0 +1,5 @@ +# 04.baz.md +: 2024.10.09 ++ science + +Blah. Foo. diff --git a/tag/testdata/05.quz.md b/tag/testdata/05.quz.md new file mode 100644 index 0000000..281d2e1 --- /dev/null +++ b/tag/testdata/05.quz.md @@ -0,0 +1,5 @@ +# 05.quz.md +: 2024.10.09 ++ diff + +Blah. diff --git a/tag/testdata/06.quz.md b/tag/testdata/06.quz.md new file mode 100644 index 0000000..213a96a --- /dev/null +++ b/tag/testdata/06.quz.md @@ -0,0 +1,4 @@ +# 06.quz.md +: 2024.10.09 + +Diff.