Check the text of structural edits and refuse stale instruments
AddRegion, RemoveRegion and MoveRegion now refuse an edit whose written text would read differently from the nodes, as when a comment that never ends takes in the text after it, and an instrument whose regions were changed other than by Apply. Build refuses an unknown trigger and an empty random range. Add tests for edits across files, defines, skipped regions and edge cases, a random-sequence test and FuzzRegionEdits. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+344
-8
@@ -2,9 +2,11 @@ package sfz
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -210,14 +212,15 @@ func TestRegionEditsKeepUnknownOpcodes(t *testing.T) {
|
||||
if src, _ := out.Source(1); src.Own["zampler_zone"] != "zb" {
|
||||
t.Errorf("zampler_zone didn't survive writing: %q", doc.String())
|
||||
}
|
||||
if problems := doc.Validate(); len(problems) == 0 {
|
||||
t.Log("zampler_zone isn't reported")
|
||||
} else {
|
||||
for _, p := range problems {
|
||||
if p.Severity != SeverityInfo {
|
||||
t.Errorf("problem %+v", p)
|
||||
}
|
||||
var reported bool
|
||||
for _, p := range doc.Validate() {
|
||||
if p.Severity != SeverityInfo {
|
||||
t.Errorf("problem %+v", p)
|
||||
}
|
||||
reported = reported || p.Code == "opcode.unknown"
|
||||
}
|
||||
if !reported {
|
||||
t.Error("zampler_zone isn't reported as an unknown opcode")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,7 +228,7 @@ func TestRegionEditsKeepUnknownOpcodes(t *testing.T) {
|
||||
// region, and that a region from NewRegion builds and reads back.
|
||||
func TestBuildRefusesZeroRegion(t *testing.T) {
|
||||
_, err := Build(&Instrument{Regions: []Region{{Sample: "a.wav"}}})
|
||||
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), "trigger") {
|
||||
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), "region 0: the trigger is empty") {
|
||||
t.Errorf("error %v", err)
|
||||
}
|
||||
inst := &Instrument{Regions: []Region{NewRegion("a.wav")}}
|
||||
@@ -288,3 +291,336 @@ func TestRegionEditsOnFixtures(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckRegion checks each way that a region is refused.
|
||||
func TestCheckRegion(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
change func(*Region)
|
||||
want string
|
||||
}{
|
||||
"empty sample": {func(r *Region) { r.Sample = "" }, "sample is empty"},
|
||||
"empty trigger": {func(r *Region) { r.Trigger = "" }, "trigger is empty"},
|
||||
"unknown trigger": {func(r *Region) { r.Trigger = "bogus" }, `trigger "bogus"`},
|
||||
"unknown off mode": {func(r *Region) { r.OffMode = "" }, "off mode"},
|
||||
"sequence length": {func(r *Region) { r.SequenceLength = 0 }, "sequence"},
|
||||
"sequence position": {func(r *Region) { r.SequencePosition = 0 }, "sequence"},
|
||||
"never plays": {func(r *Region) { r.HighRandom = 0 }, "random range"},
|
||||
"empty random range": {func(r *Region) { r.LowRandom, r.HighRandom = 0.5, 0.5 }, "random range"},
|
||||
"keys": {func(r *Region) { r.LowKey, r.HighKey = 10, 9 }, "highest key"},
|
||||
"velocities": {func(r *Region) { r.LowVelocity, r.HighVelocity = 10, 9 }, "highest velocity"},
|
||||
}
|
||||
for name, tc := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
r := NewRegion("a.wav")
|
||||
tc.change(&r)
|
||||
_, err := Build(&Instrument{Regions: []Region{r}})
|
||||
if !errors.Is(err, ErrInvalid) || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Errorf("Build returned %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, trigger := range []string{"attack", "release", "first", "legato", "release_key"} {
|
||||
r := NewRegion("a.wav")
|
||||
r.Trigger = trigger
|
||||
if _, err := Build(&Instrument{Regions: []Region{r}}); err != nil {
|
||||
t.Errorf("trigger %s: %v", trigger, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegionEditEdgeCases checks edits at the edges of the text: layout that
|
||||
// the text of a region's span can't be moved through, and documents that don't
|
||||
// end in a line break.
|
||||
func TestRegionEditEdgeCases(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
text string
|
||||
edit func(*Instrument) error
|
||||
want string // the document afterwards, or "" if the edit is refused
|
||||
}{
|
||||
"move a region that ends in a comment that never ends": {
|
||||
"<region> sample=a.wav\n<region> sample=b.wav\n/* open",
|
||||
func(i *Instrument) error { return i.MoveRegion(1, 0) }, "",
|
||||
},
|
||||
"add after a comment that never ends": {
|
||||
"<region> sample=a.wav /* open",
|
||||
func(i *Instrument) error { return i.AddRegion(NewRegion("b.wav")) }, "",
|
||||
},
|
||||
"move the last of a file with no final line break": {
|
||||
"<region> sample=a.wav\n<region> sample=b.wav",
|
||||
func(i *Instrument) error { return i.MoveRegion(1, 0) }, "<region> sample=b.wav\n<region> sample=a.wav\n",
|
||||
},
|
||||
"remove the only region": {
|
||||
"<region> sample=a.wav\n",
|
||||
func(i *Instrument) error { return i.RemoveRegion(0) }, "",
|
||||
},
|
||||
"comments above a header stay with the header before": {
|
||||
"// first\n<region> sample=a.wav\n// second\n<region> sample=b.wav\n",
|
||||
func(i *Instrument) error { return i.RemoveRegion(0) }, "// first\n<region> sample=b.wav\n",
|
||||
},
|
||||
"add to an empty document": {
|
||||
"",
|
||||
func(i *Instrument) error { return i.AddRegion(NewRegion("a.wav")) }, "<region>\nsample=a.wav\n",
|
||||
},
|
||||
}
|
||||
for name, tc := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
doc := mustParse(t, tc.text, nil)
|
||||
inst := readInstrument(t, doc)
|
||||
n := len(inst.Regions)
|
||||
err := tc.edit(inst)
|
||||
switch {
|
||||
case tc.want == "" && name == "remove the only region":
|
||||
if err != nil || doc.String() != "" || len(inst.Regions) != 0 {
|
||||
t.Errorf("err %v, document %q, %d regions", err, doc.String(), len(inst.Regions))
|
||||
}
|
||||
case tc.want == "":
|
||||
if !(errors.Is(err, ErrUnpatchable) || errors.Is(err, ErrInvalid)) || doc.String() != tc.text || len(inst.Regions) != n {
|
||||
t.Errorf("err %v, document %q, %d regions", err, doc.String(), len(inst.Regions))
|
||||
}
|
||||
default:
|
||||
if err != nil || doc.String() != tc.want {
|
||||
t.Errorf("err %v, document %q, want %q", err, doc.String(), tc.want)
|
||||
}
|
||||
}
|
||||
if got := readInstrument(t, mustParse(t, doc.String(), nil)); len(got.Regions) != len(inst.Regions) {
|
||||
t.Errorf("the written text reads as %d regions, the instrument has %d", len(got.Regions), len(inst.Regions))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegionEditsNeedAnUnchangedInstrument checks that an edit refuses an
|
||||
// instrument that the document or the caller has changed since it was read.
|
||||
func TestRegionEditsNeedAnUnchangedInstrument(t *testing.T) {
|
||||
doc := mustParse(t, "<region> sample=a.wav\n", nil)
|
||||
inst := readInstrument(t, doc)
|
||||
inst.Regions[0].Volume = -9
|
||||
if err := inst.AddRegion(NewRegion("b.wav")); !errors.Is(err, ErrNoSource) || doc.String() != "<region> sample=a.wav\n" {
|
||||
t.Errorf("a changed region: %v, %q", err, doc.String())
|
||||
}
|
||||
// Apply is how a region is changed.
|
||||
inst = readInstrument(t, doc)
|
||||
r := inst.Regions[0]
|
||||
r.Volume = -9
|
||||
if err := inst.Apply(0, r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := inst.AddRegion(NewRegion("b.wav")); err != nil {
|
||||
t.Errorf("after Apply: %v", err)
|
||||
}
|
||||
h, _ := doc.AddHeader("region", nil)
|
||||
h.Set("sample", "q.wav")
|
||||
if err := inst.RemoveRegion(0); !errors.Is(err, ErrNoSource) {
|
||||
t.Errorf("a changed document: %v", err)
|
||||
}
|
||||
if err := (&Instrument{Regions: []Region{NewRegion("a.wav")}}).AddRegion(NewRegion("b.wav")); !errors.Is(err, ErrNoSource) {
|
||||
t.Errorf("an instrument not read from a document: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegionEditsWithSkippedRegions checks that typed indexes count only the
|
||||
// regions the model has, that skipped regions stay where they are, and that
|
||||
// Apply and Source follow the edits.
|
||||
func TestRegionEditsWithSkippedRegions(t *testing.T) {
|
||||
text := "<region> sample=a.wav\n<region> lokey=1\n<region> sample=b.wav\n<region> hikey=2\n<region> sample=c.wav\n"
|
||||
doc := mustParse(t, text, nil)
|
||||
inst := readInstrument(t, doc)
|
||||
if err := inst.MoveRegion(2, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := "<region> sample=c.wav\n<region> sample=a.wav\n<region> lokey=1\n<region> sample=b.wav\n<region> hikey=2\n"; doc.String() != want {
|
||||
t.Errorf("document %q", doc.String())
|
||||
}
|
||||
if regionSamples(inst) != "c.wav a.wav b.wav" || inst.Skipped[SkippedNoSample] != 2 {
|
||||
t.Errorf("regions %q, skipped %v", regionSamples(inst), inst.Skipped)
|
||||
}
|
||||
r := inst.Regions[2]
|
||||
r.Pan = 10
|
||||
if err := inst.Apply(2, r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(doc.String(), "sample=b.wav pan=10") {
|
||||
t.Errorf("Apply changed the wrong header: %q", doc.String())
|
||||
}
|
||||
if err := inst.RemoveRegion(1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if regionSamples(inst) != "c.wav b.wav" || strings.Count(doc.String(), "<region>") != 4 {
|
||||
t.Errorf("regions %q, document %q", regionSamples(inst), doc.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegionEditsAcrossFiles checks edits of documents that have includes.
|
||||
func TestRegionEditsAcrossFiles(t *testing.T) {
|
||||
files := map[string]string{"one.sfz": "<region> sample=a.wav\n<region> sample=b.wav\n", "two.sfz": "<region> sample=c.wav\n"}
|
||||
doc := mustParse(t, "#include \"one.sfz\"\n#include \"two.sfz\"\n", files)
|
||||
inst := readInstrument(t, doc)
|
||||
if err := inst.MoveRegion(2, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if regionSamples(inst) != "c.wav a.wav b.wav" {
|
||||
t.Errorf("regions %q", regionSamples(inst))
|
||||
}
|
||||
var one, two string
|
||||
for _, f := range doc.Files() {
|
||||
if f.Path == "one.sfz" {
|
||||
one = f.String()
|
||||
} else if f.Path == "two.sfz" {
|
||||
two = f.String()
|
||||
}
|
||||
}
|
||||
if one != "<region> sample=c.wav\n<region> sample=a.wav\n<region> sample=b.wav\n" || strings.Contains(two, "region") {
|
||||
t.Errorf("one.sfz %q, two.sfz %q", one, two)
|
||||
}
|
||||
if err := inst.AddRegion(NewRegion("d.wav")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := doc.Root.String(); !strings.HasSuffix(got, "<region>\nsample=d.wav\n") || strings.Count(got, "#include") != 2 {
|
||||
t.Errorf("main file %q", got)
|
||||
}
|
||||
if regionSamples(inst) != "c.wav a.wav b.wav d.wav" {
|
||||
t.Errorf("regions %q", regionSamples(inst))
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegionEditsWithDefines checks that moving a region over a #define that
|
||||
// changes it is refused, and that one that doesn't is allowed.
|
||||
func TestRegionEditsWithDefines(t *testing.T) {
|
||||
text := "#define $v 1\n<region> sample=a.wav volume=$v\n#define $v 2\n<region> sample=b.wav volume=$v\n"
|
||||
doc := mustParse(t, text, nil)
|
||||
inst := readInstrument(t, doc)
|
||||
if err := inst.MoveRegion(1, 0); !errors.Is(err, ErrUnpatchable) || doc.String() != text {
|
||||
t.Errorf("a move past a #define: %v, %q", err, doc.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestAddRegionFields checks that a region with settings that are read from
|
||||
// several opcodes is added as it is.
|
||||
func TestAddRegionFields(t *testing.T) {
|
||||
doc := mustParse(t, "<region> sample=a.wav\n", nil)
|
||||
inst := readInstrument(t, doc)
|
||||
r := NewRegion("b.wav")
|
||||
width := 50.0
|
||||
r.RootFromSample, r.Width, r.Tune, r.Pan = true, &width, 12, -30
|
||||
r.LowKey, r.HighKey, r.LowVelocity, r.HighVelocity = 36, 72, 20, 100
|
||||
r.VelocityCurve = []CurvePoint{{Velocity: 0, Gain: 0}, {Velocity: 30, Gain: 0.5}, {Velocity: 100, Gain: 0.9}, {Velocity: 127, Gain: 1}}
|
||||
r.Envelope.Attack, r.Envelope.Release = 0.1, 1.5
|
||||
r.Trigger, r.SequenceLength, r.SequencePosition = "release", 4, 2
|
||||
r.LowRandom, r.HighRandom = 0.25, 0.5
|
||||
if err := inst.AddRegion(r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if wrong := differing(inst.Regions[1], r); len(wrong) > 0 {
|
||||
t.Errorf("the region differs in %v", wrong)
|
||||
}
|
||||
}
|
||||
|
||||
// editOps applies the edits that ops asks for to the document of text, and
|
||||
// fails the test if an edit that is accepted writes text that reads as
|
||||
// something else than the instrument, or one that is refused changes anything.
|
||||
// Each byte of ops is an edit: its low bits choose which, and the rest which
|
||||
// regions.
|
||||
func editOps(t *testing.T, text string, inc map[string]string, ops []byte) {
|
||||
t.Helper()
|
||||
doc, err := Parse(strings.NewReader(text), WithResolver(files(inc)))
|
||||
if err != nil {
|
||||
t.Skip()
|
||||
}
|
||||
inst := doc.Flatten().Instrument()
|
||||
for _, op := range ops {
|
||||
n := len(inst.Regions)
|
||||
before := doc.String()
|
||||
var err error
|
||||
var name string
|
||||
switch op % 3 {
|
||||
case 0:
|
||||
name = "AddRegion"
|
||||
err = inst.AddRegion(NewRegion("n" + strconv.Itoa(int(op)) + ".wav"))
|
||||
case 1:
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
name = "RemoveRegion"
|
||||
err = inst.RemoveRegion(int(op/3) % n)
|
||||
case 2:
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
name = "MoveRegion"
|
||||
err = inst.MoveRegion(int(op/3)%n, int(op/3/7)%n)
|
||||
}
|
||||
if err != nil {
|
||||
if doc.String() != before || len(inst.Regions) != n {
|
||||
t.Fatalf("%s was refused with %v and changed the document to %q", name, err, doc.String())
|
||||
}
|
||||
continue
|
||||
}
|
||||
fresh := doc.Flatten().Instrument()
|
||||
reparsed, perr := Parse(strings.NewReader(doc.String()), WithResolver(files(inc)))
|
||||
if perr != nil {
|
||||
t.Fatalf("%s wrote text that doesn't parse: %v", name, perr)
|
||||
}
|
||||
// The text of each file has to read back as its nodes. A file with
|
||||
// includes isn't written by String, so each file is checked on its own.
|
||||
for _, f := range doc.Files() {
|
||||
if got, want := significant(lex(f.String())), significant(f.Nodes); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("%s wrote %q, which reads as %q, not %q", name, f.String(), got, want)
|
||||
}
|
||||
}
|
||||
if len(fresh.Regions) != len(inst.Regions) {
|
||||
t.Fatalf("%s: the instrument has %d regions, the document %d", name, len(inst.Regions), len(fresh.Regions))
|
||||
}
|
||||
for i := range fresh.Regions {
|
||||
if wrong := differing(fresh.Regions[i], inst.Regions[i]); len(wrong) > 0 {
|
||||
t.Fatalf("%s: region %d differs from a fresh read in %v", name, i, wrong)
|
||||
}
|
||||
}
|
||||
if len(reparsed.Flatten().Instrument().Regions) != len(inst.Regions) && len(inc) == 0 {
|
||||
t.Fatalf("%s: the written text %q reads as other than the instrument", name, doc.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// regionEditTexts are documents for the tests of edits in sequence.
|
||||
var regionEditTexts = []string{
|
||||
"<region> sample=a.wav\n<region> sample=b.wav\n<region> sample=c.wav\n",
|
||||
"<group> volume=-3\n<region> sample=a.wav // one\n zampler_zone=1\n<region> lokey=3\n<group> hikey=60\n<region> sample=b.wav\n<region> sample=c.wav\n",
|
||||
"// top\n<control> default_path=S/\n<region> sample=S/a.wav /* note */ <region> sample=S/b.wav\n",
|
||||
"<region> sample=a.wav\n<region> sample=b.wav\n/* open",
|
||||
"#define $x 1\n<region> sample=a.wav volume=$x\n#define $x 2\n<region> sample=b.wav volume=$x",
|
||||
"<region> sample=a.wav\r\n<region> sample=b.wav\r\n",
|
||||
"",
|
||||
}
|
||||
|
||||
// TestRegionEditSequences applies random sequences of edits to documents.
|
||||
func TestRegionEditSequences(t *testing.T) {
|
||||
rng := rand.New(rand.NewSource(1))
|
||||
for _, text := range regionEditTexts {
|
||||
for run := 0; run < 60; run++ {
|
||||
ops := make([]byte, 1+rng.Intn(12))
|
||||
for i := range ops {
|
||||
ops[i] = byte(rng.Intn(256))
|
||||
}
|
||||
editOps(t, text, nil, ops)
|
||||
}
|
||||
}
|
||||
files := map[string]string{"one.sfz": "<region> sample=a.wav\n<group> volume=1\n<region> sample=b.wav\n", "two.sfz": "<region> sample=c.wav\n"}
|
||||
for run := 0; run < 60; run++ {
|
||||
ops := make([]byte, 1+rng.Intn(12))
|
||||
for i := range ops {
|
||||
ops[i] = byte(rng.Intn(256))
|
||||
}
|
||||
editOps(t, "#include \"one.sfz\"\n#include \"two.sfz\"\n", files, ops)
|
||||
}
|
||||
}
|
||||
|
||||
// FuzzRegionEdits checks that any sequence of region edits, of any document,
|
||||
// either fails and changes nothing, or writes text that reads as the instrument.
|
||||
func FuzzRegionEdits(f *testing.F) {
|
||||
for _, text := range regionEditTexts {
|
||||
f.Add(text, []byte{0, 4, 5, 8, 14, 3, 20})
|
||||
}
|
||||
f.Fuzz(func(t *testing.T, text string, ops []byte) {
|
||||
editOps(t, text, nil, ops)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user