Check edited values, keep comments in place and make edits cheap
- Header.Set, Document.SetDefine and Document.AddHeader return an error for a value or name that can't be written as one opcode, define or header, and Build returns the same error for what it can't write. - Set puts a new opcode after the last one's trailing comment, and never where text on its line would become part of its value. Delete keeps the line breaks it doesn't own. - Node.Line and Node.Col are replaced by File.Position, which works lines and columns out when asked, and a node remembers where it was in its file, so an edit no longer renumbers the file or searches it from the start. - Add FuzzEdit checks that an edited file reads back as its nodes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+44
-6
@@ -207,24 +207,59 @@ func FuzzWrite(f *testing.F) {
|
||||
})
|
||||
}
|
||||
|
||||
// FuzzEdit checks that editing any document doesn't panic, and keeps the
|
||||
// layout it didn't change.
|
||||
// significant returns the kind and text of the nodes of a file that aren't
|
||||
// whitespace.
|
||||
func significant(nodes []*Node) []string {
|
||||
var out []string
|
||||
for _, n := range nodes {
|
||||
if n.Kind != KindSpace {
|
||||
out = append(out, n.Kind.String()+":"+n.Raw)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// FuzzEdit checks that editing any document doesn't panic, that what is
|
||||
// written reads back as the nodes that were edited, and that an edit that
|
||||
// is refused changes nothing.
|
||||
func FuzzEdit(f *testing.F) {
|
||||
for _, text := range roundTripTexts {
|
||||
f.Add(text, "lokey", "60")
|
||||
}
|
||||
f.Add("<region> sample=a.wav // c\n", "hikey", "3")
|
||||
f.Add("<region> sample=a.wav", "x", "1\n<region>")
|
||||
f.Fuzz(func(t *testing.T, text, name, value string) {
|
||||
doc, err := Parse(strings.NewReader(text))
|
||||
if err != nil {
|
||||
t.Skip()
|
||||
}
|
||||
check := func(what string) {
|
||||
for _, file := range doc.Files() {
|
||||
if got, want := significant(lex(file.String())), significant(file.Nodes); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("%s: wrote %q, which reads as %q, not %q", what, file.String(), got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, h := range doc.Headers() {
|
||||
h.Set(name, value)
|
||||
before := doc.String()
|
||||
if err := h.Set(name, value); err != nil {
|
||||
if doc.String() != before {
|
||||
t.Fatalf("refused Set changed the text to %q", doc.String())
|
||||
}
|
||||
continue
|
||||
}
|
||||
check("Set")
|
||||
h.Get(name)
|
||||
h.Delete(name)
|
||||
check("Delete")
|
||||
}
|
||||
if h, err := doc.AddHeader("region", nil); err == nil {
|
||||
h.Set(name, value)
|
||||
check("AddHeader")
|
||||
}
|
||||
if err := doc.SetDefine("$X", value); err == nil {
|
||||
check("SetDefine")
|
||||
}
|
||||
doc.AddHeader("region", nil).Set(name, value)
|
||||
doc.SetDefine("$X", value)
|
||||
doc.Flatten()
|
||||
doc.Validate()
|
||||
for _, f := range doc.Files() {
|
||||
@@ -253,7 +288,10 @@ func FuzzBuild(f *testing.F) {
|
||||
for _, opts := range []BuildOptions{{}, {Variables: true, MinRepeat: 1}, {SplitFiles: true, RegionsPerFile: 1}} {
|
||||
built, err := Build(inst, opts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
if !errors.Is(err, ErrInvalid) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
built.Validate()
|
||||
built.Flatten().Instrument()
|
||||
|
||||
Reference in New Issue
Block a user