github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/formatter/reflect_test.go (about)

     1  // FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
     2  //go:build go1.19
     3  
     4  package formatter
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  type dummy struct{}
    12  
    13  func (d *dummy) Func1() string {
    14  	return "Func1"
    15  }
    16  
    17  func (d *dummy) func2() string { //nolint:unused
    18  	return "func2(should not be marshalled)"
    19  }
    20  
    21  func (d *dummy) Func3() (string, int) {
    22  	return "Func3(should not be marshalled)", -42
    23  }
    24  
    25  func (d *dummy) Func4() int {
    26  	return 4
    27  }
    28  
    29  type dummyType string
    30  
    31  func (d *dummy) Func5() dummyType {
    32  	return dummyType("Func5")
    33  }
    34  
    35  func (d *dummy) FullHeader() string {
    36  	return "FullHeader(should not be marshalled)"
    37  }
    38  
    39  var dummyExpected = map[string]any{
    40  	"Func1": "Func1",
    41  	"Func4": 4,
    42  	"Func5": dummyType("Func5"),
    43  }
    44  
    45  func TestMarshalMap(t *testing.T) {
    46  	d := dummy{}
    47  	m, err := marshalMap(&d)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if !reflect.DeepEqual(dummyExpected, m) {
    52  		t.Fatalf("expected %+v, got %+v",
    53  			dummyExpected, m)
    54  	}
    55  }
    56  
    57  func TestMarshalMapBad(t *testing.T) {
    58  	if _, err := marshalMap(nil); err == nil {
    59  		t.Fatal("expected an error (argument is nil)")
    60  	}
    61  	if _, err := marshalMap(dummy{}); err == nil {
    62  		t.Fatal("expected an error (argument is non-pointer)")
    63  	}
    64  	x := 42
    65  	if _, err := marshalMap(&x); err == nil {
    66  		t.Fatal("expected an error (argument is a pointer to non-struct)")
    67  	}
    68  }