2015-02-20 06:21:05 +00:00
|
|
|
package options
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2015-02-24 19:19:00 +00:00
|
|
|
"strings"
|
2015-02-20 06:21:05 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerate(t *testing.T) {
|
2023-07-16 17:17:26 +00:00
|
|
|
gen := Generic{
|
|
|
|
"Int": 1,
|
|
|
|
"Rune": 'b',
|
|
|
|
"Float64": 2.0,
|
|
|
|
}
|
2015-02-20 06:21:05 +00:00
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
Int int
|
|
|
|
Rune rune
|
|
|
|
Float64 float64
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := GenerateFromModel(gen, Model{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-24 19:19:00 +00:00
|
|
|
cast, ok := result.(Model)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("result has unexpected type %s", reflect.TypeOf(result))
|
|
|
|
}
|
|
|
|
if expected := 1; cast.Int != expected {
|
|
|
|
t.Fatalf("wrong value for field Int: expected %v, got %v", expected, cast.Int)
|
|
|
|
}
|
|
|
|
if expected := 'b'; cast.Rune != expected {
|
|
|
|
t.Fatalf("wrong value for field Rune: expected %v, got %v", expected, cast.Rune)
|
|
|
|
}
|
|
|
|
if expected := 2.0; cast.Float64 != expected {
|
|
|
|
t.Fatalf("wrong value for field Int: expected %v, got %v", expected, cast.Float64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGeneratePtr(t *testing.T) {
|
2023-07-16 17:17:26 +00:00
|
|
|
gen := Generic{
|
|
|
|
"Int": 1,
|
|
|
|
"Rune": 'b',
|
|
|
|
"Float64": 2.0,
|
|
|
|
}
|
2015-02-24 19:19:00 +00:00
|
|
|
|
|
|
|
type Model struct {
|
|
|
|
Int int
|
|
|
|
Rune rune
|
|
|
|
Float64 float64
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := GenerateFromModel(gen, &Model{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-20 06:21:05 +00:00
|
|
|
cast, ok := result.(*Model)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("result has unexpected type %s", reflect.TypeOf(result))
|
|
|
|
}
|
|
|
|
if expected := 1; cast.Int != expected {
|
|
|
|
t.Fatalf("wrong value for field Int: expected %v, got %v", expected, cast.Int)
|
|
|
|
}
|
|
|
|
if expected := 'b'; cast.Rune != expected {
|
|
|
|
t.Fatalf("wrong value for field Rune: expected %v, got %v", expected, cast.Rune)
|
|
|
|
}
|
|
|
|
if expected := 2.0; cast.Float64 != expected {
|
|
|
|
t.Fatalf("wrong value for field Int: expected %v, got %v", expected, cast.Float64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateMissingField(t *testing.T) {
|
|
|
|
type Model struct{}
|
|
|
|
_, err := GenerateFromModel(Generic{"foo": "bar"}, Model{})
|
|
|
|
|
|
|
|
if _, ok := err.(NoSuchFieldError); !ok {
|
|
|
|
t.Fatalf("expected NoSuchFieldError, got %#v", err)
|
2022-09-03 21:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const expected = "no field"
|
|
|
|
if !strings.Contains(err.Error(), expected) {
|
2015-02-24 19:19:00 +00:00
|
|
|
t.Fatalf("expected %q in error message, got %s", expected, err.Error())
|
2015-02-20 06:21:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFieldCannotBeSet(t *testing.T) {
|
2022-09-03 21:20:23 +00:00
|
|
|
type Model struct{ foo int } //nolint:nolintlint,unused // un-exported field is used to test error-handling
|
2015-02-20 06:21:05 +00:00
|
|
|
_, err := GenerateFromModel(Generic{"foo": "bar"}, Model{})
|
|
|
|
|
|
|
|
if _, ok := err.(CannotSetFieldError); !ok {
|
|
|
|
t.Fatalf("expected CannotSetFieldError, got %#v", err)
|
2022-09-03 21:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const expected = "cannot set field"
|
|
|
|
if !strings.Contains(err.Error(), expected) {
|
2015-02-24 19:19:00 +00:00
|
|
|
t.Fatalf("expected %q in error message, got %s", expected, err.Error())
|
2015-02-20 06:21:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 03:30:17 +00:00
|
|
|
|
|
|
|
func TestTypeMismatchError(t *testing.T) {
|
|
|
|
type Model struct{ Foo int }
|
|
|
|
_, err := GenerateFromModel(Generic{"Foo": "bar"}, Model{})
|
|
|
|
|
|
|
|
if _, ok := err.(TypeMismatchError); !ok {
|
|
|
|
t.Fatalf("expected TypeMismatchError, got %#v", err)
|
2022-09-03 21:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const expected = "type mismatch"
|
|
|
|
if !strings.Contains(err.Error(), expected) {
|
2015-08-26 03:30:17 +00:00
|
|
|
t.Fatalf("expected %q in error message, got %s", expected, err.Error())
|
|
|
|
}
|
|
|
|
}
|