map_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package jsonpointer
  2. import (
  3. "io/ioutil"
  4. "reflect"
  5. "testing"
  6. "github.com/dustin/gojson"
  7. )
  8. const objSrc = `{
  9. "foo": ["bar", "baz"],
  10. "": 0,
  11. "a/b": 1,
  12. "c%d": 2,
  13. "e^f": 3,
  14. "g|h": 4,
  15. "i\\j": 5,
  16. "k\"l": 6,
  17. " ": 7,
  18. "m~n": 8,
  19. "g/n/r": "has slash, will travel",
  20. "g": { "n": {"r": "where's tito?"}}
  21. }`
  22. var obj = map[string]interface{}{}
  23. var tests = []struct {
  24. path string
  25. exp interface{}
  26. }{
  27. {"", obj},
  28. {"/foo", []interface{}{"bar", "baz"}},
  29. {"/foo/0", "bar"},
  30. {"/foo/99", nil},
  31. {"/foo/0/3", nil},
  32. {"/", 0.0},
  33. {"/a~1b", 1.0},
  34. {"/c%d", 2.0},
  35. {"/e^f", 3.0},
  36. {"/g|h", 4.0},
  37. {"/i\\j", 5.0},
  38. {"/k\"l", 6.0},
  39. {"/ ", 7.0},
  40. {"/m~0n", 8.0},
  41. {"/g~1n~1r", "has slash, will travel"},
  42. {"/g/n/r", "where's tito?"},
  43. }
  44. func init() {
  45. err := json.Unmarshal([]byte(objSrc), &obj)
  46. if err != nil {
  47. panic(err)
  48. }
  49. }
  50. func TestPaths(t *testing.T) {
  51. for _, test := range tests {
  52. got := Get(obj, test.path)
  53. if !reflect.DeepEqual(got, test.exp) {
  54. t.Errorf("On %v, expected %+v (%T), got %+v (%T)",
  55. test.path, test.exp, test.exp, got, got)
  56. } else {
  57. t.Logf("Success - got %v for %v", got, test.path)
  58. }
  59. }
  60. }
  61. func BenchmarkPaths(b *testing.B) {
  62. for i := 0; i < b.N; i++ {
  63. for _, test := range tests {
  64. Get(obj, test.path)
  65. }
  66. }
  67. }
  68. func BenchmarkParseAndPath(b *testing.B) {
  69. for i := 0; i < b.N; i++ {
  70. for _, test := range tests {
  71. o := map[string]interface{}{}
  72. err := json.Unmarshal([]byte(objSrc), &o)
  73. if err != nil {
  74. b.Fatalf("Error parsing: %v", err)
  75. }
  76. Get(o, test.path)
  77. }
  78. }
  79. }
  80. var bug3Data = []byte(`{"foo" : "bar"}`)
  81. func TestFindSpaceBeforeColon(t *testing.T) {
  82. val, err := Find(bug3Data, "/foo")
  83. if err != nil {
  84. t.Fatalf("Failed to find /foo: %v", err)
  85. }
  86. x, ok := json.UnquoteBytes(val)
  87. if !ok {
  88. t.Fatalf("Failed to unquote json bytes from %q", val)
  89. }
  90. if string(x) != "bar" {
  91. t.Fatalf("Expected %q, got %q", "bar", val)
  92. }
  93. }
  94. func TestListSpaceBeforeColon(t *testing.T) {
  95. ptrs, err := ListPointers(bug3Data)
  96. if err != nil {
  97. t.Fatalf("Error listing pointers: %v", err)
  98. }
  99. if len(ptrs) != 2 || ptrs[0] != "" || ptrs[1] != "/foo" {
  100. t.Fatalf(`Expected ["", "/foo"], got %#v`, ptrs)
  101. }
  102. }
  103. func TestIndexNotFoundSameAsPropertyNotFound(t *testing.T) {
  104. data, err := ioutil.ReadFile("testdata/357.json")
  105. if err != nil {
  106. t.Fatalf("Error beer-sample brewery 357 data: %v", err)
  107. }
  108. expectedResult, expectedError := Find(data, "/doesNotExist")
  109. missingVals := []string{
  110. "/address/0",
  111. "/address/1",
  112. "/address2/1",
  113. "/address2/2",
  114. "/address3/0",
  115. "/address3/1",
  116. }
  117. for _, a := range missingVals {
  118. found, err := Find(data, a)
  119. if !reflect.DeepEqual(err, expectedError) {
  120. t.Errorf("Expected %v at %v, got %v", expectedError, a, err)
  121. }
  122. if !reflect.DeepEqual(expectedResult, found) {
  123. t.Errorf("Expected %v at %v, got %v", expectedResult, a, found)
  124. }
  125. }
  126. }
  127. const bug822src = `{
  128. "foo": ["bar", "baz"],
  129. "": 0,
  130. "a/b": 1,
  131. "c%d": 2,
  132. "e^f": 3,
  133. "g|h": 4,
  134. "i\\j": 5,
  135. "k\"l": 6,
  136. "k2": {},
  137. " ": 7,
  138. "m~n": 8,
  139. "g/n/r": "has slash, will travel",
  140. "g": { "n": {"r": "where's tito?"}},
  141. "h": {}
  142. }`
  143. func TestListEmptyObjectPanic822(t *testing.T) {
  144. ptrs, err := ListPointers([]byte(bug822src))
  145. if err != nil {
  146. t.Fatalf("Error parsing: %v", err)
  147. }
  148. t.Logf("Got pointers: %v", ptrs)
  149. }
  150. func TestFindEmptyObjectPanic823(t *testing.T) {
  151. for _, test := range tests {
  152. _, err := Find([]byte(bug822src), test.path)
  153. if err != nil {
  154. t.Errorf("Error looking for %v: %v", test.path, err)
  155. }
  156. }
  157. }