helpers.go
package testmark
import (
"io/fs"
"path/filepath"
"strings"
)
// ParseFromFS is a convenience function for parsing testmark files from an fs.FS.
// This is useful when working with embedded files or testing.
func ParseFromFS(filesystem fs.FS, filename string) (*Document, error) {
data, err := fs.ReadFile(filesystem, filename)
if err != nil {
return nil, err
}
return Parse(data)
}
// MustParseFromFS is like ParseFromFS but panics on error.
func MustParseFromFS(filesystem fs.FS, filename string) *Document {
doc, err := ParseFromFS(filesystem, filename)
if err != nil {
panic(err)
}
return doc
}
// ParseAllFromFS finds and parses all .md files in a filesystem that contain testmark content.
// Returns a map of filename to Document.
func ParseAllFromFS(filesystem fs.FS, pattern string) (map[string]*Document, error) {
if pattern == "" {
pattern = "*.md"
}
matches, err := fs.Glob(filesystem, pattern)
if err != nil {
return nil, err
}
result := make(map[string]*Document)
for _, filename := range matches {
doc, err := ParseFromFS(filesystem, filename)
if err != nil {
// Skip files that aren't valid testmark
continue
}
// Only include if it actually has hunks
if doc.Count() > 0 {
result[filename] = doc
}
}
return result, nil
}
// HunksByPattern returns all hunks whose names match a glob pattern.
// This is useful for processing groups of related hunks.
func (d *Document) HunksByPattern(pattern string) ([]*Hunk, error) {
var result []*Hunk
for hunk := range d.Hunks() {
matched, err := filepath.Match(pattern, hunk.Name())
if err != nil {
return nil, err
}
if matched {
result = append(result, hunk)
}
}
return result, nil
}
// HunksByPrefix returns all hunks whose names start with the given prefix.
func (d *Document) HunksByPrefix(prefix string) []*Hunk {
var result []*Hunk
for hunk := range d.Hunks() {
if strings.HasPrefix(hunk.Name(), prefix) {
result = append(result, hunk)
}
}
return result
}
// HunksBySuffix returns all hunks whose names end with the given suffix.
func (d *Document) HunksBySuffix(suffix string) []*Hunk {
var result []*Hunk
for hunk := range d.Hunks() {
if strings.HasSuffix(hunk.Name(), suffix) {
result = append(result, hunk)
}
}
return result
}
// HunksByInfoString returns all hunks with the given info string (language hint).
func (d *Document) HunksByInfoString(infoString string) []*Hunk {
var result []*Hunk
for hunk := range d.Hunks() {
if hunk.InfoString() == infoString {
result = append(result, hunk)
}
}
return result
}
// ToMap returns all hunks as a map from name to content string.
// This is useful for quick access and comparison in tests.
func (d *Document) ToMap() map[string]string {
result := make(map[string]string, d.Count())
for hunk := range d.Hunks() {
result[hunk.Name()] = hunk.String()
}
return result
}
// Clone creates a deep copy of the document.
// This is useful when you want to make modifications without affecting the original.
func (d *Document) Clone() *Document {
// The easiest way to clone is to patch with no changes
return d.Patch()
}
v2/helpers.go · 3.1 KiB · 61d171ddc5