package sfz import ( "flag" "fmt" "os" "regexp" "strconv" "strings" "testing" ) var update = flag.Bool("update", false, "rewrite docs/opcodes.md") // TestLookup checks that Lookup finds an opcode by its name, its aliases and // the name of its family, and doesn't find a name that isn't the format's. func TestLookup(t *testing.T) { for _, tc := range []struct { name, canonical string }{ {"lokey", "lokey"}, {"loopmode", "loop_mode"}, {"loop_mode", "loop_mode"}, {"locc64", "loccN"}, {"amp_velcurve_127", "amp_velcurve_N"}, {"eg02_time3", "egN_timeX"}, {"ampeg_attack_oncc12", "ampeg_attackccN"}, {"v064", "vNNN"}, {"hint_anything_here", "hint_*"}, {"cutoff2_cc5", "cutoff2_ccN"}, {"eg1_time2_oncc3", "egN_timeX_onccY"}, {"lfo01_freq_lfo2_oncc4", "lfoN_freq_lfoX_onccY"}, } { info, ok := Lookup(tc.name) if !ok || info.Name != tc.canonical { t.Errorf("Lookup(%q) = %q, %v; want %q", tc.name, info.Name, ok, tc.canonical) } } for _, name := range []string{"", "zampler_gain", "lokey2", "loccN7x", "lokey "} { if info, ok := Lookup(name); ok { t.Errorf("Lookup(%q) = %q", name, info.Name) } } info, _ := Lookup("lokey") if info.Type != TypeInteger || !info.Note || *info.Min != 0 || *info.Max != 127 { t.Errorf("lokey %+v", info) } if h, ok := LookupHeader("REGION"); !ok || h.Name != "region" { t.Errorf("header %+v", h) } } // TestRegistryComplete checks the registry holds what the spec lists, each // name once, and that every name reads back as the opcode it belongs to. func TestRegistryComplete(t *testing.T) { count := 0 for info := range KnownOpcodes() { count++ for _, name := range append([]string{info.Name}, info.Aliases...) { if strings.ContainsAny(name, "*") || placeholders.MatchString(name) { continue } got, ok := Lookup(name) if !ok || got.Name != info.Name { t.Errorf("%s is %q", name, got.Name) } } } if count < 700 { t.Errorf("%d opcodes", count) } for _, h := range []string{"region", "group", "master", "global", "control", "curve", "effect", "midi", "sample"} { if _, ok := LookupHeader(h); !ok { t.Errorf("no header %s", h) } } } // TestGetAs checks the types that GetAs reads an opcode as, and that it reads // note names and finds aliases. func TestGetAs(t *testing.T) { ops := Opcodes{"lokey": "c4", "hikey": " 72 ", "loopmode": "one_shot", "volume": "-3.5", "offset": "12", "bad": "x"} if n, ok := GetAs[int](ops, "lokey"); !ok || n != 60 { t.Errorf("lokey %d %v", n, ok) } if n, ok := GetAs[int64](ops, "hikey"); !ok || n != 72 { t.Errorf("hikey %d %v", n, ok) } if s, ok := GetAs[string](ops, "loop_mode"); !ok || s != "one_shot" { t.Errorf("loop_mode %q %v", s, ok) } if f, ok := GetAs[float64](ops, "volume"); !ok || f != -3.5 { t.Errorf("volume %v %v", f, ok) } if n, ok := GetAs[int](ops, "offset"); !ok || n != 12 { t.Errorf("offset %d %v", n, ok) } if _, ok := GetAs[int](ops, "bad"); ok { t.Error("bad is an int") } if _, ok := GetAs[int](ops, "missing"); ok { t.Error("missing is set") } } // TestValidateValues checks the problems that Validate reports for the values // of opcodes, and that it leaves alone what it can't check. func TestValidateValues(t *testing.T) { text := " sample=a.wav lokey=c4 hikey=60 lovel=100 hivel=20 volume=loud pan=300 loop_mode=sideways\n" + " zampler_gain=3 locc7=1 pitch_keycenter=sample key=$K \n" + " lokey=10 hikey=9 amp_veltrack=1.5 ampeg_attack=$A\n" doc, err := Parse(strings.NewReader(text)) if err != nil { t.Fatal(err) } var got []string for _, p := range doc.Validate() { got = append(got, fmt.Sprintf("%d:%s", p.Line, p.Code)) } want := []string{ "1:value.type", // volume=loud "1:value.range", // pan=300 "1:value.option", // loop_mode=sideways "1:range.inverted", // lovel above hivel "2:opcode.unknown", // zampler_gain "2:define.undefined", "2:header.unknown", // "3:range.inverted", // lokey above hikey "3:define.undefined", // $A } for _, w := range want { found := false for _, g := range got { if g == w { found = true } } if !found { t.Errorf("no %s in %v", w, got) } } if len(got) != len(want) { t.Errorf("problems %v", got) } } // TestOpcodeDoc checks docs/opcodes.md is the table the registry makes. Run // the tests with -update to rewrite it. func TestOpcodeDoc(t *testing.T) { var b strings.Builder b.WriteString("# Opcodes\n\n" + "Every opcode the library knows, from [sfzformat.com](https://sfzformat.com/)'s data. " + "**Typed** opcodes are read into the [`Instrument`](../instrument.go) model; the rest are known, so the " + "library validates them, and reads and writes them as text. This file is generated by `go test -update`.\n\n" + "| Opcode | Version | Value | Typed |\n|---|---|---|---|\n") for info := range KnownOpcodes() { value := info.Type.String() if info.Unit != "" { value += " (" + info.Unit + ")" } names := "`" + info.Name + "`" for _, a := range info.Aliases { names += ", `" + a + "`" } typed := "" if isTyped(info) { typed = "yes" } deprecated := "" if info.Deprecated { deprecated = ", deprecated" } fmt.Fprintf(&b, "| %s | %s%s | %s | %s |\n", names, info.Version, deprecated, value, typed) } if *update { if err := os.WriteFile("docs/opcodes.md", []byte(b.String()), 0o644); err != nil { t.Fatal(err) } return } got, err := os.ReadFile("docs/opcodes.md") if err != nil { t.Fatal(err) } if string(got) != b.String() { t.Error("docs/opcodes.md is out of date; run go test -update") } } // placeholders finds the placeholders in a family's name. var placeholders = patternRE // isTyped reports whether the typed model reads any name of an opcode. func isTyped(info OpcodeInfo) bool { for _, name := range append([]string{info.Name}, info.Aliases...) { if strings.Contains(name, "*") { continue } if handled(placeholders.ReplaceAllString(name, "1")) { return true } } return false } // TestTypedOpcodesKnown checks that the registry knows every opcode the typed // model reads by name. func TestTypedOpcodesKnown(t *testing.T) { for name := range handledNames { if _, ok := Lookup(name); !ok { t.Errorf("%s isn't in the registry", name) } } } // TestFamiliesMatchTheirNames checks that a name made from each family's, with // numbers where its placeholders are, is found as that family, or as another // that is as specific. func TestFamiliesMatchTheirNames(t *testing.T) { placeholderRE := regexp.MustCompile(`[NXY]+`) for info := range KnownOpcodes() { for _, name := range append([]string{info.Name}, info.Aliases...) { if !strings.ContainsAny(name, "NXY*") { continue } digit := 0 made := placeholderRE.ReplaceAllStringFunc(name, func(string) string { digit++; return strconv.Itoa(digit + 10) }) made = strings.ReplaceAll(made, "*", "anything") got, ok := Lookup(made) if !ok { t.Errorf("%s: Lookup(%q) found nothing", name, made) } else if got.Name != info.Name && !strings.ContainsAny(got.Name, "NXY*") { t.Errorf("%s: Lookup(%q) found %q", name, made, got.Name) } } } }