Test the CSV header at -n 0, --separator refused with a record format, a date field in time() refused, a bare number template printed as written, and the no-data note

This commit is contained in:
2026-10-08 12:21:13 +02:00
committed by Lillem4n
parent f964816830
commit 332b99ad88
4 changed files with 29 additions and 8 deletions
+1
View File
@@ -276,6 +276,7 @@ func TestBuiltinDateAndTime(t *testing.T) {
func TestBuiltinDateArgs(t *testing.T) {
for tmpl, want := range map[string]string{
`"{date(1990-13-01,1990-12-31,'2006-01-02')}"`: "1990-13-01",
`"{time('2006-01-02 15:04')}"`: "write date(from,to,layout)",
`"{date(1990-12-31,1990-01-01,'2006-01-02')}"`: "is after",
`"{date(1990-01-01,1990-12-31,2006-01-02)}"`: "'2006-01-02'",
`"{date(1990-01-01,1990-12-31,'January 2, 2006)}"`: "'",
+13 -4
View File
@@ -517,7 +517,7 @@ func TestRunNoShippedData(t *testing.T) {
if code, out, errb := runOut("{digits(3)}", "--no-shipped-data"); code != 0 || len(out) != 3 {
t.Errorf("--no-shipped-data alone = %d, %q, %q, want a template reading no data rendered", code, out, errb)
}
if code, _, errb := runOut("{/sv_SE.person}", "--no-shipped-data"); code != 2 || !strings.Contains(errb, "no entry") {
if code, _, errb := runOut("{/sv_SE.person}", "--no-shipped-data"); code != 2 || !strings.Contains(errb, "no data is loaded: --no-shipped-data and no --data-path") {
t.Errorf("--no-shipped-data alone = %d, %q, want a reference to nothing refused", code, errb)
}
}
@@ -531,7 +531,7 @@ func TestRunRepeatOfZeroWritesNoRecord(t *testing.T) {
{nil, ""},
{[]string{"--format", "json"}, "[]\n"},
{[]string{"--format", "ndjson"}, ""},
{[]string{"--format", "csv"}, ""},
{[]string{"--format", "csv"}, "first,last\n"},
{[]string{"--format", "sql"}, ""},
} {
if code, out, errb := runOut("{/users}", append(c.args, "-n", "0", "-d", dir)...); code != 0 || out != c.want {
@@ -574,7 +574,6 @@ func TestRunIgnoresAFlagWithNoEffect(t *testing.T) {
{"", []string{"--list"}, []string{"--separator", ","}},
{"", []string{"--list"}, []string{"--format", "json"}},
{"", []string{"--list"}, []string{"--table", "t"}},
{"{/users}", []string{"--format", "json", "-n", "2"}, []string{"--separator", ","}},
{"{/users}", nil, []string{"--table", "t"}},
{"{/users}", []string{"--format", "json"}, []string{"--table", "t"}},
} {
@@ -587,8 +586,14 @@ func TestRunIgnoresAFlagWithNoEffect(t *testing.T) {
}
}
func TestRunReadsABareNumberAsAFormatString(t *testing.T) {
if code, out, errb := runOut("42\n", "-n", "3"); code != 0 || out != "42\n42\n42\n" {
t.Errorf("echo 42 | fejkdata -n 3 = %d, %q, %q, want 42 on three lines", code, out, errb)
}
}
func TestRunRendersAnyJSONValue(t *testing.T) {
for stdin, want := range map[string]string{"42": "42", "42\n": "42", "true\n": "true", "null\n": "", " null ": ""} {
for stdin, want := range map[string]string{"42": "42", "42\n": "42\n", "true\n": "true\n", " 7 ": " 7 ", "null\n": "", " null ": ""} {
if code, out, errb := runOut(stdin); code != 0 || out != want {
t.Errorf("run(%q) = %d, %q, %q, want %q", stdin, code, out, errb, want)
}
@@ -718,6 +723,10 @@ func TestRunRecordMisuse(t *testing.T) {
}{
{[]string{"--format", "yaml"}, "--format takes csv, json, ndjson, sql or text"},
{[]string{"--format", "sql", "--table", ""}, "--table names the INSERT target"},
{[]string{"--format", "csv", "--separator", ";"}, `--separator joins text renders; --format csv always separates fields with ","`},
{[]string{"--format", "json", "--separator", ","}, "--separator joins text renders"},
{[]string{"--format", "ndjson", "--separator", ","}, "--separator joins text renders"},
{[]string{"--format", "sql", "--separator", ","}, "--separator joins text renders"},
} {
code, out, errb := runOut("{/users}", c.args...)
if code != 2 || out != "" || !strings.Contains(errb, c.want) {
+14 -2
View File
@@ -53,6 +53,12 @@ func TestUserDataMayReferenceShipped(t *testing.T) {
}
}
func TestAGeneratorWithDataSaysNothingOfNoData(t *testing.T) {
if _, err := shipped(t).Fake("nope"); err == nil || strings.Contains(err.Error(), "no data is loaded") {
t.Errorf("Fake(nope) = %v, want no entry without the no-data note", err)
}
}
func TestAGeneratorWithNoDataRendersWhatReadsNone(t *testing.T) {
for name, opts := range map[string][]Option{
"no source": {WithoutShippedData()},
@@ -65,8 +71,14 @@ func TestAGeneratorWithNoDataRendersWhatReadsNone(t *testing.T) {
if v, err := f.FakeTemplate("{digits(3)}"); err != nil || len(v) != 3 {
t.Errorf("%s: FakeTemplate = %q, %v, want three digits", name, v, err)
}
if _, err := f.Fake("x"); err == nil || !strings.Contains(err.Error(), "no entry") {
t.Errorf("%s: Fake(x) = %v, want a reference to nothing refused", name, err)
if _, err := f.Fake("x"); err == nil || !strings.Contains(err.Error(), "no data is loaded") {
t.Errorf("%s: Fake(x) = %v, want a reference to nothing refused, saying no data is loaded", name, err)
}
if _, err := f.NewTemplate("{/x}"); err == nil || !strings.Contains(err.Error(), "no data is loaded") {
t.Errorf("%s: NewTemplate({/x}) = %v, want a reference to nothing refused, saying no data is loaded", name, err)
}
if _, err := f.FakeRecord("x"); err == nil || !strings.Contains(err.Error(), "no data is loaded") {
t.Errorf("%s: FakeRecord(x) = %v, want a reference to nothing refused, saying no data is loaded", name, err)
}
}
}
+1 -2
View File
@@ -60,7 +60,6 @@ func TestAPartWithNoEffectRendersAsWithoutIt(t *testing.T) {
`"{date(1990-01-01,1990-01-01,'2006-01-02')}"`: `"1990-01-01"`,
`"{date(1990-01-01,1990-12-31,'x')}"`: `"x"`,
`"{time('x')}"`: `"x"`,
`"{time('2006-01-02')}"`: `"1970-01-01"`,
} {
sameRenders(t, long, short)
}
@@ -108,7 +107,7 @@ func TestAJSONNumberOrBooleanIsItsText(t *testing.T) {
if v := fake(t, f, "l"); v != "1e3" {
t.Errorf("l = %q, want 1e3 as written", v)
}
for in, want := range map[string]string{"42": "42", " false ": "false", "[7]": "7"} {
for in, want := range map[string]string{"42": "42", " false ": " false ", "42\n": "42\n", "[7]": "7"} {
if v, err := f.FakeTemplate(in); err != nil || v != want {
t.Errorf("FakeTemplate(%q) = %q, %v, want %q", in, v, err, want)
}