basic_test.go
package testmark_test
import (
"strings"
"testing"
testmark "github.com/warpfork/go-testmark/v2"
)
const simpleDoc = `# Test Document
[testmark]:# (greeting)
` + "```text\nHello, World!\n```" + `
[testmark]:# (config/db)
` + "```json\n{\"host\": \"localhost\"}\n```" + `
`
func TestBasicParsing(t *testing.T) {
doc, err := testmark.Parse([]byte(simpleDoc))
if err != nil {
t.Fatalf("failed to parse: %v", err)
}
if doc.Count() != 2 {
t.Errorf("expected 2 hunks, got %d", doc.Count())
}
greeting := doc.HunkByName("greeting")
if greeting == nil {
t.Fatal("greeting hunk not found")
}
if greeting.String() != "Hello, World!\n" {
t.Errorf("unexpected greeting content: %q", greeting.String())
}
}
func TestDirectoryStructure(t *testing.T) {
doc, err := testmark.Parse([]byte(simpleDoc))
if err != nil {
t.Fatalf("failed to parse: %v", err)
}
root := doc.Root()
if root == nil {
t.Fatal("root is nil")
}
// Test direct hunk access
greetingEntry := root.Child("greeting")
if greetingEntry == nil {
t.Fatal("greeting entry not found")
}
if greetingEntry.Hunk() == nil {
t.Fatal("greeting hunk is nil")
}
// Test nested access
configEntry := root.Child("config")
if configEntry == nil {
t.Fatal("config entry not found")
}
dbEntry := configEntry.Child("db")
if dbEntry == nil {
t.Fatal("db entry not found")
}
if strings.TrimSpace(dbEntry.Hunk().String()) != `{"host": "localhost"}` {
t.Errorf("unexpected db content: %q", dbEntry.Hunk().String())
}
}
func TestPatching(t *testing.T) {
doc, err := testmark.Parse([]byte(simpleDoc))
if err != nil {
t.Fatalf("failed to parse: %v", err)
}
// Create a patch using the generic constructor
newHunk := testmark.NewHunk("greeting", "text", "Hello, Universe!\n")
patched := doc.Patch(newHunk)
// Verify the change
modified := patched.HunkByName("greeting")
if modified == nil {
t.Fatal("patched hunk not found")
}
if modified.String() != "Hello, Universe!\n" {
t.Errorf("unexpected patched content: %q", modified.String())
}
// Verify original is unchanged
original := doc.HunkByName("greeting")
if original.String() != "Hello, World!\n" {
t.Error("original document was modified")
}
// Verify the patched document can be serialized
patchedStr := patched.String()
if !strings.Contains(patchedStr, "Hello, Universe!") {
t.Error("patched document doesn't contain modified content")
}
}
func TestIterators(t *testing.T) {
doc, err := testmark.Parse([]byte(simpleDoc))
if err != nil {
t.Fatalf("failed to parse: %v", err)
}
// Test hunk iterator
var hunkNames []string
for iter := doc.Hunks(); iter.Next(); {
hunk := iter.Hunk()
hunkNames = append(hunkNames, hunk.Name())
}
expected := []string{"greeting", "config/db"}
if len(hunkNames) != len(expected) {
t.Errorf("expected %d hunks, got %d", len(expected), len(hunkNames))
}
for i, name := range expected {
if i >= len(hunkNames) || hunkNames[i] != name {
t.Errorf("expected hunk %d to be %q, got %q", i, name, hunkNames[i])
}
}
// Test directory iterator
root := doc.Root()
var childNames []string
for iter := root.Children(); iter.Next(); {
entry := iter.Entry()
childNames = append(childNames, entry.Name())
}
expectedChildren := []string{"greeting", "config"}
if len(childNames) != len(expectedChildren) {
t.Errorf("expected %d children, got %d", len(expectedChildren), len(childNames))
}
for i, name := range expectedChildren {
if i >= len(childNames) || childNames[i] != name {
t.Errorf("expected child %d to be %q, got %q", i, name, childNames[i])
}
}
}
v2/basic_test.go · 3.5 KiB · 07c6e4c9e7