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
|
package hjson
import (
"encoding/json"
"testing"
)
func compareStrings(t *testing.T, bOut []byte, txtExpected string) {
if string(bOut) != txtExpected {
t.Errorf("Expected:\n%s\n\nGot:\n%s\n\n", txtExpected, string(bOut))
}
}
func verifyNodeContent(t *testing.T, node *Node, txtExpected string) {
opt := DefaultOptions()
opt.EmitRootBraces = false
bOut, err := MarshalWithOptions(node, opt)
if err != nil {
t.Error(err)
}
compareStrings(t, bOut, txtExpected)
}
func TestNode1(t *testing.T) {
txt := `b: 1
a: 2`
var node *Node
err := Unmarshal([]byte(txt), &node)
if err != nil {
t.Error(err)
}
verifyNodeContent(t, node, txt)
}
func TestNode2(t *testing.T) {
txt := `# comment before
b: 1 # comment after
// Comment B4
a: 2
/* Last comment */`
var node *Node
err := Unmarshal([]byte(txt), &node)
if err != nil {
t.Error(err)
}
if node.Len() != 2 {
t.Errorf("Unexpected map length: %v", node.Len())
}
aVal, ok, err := node.AtKey("a")
if err != nil {
t.Error(err)
}
// The value will be a float64 even though it was written without decimals.
if aVal != 2.0 {
t.Errorf("Unexpected value for key 'a': %v", aVal)
} else if !ok {
t.Errorf("node.AtKey('a') returned false")
}
bKey, bVal, err := node.AtIndex(0)
if err != nil {
t.Error(err)
}
if bKey != "b" {
t.Errorf("Expected key 'b', got: %v", bKey)
}
// The value will be a float64 even though it was written without decimals.
if bVal != 1.0 {
t.Errorf("Unexpected value for key 'b': %v", bVal)
}
verifyNodeContent(t, node, txt)
opt := DefaultOptions()
opt.Comments = false
bOut, err := MarshalWithOptions(node, opt)
if err != nil {
t.Error(err)
}
compareStrings(t, bOut, `{
b: 1
a: 2
}`)
bOut, err = json.Marshal(node)
if err != nil {
t.Error(err)
}
compareStrings(t, bOut, `{"b":1,"a":2}`)
intLen := node.Value.(*OrderedMap).Map["b"].(*Node).Len()
if intLen != 0 {
t.Errorf("Unexpected int length: %v", intLen)
}
node.SetIndex(0, 3)
verifyNodeContent(t, node, `# comment before
b: 3 # comment after
// Comment B4
a: 2
/* Last comment */`)
oldVal, found, err := node.SetKey("b", "abcdef")
if err != nil {
t.Error(err)
}
if !found {
t.Errorf("Should have returned true, the key should already exist.")
}
if oldVal != 3 {
t.Errorf("Expected old value 3, got: '%v'", oldVal)
}
verifyNodeContent(t, node, `# comment before
b: "abcdef" # comment after
// Comment B4
a: 2
/* Last comment */`)
strLen := node.Value.(*OrderedMap).Map["b"].(*Node).Len()
if strLen != 6 {
t.Errorf("Unexpected string length: %v", strLen)
}
node.Value.(*OrderedMap).Map["b"] = "xyz"
verifyNodeContent(t, node, `b: xyz
// Comment B4
a: 2
/* Last comment */`)
}
func TestNode3(t *testing.T) {
txt := `# comment before
[
# after [
1 # comment after
// Comment B4
2
# COmment After
]
/* Last comment */`
var node *Node
Unmarshal([]byte(txt), &node)
if node.Len() != 2 {
t.Errorf("Unexpected slice length: %v", node.Len())
}
firstKey, firstVal, err := node.AtIndex(0)
if err != nil {
t.Error(err)
}
if firstKey != "" {
t.Errorf("Expected empty key, got: %v", firstKey)
}
// The value will be a float64 even though it was written without decimals.
if firstVal != 1.0 {
t.Errorf("Unexpected value for index 0: %v", firstVal)
}
verifyNodeContent(t, node, txt)
opt := DefaultOptions()
opt.Comments = false
bOut, err := MarshalWithOptions(node, opt)
if err != nil {
t.Error(err)
}
compareStrings(t, bOut, `[
1
2
]`)
bOut, err = json.Marshal(node)
compareStrings(t, bOut, `[1,2]`)
intLen := node.Value.([]interface{})[1].(*Node).Len()
if intLen != 0 {
t.Errorf("Unexpected int length: %v", intLen)
}
key, oldVal, err := node.SetIndex(1, "abcdef")
if err != nil {
t.Error(err)
}
if key != "" {
t.Errorf("Expected empty key, got: '%v'", key)
}
if oldVal != 2.0 {
t.Errorf("Expected old value 2.0, got: '%v'", oldVal)
}
verifyNodeContent(t, node, `# comment before
[
# after [
1 # comment after
// Comment B4
abcdef
# COmment After
]
/* Last comment */`)
strLen := node.Value.([]interface{})[1].(*Node).Len()
if strLen != 6 {
t.Errorf("Unexpected string length: %v", strLen)
}
node.Value.([]interface{})[0] = "xyz"
verifyNodeContent(t, node, `# comment before
[
xyz
// Comment B4
abcdef
# COmment After
]
/* Last comment */`)
}
func TestNode4(t *testing.T) {
txt := `# comment before
b: /* key comment */ {
sub1: 1 # comment after
} # cm after obj
// Comment B4
a: 2
/* Last comment */`
var node *Node
err := Unmarshal([]byte(txt), &node)
if err != nil {
t.Error(err)
}
verifyNodeContent(t, node, txt)
sub1Val, ok, err := node.NK("b").AtKey("sub1")
if err != nil {
t.Error(err)
}
// The value will be a float64 even though it was written without decimals.
if sub1Val != 1.0 {
t.Errorf("Unexpected value for sub1: %v", sub1Val)
} else if !ok {
t.Errorf("AtKey('sub1') returned false")
}
sub1Val, ok, err = node.NK("Z").AtKey("sub2")
if err != nil {
t.Error(err)
}
if ok {
t.Errorf("Should have returned false when calling AtKey() on nil")
}
oldVal, found, err := node.NK("Z").SetKey("sub2", 3)
if err == nil {
t.Errorf("Should have returned an error calling SetKey() on nil")
}
oldVal, found, err = node.NKC("Z").SetKey("sub2", 3)
if err != nil {
t.Error(err)
}
if found {
t.Errorf("Should have returned false, the key should not already exist.")
}
oldVal, found, err = node.NKC("Z").SetKey("sub2", 4)
if err != nil {
t.Error(err)
}
if !found {
t.Errorf("Should have returned true, the key should already exist.")
}
if oldVal != 3 {
t.Errorf("Expected old value 3, got: '%v'", oldVal)
}
oldVal, found, err = node.NKC("X").NKC("Y").SetKey("sub3", 5)
if err != nil {
t.Error(err)
}
if found {
t.Errorf("Should have returned false, the key should not already exist.")
}
verifyNodeContent(t, node, `# comment before
b: /* key comment */ {
sub1: 1 # comment after
} # cm after obj
// Comment B4
a: 2
Z: {
sub2: 4
}
X: {
Y: {
sub3: 5
}
}
/* Last comment */`)
}
func TestDisallowDuplicateKeys(t *testing.T) {
txt := `a: 1
a: 2
b: 3
c: 4
b: 5`
var node *Node
err := Unmarshal([]byte(txt), &node)
if err != nil {
t.Error(err)
}
verifyNodeContent(t, node, `a: 2
b: 5
c: 4`)
decOpt := DefaultDecoderOptions()
decOpt.DisallowDuplicateKeys = true
err = UnmarshalWithOptions([]byte(txt), &node, decOpt)
if err == nil {
t.Errorf("Should have returned error because of duplicate keys.")
}
}
func TestWhitespaceAsComments(t *testing.T) {
txt := `
a: 2
b: 3
`
var node *Node
err := Unmarshal([]byte(txt), &node)
if err != nil {
t.Error(err)
}
verifyNodeContent(t, node, txt)
decOpt := DefaultDecoderOptions()
decOpt.WhitespaceAsComments = false
err = UnmarshalWithOptions([]byte(txt), &node, decOpt)
if err != nil {
t.Error(err)
}
verifyNodeContent(t, node, `a: 2
b: 3`)
}
func TestDeclareNodeMap(t *testing.T) {
var node Node
node2 := node.NK("a")
if node2 != nil {
t.Errorf("node.NK() created a node")
}
oldVal, found, err := node.NKC("a").NKC("aa").NKC("aaa").SetKey("aaaa", "a string")
if err != nil {
t.Error(err)
}
if found {
t.Errorf("Should have returned false, the key should not already exist.")
}
oldVal, found, err = node.SetKey("b", 2)
if err != nil {
t.Error(err)
}
if found {
t.Errorf("Should have returned false, the key should not already exist.")
}
key, oldVal, err := node.SetIndex(1, 3.0)
if err != nil {
t.Error(err)
}
if key != "b" {
t.Errorf("Expected key 'b', got: '%v'", key)
}
if oldVal != 2 {
t.Errorf("Expected old value 2, got: '%v'", oldVal)
}
verifyNodeContent(t, &node, `a: {
aa: {
aaa: {
aaaa: a string
}
}
}
b: 3`)
err = node.Append(4)
if err == nil {
t.Errorf("Should have returned error when trying to append to a map")
}
}
func TestDeclareNodeSlice(t *testing.T) {
var node Node
node2 := node.NI(0)
if node2 != nil {
t.Errorf("node.NI() created a node")
}
err := node.Append(13)
if err != nil {
t.Error(err)
}
err = node.Append("b")
if err != nil {
t.Error(err)
}
key, oldVal, err := node.SetIndex(1, false)
if err != nil {
t.Error(err)
}
if key != "" {
t.Errorf("Expected empty key, got: '%v'", key)
}
if oldVal != "b" {
t.Errorf("Expected old value 'b', got: '%v'", oldVal)
}
verifyNodeContent(t, &node, `[
13
false
]`)
node2 = node.NKC("sub")
if node2 != nil {
t.Errorf("Should not have been able to create a node by key in a slice")
}
_, _, err = node.SetKey("a", 4)
if err == nil {
t.Errorf("Should have returned error when trying to set by key on a slice")
}
}
func TestNodeNoPointer(t *testing.T) {
txt := `setting1: null # nada
setting2: true // yes`
var node Node
err := Unmarshal([]byte(txt), &node)
if err != nil {
t.Error(err)
}
oldVal, found, err := node.SetKey("setting1", 3)
if err != nil {
t.Error(err)
}
if !found {
t.Errorf("Should have returned true, the key should already exist")
}
if oldVal != nil {
t.Errorf("Expected old value nil, got: '%v'", oldVal)
}
output, err := Marshal(node)
if err != nil {
t.Error(err)
}
compareStrings(t, output, `{
setting1: 3 # nada
setting2: true // yes
}`)
}
func TestNodeOrderedMapInsertDelete(t *testing.T) {
txt := `a: 1
b: 2`
var node Node
err := Unmarshal([]byte(txt), &node)
if err != nil {
t.Error(err)
}
key, val, err := node.DeleteIndex(0)
if err != nil {
t.Error(err)
}
if key != "a" {
t.Errorf("Expected key 'a', got: '%v'", key)
}
if val != 1.0 {
t.Errorf("Expected old value 1.0, got: '%#v'", val)
}
val, found, err := node.Insert(1, key, val)
if err != nil {
t.Error(err)
}
if found {
t.Errorf("Found key '%v' that should not have been found", key)
}
val, found, err = node.DeleteKey("a")
if err != nil {
t.Error(err)
}
if !found {
t.Errorf("Expected to find key 'a' but did not")
}
if val != 1.0 {
t.Errorf("Expected deleted value 1.0, got: '%#v'", val)
}
val, found, err = node.Insert(0, "c", 3)
if err != nil {
t.Error(err)
}
if found {
t.Error("Found key c that should not have been found")
}
verifyNodeContent(t, &node, `c: 3
b: 2`)
}
func TestNodeSliceInsertDelete(t *testing.T) {
txt := `[
1
2
]`
var node Node
err := Unmarshal([]byte(txt), &node)
if err != nil {
t.Error(err)
}
key, val, err := node.DeleteIndex(0)
if err != nil {
t.Error(err)
}
if key != "" {
t.Errorf("Expected empty key, got: '%v'", key)
}
if val != 1.0 {
t.Errorf("Expected old value 1.0, got: '%#v'", val)
}
val, found, err := node.Insert(1, key, val)
if err != nil {
t.Error(err)
}
if found {
t.Errorf("Found key '%v' that should not have been found", key)
}
val, found, err = node.Insert(0, "c", 3)
if err != nil {
t.Error(err)
}
if found {
t.Error("Found key c that should not have been found")
}
// The value '2' has a line break as Cm.After
verifyNodeContent(t, &node, `[
3
2
1
]`)
}
|