tag : import tests from gag

This commit is contained in:
bth 2026-01-20 14:57:11 -08:00
commit d35b26b754
11 changed files with 190 additions and 3 deletions

7
go.mod
View file

@ -1,3 +1,10 @@
module github.com/brtholomy/um/go module github.com/brtholomy/um/go
go 1.25.4 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
)

9
go.sum Normal file
View file

@ -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=

View file

@ -91,8 +91,8 @@ func getFilelist(glob string) []string {
} }
// create []Entry representing qualifying files in current directory or from stdin // create []Entry representing qualifying files in current directory or from stdin
func entriesGlobOrStdin() []Entry { func entriesGlobOrStdin(glob string) []Entry {
filelist := getFilelist(GLOB) filelist := getFilelist(glob)
// NOTE: size 0, capacity specified: // NOTE: size 0, capacity specified:
entries := make([]Entry, 0, len(filelist)) entries := make([]Entry, 0, len(filelist))

View file

@ -54,7 +54,7 @@ func parseArgs(args []string) options {
func Tag(args []string) { func Tag(args []string) {
opts := parseArgs(args) opts := parseArgs(args)
queries := parseQuery(opts.Query.Val) queries := parseQuery(opts.Query.Val)
entries := entriesGlobOrStdin() entries := entriesGlobOrStdin(GLOB)
// we shrink the entries list immediately if we want a date range: // we shrink the entries list immediately if we want a date range:
if opts.Date.IsSet() { if opts.Date.IsSet() {

139
tag/tag_test.go Normal file
View file

@ -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)
}
})
}

6
tag/testdata/01.foo.md vendored Normal file
View file

@ -0,0 +1,6 @@
# 01.foo.md
: 2024.09.25
+ bar
+ foo
Foo bar.

6
tag/testdata/02.foo.md vendored Normal file
View file

@ -0,0 +1,6 @@
# 02.foo.md
: 2024.09.25
+ bar
+ science
Foo.

6
tag/testdata/03.bar.md vendored Normal file
View file

@ -0,0 +1,6 @@
# 03.bar.md
: 2024.09.25
+ bar
+ science
Bar.

5
tag/testdata/04.baz.md vendored Normal file
View file

@ -0,0 +1,5 @@
# 04.baz.md
: 2024.10.09
+ science
Blah. Foo.

5
tag/testdata/05.quz.md vendored Normal file
View file

@ -0,0 +1,5 @@
# 05.quz.md
: 2024.10.09
+ diff
Blah.

4
tag/testdata/06.quz.md vendored Normal file
View file

@ -0,0 +1,4 @@
# 06.quz.md
: 2024.10.09
Diff.