Files
sfz/edit_test.go
T

106 lines
3.4 KiB
Go
Raw Normal View History

2026-12-01 17:41:34 +00:00
package sfz
import (
"strings"
"testing"
)
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
}
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.
hs[0].Set("volume", "-2")
hs[0].Set("tune", "10")
hs[1].Set("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" || n.Line != 5 {
t.Errorf("tune %+v", n)
}
// What was written reads back.
if v, _ := GetAs[int](doc.Flatten().Regions[0].Opcodes, "lokey"); v != 60 {
t.Errorf("lokey %d", v)
}
}
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)
}
}
func TestAddHeader(t *testing.T) {
doc := mustParse(t, "<group> volume=1\n<region> sample=a.wav", nil)
hs := doc.Headers()
h := doc.AddHeader("region", hs[0])
h.Set("sample", "b.wav")
h2 := doc.AddHeader("region", nil)
h2.Set("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)
doc.Headers()[0].Set("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)
}
doc.SetDefine("$V", "9")
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)
}
doc.SetDefine("$NEW", "1")
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)
}
hs[0].Set("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")
}
}