...

Source file src/github.com/xlab/treeprint/struct_test.go

Documentation: github.com/xlab/treeprint

     1  package treeprint
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  type nameStruct struct {
    11  	One   string `json:"one" tree:"one"`
    12  	Two   int    `tree:"two"`
    13  	Three struct {
    14  		SubOne   []string
    15  		SubTwo   []interface{}
    16  		SubThree struct {
    17  			InnerOne   *float64  `tree:"inner_one,omitempty"`
    18  			InnerTwo   *struct{} `tree:",omitempty"`
    19  			InnerThree *float64  `tree:"inner_three"`
    20  		}
    21  	}
    22  }
    23  
    24  func TestFromStructName(t *testing.T) {
    25  	assert := assert.New(t)
    26  
    27  	tree, err := FromStruct(nameStruct{}, StructNameTree)
    28  	assert.NoError(err)
    29  
    30  	actual := tree.String()
    31  	expected := `.
    32  ├── one
    33  ├── two
    34  └── Three
    35      ├── SubOne
    36      ├── SubTwo
    37      └── SubThree
    38          └── inner_three
    39  `
    40  	assert.Equal(expected, actual)
    41  }
    42  
    43  func TestFromStructTags(t *testing.T) {
    44  	assert := assert.New(t)
    45  
    46  	tree, err := FromStruct(nameStruct{}, StructTagTree)
    47  	assert.NoError(err)
    48  
    49  	actual := tree.String()
    50  	expected := `.
    51  ├── [json:"one"]  one
    52  ├── []  two
    53  └── []  Three
    54      ├── []  SubOne
    55      ├── []  SubTwo
    56      └── []  SubThree
    57          └── []  inner_three
    58  `
    59  	assert.Equal(expected, actual)
    60  }
    61  
    62  type typeStruct struct {
    63  	One   string `json:"one" tree:"one"`
    64  	Two   int    `tree:"two"`
    65  	Three subtypeStruct
    66  }
    67  
    68  type subtypeStruct struct {
    69  	SubOne   []string
    70  	SubTwo   []interface{}
    71  	SubThree subsubTypeStruct
    72  }
    73  
    74  type subsubTypeStruct struct {
    75  	InnerOne   *float64  `tree:"inner_one,omitempty"`
    76  	InnerTwo   *struct{} `tree:",omitempty"`
    77  	InnerThree *float64  `tree:"inner_three"`
    78  }
    79  
    80  func TestFromStructType(t *testing.T) {
    81  	assert := assert.New(t)
    82  
    83  	tree, err := FromStruct(typeStruct{}, StructTypeTree)
    84  	assert.NoError(err)
    85  
    86  	actual := tree.String()
    87  	expected := `.
    88  ├── [string]  one
    89  ├── [int]  two
    90  └── [treeprint.subtypeStruct]  Three
    91      ├── [[]string]  SubOne
    92      ├── [[]interface {}]  SubTwo
    93      └── [treeprint.subsubTypeStruct]  SubThree
    94          └── [*float64]  inner_three
    95  `
    96  	assert.Equal(expected, actual)
    97  }
    98  
    99  func TestFromStructTypeSize(t *testing.T) {
   100  	assert := assert.New(t)
   101  
   102  	tree, err := FromStruct(typeStruct{}, StructTypeSizeTree)
   103  	assert.NoError(err)
   104  
   105  	actual := tree.String()
   106  	expected := `.
   107  ├── [16]  one
   108  ├── [8]  two
   109  └── [72]  Three
   110      ├── [24]  SubOne
   111      ├── [24]  SubTwo
   112      └── [24]  SubThree
   113          └── [8]  inner_three
   114  `
   115  	assert.Equal(expected, actual)
   116  }
   117  
   118  type valueStruct struct {
   119  	Name string
   120  	Bio  struct {
   121  		Age  int
   122  		City string
   123  		Meta interface{}
   124  	}
   125  }
   126  
   127  func TestFromStructValue(t *testing.T) {
   128  	assert := assert.New(t)
   129  
   130  	val := valueStruct{
   131  		Name: "Max",
   132  	}
   133  	val.Bio.Age = 100
   134  	val.Bio.City = "NYC"
   135  	val.Bio.Meta = []byte("hello")
   136  	tree, err := FromStruct(val, StructValueTree)
   137  	assert.NoError(err)
   138  
   139  	actual := tree.String()
   140  	expected := `.
   141  ├── [Max]  Name
   142  └── Bio
   143      ├── [100]  Age
   144      ├── [NYC]  City
   145      └── [[104 101 108 108 111]]  Meta
   146  `
   147  	assert.Equal(expected, actual)
   148  }
   149  
   150  func TestFromStructWithMeta(t *testing.T) {
   151  	assert := assert.New(t)
   152  
   153  	val := valueStruct{
   154  		Name: "Max",
   155  	}
   156  	val.Bio.Age = 100
   157  	val.Bio.City = "NYC"
   158  	val.Bio.Meta = []byte("hello")
   159  	tree, err := FromStructWithMeta(val, func(_ string, v interface{}) (string, bool) {
   160  		return fmt.Sprintf("lol %T", v), true
   161  	})
   162  	assert.NoError(err)
   163  
   164  	actual := tree.String()
   165  	expected := `.
   166  ├── [lol string]  Name
   167  └── [lol struct { Age int; City string; Meta interface {} }]  Bio
   168      ├── [lol int]  Age
   169      ├── [lol string]  City
   170      └── [lol []uint8]  Meta
   171  `
   172  	assert.Equal(expected, actual)
   173  }
   174  

View as plain text