MCPcopy Create free account
hub / github.com/buger/jsonparser / TestGetRequirementSlices

Function TestGetRequirementSlices

parser_test.go:1414–1518  ·  view source on GitHub ↗

Verifies: SYS-REQ-016 [boundary] MCDC SYS-REQ-016: N/A Verifies: SYS-REQ-017 [boundary] MCDC SYS-REQ-017: N/A Verifies: SYS-REQ-018 [boundary] MCDC SYS-REQ-018: N/A Verifies: SYS-REQ-019 [boundary] MCDC SYS-REQ-019: N/A Verifies: SYS-REQ-020 [boundary] MCDC SYS-REQ-020: N/A Verifies: SYS-REQ-021 [bo

(t *testing.T)

Source from the content-addressed store, hash-verified

1412// Verifies: SYS-REQ-027 [boundary]
1413// MCDC SYS-REQ-027: N/A
1414func TestGetRequirementSlices(t *testing.T) {
1415 t.Run("well formed missing path returns not found", func(t *testing.T) {
1416 value, dataType, offset, err := Get([]byte(`{"a":"b"}`), "missing")
1417 if !errors.Is(err, KeyPathNotFoundError) {
1418 t.Fatalf("expected KeyPathNotFoundError, got %v", err)
1419 }
1420 if dataType != NotExist || offset != -1 || value != nil {
1421 t.Fatalf("expected not-found tuple, got value=%v type=%v offset=%d", value, dataType, offset)
1422 }
1423 })
1424
1425 t.Run("incomplete input returns parse error", func(t *testing.T) {
1426 if _, _, _, err := Get([]byte(`{"a":`), "a"); err == nil {
1427 t.Fatal("expected parse-related error for incomplete input")
1428 }
1429 })
1430
1431 t.Run("no key path returns closest root value", func(t *testing.T) {
1432 value, dataType, _, err := Get([]byte(`{"a":1}`))
1433 if err != nil {
1434 t.Fatalf("Get without key path returned error: %v", err)
1435 }
1436 if dataType != Object || string(value) != `{"a":1}` {
1437 t.Fatalf("unexpected root value result: value=%s type=%v", string(value), dataType)
1438 }
1439 })
1440
1441 t.Run("empty input with key path returns not found", func(t *testing.T) {
1442 _, dataType, offset, err := Get([]byte(""), "a")
1443 if !errors.Is(err, KeyPathNotFoundError) {
1444 t.Fatalf("expected KeyPathNotFoundError, got %v", err)
1445 }
1446 if dataType != NotExist || offset != -1 {
1447 t.Fatalf("expected empty-input not-found tuple, got type=%v offset=%d", dataType, offset)
1448 }
1449 })
1450
1451 t.Run("object key lookup respects current scope", func(t *testing.T) {
1452 value, dataType, _, err := Get([]byte(`{"a":{"b":1},"b":2}`), "a", "b")
1453 if err != nil {
1454 t.Fatalf("nested object lookup returned error: %v", err)
1455 }
1456 if dataType != Number || string(value) != "1" {
1457 t.Fatalf("unexpected nested object lookup result: value=%s type=%v", string(value), dataType)
1458 }
1459 })
1460
1461 t.Run("array index lookup returns in-bounds element", func(t *testing.T) {
1462 value, dataType, _, err := Get([]byte(`{"a":[{"b":1},"foo",3]}`), "a", "[1]")
1463 if err != nil {
1464 t.Fatalf("array index lookup returned error: %v", err)
1465 }
1466 if dataType != String || string(value) != "foo" {
1467 t.Fatalf("unexpected array lookup result: value=%s type=%v", string(value), dataType)
1468 }
1469 })
1470
1471 t.Run("invalid array index syntax returns not found", func(t *testing.T) {

Callers

nothing calls this directly

Calls 1

GetFunction · 0.85

Tested by

no test coverage detected