diff --git a/calc_test.go b/calc_test.go index 2efdf89..77adf77 100644 --- a/calc_test.go +++ b/calc_test.go @@ -247,11 +247,21 @@ func TestCalcOverANeverNumericOperandIsRejected(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 "}`} { - if _, err := compile(parse(t, bad)); err == nil || !strings.Contains(err.Error(), "zero") { - t.Errorf("compile(%s) = %v, want the constant zero divisor rejected", bad, err) + for src, want := range map[string]string{ + `"{calc(1/0)}"`: "divides by 0", + `"{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) for i := 0; i < 50; i++ { if got := mustRender(t, f, `{"format":"{calc(x/y)}","x":"1","y":["0","1"]}`); got == "+Inf" { diff --git a/cmd/fejkdata/main_test.go b/cmd/fejkdata/main_test.go index cb08507..acc5a23 100644 --- a/cmd/fejkdata/main_test.go +++ b/cmd/fejkdata/main_test.go @@ -362,3 +362,11 @@ func TestRunEmptyDataPathIsNamed(t *testing.T) { 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) + } +} diff --git a/template_test.go b/template_test.go index d5aa2ad..74dbc30 100644 --- a/template_test.go +++ b/template_test.go @@ -365,3 +365,17 @@ func TestAlternationThreeWay(t *testing.T) { 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) + } + } +}