Move the builtin argument tests to builtins_test.go and checkFunc's cases to format_test.go

This commit is contained in:
2026-09-25 18:21:32 +02:00
committed by Lillem4n
parent 5e84b26a5f
commit 4b087df57e
2 changed files with 19 additions and 20 deletions
+14 -6
View File
@@ -410,22 +410,16 @@ func TestBuiltinCompileErrors(t *testing.T) {
for _, bad := range []string{ for _, bad := range []string{
`"{digits(0)}"`, // count must be positive `"{digits(0)}"`, // count must be positive
`"{upper(x)}"`, // count must be an integer `"{upper(x)}"`, // count must be an integer
`"{lower()}"`, // wrong arity
`"{nope()}"`, // unknown function
`"{luhn(x)}"`, // function given args it takes none of
`"{int(1)}"`, // wrong arity
`"{int(a,b)}"`, // non-integer args `"{int(a,b)}"`, // non-integer args
`"{int(5,1)}"`, // min > max `"{int(5,1)}"`, // min > max
`"{hex(0)}"`, // count must be positive `"{hex(0)}"`, // count must be positive
`"{nanoid(-1)}"`, // negative count `"{nanoid(-1)}"`, // negative count
`"{base64(0)}"`, // count must be positive `"{base64(0)}"`, // count must be positive
`"{float(1,2)}"`, // wrong arity
`"{float(1,2,-1)}"`, // negative decimals `"{float(1,2,-1)}"`, // negative decimals
`"{float(NaN,NaN,2)}"`, // bounds must be finite `"{float(NaN,NaN,2)}"`, // bounds must be finite
`"{float(Inf,Inf,2)}"`, // same-sign infinities `"{float(Inf,Inf,2)}"`, // same-sign infinities
`"{float(1,NaN,2)}"`, // one NaN bound `"{float(1,NaN,2)}"`, // one NaN bound
`"{float(-Inf,1,2)}"`, // one infinite bound `"{float(-Inf,1,2)}"`, // one infinite bound
`"{digits(+5)}"`, // a count is a plain integer
`"{digits(05)}"`, // no leading zero `"{digits(05)}"`, // no leading zero
`"{int(+1,5)}"`, // a bound is a plain integer `"{int(+1,5)}"`, // a bound is a plain integer
`"{int(5,5)}"`, // a constant is written as text `"{int(5,5)}"`, // a constant is written as text
@@ -438,3 +432,17 @@ func TestBuiltinCompileErrors(t *testing.T) {
} }
} }
} }
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)
}
}
}
+5 -14
View File
@@ -120,6 +120,11 @@ func TestFormatCompileErrors(t *testing.T) {
`"{}"`, // empty token name `"{}"`, // empty token name
`{"format":"{a|}","a":"Q"}`, // empty alternation segment `{"format":"{a|}","a":"Q"}`, // empty alternation segment
`"{luhn(}"`, // malformed function token `"{luhn(}"`, // malformed function token
`"{lower()}"`, // wrong arity
`"{nope()}"`, // unknown function
`"{luhn(x)}"`, // function given args it takes none of
`"{int(1)}"`, // wrong arity
`"{float(1,2)}"`, // wrong arity
} { } {
if _, err := compile(parse(t, bad)); err == nil { if _, err := compile(parse(t, bad)); err == nil {
t.Errorf("compile(%s) = nil error, want error", bad) t.Errorf("compile(%s) = nil error, want error", bad)
@@ -127,20 +132,6 @@ func TestFormatCompileErrors(t *testing.T) {
} }
} }
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)
}
}
}
// TestSplitArgsQuotesOutsideSelectors pins the arg grammar: a comma splits outside a // TestSplitArgsQuotesOutsideSelectors pins the arg grammar: a comma splits outside a
// selector and outside a quoted layout, and a quote inside a selector is a name's text. // selector and outside a quoted layout, and a quote inside a selector is a name's text.
func TestSplitArgsQuotesOutsideSelectors(t *testing.T) { func TestSplitArgsQuotesOutsideSelectors(t *testing.T) {