summaryrefslogtreecommitdiff
path: root/internal/config/print_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/config/print_test.go')
-rw-r--r--internal/config/print_test.go24
1 files changed, 23 insertions, 1 deletions
diff --git a/internal/config/print_test.go b/internal/config/print_test.go
index 9b8d479..510fdd9 100644
--- a/internal/config/print_test.go
+++ b/internal/config/print_test.go
@@ -5,6 +5,8 @@ package config
import (
"strings"
"testing"
+
+ "krino/internal/sexp"
)
func parseOne(t *testing.T, body string) *Dir {
@@ -70,7 +72,11 @@ func TestSpliceLeavesTheRestAlone(t *testing.T) {
if len(errs) > 0 {
t.Fatal(errs)
}
- got := string(Splice(src, dir.Rules[0].Pos, dir.Rules[0].End, "(rule \"a\"\n (move \"In\"))"))
+ spliced, err := Splice(src, dir.Rules[0].Pos, dir.Rules[0].End, "(rule \"a\"\n (move \"In\"))")
+ if err != nil {
+ t.Fatal(err)
+ }
+ got := string(spliced)
want := "; keep me\n(path \"/tmp\")\n(rule \"a\"\n (move \"In\"))\n; and me\n"
if got != want {
t.Errorf("got\n%q\nwant\n%q", got, want)
@@ -79,3 +85,19 @@ func TestSpliceLeavesTheRestAlone(t *testing.T) {
t.Error("Splice changed the source it was given")
}
}
+
+// TestSpliceRefusesOffsetsOutsideTheSource: a stale end offset - the file
+// changed since the form was parsed - is an error, not a panic (plan 13
+// review F6).
+func TestSpliceRefusesOffsetsOutsideTheSource(t *testing.T) {
+ src := []byte("(path \"/tmp\")")
+ for _, c := range []struct{ start, end int }{{0, len(src) + 2}, {8, 4}, {-1, 3}} {
+ if _, err := Splice(src, sexp.Pos{Offset: c.start}, sexp.Pos{Offset: c.end}, "x"); err == nil {
+ t.Errorf("Splice(%d, %d) did not refuse", c.start, c.end)
+ }
+ }
+ out, err := Splice(src, sexp.Pos{Offset: 0}, sexp.Pos{Offset: len(src)}, "(path \"/x\")")
+ if err != nil || string(out) != "(path \"/x\")" {
+ t.Errorf("Splice over the whole source = %q, %v", out, err)
+ }
+}