1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
package orderedmap
import (
"encoding/json"
"fmt"
"sort"
"strings"
"testing"
)
func TestOrderedMap(t *testing.T) {
o := New()
// number
o.Set("number", 3)
v, _ := o.Get("number")
if v.(int) != 3 {
t.Error("Set number")
}
// string
o.Set("string", "x")
v, _ = o.Get("string")
if v.(string) != "x" {
t.Error("Set string")
}
// string slice
o.Set("strings", []string{
"t",
"u",
})
v, _ = o.Get("strings")
if v.([]string)[0] != "t" {
t.Error("Set strings first index")
}
if v.([]string)[1] != "u" {
t.Error("Set strings second index")
}
// mixed slice
o.Set("mixed", []interface{}{
1,
"1",
})
v, _ = o.Get("mixed")
if v.([]interface{})[0].(int) != 1 {
t.Error("Set mixed int")
}
if v.([]interface{})[1].(string) != "1" {
t.Error("Set mixed string")
}
// overriding existing key
o.Set("number", 4)
v, _ = o.Get("number")
if v.(int) != 4 {
t.Error("Override existing key")
}
// Keys method
keys := o.Keys()
expectedKeys := []string{
"number",
"string",
"strings",
"mixed",
}
for i, key := range keys {
if key != expectedKeys[i] {
t.Error("Keys method", key, "!=", expectedKeys[i])
}
}
for i, key := range expectedKeys {
if key != expectedKeys[i] {
t.Error("Keys method", key, "!=", expectedKeys[i])
}
}
// delete
o.Delete("strings")
o.Delete("not a key being used")
if len(o.Keys()) != 3 {
t.Error("Delete method")
}
_, ok := o.Get("strings")
if ok {
t.Error("Delete did not remove 'strings' key")
}
}
func TestBlankMarshalJSON(t *testing.T) {
o := New()
// blank map
b, err := json.Marshal(o)
if err != nil {
t.Error("Marshalling blank map to json", err)
}
s := string(b)
// check json is correctly ordered
if s != `{}` {
t.Error("JSON Marshaling blank map value is incorrect", s)
}
// convert to indented json
bi, err := json.MarshalIndent(o, "", " ")
if err != nil {
t.Error("Marshalling indented json for blank map", err)
}
si := string(bi)
ei := `{}`
if si != ei {
fmt.Println(ei)
fmt.Println(si)
t.Error("JSON MarshalIndent blank map value is incorrect", si)
}
}
func TestMarshalJSON(t *testing.T) {
o := New()
// number
o.Set("number", 3)
// string
o.Set("string", "x")
// string
o.Set("specialstring", "\\.<>[]{}_-")
// new value keeps key in old position
o.Set("number", 4)
// keys not sorted alphabetically
o.Set("z", 1)
o.Set("a", 2)
o.Set("b", 3)
// slice
o.Set("slice", []interface{}{
"1",
1,
})
// orderedmap
v := New()
v.Set("e", 1)
v.Set("a", 2)
o.Set("orderedmap", v)
// escape key
o.Set("test\n\r\t\\\"ing", 9)
// convert to json
b, err := json.Marshal(o)
if err != nil {
t.Error("Marshalling json", err)
}
s := string(b)
// check json is correctly ordered
if s != `{"number":4,"string":"x","specialstring":"\\.\u003c\u003e[]{}_-","z":1,"a":2,"b":3,"slice":["1",1],"orderedmap":{"e":1,"a":2},"test\n\r\t\\\"ing":9}` {
t.Error("JSON Marshal value is incorrect", s)
}
// convert to indented json
bi, err := json.MarshalIndent(o, "", " ")
if err != nil {
t.Error("Marshalling indented json", err)
}
si := string(bi)
ei := `{
"number": 4,
"string": "x",
"specialstring": "\\.\u003c\u003e[]{}_-",
"z": 1,
"a": 2,
"b": 3,
"slice": [
"1",
1
],
"orderedmap": {
"e": 1,
"a": 2
},
"test\n\r\t\\\"ing": 9
}`
if si != ei {
fmt.Println(ei)
fmt.Println(si)
t.Error("JSON MarshalIndent value is incorrect", si)
}
}
func TestMarshalJSONNoEscapeHTML(t *testing.T) {
o := New()
o.SetEscapeHTML(false)
// string special characters
o.Set("specialstring", "\\.<>[]{}_-")
// convert to json
b, err := o.MarshalJSON()
if err != nil {
t.Error("Marshalling json", err)
}
s := strings.Replace(string(b), "\n", "", -1)
// check json is correctly ordered
if s != `{"specialstring":"\\.<>[]{}_-"}` {
t.Error("JSON Marshal value is incorrect", s)
}
}
func TestMarshalJSONNoEscapeHTMLRecursive(t *testing.T) {
src := `{"x":"<>","y":[{"z":["<>"]}]}`
o := New()
o.SetEscapeHTML(false)
err := json.Unmarshal([]byte(src), &o)
if err != nil {
t.Error("JSON Unmarshal error with special chars", err)
}
b, err := o.MarshalJSON()
if err != nil {
t.Error("Marshalling json", err)
}
s := strings.Replace(string(b), "\n", "", -1)
if s != src {
t.Error("JSON Marshal value is incorrect", s)
}
}
func TestUnmarshalJSON(t *testing.T) {
s := `{
"number": 4,
"string": "x",
"z": 1,
"a": "should not break with unclosed { character in value",
"b": 3,
"slice": [
"1",
1
],
"orderedmap": {
"e": 1,
"a { nested key with brace": "with a }}}} }} {{{ brace value",
"after": {
"link": "test {{{ with even deeper nested braces }"
}
},
"test\"ing": 9,
"after": 1,
"multitype_array": [
"test",
1,
{ "map": "obj", "it" : 5, ":colon in key": "colon: in value" },
[{"inner": "map"}]
],
"should not break with { character in key": 1
}`
o := New()
err := json.Unmarshal([]byte(s), &o)
if err != nil {
t.Error("JSON Unmarshal error", err)
}
// Check the root keys
expectedKeys := []string{
"number",
"string",
"z",
"a",
"b",
"slice",
"orderedmap",
"test\"ing",
"after",
"multitype_array",
"should not break with { character in key",
}
k := o.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Unmarshal root key order", i, k[i], "!=", expectedKeys[i])
}
}
// Check nested maps are converted to orderedmaps
// nested 1 level deep
expectedKeys = []string{
"e",
"a { nested key with brace",
"after",
}
vi, ok := o.Get("orderedmap")
if !ok {
t.Error("Missing key for nested map 1 deep")
}
v := vi.(OrderedMap)
k = v.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Key order for nested map 1 deep ", i, k[i], "!=", expectedKeys[i])
}
}
// nested 2 levels deep
expectedKeys = []string{
"link",
}
vi, ok = v.Get("after")
if !ok {
t.Error("Missing key for nested map 2 deep")
}
v = vi.(OrderedMap)
k = v.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Key order for nested map 2 deep", i, k[i], "!=", expectedKeys[i])
}
}
// multitype array
expectedKeys = []string{
"map",
"it",
":colon in key",
}
vislice, ok := o.Get("multitype_array")
if !ok {
t.Error("Missing key for multitype array")
}
vslice := vislice.([]interface{})
vmap := vslice[2].(OrderedMap)
k = vmap.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Key order for nested map 2 deep", i, k[i], "!=", expectedKeys[i])
}
}
// nested map 3 deep
vislice, _ = o.Get("multitype_array")
vslice = vislice.([]interface{})
expectedKeys = []string{"inner"}
vinnerslice := vslice[3].([]interface{})
vinnermap := vinnerslice[0].(OrderedMap)
k = vinnermap.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Key order for nested map 3 deep", i, k[i], "!=", expectedKeys[i])
}
}
}
func TestUnmarshalJSONDuplicateKeys(t *testing.T) {
s := `{
"a": [{}, []],
"b": {"x":[1]},
"c": "x",
"d": {"x":1},
"b": [{"x":[]}],
"c": 1,
"d": {"y": 2},
"e": [{"x":1}],
"e": [[]],
"e": [{"z":2}],
"a": {},
"b": [[1]]
}`
o := New()
err := json.Unmarshal([]byte(s), &o)
if err != nil {
t.Error("JSON Unmarshal error with special chars", err)
}
expectedKeys := []string{
"c",
"d",
"e",
"a",
"b",
}
keys := o.Keys()
if len(keys) != len(expectedKeys) {
t.Error("Unmarshal key count", len(keys), "!=", len(expectedKeys))
}
for i, key := range keys {
if key != expectedKeys[i] {
t.Errorf("Unmarshal root key order: %d, %q != %q", i, key, expectedKeys[i])
}
}
vimap, _ := o.Get("a")
_ = vimap.(OrderedMap)
vislice, _ := o.Get("b")
_ = vislice.([]interface{})
vival, _ := o.Get("c")
_ = vival.(float64)
vimap, _ = o.Get("d")
m := vimap.(OrderedMap)
expectedKeys = []string{"y"}
keys = m.Keys()
if len(keys) != len(expectedKeys) {
t.Error("Unmarshal key count", len(keys), "!=", len(expectedKeys))
}
for i, key := range keys {
if key != expectedKeys[i] {
t.Errorf("Unmarshal key order: %d, %q != %q", i, key, expectedKeys[i])
}
}
vislice, _ = o.Get("e")
m = vislice.([]interface{})[0].(OrderedMap)
expectedKeys = []string{"z"}
keys = m.Keys()
if len(keys) != len(expectedKeys) {
t.Error("Unmarshal key count", len(keys), "!=", len(expectedKeys))
}
for i, key := range keys {
if key != expectedKeys[i] {
t.Errorf("Unmarshal key order: %d, %q != %q", i, key, expectedKeys[i])
}
}
}
func TestUnmarshalJSONSpecialChars(t *testing.T) {
s := `{ " \u0041\n\r\t\\\\\\\\\\\\ " : { "\\\\\\" : "\\\\\"\\" }, "\\": " \\\\ test ", "\n": "\r" }`
o := New()
err := json.Unmarshal([]byte(s), &o)
if err != nil {
t.Error("JSON Unmarshal error with special chars", err)
}
expectedKeys := []string{
" \u0041\n\r\t\\\\\\\\\\\\ ",
"\\",
"\n",
}
keys := o.Keys()
if len(keys) != len(expectedKeys) {
t.Error("Unmarshal key count", len(keys), "!=", len(expectedKeys))
}
for i, key := range keys {
if key != expectedKeys[i] {
t.Errorf("Unmarshal root key order: %d, %q != %q", i, key, expectedKeys[i])
}
}
}
func TestUnmarshalJSONArrayOfMaps(t *testing.T) {
s := `
{
"name": "test",
"percent": 6,
"breakdown": [
{
"name": "a",
"percent": 0.9
},
{
"name": "b",
"percent": 0.9
},
{
"name": "d",
"percent": 0.4
},
{
"name": "e",
"percent": 2.7
}
]
}
`
o := New()
err := json.Unmarshal([]byte(s), &o)
if err != nil {
t.Error("JSON Unmarshal error", err)
}
// Check the root keys
expectedKeys := []string{
"name",
"percent",
"breakdown",
}
k := o.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Unmarshal root key order", i, k[i], "!=", expectedKeys[i])
}
}
// Check nested maps are converted to orderedmaps
// nested 1 level deep
expectedKeys = []string{
"name",
"percent",
}
vi, ok := o.Get("breakdown")
if !ok {
t.Error("Missing key for nested map 1 deep")
}
vs := vi.([]interface{})
for _, vInterface := range vs {
v := vInterface.(OrderedMap)
k = v.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Key order for nested map 1 deep ", i, k[i], "!=", expectedKeys[i])
}
}
}
}
func TestUnmarshalJSONStruct(t *testing.T) {
var v struct {
Data *OrderedMap `json:"data"`
}
err := json.Unmarshal([]byte(`{ "data": { "x": 1 } }`), &v)
if err != nil {
t.Fatalf("JSON unmarshal error: %v", err)
}
x, ok := v.Data.Get("x")
if !ok {
t.Errorf("missing expected key")
} else if x != float64(1) {
t.Errorf("unexpected value: %#v", x)
}
}
func TestOrderedMap_SortKeys(t *testing.T) {
s := `
{
"b": 2,
"a": 1,
"c": 3
}
`
o := New()
json.Unmarshal([]byte(s), &o)
o.SortKeys(sort.Strings)
// Check the root keys
expectedKeys := []string{
"a",
"b",
"c",
}
k := o.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("SortKeys root key order", i, k[i], "!=", expectedKeys[i])
}
}
}
func TestOrderedMap_Sort(t *testing.T) {
s := `
{
"b": 2,
"a": 1,
"c": 3
}
`
o := New()
json.Unmarshal([]byte(s), &o)
o.Sort(func(a *Pair, b *Pair) bool {
return a.value.(float64) > b.value.(float64)
})
// Check the root keys
expectedKeys := []string{
"c",
"b",
"a",
}
k := o.Keys()
for i := range k {
if k[i] != expectedKeys[i] {
t.Error("Sort root key order", i, k[i], "!=", expectedKeys[i])
}
}
}
// https://github.com/iancoleman/orderedmap/issues/11
func TestOrderedMap_empty_array(t *testing.T) {
srcStr := `{"x":[]}`
src := []byte(srcStr)
om := New()
json.Unmarshal(src, om)
bs, _ := json.Marshal(om)
marshalledStr := string(bs)
if marshalledStr != srcStr {
t.Error("Empty array does not serialise to json correctly")
t.Error("Expect", srcStr)
t.Error("Got", marshalledStr)
}
}
// Inspired by
// https://github.com/iancoleman/orderedmap/issues/11
// but using empty maps instead of empty slices
func TestOrderedMap_empty_map(t *testing.T) {
srcStr := `{"x":{}}`
src := []byte(srcStr)
om := New()
json.Unmarshal(src, om)
bs, _ := json.Marshal(om)
marshalledStr := string(bs)
if marshalledStr != srcStr {
t.Error("Empty map does not serialise to json correctly")
t.Error("Expect", srcStr)
t.Error("Got", marshalledStr)
}
}
|