Test a struct tag's bare number or boolean typed by its Go type, and a column's refusal offering only a datatype that holds the number
This commit is contained in:
+12
-4
@@ -75,10 +75,11 @@ func TestAPartWithNoEffectRendersAsWithoutIt(t *testing.T) {
|
|||||||
|
|
||||||
func TestAJSONNumberOrBooleanInARecordColumnIsRefused(t *testing.T) {
|
func TestAJSONNumberOrBooleanInARecordColumnIsRefused(t *testing.T) {
|
||||||
for src, want := range map[string][]string{
|
for src, want := range map[string][]string{
|
||||||
`{"format":"","n":5}`: {`"5"`, `{"format":"5","datatype":"integer"}`},
|
`{"format":"","n":5}`: {`"5"`, `{"format":"5","datatype":"integer"}`},
|
||||||
`{"format":"","n":1.50}`: {`"1.50"`, `{"format":"1.50","datatype":"number"}`},
|
`{"format":"","n":1.50}`: {`"1.50"`, `{"format":"1.50","datatype":"number"}`},
|
||||||
`{"format":"","b":true}`: {`"true"`, `{"format":"true","datatype":"boolean"}`},
|
`{"format":"","b":true}`: {`"true"`, `{"format":"true","datatype":"boolean"}`},
|
||||||
`{"format":"","n":[5,"x"]}`: {`"5"`, `{"format":"5","datatype":"integer"}`},
|
`{"format":"","n":12345678901234567890}`: {`"12345678901234567890"`, `{"format":"12345678901234567890","datatype":"number"}`},
|
||||||
|
`{"format":"","n":[5,"x"]}`: {`"5"`, `{"format":"5","datatype":"integer"}`},
|
||||||
} {
|
} {
|
||||||
_, err := resolved(t, src)
|
_, err := resolved(t, src)
|
||||||
for _, w := range want {
|
for _, w := range want {
|
||||||
@@ -89,6 +90,13 @@ func TestAJSONNumberOrBooleanInARecordColumnIsRefused(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAJSONNumberNoDatatypeHoldsIsOfferedAsTextAlone(t *testing.T) {
|
||||||
|
_, err := resolved(t, `{"format":"","n":1e400}`)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), `write "1e400" for text`) || strings.Contains(err.Error(), `"datatype"`) {
|
||||||
|
t.Errorf("compile(1e400 in a column) = %v, want text offered alone", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAJSONNumberOrBooleanIsItsText(t *testing.T) {
|
func TestAJSONNumberOrBooleanIsItsText(t *testing.T) {
|
||||||
f := newGenerator(t, writeData(t, map[string]string{
|
f := newGenerator(t, writeData(t, map[string]string{
|
||||||
"n": `{"format":"{x}","x":{"format":"{a} {b} {c} {d}","a":5,"b":1.50,"c":12345678901234567890,"d":true}}`,
|
"n": `{"format":"{x}","x":{"format":"{a} {b} {c} {d}","a":5,"b":1.50,"c":12345678901234567890,"d":true}}`,
|
||||||
|
|||||||
@@ -253,6 +253,17 @@ func TestFakeStructTakesTheDatatypeItsGoTypeSets(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFakeStructTypesABareNumberOrBooleanByItsGoType(t *testing.T) {
|
||||||
|
v := struct {
|
||||||
|
N int `fake:"[5, 6]"`
|
||||||
|
B bool `fake:"[true, false]"`
|
||||||
|
S string `fake:"[7.50]"`
|
||||||
|
}{}
|
||||||
|
if err := structData(t).FakeStruct(&v); err != nil || (v.N != 5 && v.N != 6) || v.S != "7.50" {
|
||||||
|
t.Errorf("FakeStruct = %+v, %v, want each value typed by its field", v, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFakeStructWithNoTagsFillsNothing(t *testing.T) {
|
func TestFakeStructWithNoTagsFillsNothing(t *testing.T) {
|
||||||
v := struct{ A string }{A: "kept"}
|
v := struct{ A string }{A: "kept"}
|
||||||
if err := structData(t).FakeStruct(&v); err != nil || v.A != "kept" {
|
if err := structData(t).FakeStruct(&v); err != nil || v.A != "kept" {
|
||||||
|
|||||||
Reference in New Issue
Block a user