|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/types" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestGetSimpleOptionalFieldFormat(t *testing.T) { |
| 9 | + newNamedType := func(name string, underlying types.Type) types.Type { |
| 10 | + return types.NewNamed( |
| 11 | + types.NewTypeName(0, nil, name, nil), |
| 12 | + underlying, |
| 13 | + nil, |
| 14 | + ) |
| 15 | + } |
| 16 | + |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + typ types.Type |
| 20 | + expected string |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "string type", |
| 24 | + typ: types.Typ[types.String], |
| 25 | + expected: "\nif s.{{.name}} != \"\" {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "int type", |
| 29 | + typ: types.Typ[types.Int], |
| 30 | + expected: "\nif s.{{.name}} != 0 {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "float64 type", |
| 34 | + typ: types.Typ[types.Float64], |
| 35 | + expected: "\nif s.{{.name}} != 0.0 {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "bool type", |
| 39 | + typ: types.Typ[types.Bool], |
| 40 | + expected: "\nif s.{{.name}} != false {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "custom string type (type Token string)", |
| 44 | + typ: newNamedType("Token", types.Typ[types.String]), |
| 45 | + expected: "\nif s.{{.name}} != \"\" {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "custom int type (type Digits int)", |
| 49 | + typ: newNamedType("Digits", types.Typ[types.Int]), |
| 50 | + expected: "\nif s.{{.name}} != 0 {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "custom float type (type Decimal float64)", |
| 54 | + typ: newNamedType("Decimal", types.Typ[types.Float64]), |
| 55 | + expected: "\nif s.{{.name}} != 0.0 {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "custom bool type (type Truther bool)", |
| 59 | + typ: newNamedType("Truther", types.Typ[types.Bool]), |
| 60 | + expected: "\nif s.{{.name}} != false {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "pointer type", |
| 64 | + typ: types.NewPointer(types.Typ[types.Int]), |
| 65 | + expected: "\nif s.{{.name}} != nil {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "pointer to custom type", |
| 69 | + typ: types.NewPointer(newNamedType("Nullable", types.Typ[types.String])), |
| 70 | + expected: "\nif s.{{.name}} != nil {\n\taddField(\"{{.key}}\", s.{{.name}})\n}", |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tt := range tests { |
| 75 | + t.Run(tt.name, func(t *testing.T) { |
| 76 | + got := getSimpleOptionalFieldFormat(tt.typ) |
| 77 | + if got != tt.expected { |
| 78 | + t.Errorf("getSimpleOptionalFieldFormat() =\n%v\nwant:\n%v", got, tt.expected) |
| 79 | + } |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments