...

Source file src/k8s.io/apimachinery/pkg/util/dump/dump_test.go

Documentation: k8s.io/apimachinery/pkg/util/dump

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package dump
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  )
    23  
    24  func ptrint(i int) *int {
    25  	return &i
    26  }
    27  
    28  func ptrstr(s string) *string {
    29  	return &s
    30  }
    31  
    32  // custom type to test Stringer interface on non-pointer receiver.
    33  type customString string
    34  
    35  // String implements the Stringer interface for testing invocation
    36  func (s customString) String() string {
    37  	return "custom string " + string(s)
    38  }
    39  
    40  // custom type to test error interface on non-pointer receiver.
    41  type customError int
    42  
    43  // Error implements the error interface for testing invocation
    44  func (e customError) Error() string {
    45  	return fmt.Sprintf("custom error: %d", int(e))
    46  }
    47  
    48  // custom type to test Stringer interface on pointer receiver.
    49  type pCustomString string
    50  
    51  // String implements the Stringer interface for testing invocation
    52  func (s *pCustomString) String() string {
    53  	return "custom string " + string(*s)
    54  }
    55  
    56  // custom type to test error interface on pointer receiver.
    57  type pCustomError int
    58  
    59  // Error implements the error interface for testing invocation
    60  func (e *pCustomError) Error() string {
    61  	return fmt.Sprintf("custom error: %d", int(*e))
    62  }
    63  
    64  // embed is used to test embedded structures.
    65  type embed struct {
    66  	s string
    67  }
    68  
    69  // embedwrap is used to test embedded structures.
    70  type embedwrap struct {
    71  	embed
    72  	e *embed
    73  }
    74  
    75  func TestPretty(t *testing.T) {
    76  	tcs := customString("test")
    77  	tpcs := pCustomString("&test")
    78  
    79  	tce := customError(0)
    80  	tpce := pCustomError(0)
    81  
    82  	teb := embed{"test"}
    83  	tebw := embedwrap{teb, &teb}
    84  
    85  	testCases := []struct {
    86  		a    interface{}
    87  		want string
    88  	}{
    89  		{int8(93), "(int8) 93\n"},
    90  		{int16(93), "(int16) 93\n"},
    91  		{int32(93), "(int32) 93\n"},
    92  		{int64(93), "(int64) 93\n"},
    93  		{int(-93), "(int) -93\n"},
    94  		{int8(-93), "(int8) -93\n"},
    95  		{int16(-93), "(int16) -93\n"},
    96  		{int32(-93), "(int32) -93\n"},
    97  		{int64(-93), "(int64) -93\n"},
    98  		{uint(93), "(uint) 93\n"},
    99  		{uint8(93), "(uint8) 93\n"},
   100  		{uint16(93), "(uint16) 93\n"},
   101  		{uint32(93), "(uint32) 93\n"},
   102  		{uint64(93), "(uint64) 93\n"},
   103  		{uintptr(93), "(uintptr) 0x5d\n"},
   104  		{ptrint(93), "(*int)(93)\n"},
   105  		{float32(93.76), "(float32) 93.76\n"},
   106  		{float64(93.76), "(float64) 93.76\n"},
   107  		{complex64(93i), "(complex64) (0+93i)\n"},
   108  		{complex128(93i), "(complex128) (0+93i)\n"},
   109  		{bool(true), "(bool) true\n"},
   110  		{bool(false), "(bool) false\n"},
   111  		{string("test"), "(string) (len=4) \"test\"\n"},
   112  		{ptrstr("test"), "(*string)((len=4) \"test\")\n"},
   113  		{[1]string{"arr"}, "([1]string) (len=1) {\n  (string) (len=3) \"arr\"\n}\n"},
   114  		{[]string{"slice"}, "([]string) (len=1) {\n  (string) (len=5) \"slice\"\n}\n"},
   115  		{tcs, "(dump.customString) (len=4) \"test\"\n"},
   116  		{&tcs, "(*dump.customString)((len=4) \"test\")\n"},
   117  		{tpcs, "(dump.pCustomString) (len=5) \"&test\"\n"},
   118  		{&tpcs, "(*dump.pCustomString)((len=5) \"&test\")\n"},
   119  		{tce, "(dump.customError) 0\n"},
   120  		{&tce, "(*dump.customError)(0)\n"},
   121  		{tpce, "(dump.pCustomError) 0\n"},
   122  		{&tpce, "(*dump.pCustomError)(0)\n"},
   123  		{
   124  			struct {
   125  				arr   [1]string
   126  				slice []string
   127  				m     map[string]int
   128  			}{
   129  				[1]string{"arr"},
   130  				[]string{"slice"},
   131  				map[string]int{"one": 1},
   132  			},
   133  			"(struct { arr [1]string; slice []string; m map[string]int }) {\n  arr: ([1]string) (len=1) {\n    (string) (len=3) \"arr\"\n  },\n  slice: ([]string) (len=1) {\n    (string) (len=5) \"slice\"\n  },\n  m: (map[string]int) (len=1) {\n    (string) (len=3) \"one\": (int) 1\n  }\n}\n",
   134  		},
   135  		{teb, "(dump.embed) {\n  s: (string) (len=4) \"test\"\n}\n"},
   136  		{tebw, "(dump.embedwrap) {\n  embed: (dump.embed) {\n    s: (string) (len=4) \"test\"\n  },\n  e: (*dump.embed)({\n    s: (string) (len=4) \"test\"\n  })\n}\n"},
   137  		{map[string]int{}, "(map[string]int) {\n}\n"},
   138  		{map[string]int{"one": 1}, "(map[string]int) (len=1) {\n  (string) (len=3) \"one\": (int) 1\n}\n"},
   139  		{map[string]interface{}{"one": 1}, "(map[string]interface {}) (len=1) {\n  (string) (len=3) \"one\": (int) 1\n}\n"},
   140  		{map[string]customString{"key": tcs}, "(map[string]dump.customString) (len=1) {\n  (string) (len=3) \"key\": (dump.customString) (len=4) \"test\"\n}\n"},
   141  		{map[string]customError{"key": tce}, "(map[string]dump.customError) (len=1) {\n  (string) (len=3) \"key\": (dump.customError) 0\n}\n"},
   142  		{map[string]embed{"key": teb}, "(map[string]dump.embed) (len=1) {\n  (string) (len=3) \"key\": (dump.embed) {\n    s: (string) (len=4) \"test\"\n  }\n}\n"},
   143  		{map[string]embedwrap{"key": tebw}, "(map[string]dump.embedwrap) (len=1) {\n  (string) (len=3) \"key\": (dump.embedwrap) {\n    embed: (dump.embed) {\n      s: (string) (len=4) \"test\"\n    },\n    e: (*dump.embed)({\n      s: (string) (len=4) \"test\"\n    })\n  }\n}\n"},
   144  	}
   145  
   146  	for i, tc := range testCases {
   147  		s := Pretty(tc.a)
   148  		if tc.want != s {
   149  			t.Errorf("[%d]:\n\texpected %q\n\tgot      %q", i, tc.want, s)
   150  		}
   151  	}
   152  }
   153  
   154  func TestForHash(t *testing.T) {
   155  	tcs := customString("test")
   156  	tpcs := pCustomString("&test")
   157  
   158  	tce := customError(0)
   159  	tpce := pCustomError(0)
   160  
   161  	teb := embed{"test"}
   162  	tebw := embedwrap{teb, &teb}
   163  
   164  	testCases := []struct {
   165  		a    interface{}
   166  		want string
   167  	}{
   168  		{int8(93), "(int8)93"},
   169  		{int16(93), "(int16)93"},
   170  		{int32(93), "(int32)93"},
   171  		{int64(93), "(int64)93"},
   172  		{int(-93), "(int)-93"},
   173  		{int8(-93), "(int8)-93"},
   174  		{int16(-93), "(int16)-93"},
   175  		{int32(-93), "(int32)-93"},
   176  		{int64(-93), "(int64)-93"},
   177  		{uint(93), "(uint)93"},
   178  		{uint8(93), "(uint8)93"},
   179  		{uint16(93), "(uint16)93"},
   180  		{uint32(93), "(uint32)93"},
   181  		{uint64(93), "(uint64)93"},
   182  		{uintptr(93), "(uintptr)0x5d"},
   183  		{ptrint(93), "(*int)93"},
   184  		{float32(93.76), "(float32)93.76"},
   185  		{float64(93.76), "(float64)93.76"},
   186  		{complex64(93i), "(complex64)(0+93i)"},
   187  		{complex128(93i), "(complex128)(0+93i)"},
   188  		{bool(true), "(bool)true"},
   189  		{bool(false), "(bool)false"},
   190  		{string("test"), "(string)test"},
   191  		{ptrstr("test"), "(*string)test"},
   192  		{[1]string{"arr"}, "([1]string)[arr]"},
   193  		{[]string{"slice"}, "([]string)[slice]"},
   194  		{tcs, "(dump.customString)test"},
   195  		{&tcs, "(*dump.customString)test"},
   196  		{tpcs, "(dump.pCustomString)&test"},
   197  		{&tpcs, "(*dump.pCustomString)&test"},
   198  		{tce, "(dump.customError)0"},
   199  		{&tce, "(*dump.customError)0"},
   200  		{tpce, "(dump.pCustomError)0"},
   201  		{&tpce, "(*dump.pCustomError)0"},
   202  		{
   203  			struct {
   204  				arr   [1]string
   205  				slice []string
   206  				m     map[string]int
   207  			}{
   208  				[1]string{"arr"},
   209  				[]string{"slice"},
   210  				map[string]int{"one": 1},
   211  			},
   212  			"(struct { arr [1]string; slice []string; m map[string]int }){arr:([1]string)[arr] slice:([]string)[slice] m:(map[string]int)map[one:1]}",
   213  		},
   214  		{teb, "(dump.embed){s:(string)test}"},
   215  		{tebw, "(dump.embedwrap){embed:(dump.embed){s:(string)test} e:(*dump.embed){s:(string)test}}"},
   216  		{map[string]int{}, "(map[string]int)map[]"},
   217  		{map[string]int{"one": 1, "two": 2}, "(map[string]int)map[one:1 two:2]"},
   218  		{map[string]interface{}{"one": 1}, "(map[string]interface {})map[one:(int)1]"},
   219  		{map[string]customString{"key": tcs}, "(map[string]dump.customString)map[key:test]"},
   220  		{map[string]customError{"key": tce}, "(map[string]dump.customError)map[key:0]"},
   221  		{map[string]embed{"key": teb}, "(map[string]dump.embed)map[key:{s:(string)test}]"},
   222  		{map[string]embedwrap{"key": tebw}, "(map[string]dump.embedwrap)map[key:{embed:(dump.embed){s:(string)test} e:(*dump.embed){s:(string)test}}]"},
   223  	}
   224  
   225  	for i, tc := range testCases {
   226  		s := ForHash(tc.a)
   227  		if tc.want != s {
   228  			t.Errorf("[%d]:\n\texpected %q\n\tgot      %q", i, tc.want, s)
   229  		}
   230  	}
   231  }
   232  
   233  func TestOneLine(t *testing.T) {
   234  	tcs := customString("test")
   235  	tpcs := pCustomString("&test")
   236  
   237  	tce := customError(0)
   238  	tpce := pCustomError(0)
   239  
   240  	teb := embed{"test"}
   241  	tebw := embedwrap{teb, &teb}
   242  
   243  	testCases := []struct {
   244  		a    interface{}
   245  		want string
   246  	}{
   247  		{int8(93), "(int8)93"},
   248  		{int16(93), "(int16)93"},
   249  		{int32(93), "(int32)93"},
   250  		{int64(93), "(int64)93"},
   251  		{int(-93), "(int)-93"},
   252  		{int8(-93), "(int8)-93"},
   253  		{int16(-93), "(int16)-93"},
   254  		{int32(-93), "(int32)-93"},
   255  		{int64(-93), "(int64)-93"},
   256  		{uint(93), "(uint)93"},
   257  		{uint8(93), "(uint8)93"},
   258  		{uint16(93), "(uint16)93"},
   259  		{uint32(93), "(uint32)93"},
   260  		{uint64(93), "(uint64)93"},
   261  		{uintptr(93), "(uintptr)0x5d"},
   262  		{ptrint(93), "(*int)93"},
   263  		{float32(93.76), "(float32)93.76"},
   264  		{float64(93.76), "(float64)93.76"},
   265  		{complex64(93i), "(complex64)(0+93i)"},
   266  		{complex128(93i), "(complex128)(0+93i)"},
   267  		{bool(true), "(bool)true"},
   268  		{bool(false), "(bool)false"},
   269  		{string("test"), "(string)test"},
   270  		{ptrstr("test"), "(*string)test"},
   271  		{[1]string{"arr"}, "([1]string)[arr]"},
   272  		{[]string{"slice"}, "([]string)[slice]"},
   273  		{tcs, "(dump.customString)test"},
   274  		{&tcs, "(*dump.customString)test"},
   275  		{tpcs, "(dump.pCustomString)&test"},
   276  		{&tpcs, "(*dump.pCustomString)&test"},
   277  		{tce, "(dump.customError)0"},
   278  		{&tce, "(*dump.customError)0"},
   279  		{tpce, "(dump.pCustomError)0"},
   280  		{&tpce, "(*dump.pCustomError)0"},
   281  		{
   282  			struct {
   283  				arr   [1]string
   284  				slice []string
   285  				m     map[string]int
   286  			}{
   287  				[1]string{"arr"},
   288  				[]string{"slice"},
   289  				map[string]int{"one": 1},
   290  			},
   291  			"(struct { arr [1]string; slice []string; m map[string]int }){arr:([1]string)[arr] slice:([]string)[slice] m:(map[string]int)map[one:1]}",
   292  		},
   293  		{teb, "(dump.embed){s:(string)test}"},
   294  		{tebw, "(dump.embedwrap){embed:(dump.embed){s:(string)test} e:(*dump.embed){s:(string)test}}"},
   295  		{map[string]int{}, "(map[string]int)map[]"},
   296  		{map[string]int{"one": 1}, "(map[string]int)map[one:1]"},
   297  		{map[string]interface{}{"one": 1}, "(map[string]interface {})map[one:(int)1]"},
   298  		{map[string]customString{"key": tcs}, "(map[string]dump.customString)map[key:test]"},
   299  		{map[string]customError{"key": tce}, "(map[string]dump.customError)map[key:0]"},
   300  		{map[string]embed{"key": teb}, "(map[string]dump.embed)map[key:{s:(string)test}]"},
   301  		{map[string]embedwrap{"key": tebw}, "(map[string]dump.embedwrap)map[key:{embed:(dump.embed){s:(string)test} e:(*dump.embed){s:(string)test}}]"},
   302  	}
   303  
   304  	for i, tc := range testCases {
   305  		s := OneLine(tc.a)
   306  		if tc.want != s {
   307  			t.Errorf("[%d]:\n\texpected %q\n\tgot      %q", i, tc.want, s)
   308  		}
   309  	}
   310  }
   311  

View as plain text