|
|
@@ -1,8 +1,10 @@
|
|
|
package project
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
|
|
|
@@ -166,3 +168,159 @@ func FuzzLoad(f *testing.F) {
|
|
|
p.PoolFiles()
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+// TestBrokenFieldsDoNotPanic changes, one at a time, each field that project
|
|
|
+// reads in DemoFile: replaced by null, text, a negative or large number, an
|
|
|
+// empty list or object, or deleted. Whatever wrap makes of the result, reads
|
|
|
+// and edits must not panic. Only the first item of each list is changed, so
|
|
|
+// the test stays fast; a full sweep (all fields to depth 5, 12,726 cases)
|
|
|
+// found nothing either and takes about 10 minutes.
|
|
|
+func TestBrokenFieldsDoNotPanic(t *testing.T) {
|
|
|
+ if testing.Short() {
|
|
|
+ t.Skip("slow")
|
|
|
+ }
|
|
|
+ base, err := xpj.ReadFile(filepath.Join(testdata, "device", "DemoFile.xpj"))
|
|
|
+ must(t, err)
|
|
|
+ d, err := base.Data()
|
|
|
+ must(t, err)
|
|
|
+
|
|
|
+ events := "sequences[0].value.trackClipMaps[0][0].value.eventList.events"
|
|
|
+ roots := []struct {
|
|
|
+ path string
|
|
|
+ depth int
|
|
|
+ }{
|
|
|
+ {"version", 0}, {"currentSequence", 0}, {"tracks[0].name", 0},
|
|
|
+ {"samples", 3}, {"tracks[0].samples", 2},
|
|
|
+ {"tracks[0].program.drum.instruments", 1},
|
|
|
+ {"tracks[0].program.drum.instruments[0]", 1},
|
|
|
+ {"tracks[0].program.drum.instruments[0].layersv", 2},
|
|
|
+ {"tracks[0].program.drum.instruments[0].layersv[0].sliceInfo", 1},
|
|
|
+ {"sequences", 3},
|
|
|
+ {"sequences[0].value.trackClipMaps", 4},
|
|
|
+ {events, 3},
|
|
|
+ }
|
|
|
+ // The first event is automation; add the first note event too.
|
|
|
+ evs, err := d.Get(events)
|
|
|
+ must(t, err)
|
|
|
+ for i, ev := range evs.Items {
|
|
|
+ if num(ev.Lookup("type")) == eventNote {
|
|
|
+ roots = append(roots, struct {
|
|
|
+ path string
|
|
|
+ depth int
|
|
|
+ }{fmt.Sprintf("%s[%d]", events, i), 1}, struct {
|
|
|
+ path string
|
|
|
+ depth int
|
|
|
+ }{fmt.Sprintf("%s[%d].note.note", events, i), 0})
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var paths []string
|
|
|
+ for _, r := range roots {
|
|
|
+ n, err := d.Get(r.path)
|
|
|
+ must(t, err)
|
|
|
+ fieldPaths(n, r.path, r.depth, &paths)
|
|
|
+ }
|
|
|
+
|
|
|
+ changes := []func(parent *xpj.Node, key string, i int){
|
|
|
+ func(p *xpj.Node, k string, i int) { replace(p, k, i, xpj.NewNull()) },
|
|
|
+ func(p *xpj.Node, k string, i int) { replace(p, k, i, xpj.NewString("../x.wav")) },
|
|
|
+ func(p *xpj.Node, k string, i int) { replace(p, k, i, xpj.NewInt(-1)) },
|
|
|
+ func(p *xpj.Node, k string, i int) { replace(p, k, i, xpj.NewInt(1000)) },
|
|
|
+ func(p *xpj.Node, k string, i int) { replace(p, k, i, xpj.NewArray()) },
|
|
|
+ func(p *xpj.Node, k string, i int) { replace(p, k, i, xpj.NewObject()) },
|
|
|
+ func(p *xpj.Node, k string, i int) {
|
|
|
+ if k != "" {
|
|
|
+ p.DeleteKey(k)
|
|
|
+ } else {
|
|
|
+ _ = p.Remove(i)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ }
|
|
|
+ names := []string{"null", "text", "-1", "1000", "list", "object", "deleted"}
|
|
|
+ for _, path := range paths {
|
|
|
+ parentPath, key, i := splitPath(path)
|
|
|
+ for c, change := range changes {
|
|
|
+ f := base.Clone()
|
|
|
+ fd, _ := f.Data()
|
|
|
+ parent, err := fd.Get(parentPath)
|
|
|
+ if err != nil {
|
|
|
+ continue // a key holding '.' or '[' cannot be addressed by path
|
|
|
+ }
|
|
|
+ change(parent, key, i)
|
|
|
+ func() {
|
|
|
+ defer func() {
|
|
|
+ if r := recover(); r != nil {
|
|
|
+ t.Errorf("%s %s: panic: %v", path, names[c], r)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ p, err := wrap(f, "Broken", "")
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ exercise(p)
|
|
|
+ }()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ t.Logf("%d fields, %d cases", len(paths), len(paths)*len(changes))
|
|
|
+}
|
|
|
+
|
|
|
+// fieldPaths lists path and the paths below n to depth levels, taking only
|
|
|
+// the first item of each list.
|
|
|
+func fieldPaths(n *xpj.Node, path string, depth int, out *[]string) {
|
|
|
+ *out = append(*out, path)
|
|
|
+ if depth == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(n.Items) > 0 {
|
|
|
+ fieldPaths(n.Items[0], path+"[0]", depth-1, out)
|
|
|
+ }
|
|
|
+ for _, m := range n.Members {
|
|
|
+ fieldPaths(m.Value, path+"."+m.Key, depth-1, out)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// splitPath splits a path into its parent and last step: a key, or an index
|
|
|
+// when key is "".
|
|
|
+func splitPath(path string) (parent, key string, i int) {
|
|
|
+ if strings.HasSuffix(path, "]") {
|
|
|
+ open := strings.LastIndexByte(path, '[')
|
|
|
+ i, _ = strconv.Atoi(path[open+1 : len(path)-1])
|
|
|
+ return path[:open], "", i
|
|
|
+ }
|
|
|
+ dot := strings.LastIndexByte(path, '.')
|
|
|
+ if dot < 0 {
|
|
|
+ return "", path, 0
|
|
|
+ }
|
|
|
+ return path[:dot], path[dot+1:], 0
|
|
|
+}
|
|
|
+
|
|
|
+func replace(parent *xpj.Node, key string, i int, v *xpj.Node) {
|
|
|
+ if key != "" {
|
|
|
+ _ = parent.SetKey(key, v)
|
|
|
+ } else if i < len(parent.Items) {
|
|
|
+ parent.Items[i] = v
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// exercise runs every read and most edits on p.
|
|
|
+func exercise(p *Project) {
|
|
|
+ for slot := range Pads {
|
|
|
+ _, _ = p.Pad(slot)
|
|
|
+ p.PadHasNotes(slot)
|
|
|
+ }
|
|
|
+ p.PoolFiles()
|
|
|
+ for slot := range 2 {
|
|
|
+ _ = p.SetTune(slot, 1, 1)
|
|
|
+ _ = p.SetMuteGroup(slot, 1)
|
|
|
+ _ = p.CopyPad(slot, slot+20)
|
|
|
+ _ = p.MovePad(slot+20, slot+40)
|
|
|
+ _ = p.SwapPads(slot, slot+1)
|
|
|
+ _ = p.ClearPad(slot + 1)
|
|
|
+ }
|
|
|
+ for _, s := range p.Sequences() {
|
|
|
+ _ = p.SetSequence(s.Slot, "Changed", 100, 4)
|
|
|
+ }
|
|
|
+ _ = p.AddSequence(50)
|
|
|
+ _ = p.RemoveSequence(50)
|
|
|
+ _ = p.RemoveSequence(0)
|
|
|
+}
|