stringify_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright (c) 2015 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package key
  5. import (
  6. "math"
  7. "testing"
  8. )
  9. func TestStringify(t *testing.T) {
  10. testcases := []struct {
  11. name string
  12. input interface{}
  13. output string // or expected panic error message.
  14. }{{
  15. name: "nil",
  16. input: nil,
  17. output: "Unable to stringify nil",
  18. }, {
  19. name: "struct{}",
  20. input: struct{}{},
  21. output: "Unable to stringify type struct {}: struct {}{}",
  22. }, {
  23. name: "string",
  24. input: "foobar",
  25. output: "foobar",
  26. }, {
  27. name: "valid non-ASCII UTF-8 string",
  28. input: "日本語",
  29. output: "日本語",
  30. }, {
  31. name: "invalid UTF-8 string 1",
  32. input: string([]byte{0xef, 0xbf, 0xbe, 0xbe, 0xbe, 0xbe, 0xbe}),
  33. output: "77++vr6+vg==",
  34. }, {
  35. name: "invalid UTF-8 string 2",
  36. input: string([]byte{0xef, 0xbf, 0xbe, 0xbe, 0xbe, 0xbe, 0xbe, 0x23}),
  37. output: "77++vr6+viM=",
  38. }, {
  39. name: "invalid UTF-8 string 3",
  40. input: string([]byte{0xef, 0xbf, 0xbe, 0xbe, 0xbe, 0xbe, 0xbe, 0x23, 0x24}),
  41. output: "77++vr6+viMk",
  42. }, {
  43. name: "uint8",
  44. input: uint8(43),
  45. output: "43",
  46. }, {
  47. name: "uint16",
  48. input: uint16(43),
  49. output: "43",
  50. }, {
  51. name: "uint32",
  52. input: uint32(43),
  53. output: "43",
  54. }, {
  55. name: "uint64",
  56. input: uint64(43),
  57. output: "43",
  58. }, {
  59. name: "max uint64",
  60. input: uint64(math.MaxUint64),
  61. output: "18446744073709551615",
  62. }, {
  63. name: "int8",
  64. input: int8(-32),
  65. output: "-32",
  66. }, {
  67. name: "int16",
  68. input: int16(-32),
  69. output: "-32",
  70. }, {
  71. name: "int32",
  72. input: int32(-32),
  73. output: "-32",
  74. }, {
  75. name: "int64",
  76. input: int64(-32),
  77. output: "-32",
  78. }, {
  79. name: "true",
  80. input: true,
  81. output: "true",
  82. }, {
  83. name: "false",
  84. input: false,
  85. output: "false",
  86. }, {
  87. name: "float32",
  88. input: float32(2.345),
  89. output: "f1075188859",
  90. }, {
  91. name: "float64",
  92. input: float64(-34.6543),
  93. output: "f-4593298060402564373",
  94. }, {
  95. name: "map[string]interface{}",
  96. input: map[string]interface{}{
  97. "b": uint32(43),
  98. "a": "foobar",
  99. "ex": map[string]interface{}{
  100. "d": "barfoo",
  101. "c": uint32(45),
  102. },
  103. },
  104. output: "foobar_43_45_barfoo",
  105. }, {
  106. name: "map[Key]interface{}",
  107. input: map[Key]interface{}{
  108. New(uint32(42)): true,
  109. New("foo"): "bar",
  110. New(map[string]interface{}{"hello": "world"}): "yolo",
  111. },
  112. output: "42=true_foo=bar_world=yolo",
  113. }, {
  114. name: "nil inside map[string]interface{}",
  115. input: map[string]interface{}{
  116. "n": nil,
  117. },
  118. output: "Unable to stringify nil",
  119. }, {
  120. name: "[]interface{}",
  121. input: []interface{}{
  122. uint32(42),
  123. true,
  124. "foo",
  125. map[Key]interface{}{
  126. New("a"): "b",
  127. New("b"): "c",
  128. },
  129. },
  130. output: "42,true,foo,a=b_b=c",
  131. }}
  132. for _, tcase := range testcases {
  133. // Pardon the contraption used to catch panic's in error cases.
  134. func() {
  135. defer func() {
  136. if e := recover(); e != nil {
  137. if tcase.output != e.(error).Error() {
  138. t.Errorf("Test %s: Error returned: %q but wanted %q",
  139. tcase.name, e, tcase.output)
  140. }
  141. }
  142. }()
  143. result := stringify(tcase.input)
  144. if tcase.output != result {
  145. t.Errorf("Test %s: Result is different\nReceived: %s\nExpected: %s",
  146. tcase.name, result, tcase.output)
  147. }
  148. }()
  149. }
  150. }