github.com/sld880311/docker@v0.0.0-20200524143708-d5593973a475/cli/command/formatter/reflect_test.go (about) 1 package formatter 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 type dummy struct { 9 } 10 11 func (d *dummy) Func1() string { 12 return "Func1" 13 } 14 15 func (d *dummy) func2() string { 16 return "func2(should not be marshalled)" 17 } 18 19 func (d *dummy) Func3() (string, int) { 20 return "Func3(should not be marshalled)", -42 21 } 22 23 func (d *dummy) Func4() int { 24 return 4 25 } 26 27 type dummyType string 28 29 func (d *dummy) Func5() dummyType { 30 return dummyType("Func5") 31 } 32 33 func (d *dummy) FullHeader() string { 34 return "FullHeader(should not be marshalled)" 35 } 36 37 var dummyExpected = map[string]interface{}{ 38 "Func1": "Func1", 39 "Func4": 4, 40 "Func5": dummyType("Func5"), 41 } 42 43 func TestMarshalMap(t *testing.T) { 44 d := dummy{} 45 m, err := marshalMap(&d) 46 if err != nil { 47 t.Fatal(err) 48 } 49 if !reflect.DeepEqual(dummyExpected, m) { 50 t.Fatalf("expected %+v, got %+v", 51 dummyExpected, m) 52 } 53 } 54 55 func TestMarshalMapBad(t *testing.T) { 56 if _, err := marshalMap(nil); err == nil { 57 t.Fatal("expected an error (argument is nil)") 58 } 59 if _, err := marshalMap(dummy{}); err == nil { 60 t.Fatal("expected an error (argument is non-pointer)") 61 } 62 x := 42 63 if _, err := marshalMap(&x); err == nil { 64 t.Fatal("expected an error (argument is a pointer to non-struct)") 65 } 66 }