github.com/shipa-corp/ketch@v0.6.0/cmd/ketch/output/column_test.go (about)

     1  package output
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type Item struct {
     9  	Name          string `column:"name"`
    10  	Value         int    `column:"VALUE"`
    11  	UnlabeledData float64
    12  	Omit          string `column:"-"`
    13  }
    14  
    15  func TestMarshal(t *testing.T) {
    16  	tests := []struct {
    17  		v           interface{}
    18  		expected    []byte
    19  		err         error
    20  		description string
    21  	}{
    22  		{
    23  			v:           Item{Name: "test", Value: 2, UnlabeledData: 3.1},
    24  			expected:    []byte("name    VALUE    UNLABELED DATA    \ntest    2        3.1"),
    25  			description: "struct",
    26  		},
    27  		{
    28  			v: []Item{{
    29  				Name: "test", Value: 2,
    30  			}, {
    31  				Name: "test-2", Value: 3,
    32  			}},
    33  			expected:    []byte("name      VALUE    UNLABELED DATA    \ntest      2        0                 \ntest-2    3        0"),
    34  			description: "slice",
    35  		},
    36  		{
    37  			v:           &Item{Name: "test", Value: 2},
    38  			expected:    []byte("name    VALUE    UNLABELED DATA    \ntest    2        0"),
    39  			description: "pointer",
    40  		},
    41  		{
    42  			v:           map[string]interface{}{"number": 3, "str": "this is a string", "float": 4.3},
    43  			expected:    []byte("float    number    str\n4.3      3         this is a string"),
    44  			description: "map",
    45  		},
    46  	}
    47  	for _, test := range tests {
    48  		c := &columnOutput{}
    49  		res, err := c.marshal(test.v)
    50  		if err != test.err {
    51  			t.Errorf("expected error %v, got %v", test.err, err)
    52  		}
    53  		if !reflect.DeepEqual(res, test.expected) {
    54  			t.Errorf("test: %s...expected \n%s\n got \n%s\n", test.description, string(test.expected), string(res))
    55  			t.Log(test.expected)
    56  			t.Log(res)
    57  		}
    58  	}
    59  }