github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/dump/dump_test.go (about)

     1  package dump
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/gookit/color"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func ExamplePrint() {
    13  	Config(func(d *Options) {
    14  		d.NoColor = true
    15  	})
    16  	defer Reset()
    17  
    18  	Print(
    19  		23,
    20  		[]string{"ab", "cd"},
    21  		[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
    22  		map[string]string{"key": "val"},
    23  		map[string]interface{}{
    24  			"sub": map[string]string{"k": "v"},
    25  		},
    26  		struct {
    27  			ab string
    28  			Cd int
    29  		}{
    30  			"ab", 23,
    31  		},
    32  	)
    33  
    34  	// Output like:
    35  	// PRINT AT github.com/gookit/goutil/dump.ExamplePrint(LINE 14):
    36  	// int(23)
    37  	// []string{"ab", "cd"}
    38  	// []int [
    39  	//   1,
    40  	//   2,
    41  	//   3,
    42  	//   4,
    43  	//   5,
    44  	//   6,
    45  	//   7,
    46  	//   8,
    47  	//   9,
    48  	//   10,
    49  	//   11,
    50  	// ]
    51  	// map[string]string {
    52  	//   key: "val",
    53  	// }
    54  	// map[string]interface {} {
    55  	//   sub: map[string]string{"k":"v"},
    56  	// }
    57  	// struct { ab string; Cd int } {
    58  	//   ab: "ab",
    59  	//   Cd: 23,
    60  	// }
    61  	//
    62  }
    63  
    64  func TestStd(t *testing.T) {
    65  	assert.Equal(t, Std().NoColor, false)
    66  	assert.Equal(t, Std().IndentLen, 2)
    67  }
    68  
    69  func TestConfig(t *testing.T) {
    70  	is := assert.New(t)
    71  	buf := new(bytes.Buffer)
    72  
    73  	Config(func(d *Options) {
    74  		d.Output = buf
    75  		// no color on tests
    76  		d.NoColor = true
    77  		// show file
    78  		d.ShowFlag = Ffile | Fline
    79  	})
    80  	defer Reset()
    81  
    82  	P("hi")
    83  	// PRINT AT /Users/inhere/Workspace/godev/gookit/goutil/dump/dump_test.go:171
    84  	is.Contains(buf.String(), "pkg/dump/dump_test.go:")
    85  	buf.Reset()
    86  }
    87  
    88  func TestPrint(t *testing.T) {
    89  	is := assert.New(t)
    90  	buf := new(bytes.Buffer)
    91  
    92  	// disable caller position for test
    93  	Config(func(d *Options) {
    94  		d.NoColor = true
    95  	})
    96  	defer Reset()
    97  
    98  	// print position
    99  	Fprint(buf, 123)
   100  	// "PRINT AT github.com/bingoohuang/gg/pkg/dump.TestPrint(dump_test.go:65)"
   101  	str := buf.String()
   102  	is.Contains(str, "PRINT AT github.com/bingoohuang/gg/pkg/dump.TestPrint(dump_test.go:")
   103  	is.Contains(str, "int(123)")
   104  
   105  	// dont print position
   106  	Std().ShowFlag = Fnopos
   107  
   108  	buf.Reset()
   109  	Fprint(buf, "abc")
   110  	is.Equal(`string("abc"), #len=3
   111  `, buf.String())
   112  
   113  	buf.Reset()
   114  	Fprint(buf, []string{"ab", "cd"})
   115  	is.Contains(buf.String(), `[]string [ #len=2`)
   116  
   117  	buf.Reset()
   118  	Fprint(buf, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})
   119  	is.Equal(`[]int [ #len=11
   120    int(1),
   121    int(2),
   122    int(3),
   123    int(4),
   124    int(5),
   125    int(6),
   126    int(7),
   127    int(8),
   128    int(9),
   129    int(10),
   130    int(11),
   131  ],
   132  `, buf.String())
   133  
   134  	buf.Reset()
   135  	Fprint(buf, struct {
   136  		ab string
   137  		Cd int
   138  	}{
   139  		"ab", 23,
   140  	})
   141  	is.Equal(`struct { ab string; Cd int } {
   142    ab: string("ab"), #len=2
   143    Cd: int(23),
   144  },
   145  `, buf.String())
   146  
   147  	buf.Reset()
   148  	Fprint(buf, map[string]interface{}{
   149  		"key": "val",
   150  		"sub": map[string]string{"k": "v"},
   151  	})
   152  	is.Contains(buf.String(), `
   153    "sub": map[string]string { #len=1
   154      "k": string("v"), #len=1
   155    },
   156  `)
   157  
   158  	buf.Reset()
   159  }
   160  
   161  func TestPrintNil(t *testing.T) {
   162  	is := assert.New(t)
   163  
   164  	buf := newBuffer()
   165  	Config(func(d *Options) {
   166  		d.ShowFlag = Fnopos
   167  	})
   168  	defer Reset()
   169  
   170  	Print(nil)
   171  	is.Equal("<nil>,\n", buf.String())
   172  	buf.Reset()
   173  
   174  	var i int
   175  	Println(i)
   176  	is.Equal("int(0),\n", buf.String())
   177  	buf.Reset()
   178  
   179  	var f interface{}
   180  	V(f)
   181  	is.Equal("<nil>,\n", buf.String())
   182  	buf.Reset()
   183  }
   184  
   185  func TestStruct_CannotExportField(t *testing.T) {
   186  	Print(myOpts)
   187  
   188  	// OUT:
   189  	// PRINT AT github.com/bingoohuang/gg/pkg/dump.TestStruct_CannotExportField(dump_test.go:202)
   190  	// struct { opt0 *int; opt1 bool; opt2 int; opt3 float64; opt4 string } {
   191  	//  opt0: <nil>,
   192  	//  opt1: true,
   193  	//  opt2: int(22),
   194  	//  opt3: float64(34.45),
   195  	//  opt4: string("abc"),
   196  	// }
   197  
   198  	buf := newBuffer()
   199  	defer Reset()
   200  
   201  	Println(myOpts)
   202  
   203  	str := buf.String()
   204  	assert.Contains(t, str, "struct {")
   205  	assert.Contains(t, str, "opt0: *int<nil>,")
   206  	assert.Contains(t, str, "opt2: int(22),")
   207  	assert.Contains(t, str, "opt3: float64(34.45)")
   208  	assert.Contains(t, str, "opt4: string(\"abc\"),")
   209  }
   210  
   211  func TestStruct_InterfaceField(t *testing.T) {
   212  	myS1 := struct {
   213  		// cannotExport interface{} // ok
   214  		cannotExport st1 // ok
   215  		// CanExport interface{} ok
   216  		CanExport st1 // ok
   217  	}{
   218  		cannotExport: s1,
   219  		CanExport:    s1,
   220  	}
   221  
   222  	Println(myS1)
   223  	color.Infoln("\nUse fmt.Println:")
   224  	fmt.Println(myS1)
   225  }
   226  
   227  func TestStruct_MapInterfacedValue(t *testing.T) {
   228  	myS2 := &struct {
   229  		cannotExport map[string]interface{}
   230  	}{
   231  		cannotExport: map[string]interface{}{
   232  			"key1": 12,
   233  			"key2": "abcd123",
   234  		},
   235  	}
   236  
   237  	Println(myS2)
   238  	color.Infoln("\nUse fmt.Println:")
   239  	fmt.Println(myS2)
   240  	fmt.Println("---------------------------------------------------------------")
   241  
   242  	type st2 struct {
   243  		st1
   244  		Github string
   245  		Face1  interface{}
   246  		face2  interface{}
   247  		faces  map[string]interface{}
   248  	}
   249  
   250  	s2 := st2{
   251  		st1:    s1,
   252  		Github: "https://github.com/inhere",
   253  		Face1:  s1,
   254  		face2:  s1,
   255  		faces: map[string]interface{}{
   256  			"key1": 12,
   257  			"key2": "abc2344",
   258  		},
   259  	}
   260  
   261  	Println(s2)
   262  	color.Infoln("\nUse fmt.Println:")
   263  	fmt.Println(s2)
   264  }
   265  
   266  func TestStruct_ptrField(t *testing.T) {
   267  	type userOpts struct {
   268  		Int *int
   269  		// use ptr
   270  		Str *string
   271  	}
   272  
   273  	aint := 2
   274  	astr := "xyz"
   275  	opt := &userOpts{
   276  		Int: &aint,
   277  		Str: &astr,
   278  	}
   279  
   280  	Println(opt)
   281  	color.Infoln("\nUse fmt.Println:")
   282  	fmt.Println(opt)
   283  	fmt.Println("---------------------------------------------------------------")
   284  }
   285  
   286  func TestFormat(t *testing.T) {
   287  	s := Format(23, "abc", map[string]interface{}{
   288  		"key1": 12,
   289  		"key2": "abc2344",
   290  	})
   291  
   292  	assert.NotEmpty(t, s)
   293  	fmt.Println(s)
   294  }
   295  
   296  func newBuffer() *bytes.Buffer {
   297  	buf := new(bytes.Buffer)
   298  
   299  	Config(func(d *Options) {
   300  		d.Output = buf
   301  		d.NoColor = true
   302  	})
   303  
   304  	return buf
   305  }