Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

106 linhas
2.4 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pretty
  15. import (
  16. "fmt"
  17. "strings"
  18. "testing"
  19. )
  20. type S struct {
  21. X int
  22. Y bool
  23. z *string
  24. }
  25. func TestSprint(t *testing.T) {
  26. Indent = "~"
  27. i := 17
  28. for _, test := range []struct {
  29. value interface{}
  30. want string
  31. }{
  32. // primitives and pointer
  33. {nil, "nil"},
  34. {3, "3"},
  35. {9.8, "9.8"},
  36. {true, "true"},
  37. {"foo", `"foo"`},
  38. {&i, "&17"},
  39. // array and slice
  40. {[3]int{1, 2, 3}, "[3]int{\n~1,\n~2,\n~3,\n}"},
  41. {[]int{1, 2, 3}, "[]int{\n~1,\n~2,\n~3,\n}"},
  42. {[]int{}, "[]int{}"},
  43. {[]string{"foo"}, "[]string{\n~\"foo\",\n}"},
  44. // map
  45. {map[int]bool{}, "map[int]bool{}"},
  46. {map[int]bool{1: true, 2: false, 3: true},
  47. "map[int]bool{\n~1: true,\n~3: true,\n}"},
  48. // struct
  49. {S{}, "pretty.S{\n}"},
  50. {S{3, true, ptr("foo")},
  51. "pretty.S{\n~X: 3,\n~Y: true,\n~z: &\"foo\",\n}"},
  52. // interface
  53. {[]interface{}{&i}, "[]interface {}{\n~&17,\n}"},
  54. // nesting
  55. {[]S{{1, false, ptr("a")}, {2, true, ptr("b")}},
  56. `[]pretty.S{
  57. ~pretty.S{
  58. ~~X: 1,
  59. ~~z: &"a",
  60. ~},
  61. ~pretty.S{
  62. ~~X: 2,
  63. ~~Y: true,
  64. ~~z: &"b",
  65. ~},
  66. }`},
  67. } {
  68. got := fmt.Sprintf("%v", Value(test.value))
  69. if got != test.want {
  70. t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
  71. }
  72. }
  73. }
  74. func TestWithDefaults(t *testing.T) {
  75. Indent = "~"
  76. for _, test := range []struct {
  77. value interface{}
  78. want string
  79. }{
  80. {map[int]bool{1: true, 2: false, 3: true},
  81. "map[int]bool{\n~1: true,\n~2: false,\n~3: true,\n}"},
  82. {S{}, "pretty.S{\n~X: 0,\n~Y: false,\n~z: nil,\n}"},
  83. } {
  84. got := fmt.Sprintf("%+v", Value(test.value))
  85. if got != test.want {
  86. t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
  87. }
  88. }
  89. }
  90. func TestBadVerb(t *testing.T) {
  91. got := fmt.Sprintf("%d", Value(8))
  92. want := "%!d("
  93. if !strings.HasPrefix(got, want) {
  94. t.Errorf("got %q, want prefix %q", got, want)
  95. }
  96. }
  97. func ptr(s string) *string { return &s }