Tests for every branch of the constant-divisor fold, arg errors naming the spelling, and shell numbers at the CLI
Tests / vet + fmt + tests (pull_request) Failing after 43s

This commit is contained in:
2026-09-02 19:24:46 +02:00
parent c5304bb6e7
commit a9552ed966
3 changed files with 35 additions and 3 deletions
+13 -3
View File
@@ -247,11 +247,21 @@ func TestCalcOverANeverNumericOperandIsRejected(t *testing.T) {
} }
func TestCalcConstantZeroDivisorIsRejected(t *testing.T) { func TestCalcConstantZeroDivisorIsRejected(t *testing.T) {
for _, bad := range []string{`"{calc(1/0)}"`, `"{calc(2/(1-1))}"`, `{"format":"{calc(x/y)}","x":"1","y":"0"}`, `{"format":"{calc(x/(y*2))}","x":"1","y":" 0 "}`} { for src, want := range map[string]string{
if _, err := compile(parse(t, bad)); err == nil || !strings.Contains(err.Error(), "zero") { `"{calc(1/0)}"`: "divides by 0",
t.Errorf("compile(%s) = %v, want the constant zero divisor rejected", bad, err) `"{calc(2/(1-1))}"`: "divides by (1 - 1)",
`{"format":"{calc(x/y)}","x":"1","y":"0"}`: "divides by y",
`{"format":"{calc(x/(y*2))}","x":"1","y":" 0 "}`: "divides by (y * 2)",
`{"format":"{calc((a/0)+b)}","a":"1","b":"2"}`: "divides by 0",
`{"format":"{calc(a/-0)}","a":"1"}`: "divides by -0",
} {
if _, err := compile(parse(t, src)); err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("compile(%s) = %v, want the constant zero divisor rejected naming %q", src, err, want)
} }
} }
if _, err := compile(parse(t, `{"format":"{calc(a/(b*c))}","a":"1","b":"0","c":["1","2"]}`)); err != nil {
t.Errorf("compile(a/(b*c)) = %v, want a divisor that varies accepted", err)
}
f := engine(1) f := engine(1)
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
if got := mustRender(t, f, `{"format":"{calc(x/y)}","x":"1","y":["0","1"]}`); got == "+Inf" { if got := mustRender(t, f, `{"format":"{calc(x/y)}","x":"1","y":["0","1"]}`); got == "+Inf" {
+8
View File
@@ -362,3 +362,11 @@ func TestRunEmptyDataPathIsNamed(t *testing.T) {
t.Errorf("run(--data-path=) = %d, %q, want the empty path named", code, errb) t.Errorf("run(--data-path=) = %d, %q, want the empty path named", code, errb)
} }
} }
func TestRunNumbersFollowTheShell(t *testing.T) {
_, want, _ := runOut("--seed", "7", "--repeat", "3", "sv_SE.word")
code, got, errb := runOut("--seed", "007", "--repeat", "+3", "sv_SE.word")
if code != 0 || got != want {
t.Errorf("run(--seed 007 --repeat +3) = %d, %q, stderr %q; want the same as --seed 7 --repeat 3 %q", code, got, errb, want)
}
}
+14
View File
@@ -365,3 +365,17 @@ func TestAlternationThreeWay(t *testing.T) {
t.Fatalf("3-way alternation produced %v, want A, B and C", seen) t.Fatalf("3-way alternation produced %v, want A, B and C", seen)
} }
} }
func TestArgErrorsNameTheSpelling(t *testing.T) {
for src, want := range map[string]string{
`"{float(1,2,02)}"`: "write 2",
`{"format":"{calc(a,02)}","a":"1"}`: "write 2",
`"{digits(+5)}"`: "write 5",
`"{hex(99999999999999999999)}"`: "exceeds the maximum",
`"{int(007,9)}"`: "write 7",
} {
if _, err := compile(parse(t, src)); err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("compile(%s) = %v, want an error saying %q", src, err, want)
}
}
}