Files
sfz/edit_test.go
aneurinandClaude Sonnet 5.5 158661eefe Rewrite the code comments for consistent tone and accuracy
Comments only: the tokens of every Go file are unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-01 19:08:00 +00:00

268 lines
8.8 KiB
Go

package sfz
import (
"errors"
"fmt"
"strconv"
"strings"
"testing"
)
// mustParse parses text, reading its includes from the map m of paths to
// contents, and fails the test if it can't.
func mustParse(t *testing.T, text string, m map[string]string) *Document {
t.Helper()
if m == nil {
m = map[string]string{}
}
doc, err := Parse(strings.NewReader(text), WithResolver(files(m)))
if err != nil {
t.Fatal(err)
}
return doc
}
// set sets an opcode, which the test expects to be allowed.
func set(t *testing.T, h *Header, name, value string) {
t.Helper()
if err := h.Set(name, value); err != nil {
t.Fatal(err)
}
}
// TestSetKeepsLayout checks that Set changes an opcode in place, adds a new
// one laid out like the last, and that what is written reads back.
func TestSetKeepsLayout(t *testing.T) {
doc := mustParse(t, "// piano\n<group>\n\tvolume=-1 // quiet\n\tpan=5\n<region> sample=a.wav\n", nil)
hs := doc.Headers()
if len(hs) != 2 || hs[0].Name() != "group" {
t.Fatalf("headers %v", hs)
}
// An existing opcode keeps its place and the comment after it, a new one
// goes after the last and is laid out as it is.
set(t, hs[0], "volume", "-2")
set(t, hs[0], "tune", "10")
set(t, hs[1], "lokey", "c4")
want := "// piano\n<group>\n\tvolume=-2 // quiet\n\tpan=5\n\ttune=10\n<region> sample=a.wav lokey=c4\n"
if got := doc.String(); got != want {
t.Errorf("got %q\nwant %q", got, want)
}
if n, ok := hs[0].Get("tune"); !ok || n.Value != "10" {
t.Errorf("tune %+v", n)
} else if line, col := hs[0].File.Position(n); line != 5 || col != 2 {
t.Errorf("tune at %d:%d", line, col)
}
// What was written reads back.
if v, _ := GetAs[int](doc.Flatten().Regions[0].Opcodes, "lokey"); v != 60 {
t.Errorf("lokey %d", v)
}
}
// TestDelete checks that Delete removes every copy of an opcode, and does
// nothing for one that isn't there.
func TestDelete(t *testing.T) {
doc := mustParse(t, "<region>\n sample=a.wav\n lokey=1\n hikey=2\n lokey=3\n", nil)
h := doc.Headers()[0]
h.Delete("lokey")
h.Delete("missing")
if got, want := doc.String(), "<region>\n sample=a.wav\n hikey=2\n"; got != want {
t.Errorf("got %q want %q", got, want)
}
}
// TestAddHeader checks that AddHeader puts a header after another header's
// opcodes or at the end of the file, and that Set fills it.
func TestAddHeader(t *testing.T) {
doc := mustParse(t, "<group> volume=1\n<region> sample=a.wav", nil)
hs := doc.Headers()
h, err := doc.AddHeader("region", hs[0])
if err != nil {
t.Fatal(err)
}
set(t, h, "sample", "b.wav")
h2, err := doc.AddHeader("region", nil)
if err != nil {
t.Fatal(err)
}
set(t, h2, "sample", "c.wav")
want := "<group> volume=1\n<region>\nsample=b.wav\n<region> sample=a.wav\n<region>\nsample=c.wav\n"
if got := doc.String(); got != want {
t.Errorf("got %q\nwant %q", got, want)
}
if n := len(doc.Flatten().Regions); n != 3 {
t.Errorf("%d regions", n)
}
}
// TestEditVariables checks the difference between changing an opcode that
// uses a variable and changing the variable.
func TestEditVariables(t *testing.T) {
text := "#define $V 5\n<region> sample=a.wav lokey=$V\n<region> sample=b.wav hikey=$V\n"
doc := mustParse(t, text, nil)
set(t, doc.Headers()[0], "lokey", "7")
if got, want := doc.String(), "#define $V 5\n<region> sample=a.wav lokey=7\n<region> sample=b.wav hikey=$V\n"; got != want {
t.Errorf("Set: %q", got)
}
if err := doc.SetDefine("$V", "9"); err != nil {
t.Fatal(err)
}
regions := doc.Flatten().Regions
if regions[0].Opcodes["lokey"] != "7" || regions[1].Opcodes["hikey"] != "9" {
t.Errorf("SetDefine: %v %v", regions[0].Opcodes, regions[1].Opcodes)
}
if err := doc.SetDefine("$NEW", "1"); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(doc.String(), "#define $NEW 1\n#define $V 9\n") || len(doc.Defines()) != 2 {
t.Errorf("new define: %q", doc.String())
}
}
// TestEditIncluded checks that an opcode of an included file is edited in that
// file.
func TestEditIncluded(t *testing.T) {
doc := mustParse(t, "#include \"x.sfz\"\n", map[string]string{"x.sfz": "<region> sample=a.wav\n"})
hs := doc.Headers()
if len(hs) != 1 || hs[0].File.Path != "x.sfz" {
t.Fatalf("headers %+v", hs)
}
set(t, hs[0], "lokey", "3")
if doc.String() != "#include \"x.sfz\"\n" || doc.Files()[1].String() != "<region> sample=a.wav lokey=3\n" {
t.Errorf("%q %q", doc.String(), doc.Files()[1].String())
}
if len(doc.Includes()) != 1 {
t.Error("includes")
}
}
// TestSetKeepsComments checks where Set puts a new opcode among the comments,
// directives and text that follow the last one.
func TestSetKeepsComments(t *testing.T) {
for _, tc := range []struct{ text, want string }{
{"<region>\n sample=a.wav // the sample\n", "<region>\n sample=a.wav // the sample\n lokey=1\n"},
{"<region> sample=a.wav // c\n<region>", "<region> sample=a.wav // c\nlokey=1\n<region>"},
{"<region> sample=a.wav /* c */\n", "<region> sample=a.wav /* c */ lokey=1\n"},
{"<region> sample=a.wav #include \"x\"\n", "<region> sample=a.wav lokey=1 #include \"x\"\n"},
{"<region> sample=a.wav /* open", "<region> sample=a.wav lokey=1 /* open"},
{"<region>\nstray\n", "<region>\nlokey=1\nstray\n"},
} {
doc := mustParse(t, tc.text, nil)
set(t, doc.Headers()[0], "lokey", "1")
if got := doc.String(); got != tc.want {
t.Errorf("%q\n got %q\nwant %q", tc.text, got, tc.want)
}
}
}
// TestSetRefusesBadValues checks that Set, SetDefine and AddHeader refuse
// what can't be written, and change nothing.
func TestSetRefusesBadValues(t *testing.T) {
text := "<region> sample=a.wav\n"
for _, value := range []string{"1\n<region>", "1 // c", "1 /* c", "1 hikey=2", "<group>", "1\r\n2"} {
doc := mustParse(t, text, nil)
if err := doc.Headers()[0].Set("lokey", value); !errors.Is(err, ErrInvalid) || doc.String() != text {
t.Errorf("%q: err %v, text %q", value, err, doc.String())
}
}
doc := mustParse(t, text, nil)
for _, name := range []string{"", "a b", "a=b", "lo\nkey", "<x>"} {
if err := doc.Headers()[0].Set(name, "1"); !errors.Is(err, ErrInvalid) || doc.String() != text {
t.Errorf("name %q: err %v, text %q", name, err, doc.String())
}
}
// A value may hold spaces and =.
set(t, doc.Headers()[0], "sample", " A=440 b.wav ")
if got := doc.String(); got != "<region> sample=A=440 b.wav\n" {
t.Errorf("got %q", got)
}
if err := doc.SetDefine("$X", "1\n2"); !errors.Is(err, ErrInvalid) {
t.Errorf("define err %v", err)
}
if err := doc.SetDefine("X", "1"); !errors.Is(err, ErrInvalid) {
t.Errorf("define name err %v", err)
}
if _, err := doc.AddHeader("a b", nil); !errors.Is(err, ErrInvalid) {
t.Errorf("header err %v", err)
}
if _, err := mustParse(t, "<region> x=1 /* open", nil).AddHeader("region", nil); !errors.Is(err, ErrInvalid) {
t.Errorf("header after open comment err %v", err)
}
}
// TestDeleteKeepsLines checks that Delete keeps the line breaks and comments
// that aren't the opcode's.
func TestDeleteKeepsLines(t *testing.T) {
for _, tc := range []struct{ text, want string }{
{"<region>\n a=1\n b=2\n a=3 // x\n", "<region>\n b=2\n // x\n"},
{"<region> a=1 b=2 c=3\n", "<region> b=2 c=3\n"},
{"<region> x=1 // c\na=1 /* y */ b=2\n", "<region> x=1 // c\n/* y */ b=2\n"},
{"<region> x=1 // c\na=1", "<region> x=1 // c"},
} {
doc := mustParse(t, tc.text, nil)
doc.Headers()[0].Delete("a")
if got := doc.String(); got != tc.want {
t.Errorf("%q\n got %q\nwant %q", tc.text, got, tc.want)
}
}
}
// bigDocument is a document with n regions.
func bigDocument(t testing.TB, n int) *Document {
var b strings.Builder
for i := 0; i < n; i++ {
fmt.Fprintf(&b, "<region> sample=a%d.wav lokey=%d hikey=%d pitch_keycenter=%d lovel=1 hivel=127 tune=3 // %d\n", i, i%100, i%100, i%100, i)
}
doc, err := Parse(strings.NewReader(b.String()))
if err != nil {
t.Fatal(err)
}
return doc
}
// TestPositionsAfterEdits checks that lines and columns follow edits.
func TestPositionsAfterEdits(t *testing.T) {
doc := bigDocument(t, 50)
hs := doc.Headers()
for i := 0; i < 50; i += 2 {
set(t, hs[i], "volume", "-3")
hs[i].Delete("tune")
}
file := doc.Root
for _, h := range doc.Headers() {
line, col := file.Position(h.Node)
// Each region is one line, and a new volume opcode adds none.
if col != 1 || strings.Count(file.String()[:offsetOf(file, h.Node)], "\n")+1 != line {
t.Fatalf("header at %d:%d", line, col)
}
}
}
// offsetOf returns the byte offset of a node in its file's text.
func offsetOf(f *File, n *Node) int {
off := 0
for _, m := range f.Nodes {
if m == n {
return off
}
off += len(m.Raw)
}
return -1
}
// BenchmarkSet edits each region of a large document, the way a bulk change
// does.
func BenchmarkSet(b *testing.B) {
doc := bigDocument(b, 20000)
hs := doc.Headers()
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := hs[i%len(hs)]
if err := h.Set("zz", strconv.Itoa(i)); err != nil {
b.Fatal(err)
}
if err := h.Set("lokey", strconv.Itoa(i)); err != nil {
b.Fatal(err)
}
}
}