tagkey_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. package json
  5. import (
  6. "testing"
  7. )
  8. type basicLatin2xTag struct {
  9. V string `json:"$%-/"`
  10. }
  11. type basicLatin3xTag struct {
  12. V string `json:"0123456789"`
  13. }
  14. type basicLatin4xTag struct {
  15. V string `json:"ABCDEFGHIJKLMO"`
  16. }
  17. type basicLatin5xTag struct {
  18. V string `json:"PQRSTUVWXYZ_"`
  19. }
  20. type basicLatin6xTag struct {
  21. V string `json:"abcdefghijklmno"`
  22. }
  23. type basicLatin7xTag struct {
  24. V string `json:"pqrstuvwxyz"`
  25. }
  26. type miscPlaneTag struct {
  27. V string `json:"色は匂へど"`
  28. }
  29. type percentSlashTag struct {
  30. V string `json:"text/html%"` // http://golang.org/issue/2718
  31. }
  32. type punctuationTag struct {
  33. V string `json:"!#$%&()*+-./:<=>?@[]^_{|}~"` // http://golang.org/issue/3546
  34. }
  35. type emptyTag struct {
  36. W string
  37. }
  38. type misnamedTag struct {
  39. X string `jsom:"Misnamed"`
  40. }
  41. type badFormatTag struct {
  42. Y string `:"BadFormat"`
  43. }
  44. type badCodeTag struct {
  45. Z string `json:" !\"#&'()*+,."`
  46. }
  47. type spaceTag struct {
  48. Q string `json:"With space"`
  49. }
  50. type unicodeTag struct {
  51. W string `json:"Ελλάδα"`
  52. }
  53. var structTagObjectKeyTests = []struct {
  54. raw interface{}
  55. value string
  56. key string
  57. }{
  58. {basicLatin2xTag{"2x"}, "2x", "$%-/"},
  59. {basicLatin3xTag{"3x"}, "3x", "0123456789"},
  60. {basicLatin4xTag{"4x"}, "4x", "ABCDEFGHIJKLMO"},
  61. {basicLatin5xTag{"5x"}, "5x", "PQRSTUVWXYZ_"},
  62. {basicLatin6xTag{"6x"}, "6x", "abcdefghijklmno"},
  63. {basicLatin7xTag{"7x"}, "7x", "pqrstuvwxyz"},
  64. {miscPlaneTag{"いろはにほへと"}, "いろはにほへと", "色は匂へど"},
  65. {emptyTag{"Pour Moi"}, "Pour Moi", "W"},
  66. {misnamedTag{"Animal Kingdom"}, "Animal Kingdom", "X"},
  67. {badFormatTag{"Orfevre"}, "Orfevre", "Y"},
  68. {badCodeTag{"Reliable Man"}, "Reliable Man", "Z"},
  69. {percentSlashTag{"brut"}, "brut", "text/html%"},
  70. {punctuationTag{"Union Rags"}, "Union Rags", "!#$%&()*+-./:<=>?@[]^_{|}~"},
  71. {spaceTag{"Perreddu"}, "Perreddu", "With space"},
  72. {unicodeTag{"Loukanikos"}, "Loukanikos", "Ελλάδα"},
  73. }
  74. func TestStructTagObjectKey(t *testing.T) {
  75. for _, tt := range structTagObjectKeyTests {
  76. b, err := Marshal(tt.raw)
  77. if err != nil {
  78. t.Fatalf("Marshal(%#q) failed: %v", tt.raw, err)
  79. }
  80. var f interface{}
  81. err = Unmarshal(b, &f)
  82. if err != nil {
  83. t.Fatalf("Unmarshal(%#q) failed: %v", b, err)
  84. }
  85. for i, v := range f.(map[string]interface{}) {
  86. switch i {
  87. case tt.key:
  88. if s, ok := v.(string); !ok || s != tt.value {
  89. t.Fatalf("Unexpected value: %#q, want %v", s, tt.value)
  90. }
  91. default:
  92. t.Fatalf("Unexpected key: %#q, from %#q", i, b)
  93. }
  94. }
  95. }
  96. }