GNU-style CLI flags, embedded data, and a literal format grammar #3

Merged
lilleman merged 38 commits from cli-and-syntax into main 2026-09-03 14:21:08 +02:00
2 changed files with 28 additions and 4 deletions
Showing only changes of commit 5514b42b7b - Show all commits
+24
View File
@@ -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)
}
}
}
+4 -4
View File
@@ -140,16 +140,16 @@ func TestCalcOperandReadsTheExpansionsDraw(t *testing.T) {
} }
// TestCalcOperandSharesOneDraw pins the reach of that hold: the draw belongs to the // 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) { func TestCalcOperandSharesOneDraw(t *testing.T) {
dir := writeData(t, map[string]string{ 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)) f := newGenerator(t, dir, WithSeed(5))
for i := 0; i < 200; i++ { for i := 0; i < 200; i++ {
got := fake(t, f, "same") got := fake(t, f, "same")
if p := strings.Fields(got); len(p) != 3 || p[0] != p[1] || p[0] != p[2] { if p := strings.Fields(got); len(p) != 2 || p[0] != p[1] {
t.Fatalf("same = %q, want one value three times", got) t.Fatalf("same = %q, want one value twice", got)
} }
} }
} }