mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
4f08346686
The correct formatting for machine-readable comments is; //<some alphanumeric identifier>:<options>[,<option>...][ // comment] Which basically means: - MUST NOT have a space before `<identifier>` (e.g. `nolint`) - Identified MUST be alphanumeric - MUST be followed by a colon - MUST be followed by at least one `<option>` - Optionally additional `<options>` (comma-separated) - Optionally followed by a comment Any other format will not be considered a machine-readable comment by `gofmt`, and thus formatted as a regular comment. Note that this also means that a `//nolint` (without anything after it) is considered invalid, same for `//#nosec` (starts with a `#`). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
106 lines
2.8 KiB
Go
106 lines
2.8 KiB
Go
package options
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
gen := NewGeneric()
|
|
gen["Int"] = 1
|
|
gen["Rune"] = 'b'
|
|
gen["Float64"] = 2.0
|
|
|
|
type Model struct {
|
|
Int int
|
|
Rune rune
|
|
Float64 float64
|
|
}
|
|
|
|
result, err := GenerateFromModel(gen, Model{})
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
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) {
|
|
gen := NewGeneric()
|
|
gen["Int"] = 1
|
|
gen["Rune"] = 'b'
|
|
gen["Float64"] = 2.0
|
|
|
|
type Model struct {
|
|
Int int
|
|
Rune rune
|
|
Float64 float64
|
|
}
|
|
|
|
result, err := GenerateFromModel(gen, &Model{})
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
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)
|
|
} else if expected := "no field"; !strings.Contains(err.Error(), expected) {
|
|
t.Fatalf("expected %q in error message, got %s", expected, err.Error())
|
|
}
|
|
}
|
|
|
|
func TestFieldCannotBeSet(t *testing.T) {
|
|
type Model struct{ foo int } //nolint:structcheck
|
|
_, err := GenerateFromModel(Generic{"foo": "bar"}, Model{})
|
|
|
|
if _, ok := err.(CannotSetFieldError); !ok {
|
|
t.Fatalf("expected CannotSetFieldError, got %#v", err)
|
|
} else if expected := "cannot set field"; !strings.Contains(err.Error(), expected) {
|
|
t.Fatalf("expected %q in error message, got %s", expected, err.Error())
|
|
}
|
|
}
|
|
|
|
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)
|
|
} else if expected := "type mismatch"; !strings.Contains(err.Error(), expected) {
|
|
t.Fatalf("expected %q in error message, got %s", expected, err.Error())
|
|
}
|
|
}
|