Refuse to write a document's includes into one file by accident
- 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>
This commit is contained in:
+83
-3
@@ -1,6 +1,7 @@
|
||||
package sfz
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -22,15 +23,70 @@ const writeMain = "#define $S kick\n#include \"x.sfz\"\n#define $S snare\n<regio
|
||||
|
||||
func TestWriteZeroValue(t *testing.T) {
|
||||
doc := mustParse(t, writeMain, writeFiles)
|
||||
if got := write(t, doc, WriteOptions{}); got != writeMain {
|
||||
// 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 \n#define $S snare\n<region> sample=$S.wav\n"
|
||||
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)
|
||||
}
|
||||
@@ -44,7 +100,7 @@ func TestInlineIncludes(t *testing.T) {
|
||||
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 \n<region> sample=snare.wav\n"
|
||||
want := "// kit\n<region> sample=kick.wav\n<region> sample=snare.wav\n"
|
||||
if got != want {
|
||||
t.Errorf("got %q want %q", got, want)
|
||||
}
|
||||
@@ -83,3 +139,27 @@ func TestLineEndings(t *testing.T) {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user