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