Tests for weight 0, a never-numeric calc operand, the repeat product along a path, and ErrNoData

This commit is contained in:
2026-09-02 18:22:23 +02:00
parent c5c06375d8
commit 34c933b2b3
5 changed files with 62 additions and 12 deletions
+31 -4
View File
@@ -72,12 +72,16 @@ func TestCalcFields(t *testing.T) {
} }
} }
// TestCalcNonNumericIsNaN pins the never-fail rule: a field that doesn't render // TestCalcNonNumericIsNaN pins the never-fail rule: a field that sometimes does
// to a number becomes NaN, which propagates and prints visibly. // not render to a number becomes NaN then, which propagates and prints visibly.
func TestCalcNonNumericIsNaN(t *testing.T) { func TestCalcNonNumericIsNaN(t *testing.T) {
if got := mustRender(t, engine(1), `{"format":"{calc(x * 2)}","x":"abc"}`); got != "NaN" { f := engine(1)
t.Fatalf("calc over non-numeric field = %q, want NaN", got) for i := 0; i < 50; i++ {
if got := mustRender(t, f, `{"format":"{calc(x * 2)}","x":["abc","1"]}`); got == "NaN" {
return
}
} }
t.Fatal("calc over a sometimes non-numeric field never printed NaN in 50 draws")
} }
// TestCalcReproducible pins that a calc over a random operand stays seed-stable. // TestCalcReproducible pins that a calc over a random operand stays seed-stable.
@@ -218,3 +222,26 @@ func TestCalcHoldIsPerTemplate(t *testing.T) {
t.Fatal("the nested template never differed from its parent, want its own draw") t.Fatal("the nested template never differed from its parent, want its own draw")
} }
} }
func TestCalcOverANeverNumericOperandIsRejected(t *testing.T) {
for src, want := range map[string]string{
`{"format":"{calc(x * 2)}","x":"abc"}`: `"abc"`,
`{"format":"{calc(x * 2)}","x":["a","b"]}`: `"x"`,
`{"format":"{calc(x + y)}","x":"1","y":"{{2}}"}`: `"{2}"`,
} {
_, err := compile(parse(t, src))
if err == nil || !strings.Contains(err.Error(), want) || !strings.Contains(err.Error(), "never a number") {
t.Errorf("compile(%s) = %v, want the operand rejected naming %s", src, err, want)
}
}
for _, ok := range []string{
`{"format":"{calc(x * 2)}","x":"3"}`,
`{"format":"{calc(x * 2)}","x":" 2.5 "}`,
`{"format":"{calc(x * 2)}","x":["1","abc"]}`,
`{"format":"{calc(x * 2)}","x":"{digits(2)}"}`,
} {
if _, err := compile(parse(t, ok)); err != nil {
t.Errorf("compile(%s) = %v, want it accepted", ok, err)
}
}
}
+4
View File
@@ -311,4 +311,8 @@ func TestRunNoShippedData(t *testing.T) {
if code != 0 || strings.TrimSpace(out) == "" { if code != 0 || strings.TrimSpace(out) == "" {
t.Errorf("run = %d, out=%q", code, out) t.Errorf("run = %d, out=%q", code, out)
} }
code, _, errb = runOut("--no-shipped-data", "sv_SE.person")
if code != 2 || !strings.Contains(errb, "--no-shipped-data needs at least one --data-path") {
t.Errorf("--no-shipped-data alone = %d, %q, want misuse naming --data-path", code, errb)
}
} }
+20
View File
@@ -181,3 +181,23 @@ func TestHiddenEntriesAreSkipped(t *testing.T) {
} }
} }
} }
func TestRepeatProductAlongAPathIsCapped(t *testing.T) {
for name, files := range map[string]map[string]string{
"nested": {"cat": `{"format":"{a}","repeat":2048,"a":{"format":"{b}","repeat":2048,"b":"x"}}`},
"through a reference": {
"a": `{"format":"{..b}","repeat":2048}`,
"b": `{"format":"x","repeat":2048}`,
},
} {
_, err := New(WithoutShippedData(), WithDataPath(writeData(t, files)))
if err == nil || !strings.Contains(err.Error(), "repeat") || !strings.Contains(err.Error(), "1048576") {
t.Errorf("%s: New = %v, want the repeat product rejected naming the maximum", name, err)
}
}
if _, err := New(WithoutShippedData(), WithDataPath(writeData(t, map[string]string{
"cat": `{"format":"{a}{c}","repeat":1024,"a":{"format":"{b}","repeat":1024,"b":"x"},"c":{"format":"y","repeat":1024}}`,
}))); err != nil {
t.Errorf("New = %v, want 1024 x 1024 along one path accepted", err)
}
}
+3 -2
View File
@@ -1,6 +1,7 @@
package fejkdata package fejkdata
import ( import (
"errors"
"reflect" "reflect"
"slices" "slices"
"strings" "strings"
@@ -55,8 +56,8 @@ func TestUserDataMayReferenceShipped(t *testing.T) {
func TestWithoutShippedDataNeedsASource(t *testing.T) { func TestWithoutShippedDataNeedsASource(t *testing.T) {
_, err := New(WithoutShippedData()) _, err := New(WithoutShippedData())
if err == nil || !strings.Contains(err.Error(), "WithDataPath") { if err == nil || !strings.Contains(err.Error(), "WithDataPath") || !errors.Is(err, ErrNoData) {
t.Fatalf("New(WithoutShippedData()) = %v, want an error naming WithDataPath", err) t.Fatalf("New(WithoutShippedData()) = %v, want ErrNoData naming WithDataPath", err)
} }
} }
+4 -6
View File
@@ -121,12 +121,10 @@ func TestAlternationPicksOneField(t *testing.T) {
} }
} }
func TestWeightZeroNeverChosen(t *testing.T) { func TestWeightZeroIsRejected(t *testing.T) {
f := engine(5) _, err := compile(parse(t, `[{"format":"X","weight":0},"Y"]`))
for i := 0; i < 200; i++ { if err == nil || !strings.Contains(err.Error(), "weight 0") || !strings.Contains(err.Error(), "remove") {
if got := mustRender(t, f, `[{"format":"X","weight":0},"Y"]`); got != "Y" { t.Fatalf("compile(weight 0) = %v, want it rejected naming the fix", err)
t.Fatalf("weight-0 variant chosen: %q", got)
}
} }
} }