- Write and WriteTo return an error for a document with includes unless the includes are inlined, instead of dropping the files they hold. Only WriteFiles collects included files and checks them for conflicts. - ExpandVariables leaves comments as they are. - An inlined file no longer leaves a line of its own for the include's line break. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
166 lines
6.1 KiB
Go
166 lines
6.1 KiB
Go
package sfz
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func write(t *testing.T, doc *Document, opts WriteOptions) string {
|
|
t.Helper()
|
|
var b strings.Builder
|
|
if err := doc.Write(&b, opts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
var writeFiles = map[string]string{
|
|
"x.sfz": "// kit\n<region> sample=$S.wav\n",
|
|
}
|
|
|
|
const writeMain = "#define $S kick\n#include \"x.sfz\"\n#define $S snare\n<region> sample=$S.wav\n"
|
|
|
|
func TestWriteZeroValue(t *testing.T) {
|
|
doc := mustParse(t, writeMain, writeFiles)
|
|
// Written to files, each file is as it was read.
|
|
out := map[string]string{}
|
|
err := doc.WriteFiles("m.sfz", func(p string, d []byte) error { out[p] = string(d); return nil }, WriteOptions{})
|
|
if err != nil || out["m.sfz"] != writeMain || out["x.sfz"] != writeFiles["x.sfz"] || len(out) != 2 {
|
|
t.Errorf("err %v, files %q", err, out)
|
|
}
|
|
// As one file, the include would be dropped.
|
|
var b strings.Builder
|
|
if err := doc.Write(&b, WriteOptions{}); !errors.Is(err, ErrInvalid) || b.Len() != 0 {
|
|
t.Errorf("Write: %v %q", err, b.String())
|
|
}
|
|
if n, err := doc.WriteTo(&b); !errors.Is(err, ErrInvalid) || n != 0 || b.Len() != 0 {
|
|
t.Errorf("WriteTo: %v %q", err, b.String())
|
|
}
|
|
// A document without includes is written as it was read.
|
|
plain := mustParse(t, "#define $A 1\n<region> sample=$A.wav\n", nil)
|
|
if got := write(t, plain, WriteOptions{}); got != plain.String() {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
}
|
|
|
|
// TestWriteEditedInclude checks that an edit to an included file isn't lost.
|
|
func TestWriteEditedInclude(t *testing.T) {
|
|
doc := mustParse(t, writeMain, writeFiles)
|
|
set(t, headerIn(t, doc, "x.sfz"), "lokey", "5")
|
|
out := map[string]string{}
|
|
if err := doc.WriteFiles("m.sfz", func(p string, d []byte) error { out[p] = string(d); return nil }, WriteOptions{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, want := out["x.sfz"], "// kit\n<region> sample=$S.wav lokey=5\n"; got != want {
|
|
t.Errorf("x.sfz %q, want %q", got, want)
|
|
}
|
|
if got := write(t, doc, WriteOptions{InlineIncludes: true}); !strings.Contains(got, "lokey=5") {
|
|
t.Errorf("inlined %q", got)
|
|
}
|
|
}
|
|
|
|
// TestIncludedTwiceEdited checks that a file included twice and edited in one
|
|
// place conflicts when written to files, but is fine inlined.
|
|
func TestIncludedTwiceEdited(t *testing.T) {
|
|
doc := mustParse(t, "#include \"x.sfz\"\n#include \"x.sfz\"\n", writeFiles)
|
|
set(t, doc.Headers()[0], "lokey", "5")
|
|
err := doc.WriteFiles("m.sfz", func(string, []byte) error { return nil }, WriteOptions{})
|
|
if !errors.Is(err, ErrInvalid) {
|
|
t.Errorf("WriteFiles: %v", err)
|
|
}
|
|
got := write(t, doc, WriteOptions{InlineIncludes: true})
|
|
if strings.Count(got, "<region>") != 2 || strings.Count(got, "lokey=5") != 1 {
|
|
t.Errorf("inlined %q", got)
|
|
}
|
|
}
|
|
|
|
func TestExpandVariablesKeepsComments(t *testing.T) {
|
|
doc := mustParse(t, "#define $A 1\n// uses $A\n/* $A */<region> sample=$A.wav // $A\n", nil)
|
|
want := "// uses $A\n/* $A */<region> sample=1.wav // $A\n"
|
|
if got := write(t, doc, WriteOptions{ExpandVariables: true}); got != want {
|
|
t.Errorf("got %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestInlineIncludes(t *testing.T) {
|
|
doc := mustParse(t, writeMain, writeFiles)
|
|
got := write(t, doc, WriteOptions{InlineIncludes: true})
|
|
want := "#define $S kick\n// kit\n<region> sample=$S.wav\n#define $S snare\n<region> sample=$S.wav\n"
|
|
if got != want {
|
|
t.Errorf("got %q want %q", got, want)
|
|
}
|
|
// The single file reads as the document did.
|
|
flat := mustParse(t, got, nil).Flatten()
|
|
if len(flat.Regions) != 2 || flat.Regions[0].Opcodes["sample"] != "kick.wav" || flat.Regions[1].Opcodes["sample"] != "snare.wav" {
|
|
t.Errorf("regions %+v", flat.Regions)
|
|
}
|
|
}
|
|
|
|
func TestExpandVariables(t *testing.T) {
|
|
doc := mustParse(t, writeMain, writeFiles)
|
|
got := write(t, doc, WriteOptions{InlineIncludes: true, ExpandVariables: true})
|
|
want := "// kit\n<region> sample=kick.wav\n<region> sample=snare.wav\n"
|
|
if got != want {
|
|
t.Errorf("got %q want %q", got, want)
|
|
}
|
|
|
|
// Without inlining, the included file needs the variable, so only the files
|
|
// written together are expanded.
|
|
if err := doc.Write(new(strings.Builder), WriteOptions{ExpandVariables: true}); err == nil {
|
|
t.Error("expanding variables beside includes didn't fail")
|
|
}
|
|
out := map[string]string{}
|
|
err := doc.WriteFiles("m.sfz", func(p string, d []byte) error { out[p] = string(d); return nil }, WriteOptions{ExpandVariables: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out["m.sfz"] != "#include \"x.sfz\"\n<region> sample=snare.wav\n" || out["x.sfz"] != "// kit\n<region> sample=kick.wav\n" {
|
|
t.Errorf("files %q", out)
|
|
}
|
|
|
|
// A file included twice with different values can't be written once.
|
|
twice := mustParse(t, "#define $S a\n#include \"x.sfz\"\n#define $S b\n#include \"x.sfz\"\n", writeFiles)
|
|
err = twice.WriteFiles("m.sfz", func(string, []byte) error { return nil }, WriteOptions{ExpandVariables: true})
|
|
if err == nil {
|
|
t.Error("a file written two ways didn't fail")
|
|
}
|
|
}
|
|
|
|
func TestLineEndings(t *testing.T) {
|
|
doc := mustParse(t, "<region>\r\nsample=a.wav\n", nil)
|
|
if got := write(t, doc, WriteOptions{LineEnding: LF}); got != "<region>\nsample=a.wav\n" {
|
|
t.Errorf("LF %q", got)
|
|
}
|
|
if got := write(t, doc, WriteOptions{LineEnding: CRLF}); got != "<region>\r\nsample=a.wav\r\n" {
|
|
t.Errorf("CRLF %q", got)
|
|
}
|
|
if got := write(t, doc, WriteOptions{}); got != "<region>\r\nsample=a.wav\n" {
|
|
t.Errorf("kept %q", got)
|
|
}
|
|
}
|
|
|
|
// TestInlineKeepsSeparation checks that an inlined file doesn't run into the
|
|
// text around it.
|
|
func TestInlineKeepsSeparation(t *testing.T) {
|
|
m := map[string]string{"x": "a=1", "y": "<region> b=2\n"}
|
|
doc := mustParse(t, "<group>c=0 #include \"x\"<region>#include \"x\"d=4 #include \"y\"\ne=5\n", m)
|
|
got := write(t, doc, WriteOptions{InlineIncludes: true})
|
|
flat := mustParse(t, got, nil).Flatten()
|
|
if len(flat.Regions) != 2 || flat.Regions[0].Opcodes["a"] != "1" || flat.Regions[0].Opcodes["d"] != "4" || flat.Regions[1].Opcodes["b"] != "2" {
|
|
t.Errorf("%q reads as %+v", got, flat.Regions)
|
|
}
|
|
}
|
|
|
|
// headerIn returns the first header of the file with the path.
|
|
func headerIn(t *testing.T, doc *Document, path string) *Header {
|
|
t.Helper()
|
|
for _, h := range doc.Headers() {
|
|
if h.File.Path == path {
|
|
return h
|
|
}
|
|
}
|
|
t.Fatalf("no header in %s", path)
|
|
return nil
|
|
}
|