package sfz import ( "errors" "strings" "testing" ) // write writes a document with the options, and fails the test if it can't. 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 sample=$S.wav\n", } const writeMain = "#define $S kick\n#include \"x.sfz\"\n#define $S snare\n sample=$S.wav\n" // TestWriteZeroValue checks that the zero value of WriteOptions writes each // file as it was read, and that Write refuses to lose the files a document // includes. 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 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 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, "") != 2 || strings.Count(got, "lokey=5") != 1 { t.Errorf("inlined %q", got) } } // TestExpandVariablesKeepsComments checks that ExpandVariables leaves the // text of comments as it is. func TestExpandVariablesKeepsComments(t *testing.T) { doc := mustParse(t, "#define $A 1\n// uses $A\n/* $A */ sample=$A.wav // $A\n", nil) want := "// uses $A\n/* $A */ sample=1.wav // $A\n" if got := write(t, doc, WriteOptions{ExpandVariables: true}); got != want { t.Errorf("got %q, want %q", got, want) } } // TestInlineIncludes checks that InlineIncludes writes one file that reads as // the document did. func TestInlineIncludes(t *testing.T) { doc := mustParse(t, writeMain, writeFiles) got := write(t, doc, WriteOptions{InlineIncludes: true}) want := "#define $S kick\n// kit\n sample=$S.wav\n#define $S snare\n 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) } } // TestExpandVariables checks that ExpandVariables writes each variable's value // as of where it is used, and returns an error where the files can't be written // with them expanded. func TestExpandVariables(t *testing.T) { doc := mustParse(t, writeMain, writeFiles) got := write(t, doc, WriteOptions{InlineIncludes: true, ExpandVariables: true}) want := "// kit\n sample=kick.wav\n 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 sample=snare.wav\n" || out["x.sfz"] != "// kit\n 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") } } // TestLineEndings checks that LineEnding sets the line breaks that are // written, and that the zero value keeps them. func TestLineEndings(t *testing.T) { doc := mustParse(t, "\r\nsample=a.wav\n", nil) if got := write(t, doc, WriteOptions{LineEnding: LF}); got != "\nsample=a.wav\n" { t.Errorf("LF %q", got) } if got := write(t, doc, WriteOptions{LineEnding: CRLF}); got != "\r\nsample=a.wav\r\n" { t.Errorf("CRLF %q", got) } if got := write(t, doc, WriteOptions{}); got != "\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": " b=2\n"} doc := mustParse(t, "c=0 #include \"x\"#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 }