Source file src/encoding/json/tagkey_test.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !goexperiment.jsonv2
     6  
     7  package json
     8  
     9  import (
    10  	"testing"
    11  )
    12  
    13  type basicLatin2xTag struct {
    14  	V string `json:"$%-/"`
    15  }
    16  
    17  type basicLatin3xTag struct {
    18  	V string `json:"0123456789"`
    19  }
    20  
    21  type basicLatin4xTag struct {
    22  	V string `json:"ABCDEFGHIJKLMO"`
    23  }
    24  
    25  type basicLatin5xTag struct {
    26  	V string `json:"PQRSTUVWXYZ_"`
    27  }
    28  
    29  type basicLatin6xTag struct {
    30  	V string `json:"abcdefghijklmno"`
    31  }
    32  
    33  type basicLatin7xTag struct {
    34  	V string `json:"pqrstuvwxyz"`
    35  }
    36  
    37  type miscPlaneTag struct {
    38  	V string `json:"色は匂へど"`
    39  }
    40  
    41  type percentSlashTag struct {
    42  	V string `json:"text/html%"` // https://golang.org/issue/2718
    43  }
    44  
    45  type punctuationTag struct {
    46  	V string `json:"!#$%&()*+-./:;<=>?@[]^_{|}~ "` // https://golang.org/issue/3546
    47  }
    48  
    49  type dashTag struct {
    50  	V string `json:"-,"`
    51  }
    52  
    53  type emptyTag struct {
    54  	W string
    55  }
    56  
    57  type misnamedTag struct {
    58  	X string `jsom:"Misnamed"`
    59  }
    60  
    61  type badFormatTag struct {
    62  	Y string `:"BadFormat"`
    63  }
    64  
    65  type badCodeTag struct {
    66  	Z string `json:" !\"#&'()*+,."`
    67  }
    68  
    69  type spaceTag struct {
    70  	Q string `json:"With space"`
    71  }
    72  
    73  type unicodeTag struct {
    74  	W string `json:"Ελλάδα"`
    75  }
    76  
    77  func TestStructTagObjectKey(t *testing.T) {
    78  	tests := []struct {
    79  		CaseName
    80  		raw   any
    81  		value string
    82  		key   string
    83  	}{
    84  		{Name(""), basicLatin2xTag{"2x"}, "2x", "$%-/"},
    85  		{Name(""), basicLatin3xTag{"3x"}, "3x", "0123456789"},
    86  		{Name(""), basicLatin4xTag{"4x"}, "4x", "ABCDEFGHIJKLMO"},
    87  		{Name(""), basicLatin5xTag{"5x"}, "5x", "PQRSTUVWXYZ_"},
    88  		{Name(""), basicLatin6xTag{"6x"}, "6x", "abcdefghijklmno"},
    89  		{Name(""), basicLatin7xTag{"7x"}, "7x", "pqrstuvwxyz"},
    90  		{Name(""), miscPlaneTag{"いろはにほへと"}, "いろはにほへと", "色は匂へど"},
    91  		{Name(""), dashTag{"foo"}, "foo", "-"},
    92  		{Name(""), emptyTag{"Pour Moi"}, "Pour Moi", "W"},
    93  		{Name(""), misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"},
    94  		{Name(""), badFormatTag{"Orfevre"}, "Orfevre", "Y"},
    95  		{Name(""), badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
    96  		{Name(""), percentSlashTag{"brut"}, "brut", "text/html%"},
    97  		{Name(""), punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:;<=>?@[]^_{|}~ "},
    98  		{Name(""), spaceTag{"Perreddu"}, "Perreddu", "With space"},
    99  		{Name(""), unicodeTag{"Loukanikos"}, "Loukanikos", "Ελλάδα"},
   100  	}
   101  	for _, tt := range tests {
   102  		t.Run(tt.Name, func(t *testing.T) {
   103  			b, err := Marshal(tt.raw)
   104  			if err != nil {
   105  				t.Fatalf("%s: Marshal error: %v", tt.Where, err)
   106  			}
   107  			var f any
   108  			err = Unmarshal(b, &f)
   109  			if err != nil {
   110  				t.Fatalf("%s: Unmarshal error: %v", tt.Where, err)
   111  			}
   112  			for k, v := range f.(map[string]any) {
   113  				if k == tt.key {
   114  					if s, ok := v.(string); !ok || s != tt.value {
   115  						t.Fatalf("%s: Unmarshal(%#q) value:\n\tgot:  %q\n\twant: %q", tt.Where, b, s, tt.value)
   116  					}
   117  				} else {
   118  					t.Fatalf("%s: Unmarshal(%#q): unexpected key: %q", tt.Where, b, k)
   119  				}
   120  			}
   121  		})
   122  	}
   123  }
   124  

View as plain text