134 lines
4.2 KiB
Go
134 lines
4.2 KiB
Go
package sfz
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"reflect"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
// modelTypes are the types of the typed model.
|
||
|
|
var modelTypes = []reflect.Type{
|
||
|
|
reflect.TypeFor[Instrument](), reflect.TypeFor[Region](), reflect.TypeFor[Envelope](), reflect.TypeFor[CurvePoint](),
|
||
|
|
reflect.TypeFor[Crossfade](), reflect.TypeFor[Condition](), reflect.TypeFor[Keyswitch](), reflect.TypeFor[CurveStop](),
|
||
|
|
reflect.TypeFor[Modulation](), reflect.TypeFor[Control](), reflect.TypeFor[KeyswitchRange](),
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestModelTags checks that each field of the typed model has a unique json
|
||
|
|
// name and a description, and that its bounds are numbers, in order, on a
|
||
|
|
// field that is a number.
|
||
|
|
func TestModelTags(t *testing.T) {
|
||
|
|
for _, typ := range modelTypes {
|
||
|
|
names := map[string]bool{}
|
||
|
|
for i := 0; i < typ.NumField(); i++ {
|
||
|
|
f := typ.Field(i)
|
||
|
|
tag := f.Tag
|
||
|
|
name, _, _ := strings.Cut(tag.Get("json"), ",")
|
||
|
|
if name == "" || name == "-" || names[name] || name != strings.ToLower(name[:1])+name[1:] {
|
||
|
|
t.Errorf("%s.%s: json name %q", typ.Name(), f.Name, name)
|
||
|
|
}
|
||
|
|
names[name] = true
|
||
|
|
if doc := tag.Get("doc"); !strings.HasSuffix(doc, ".") || len(doc) < 10 {
|
||
|
|
t.Errorf("%s.%s: doc %q", typ.Name(), f.Name, doc)
|
||
|
|
}
|
||
|
|
var bounds []float64
|
||
|
|
for _, key := range []string{"minimum", "maximum"} {
|
||
|
|
v, ok := tag.Lookup(key)
|
||
|
|
if !ok {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
n, err := strconv.ParseFloat(v, 64)
|
||
|
|
if err != nil {
|
||
|
|
t.Errorf("%s.%s: %s %q", typ.Name(), f.Name, key, v)
|
||
|
|
}
|
||
|
|
bounds = append(bounds, n)
|
||
|
|
kind := f.Type.Kind()
|
||
|
|
if kind == reflect.Pointer {
|
||
|
|
kind = f.Type.Elem().Kind()
|
||
|
|
}
|
||
|
|
if kind != reflect.Int && kind != reflect.Int64 && kind != reflect.Float64 {
|
||
|
|
t.Errorf("%s.%s: %s on a %s", typ.Name(), f.Name, key, f.Type)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if len(bounds) == 2 && bounds[0] > bounds[1] {
|
||
|
|
t.Errorf("%s.%s: bounds %v", typ.Name(), f.Name, bounds)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestModelJSON checks that an instrument is the same after it goes through
|
||
|
|
// JSON, so that no field is lost to its tags.
|
||
|
|
func TestModelJSON(t *testing.T) {
|
||
|
|
for name, inst := range instruments(t) {
|
||
|
|
data, err := json.Marshal(inst)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
var back Instrument
|
||
|
|
if err := json.Unmarshal(data, &back); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
// An empty list and none are the same in JSON, so the comparison is of the
|
||
|
|
// text.
|
||
|
|
again, err := json.Marshal(&back)
|
||
|
|
if err != nil || string(again) != string(data) {
|
||
|
|
t.Errorf("%s: wrote %s, then %s (%v)", name, data[:min(len(data), 300)], again[:min(len(again), 300)], err)
|
||
|
|
}
|
||
|
|
if len(back.Regions) != len(inst.Regions) || back.Regions[0].Sample != inst.Regions[0].Sample {
|
||
|
|
t.Errorf("%s: regions changed", name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestModelBounds checks that regions read from text stay within the bounds
|
||
|
|
// their fields name.
|
||
|
|
func TestModelBounds(t *testing.T) {
|
||
|
|
inst := parseText(t, "<region> sample=a.wav lokey=-5 hikey=300 pitch_keycenter=500 lovel=-3 hivel=999 pan=500 amp_veltrack=900 ampeg_sustain=400 ampeg_attack=-1 lorand=-1 hirand=7 seq_length=-4 seq_position=0 polyphony=0 off_time=-1")
|
||
|
|
if len(inst.Regions) != 1 {
|
||
|
|
t.Fatalf("%d regions", len(inst.Regions))
|
||
|
|
}
|
||
|
|
var walk func(v reflect.Value, path string)
|
||
|
|
walk = func(v reflect.Value, path string) {
|
||
|
|
switch v.Kind() {
|
||
|
|
case reflect.Pointer:
|
||
|
|
if !v.IsNil() {
|
||
|
|
walk(v.Elem(), path)
|
||
|
|
}
|
||
|
|
case reflect.Slice:
|
||
|
|
for i := 0; i < v.Len(); i++ {
|
||
|
|
walk(v.Index(i), path)
|
||
|
|
}
|
||
|
|
case reflect.Struct:
|
||
|
|
for i := 0; i < v.NumField(); i++ {
|
||
|
|
f := v.Type().Field(i)
|
||
|
|
fv := v.Field(i)
|
||
|
|
switch fv.Kind() {
|
||
|
|
case reflect.Int, reflect.Int64:
|
||
|
|
checkBounds(t, path+"."+f.Name, f.Tag, float64(fv.Int()))
|
||
|
|
case reflect.Float64:
|
||
|
|
checkBounds(t, path+"."+f.Name, f.Tag, fv.Float())
|
||
|
|
default:
|
||
|
|
walk(fv, path+"."+f.Name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
walk(reflect.ValueOf(inst.Regions[0]), "Region")
|
||
|
|
}
|
||
|
|
|
||
|
|
func checkBounds(t *testing.T, name string, tag reflect.StructTag, v float64) {
|
||
|
|
t.Helper()
|
||
|
|
if s, ok := tag.Lookup("minimum"); ok {
|
||
|
|
if lo, _ := strconv.ParseFloat(s, 64); v < lo {
|
||
|
|
t.Errorf("%s is %v, below %v", name, v, lo)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if s, ok := tag.Lookup("maximum"); ok {
|
||
|
|
if hi, _ := strconv.ParseFloat(s, 64); v > hi {
|
||
|
|
t.Errorf("%s is %v, above %v", name, v, hi)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|