diff --git a/bound_test.go b/bound_test.go index a266a34..89f6d2a 100644 --- a/bound_test.go +++ b/bound_test.go @@ -688,3 +688,27 @@ func TestBoundPathIsReachableByFake(t *testing.T) { } } } + +func TestRepeatedBareTokenOfAHeldNameIsRejected(t *testing.T) { + for src, want := range map[string]string{ + `{"format":"{w} {w} {uppercase(w)}","w":["a","b"]}`: "write {w} once", + `{"format":"{w} {w|x} {uppercase(w)}","w":["a","b"],"x":["c","d"]}`: "write {w} once", + `{"format":"{n} + {n} = {calc(n * 2)}","n":["1","2"]}`: "write {n} once", + `{"format":"{p.a} {q} {q} {lowercase(q)}","p":{"format":"{a}","a":"1"},"q":["A","B"]}`: "write {q} once", + } { + _, err := compile(parse(t, src)) + if err == nil || !strings.Contains(err.Error(), want) || !strings.Contains(err.Error(), "holds") { + t.Errorf("compile(%s) = %v, want the repeated token rejected naming %s", src, err, want) + } + } + for _, ok := range []string{ + `{"format":"{w} {w}","w":["a","b"]}`, + `{"format":"{w} {uppercase(w)}","w":["a","b"]}`, + `{"format":"{net} x {qty} = {calc(net * qty, 2)}","net":["19.99","5.00"],"qty":["3","7"]}`, + `{"format":"{uppercase(w)} {uppercase(w)}","w":["a","b"]}`, + } { + if _, err := compile(parse(t, ok)); err != nil { + t.Errorf("compile(%s) = %v, want it accepted", ok, err) + } + } +} diff --git a/calc_test.go b/calc_test.go index ab38c72..f37f783 100644 --- a/calc_test.go +++ b/calc_test.go @@ -140,16 +140,16 @@ func TestCalcOperandReadsTheExpansionsDraw(t *testing.T) { } // TestCalcOperandSharesOneDraw pins the reach of that hold: the draw belongs to the -// expansion, not to the calc, so a bare token rendering the same name reads it too. +// expansion, not to the calc, so the bare token rendering the same name reads it too. func TestCalcOperandSharesOneDraw(t *testing.T) { dir := writeData(t, map[string]string{ - "same": `{"format":"{w} {w} {calc(w)}","w":["1","2","3","4","5"]}`, + "same": `{"format":"{w} {calc(w)}","w":["1","2","3","4","5"]}`, }) f := newGenerator(t, dir, WithSeed(5)) for i := 0; i < 200; i++ { got := fake(t, f, "same") - if p := strings.Fields(got); len(p) != 3 || p[0] != p[1] || p[0] != p[2] { - t.Fatalf("same = %q, want one value three times", got) + if p := strings.Fields(got); len(p) != 2 || p[0] != p[1] { + t.Fatalf("same = %q, want one value twice", got) } } }