github.com/lingyao2333/mo-zero@v1.4.1/core/mapping/yamlunmarshaler_test.go (about)

     1  package mapping
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"k8s.io/utils/io"
    10  )
    11  
    12  func TestUnmarshalYamlBytes(t *testing.T) {
    13  	var c struct {
    14  		Name string
    15  	}
    16  	content := []byte(`Name: liao`)
    17  
    18  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
    19  	assert.Equal(t, "liao", c.Name)
    20  }
    21  
    22  func TestUnmarshalYamlBytesErrorInput(t *testing.T) {
    23  	var c struct {
    24  		Name string
    25  	}
    26  	content := []byte(`liao`)
    27  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
    28  }
    29  
    30  func TestUnmarshalYamlBytesEmptyInput(t *testing.T) {
    31  	var c struct {
    32  		Name string
    33  	}
    34  	content := []byte(``)
    35  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
    36  }
    37  
    38  func TestUnmarshalYamlBytesOptional(t *testing.T) {
    39  	var c struct {
    40  		Name string
    41  		Age  int `json:",optional"`
    42  	}
    43  	content := []byte(`Name: liao`)
    44  
    45  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
    46  	assert.Equal(t, "liao", c.Name)
    47  }
    48  
    49  func TestUnmarshalYamlBytesOptionalDefault(t *testing.T) {
    50  	var c struct {
    51  		Name string
    52  		Age  int `json:",optional,default=1"`
    53  	}
    54  	content := []byte(`Name: liao`)
    55  
    56  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
    57  	assert.Equal(t, "liao", c.Name)
    58  	assert.Equal(t, 1, c.Age)
    59  }
    60  
    61  func TestUnmarshalYamlBytesDefaultOptional(t *testing.T) {
    62  	var c struct {
    63  		Name string
    64  		Age  int `json:",default=1,optional"`
    65  	}
    66  	content := []byte(`Name: liao`)
    67  
    68  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
    69  	assert.Equal(t, "liao", c.Name)
    70  	assert.Equal(t, 1, c.Age)
    71  }
    72  
    73  func TestUnmarshalYamlBytesDefault(t *testing.T) {
    74  	var c struct {
    75  		Name string `json:",default=liao"`
    76  	}
    77  	content := []byte(`{}`)
    78  
    79  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
    80  	assert.Equal(t, "liao", c.Name)
    81  }
    82  
    83  func TestUnmarshalYamlBytesBool(t *testing.T) {
    84  	var c struct {
    85  		Great bool
    86  	}
    87  	content := []byte(`Great: true`)
    88  
    89  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
    90  	assert.True(t, c.Great)
    91  }
    92  
    93  func TestUnmarshalYamlBytesInt(t *testing.T) {
    94  	var c struct {
    95  		Age int
    96  	}
    97  	content := []byte(`Age: 1`)
    98  
    99  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   100  	assert.Equal(t, 1, c.Age)
   101  }
   102  
   103  func TestUnmarshalYamlBytesUint(t *testing.T) {
   104  	var c struct {
   105  		Age uint
   106  	}
   107  	content := []byte(`Age: 1`)
   108  
   109  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   110  	assert.Equal(t, uint(1), c.Age)
   111  }
   112  
   113  func TestUnmarshalYamlBytesFloat(t *testing.T) {
   114  	var c struct {
   115  		Age float32
   116  	}
   117  	content := []byte(`Age: 1.5`)
   118  
   119  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   120  	assert.Equal(t, float32(1.5), c.Age)
   121  }
   122  
   123  func TestUnmarshalYamlBytesMustInOptional(t *testing.T) {
   124  	var c struct {
   125  		Inner struct {
   126  			There    string
   127  			Must     string
   128  			Optional string `json:",optional"`
   129  		} `json:",optional"`
   130  	}
   131  	content := []byte(`{}`)
   132  
   133  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   134  }
   135  
   136  func TestUnmarshalYamlBytesMustInOptionalMissedPart(t *testing.T) {
   137  	var c struct {
   138  		Inner struct {
   139  			There    string
   140  			Must     string
   141  			Optional string `json:",optional"`
   142  		} `json:",optional"`
   143  	}
   144  	content := []byte(`Inner:
   145    There: sure`)
   146  
   147  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   148  }
   149  
   150  func TestUnmarshalYamlBytesMustInOptionalOnlyOptionalFilled(t *testing.T) {
   151  	var c struct {
   152  		Inner struct {
   153  			There    string
   154  			Must     string
   155  			Optional string `json:",optional"`
   156  		} `json:",optional"`
   157  	}
   158  	content := []byte(`Inner:
   159    Optional: sure`)
   160  
   161  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   162  }
   163  
   164  func TestUnmarshalYamlBytesPartial(t *testing.T) {
   165  	var c struct {
   166  		Name string
   167  		Age  float32
   168  	}
   169  	content := []byte(`Age: 1.5`)
   170  
   171  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   172  }
   173  
   174  func TestUnmarshalYamlBytesStruct(t *testing.T) {
   175  	var c struct {
   176  		Inner struct {
   177  			Name string
   178  		}
   179  	}
   180  	content := []byte(`Inner:
   181    Name: liao`)
   182  
   183  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   184  	assert.Equal(t, "liao", c.Inner.Name)
   185  }
   186  
   187  func TestUnmarshalYamlBytesStructOptional(t *testing.T) {
   188  	var c struct {
   189  		Inner struct {
   190  			Name string
   191  			Age  int `json:",optional"`
   192  		}
   193  	}
   194  	content := []byte(`Inner:
   195    Name: liao`)
   196  
   197  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   198  	assert.Equal(t, "liao", c.Inner.Name)
   199  }
   200  
   201  func TestUnmarshalYamlBytesStructPtr(t *testing.T) {
   202  	var c struct {
   203  		Inner *struct {
   204  			Name string
   205  		}
   206  	}
   207  	content := []byte(`Inner:
   208    Name: liao`)
   209  
   210  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   211  	assert.Equal(t, "liao", c.Inner.Name)
   212  }
   213  
   214  func TestUnmarshalYamlBytesStructPtrOptional(t *testing.T) {
   215  	var c struct {
   216  		Inner *struct {
   217  			Name string
   218  			Age  int `json:",optional"`
   219  		}
   220  	}
   221  	content := []byte(`Inner:
   222    Name: liao`)
   223  
   224  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   225  }
   226  
   227  func TestUnmarshalYamlBytesStructPtrDefault(t *testing.T) {
   228  	var c struct {
   229  		Inner *struct {
   230  			Name string
   231  			Age  int `json:",default=4"`
   232  		}
   233  	}
   234  	content := []byte(`Inner:
   235    Name: liao`)
   236  
   237  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   238  	assert.Equal(t, "liao", c.Inner.Name)
   239  	assert.Equal(t, 4, c.Inner.Age)
   240  }
   241  
   242  func TestUnmarshalYamlBytesSliceString(t *testing.T) {
   243  	var c struct {
   244  		Names []string
   245  	}
   246  	content := []byte(`Names:
   247  - liao
   248  - chaoxin`)
   249  
   250  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   251  
   252  	want := []string{"liao", "chaoxin"}
   253  	if !reflect.DeepEqual(c.Names, want) {
   254  		t.Fatalf("want %q, got %q", c.Names, want)
   255  	}
   256  }
   257  
   258  func TestUnmarshalYamlBytesSliceStringOptional(t *testing.T) {
   259  	var c struct {
   260  		Names []string
   261  		Age   []int `json:",optional"`
   262  	}
   263  	content := []byte(`Names:
   264  - liao
   265  - chaoxin`)
   266  
   267  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   268  
   269  	want := []string{"liao", "chaoxin"}
   270  	if !reflect.DeepEqual(c.Names, want) {
   271  		t.Fatalf("want %q, got %q", c.Names, want)
   272  	}
   273  }
   274  
   275  func TestUnmarshalYamlBytesSliceStruct(t *testing.T) {
   276  	var c struct {
   277  		People []struct {
   278  			Name string
   279  			Age  int
   280  		}
   281  	}
   282  	content := []byte(`People:
   283  - Name: liao
   284    Age: 1
   285  - Name: chaoxin
   286    Age: 2`)
   287  
   288  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   289  
   290  	want := []struct {
   291  		Name string
   292  		Age  int
   293  	}{
   294  		{"liao", 1},
   295  		{"chaoxin", 2},
   296  	}
   297  	if !reflect.DeepEqual(c.People, want) {
   298  		t.Fatalf("want %q, got %q", c.People, want)
   299  	}
   300  }
   301  
   302  func TestUnmarshalYamlBytesSliceStructOptional(t *testing.T) {
   303  	var c struct {
   304  		People []struct {
   305  			Name   string
   306  			Age    int
   307  			Emails []string `json:",optional"`
   308  		}
   309  	}
   310  	content := []byte(`People:
   311  - Name: liao
   312    Age: 1
   313  - Name: chaoxin
   314    Age: 2`)
   315  
   316  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   317  
   318  	want := []struct {
   319  		Name   string
   320  		Age    int
   321  		Emails []string `json:",optional"`
   322  	}{
   323  		{"liao", 1, nil},
   324  		{"chaoxin", 2, nil},
   325  	}
   326  	if !reflect.DeepEqual(c.People, want) {
   327  		t.Fatalf("want %q, got %q", c.People, want)
   328  	}
   329  }
   330  
   331  func TestUnmarshalYamlBytesSliceStructPtr(t *testing.T) {
   332  	var c struct {
   333  		People []*struct {
   334  			Name string
   335  			Age  int
   336  		}
   337  	}
   338  	content := []byte(`People:
   339  - Name: liao
   340    Age: 1
   341  - Name: chaoxin
   342    Age: 2`)
   343  
   344  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   345  
   346  	want := []*struct {
   347  		Name string
   348  		Age  int
   349  	}{
   350  		{"liao", 1},
   351  		{"chaoxin", 2},
   352  	}
   353  	if !reflect.DeepEqual(c.People, want) {
   354  		t.Fatalf("want %v, got %v", c.People, want)
   355  	}
   356  }
   357  
   358  func TestUnmarshalYamlBytesSliceStructPtrOptional(t *testing.T) {
   359  	var c struct {
   360  		People []*struct {
   361  			Name   string
   362  			Age    int
   363  			Emails []string `json:",optional"`
   364  		}
   365  	}
   366  	content := []byte(`People:
   367  - Name: liao
   368    Age: 1
   369  - Name: chaoxin
   370    Age: 2`)
   371  
   372  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   373  
   374  	want := []*struct {
   375  		Name   string
   376  		Age    int
   377  		Emails []string `json:",optional"`
   378  	}{
   379  		{"liao", 1, nil},
   380  		{"chaoxin", 2, nil},
   381  	}
   382  	if !reflect.DeepEqual(c.People, want) {
   383  		t.Fatalf("want %v, got %v", c.People, want)
   384  	}
   385  }
   386  
   387  func TestUnmarshalYamlBytesSliceStructPtrPartial(t *testing.T) {
   388  	var c struct {
   389  		People []*struct {
   390  			Name  string
   391  			Age   int
   392  			Email string
   393  		}
   394  	}
   395  	content := []byte(`People:
   396  - Name: liao
   397    Age: 1
   398  - Name: chaoxin
   399    Age: 2`)
   400  
   401  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   402  }
   403  
   404  func TestUnmarshalYamlBytesSliceStructPtrDefault(t *testing.T) {
   405  	var c struct {
   406  		People []*struct {
   407  			Name  string
   408  			Age   int
   409  			Email string `json:",default=chaoxin@liao.com"`
   410  		}
   411  	}
   412  	content := []byte(`People:
   413  - Name: liao
   414    Age: 1
   415  - Name: chaoxin
   416    Age: 2`)
   417  
   418  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   419  
   420  	want := []*struct {
   421  		Name  string
   422  		Age   int
   423  		Email string
   424  	}{
   425  		{"liao", 1, "chaoxin@liao.com"},
   426  		{"chaoxin", 2, "chaoxin@liao.com"},
   427  	}
   428  
   429  	for i := range c.People {
   430  		actual := c.People[i]
   431  		expect := want[i]
   432  		assert.Equal(t, expect.Age, actual.Age)
   433  		assert.Equal(t, expect.Email, actual.Email)
   434  		assert.Equal(t, expect.Name, actual.Name)
   435  	}
   436  }
   437  
   438  func TestUnmarshalYamlBytesSliceStringPartial(t *testing.T) {
   439  	var c struct {
   440  		Names []string
   441  		Age   int
   442  	}
   443  	content := []byte(`Age: 1`)
   444  
   445  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   446  }
   447  
   448  func TestUnmarshalYamlBytesSliceStructPartial(t *testing.T) {
   449  	var c struct {
   450  		Group  string
   451  		People []struct {
   452  			Name string
   453  			Age  int
   454  		}
   455  	}
   456  	content := []byte(`Group: chaoxin`)
   457  
   458  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   459  }
   460  
   461  func TestUnmarshalYamlBytesInnerAnonymousPartial(t *testing.T) {
   462  	type (
   463  		Deep struct {
   464  			A string
   465  			B string `json:",optional"`
   466  		}
   467  		Inner struct {
   468  			Deep
   469  			InnerV string `json:",optional"`
   470  		}
   471  	)
   472  
   473  	var c struct {
   474  		Value Inner `json:",optional"`
   475  	}
   476  	content := []byte(`Value:
   477    InnerV: chaoxin`)
   478  
   479  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   480  }
   481  
   482  func TestUnmarshalYamlBytesStructPartial(t *testing.T) {
   483  	var c struct {
   484  		Group  string
   485  		Person struct {
   486  			Name string
   487  			Age  int
   488  		}
   489  	}
   490  	content := []byte(`Group: chaoxin`)
   491  
   492  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   493  }
   494  
   495  func TestUnmarshalYamlBytesEmptyMap(t *testing.T) {
   496  	var c struct {
   497  		Persons map[string]int `json:",optional"`
   498  	}
   499  	content := []byte(`{}`)
   500  
   501  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   502  	assert.Empty(t, c.Persons)
   503  }
   504  
   505  func TestUnmarshalYamlBytesMap(t *testing.T) {
   506  	var c struct {
   507  		Persons map[string]int
   508  	}
   509  	content := []byte(`Persons:
   510    first: 1
   511    second: 2`)
   512  
   513  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   514  	assert.Equal(t, 2, len(c.Persons))
   515  	assert.Equal(t, 1, c.Persons["first"])
   516  	assert.Equal(t, 2, c.Persons["second"])
   517  }
   518  
   519  func TestUnmarshalYamlBytesMapStruct(t *testing.T) {
   520  	var c struct {
   521  		Persons map[string]struct {
   522  			ID   int
   523  			Name string `json:"name,optional"`
   524  		}
   525  	}
   526  	content := []byte(`Persons:
   527    first:
   528      ID: 1
   529      name: kevin`)
   530  
   531  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   532  	assert.Equal(t, 1, len(c.Persons))
   533  	assert.Equal(t, 1, c.Persons["first"].ID)
   534  	assert.Equal(t, "kevin", c.Persons["first"].Name)
   535  }
   536  
   537  func TestUnmarshalYamlBytesMapStructPtr(t *testing.T) {
   538  	var c struct {
   539  		Persons map[string]*struct {
   540  			ID   int
   541  			Name string `json:"name,optional"`
   542  		}
   543  	}
   544  	content := []byte(`Persons:
   545    first:
   546      ID: 1
   547      name: kevin`)
   548  
   549  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   550  	assert.Equal(t, 1, len(c.Persons))
   551  	assert.Equal(t, 1, c.Persons["first"].ID)
   552  	assert.Equal(t, "kevin", c.Persons["first"].Name)
   553  }
   554  
   555  func TestUnmarshalYamlBytesMapStructMissingPartial(t *testing.T) {
   556  	var c struct {
   557  		Persons map[string]*struct {
   558  			ID   int
   559  			Name string
   560  		}
   561  	}
   562  	content := []byte(`Persons:
   563    first:
   564      ID: 1`)
   565  
   566  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   567  }
   568  
   569  func TestUnmarshalYamlBytesMapStructOptional(t *testing.T) {
   570  	var c struct {
   571  		Persons map[string]*struct {
   572  			ID   int
   573  			Name string `json:"name,optional"`
   574  		}
   575  	}
   576  	content := []byte(`Persons:
   577    first:
   578      ID: 1`)
   579  
   580  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   581  	assert.Equal(t, 1, len(c.Persons))
   582  	assert.Equal(t, 1, c.Persons["first"].ID)
   583  }
   584  
   585  func TestUnmarshalYamlBytesMapStructSlice(t *testing.T) {
   586  	var c struct {
   587  		Persons map[string][]struct {
   588  			ID   int
   589  			Name string `json:"name,optional"`
   590  		}
   591  	}
   592  	content := []byte(`Persons:
   593    first:
   594    - ID: 1
   595      name: kevin`)
   596  
   597  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   598  	assert.Equal(t, 1, len(c.Persons))
   599  	assert.Equal(t, 1, c.Persons["first"][0].ID)
   600  	assert.Equal(t, "kevin", c.Persons["first"][0].Name)
   601  }
   602  
   603  func TestUnmarshalYamlBytesMapEmptyStructSlice(t *testing.T) {
   604  	var c struct {
   605  		Persons map[string][]struct {
   606  			ID   int
   607  			Name string `json:"name,optional"`
   608  		}
   609  	}
   610  	content := []byte(`Persons:
   611    first: []`)
   612  
   613  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   614  	assert.Equal(t, 1, len(c.Persons))
   615  	assert.Empty(t, c.Persons["first"])
   616  }
   617  
   618  func TestUnmarshalYamlBytesMapStructPtrSlice(t *testing.T) {
   619  	var c struct {
   620  		Persons map[string][]*struct {
   621  			ID   int
   622  			Name string `json:"name,optional"`
   623  		}
   624  	}
   625  	content := []byte(`Persons:
   626    first:
   627    - ID: 1
   628      name: kevin`)
   629  
   630  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   631  	assert.Equal(t, 1, len(c.Persons))
   632  	assert.Equal(t, 1, c.Persons["first"][0].ID)
   633  	assert.Equal(t, "kevin", c.Persons["first"][0].Name)
   634  }
   635  
   636  func TestUnmarshalYamlBytesMapEmptyStructPtrSlice(t *testing.T) {
   637  	var c struct {
   638  		Persons map[string][]*struct {
   639  			ID   int
   640  			Name string `json:"name,optional"`
   641  		}
   642  	}
   643  	content := []byte(`Persons:
   644    first: []`)
   645  
   646  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   647  	assert.Equal(t, 1, len(c.Persons))
   648  	assert.Empty(t, c.Persons["first"])
   649  }
   650  
   651  func TestUnmarshalYamlBytesMapStructPtrSliceMissingPartial(t *testing.T) {
   652  	var c struct {
   653  		Persons map[string][]*struct {
   654  			ID   int
   655  			Name string
   656  		}
   657  	}
   658  	content := []byte(`Persons:
   659    first:
   660    - ID: 1`)
   661  
   662  	assert.NotNil(t, UnmarshalYamlBytes(content, &c))
   663  }
   664  
   665  func TestUnmarshalYamlBytesMapStructPtrSliceOptional(t *testing.T) {
   666  	var c struct {
   667  		Persons map[string][]*struct {
   668  			ID   int
   669  			Name string `json:"name,optional"`
   670  		}
   671  	}
   672  	content := []byte(`Persons:
   673    first:
   674    - ID: 1`)
   675  
   676  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   677  	assert.Equal(t, 1, len(c.Persons))
   678  	assert.Equal(t, 1, c.Persons["first"][0].ID)
   679  }
   680  
   681  func TestUnmarshalYamlStructOptional(t *testing.T) {
   682  	var c struct {
   683  		Name string
   684  		Etcd struct {
   685  			Hosts []string
   686  			Key   string
   687  		} `json:",optional"`
   688  	}
   689  	content := []byte(`Name: kevin`)
   690  
   691  	err := UnmarshalYamlBytes(content, &c)
   692  	assert.Nil(t, err)
   693  	assert.Equal(t, "kevin", c.Name)
   694  }
   695  
   696  func TestUnmarshalYamlStructLowerCase(t *testing.T) {
   697  	var c struct {
   698  		Name string
   699  		Etcd struct {
   700  			Key string
   701  		} `json:"etcd"`
   702  	}
   703  	content := []byte(`Name: kevin
   704  etcd:
   705    Key: the key`)
   706  
   707  	err := UnmarshalYamlBytes(content, &c)
   708  	assert.Nil(t, err)
   709  	assert.Equal(t, "kevin", c.Name)
   710  	assert.Equal(t, "the key", c.Etcd.Key)
   711  }
   712  
   713  func TestUnmarshalYamlWithStructAllOptionalWithEmpty(t *testing.T) {
   714  	var c struct {
   715  		Inner struct {
   716  			Optional string `json:",optional"`
   717  		}
   718  		Else string
   719  	}
   720  	content := []byte(`Else: sure`)
   721  
   722  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   723  }
   724  
   725  func TestUnmarshalYamlWithStructAllOptionalPtr(t *testing.T) {
   726  	var c struct {
   727  		Inner *struct {
   728  			Optional string `json:",optional"`
   729  		}
   730  		Else string
   731  	}
   732  	content := []byte(`Else: sure`)
   733  
   734  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   735  }
   736  
   737  func TestUnmarshalYamlWithStructOptional(t *testing.T) {
   738  	type Inner struct {
   739  		Must string
   740  	}
   741  
   742  	var c struct {
   743  		In   Inner `json:",optional"`
   744  		Else string
   745  	}
   746  	content := []byte(`Else: sure`)
   747  
   748  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   749  	assert.Equal(t, "sure", c.Else)
   750  	assert.Equal(t, "", c.In.Must)
   751  }
   752  
   753  func TestUnmarshalYamlWithStructPtrOptional(t *testing.T) {
   754  	type Inner struct {
   755  		Must string
   756  	}
   757  
   758  	var c struct {
   759  		In   *Inner `json:",optional"`
   760  		Else string
   761  	}
   762  	content := []byte(`Else: sure`)
   763  
   764  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   765  	assert.Equal(t, "sure", c.Else)
   766  	assert.Nil(t, c.In)
   767  }
   768  
   769  func TestUnmarshalYamlWithStructAllOptionalAnonymous(t *testing.T) {
   770  	type Inner struct {
   771  		Optional string `json:",optional"`
   772  	}
   773  
   774  	var c struct {
   775  		Inner
   776  		Else string
   777  	}
   778  	content := []byte(`Else: sure`)
   779  
   780  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   781  }
   782  
   783  func TestUnmarshalYamlWithStructAllOptionalAnonymousPtr(t *testing.T) {
   784  	type Inner struct {
   785  		Optional string `json:",optional"`
   786  	}
   787  
   788  	var c struct {
   789  		*Inner
   790  		Else string
   791  	}
   792  	content := []byte(`Else: sure`)
   793  
   794  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   795  }
   796  
   797  func TestUnmarshalYamlWithStructAllOptionalProvoidedAnonymous(t *testing.T) {
   798  	type Inner struct {
   799  		Optional string `json:",optional"`
   800  	}
   801  
   802  	var c struct {
   803  		Inner
   804  		Else string
   805  	}
   806  	content := []byte(`Else: sure
   807  Optional: optional`)
   808  
   809  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   810  	assert.Equal(t, "sure", c.Else)
   811  	assert.Equal(t, "optional", c.Optional)
   812  }
   813  
   814  func TestUnmarshalYamlWithStructAllOptionalProvoidedAnonymousPtr(t *testing.T) {
   815  	type Inner struct {
   816  		Optional string `json:",optional"`
   817  	}
   818  
   819  	var c struct {
   820  		*Inner
   821  		Else string
   822  	}
   823  	content := []byte(`Else: sure
   824  Optional: optional`)
   825  
   826  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   827  	assert.Equal(t, "sure", c.Else)
   828  	assert.Equal(t, "optional", c.Optional)
   829  }
   830  
   831  func TestUnmarshalYamlWithStructAnonymous(t *testing.T) {
   832  	type Inner struct {
   833  		Must string
   834  	}
   835  
   836  	var c struct {
   837  		Inner
   838  		Else string
   839  	}
   840  	content := []byte(`Else: sure
   841  Must: must`)
   842  
   843  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   844  	assert.Equal(t, "sure", c.Else)
   845  	assert.Equal(t, "must", c.Must)
   846  }
   847  
   848  func TestUnmarshalYamlWithStructAnonymousPtr(t *testing.T) {
   849  	type Inner struct {
   850  		Must string
   851  	}
   852  
   853  	var c struct {
   854  		*Inner
   855  		Else string
   856  	}
   857  	content := []byte(`Else: sure
   858  Must: must`)
   859  
   860  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   861  	assert.Equal(t, "sure", c.Else)
   862  	assert.Equal(t, "must", c.Must)
   863  }
   864  
   865  func TestUnmarshalYamlWithStructAnonymousOptional(t *testing.T) {
   866  	type Inner struct {
   867  		Must string
   868  	}
   869  
   870  	var c struct {
   871  		Inner `json:",optional"`
   872  		Else  string
   873  	}
   874  	content := []byte(`Else: sure`)
   875  
   876  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   877  	assert.Equal(t, "sure", c.Else)
   878  	assert.Equal(t, "", c.Must)
   879  }
   880  
   881  func TestUnmarshalYamlWithStructPtrAnonymousOptional(t *testing.T) {
   882  	type Inner struct {
   883  		Must string
   884  	}
   885  
   886  	var c struct {
   887  		*Inner `json:",optional"`
   888  		Else   string
   889  	}
   890  	content := []byte(`Else: sure`)
   891  
   892  	assert.Nil(t, UnmarshalYamlBytes(content, &c))
   893  	assert.Equal(t, "sure", c.Else)
   894  	assert.Nil(t, c.Inner)
   895  }
   896  
   897  func TestUnmarshalYamlWithZeroValues(t *testing.T) {
   898  	type inner struct {
   899  		False  bool   `json:"negative"`
   900  		Int    int    `json:"int"`
   901  		String string `json:"string"`
   902  	}
   903  	content := []byte(`negative: false
   904  int: 0
   905  string: ""`)
   906  
   907  	var in inner
   908  	ast := assert.New(t)
   909  	ast.Nil(UnmarshalYamlBytes(content, &in))
   910  	ast.False(in.False)
   911  	ast.Equal(0, in.Int)
   912  	ast.Equal("", in.String)
   913  }
   914  
   915  func TestUnmarshalYamlBytesError(t *testing.T) {
   916  	payload := `abcd:
   917  - cdef`
   918  	var v struct {
   919  		Any []string `json:"abcd"`
   920  	}
   921  
   922  	err := UnmarshalYamlBytes([]byte(payload), &v)
   923  	assert.Nil(t, err)
   924  	assert.Equal(t, 1, len(v.Any))
   925  	assert.Equal(t, "cdef", v.Any[0])
   926  }
   927  
   928  func TestUnmarshalYamlReaderError(t *testing.T) {
   929  	var v struct {
   930  		Any string
   931  	}
   932  
   933  	reader := strings.NewReader(`abcd: cdef`)
   934  	err := UnmarshalYamlReader(reader, &v)
   935  	assert.NotNil(t, err)
   936  
   937  	reader = strings.NewReader("chenquan")
   938  	err = UnmarshalYamlReader(reader, &v)
   939  	assert.ErrorIs(t, err, ErrUnsupportedType)
   940  }
   941  
   942  func TestUnmarshalYamlBadReader(t *testing.T) {
   943  	var v struct {
   944  		Any string
   945  	}
   946  
   947  	err := UnmarshalYamlReader(new(badReader), &v)
   948  	assert.NotNil(t, err)
   949  }
   950  
   951  func TestUnmarshalYamlMapBool(t *testing.T) {
   952  	text := `machine:
   953    node1: true
   954    node2: true
   955    node3: true
   956  `
   957  	var v struct {
   958  		Machine map[string]bool `json:"machine,optional"`
   959  	}
   960  	reader := strings.NewReader(text)
   961  	assert.Nil(t, UnmarshalYamlReader(reader, &v))
   962  	assert.True(t, v.Machine["node1"])
   963  	assert.True(t, v.Machine["node2"])
   964  	assert.True(t, v.Machine["node3"])
   965  }
   966  
   967  func TestUnmarshalYamlMapInt(t *testing.T) {
   968  	text := `machine:
   969    node1: 1
   970    node2: 2
   971    node3: 3
   972  `
   973  	var v struct {
   974  		Machine map[string]int `json:"machine,optional"`
   975  	}
   976  	reader := strings.NewReader(text)
   977  	assert.Nil(t, UnmarshalYamlReader(reader, &v))
   978  	assert.Equal(t, 1, v.Machine["node1"])
   979  	assert.Equal(t, 2, v.Machine["node2"])
   980  	assert.Equal(t, 3, v.Machine["node3"])
   981  }
   982  
   983  func TestUnmarshalYamlMapByte(t *testing.T) {
   984  	text := `machine:
   985    node1: 1
   986    node2: 2
   987    node3: 3
   988  `
   989  	var v struct {
   990  		Machine map[string]byte `json:"machine,optional"`
   991  	}
   992  	reader := strings.NewReader(text)
   993  	assert.Nil(t, UnmarshalYamlReader(reader, &v))
   994  	assert.Equal(t, byte(1), v.Machine["node1"])
   995  	assert.Equal(t, byte(2), v.Machine["node2"])
   996  	assert.Equal(t, byte(3), v.Machine["node3"])
   997  }
   998  
   999  func TestUnmarshalYamlMapRune(t *testing.T) {
  1000  	text := `machine:
  1001    node1: 1
  1002    node2: 2
  1003    node3: 3
  1004  `
  1005  	var v struct {
  1006  		Machine map[string]rune `json:"machine,optional"`
  1007  	}
  1008  	reader := strings.NewReader(text)
  1009  	assert.Nil(t, UnmarshalYamlReader(reader, &v))
  1010  	assert.Equal(t, rune(1), v.Machine["node1"])
  1011  	assert.Equal(t, rune(2), v.Machine["node2"])
  1012  	assert.Equal(t, rune(3), v.Machine["node3"])
  1013  }
  1014  
  1015  type badReader struct{}
  1016  
  1017  func (b *badReader) Read(_ []byte) (n int, err error) {
  1018  	return 0, io.ErrLimitReached
  1019  }