Files
docker-cli/cli/command/formatter/reflect_test.go
Sebastiaan van Stijn 7c80e4f938 update go:build tags to use go1.22
commit 4a7b04d412 configured golangci-lint
to use go1.23 semantics, which enabled the copyloopvar linter.

go1.22 now creates a copy of variables when assigned in a loop; make sure we
don't have files that may downgrade semantics to go1.21 in case that also means
disabling that feature; https://go.dev/ref/spec#Go_1.22

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-11-12 12:38:18 +01:00

69 lines
1.3 KiB
Go

// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.22
package formatter
import (
"reflect"
"testing"
)
type dummy struct{}
func (d *dummy) Func1() string {
return "Func1"
}
func (d *dummy) func2() string { //nolint:unused
return "func2(should not be marshalled)"
}
func (d *dummy) Func3() (string, int) {
return "Func3(should not be marshalled)", -42
}
func (d *dummy) Func4() int {
return 4
}
type dummyType string
func (d *dummy) Func5() dummyType {
return dummyType("Func5")
}
func (d *dummy) FullHeader() string {
return "FullHeader(should not be marshalled)"
}
var dummyExpected = map[string]any{
"Func1": "Func1",
"Func4": 4,
"Func5": dummyType("Func5"),
}
func TestMarshalMap(t *testing.T) {
d := dummy{}
m, err := marshalMap(&d)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(dummyExpected, m) {
t.Fatalf("expected %+v, got %+v",
dummyExpected, m)
}
}
func TestMarshalMapBad(t *testing.T) {
if _, err := marshalMap(nil); err == nil {
t.Fatal("expected an error (argument is nil)")
}
if _, err := marshalMap(dummy{}); err == nil {
t.Fatal("expected an error (argument is non-pointer)")
}
x := 42
if _, err := marshalMap(&x); err == nil {
t.Fatal("expected an error (argument is a pointer to non-struct)")
}
}