MCPcopy
hub / github.com/buger/jsonparser / TestGetRequirementSlices

Function TestGetRequirementSlices

parser_test.go:1396–1500  ·  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

1394// Verifies: SYS-REQ-027 [boundary]
1395// MCDC SYS-REQ-027: N/A
1396func TestGetRequirementSlices(t *testing.T) {
1397 t.Run("well formed missing path returns not found", func(t *testing.T) {
1398 value, dataType, offset, err := Get([]byte(`{"a":"b"}`), "missing")
1399 if !errors.Is(err, KeyPathNotFoundError) {
1400 t.Fatalf("expected KeyPathNotFoundError, got %v", err)
1401 }
1402 if dataType != NotExist || offset != -1 || value != nil {
1403 t.Fatalf("expected not-found tuple, got value=%v type=%v offset=%d", value, dataType, offset)
1404 }
1405 })
1406
1407 t.Run("incomplete input returns parse error", func(t *testing.T) {
1408 if _, _, _, err := Get([]byte(`{"a":`), "a"); err == nil {
1409 t.Fatal("expected parse-related error for incomplete input")
1410 }
1411 })
1412
1413 t.Run("no key path returns closest root value", func(t *testing.T) {
1414 value, dataType, _, err := Get([]byte(`{"a":1}`))
1415 if err != nil {
1416 t.Fatalf("Get without key path returned error: %v", err)
1417 }
1418 if dataType != Object || string(value) != `{"a":1}` {
1419 t.Fatalf("unexpected root value result: value=%s type=%v", string(value), dataType)
1420 }
1421 })
1422
1423 t.Run("empty input with key path returns not found", func(t *testing.T) {
1424 _, dataType, offset, err := Get([]byte(""), "a")
1425 if !errors.Is(err, KeyPathNotFoundError) {
1426 t.Fatalf("expected KeyPathNotFoundError, got %v", err)
1427 }
1428 if dataType != NotExist || offset != -1 {
1429 t.Fatalf("expected empty-input not-found tuple, got type=%v offset=%d", dataType, offset)
1430 }
1431 })
1432
1433 t.Run("object key lookup respects current scope", func(t *testing.T) {
1434 value, dataType, _, err := Get([]byte(`{"a":{"b":1},"b":2}`), "a", "b")
1435 if err != nil {
1436 t.Fatalf("nested object lookup returned error: %v", err)
1437 }
1438 if dataType != Number || string(value) != "1" {
1439 t.Fatalf("unexpected nested object lookup result: value=%s type=%v", string(value), dataType)
1440 }
1441 })
1442
1443 t.Run("array index lookup returns in-bounds element", func(t *testing.T) {
1444 value, dataType, _, err := Get([]byte(`{"a":[{"b":1},"foo",3]}`), "a", "[1]")
1445 if err != nil {
1446 t.Fatalf("array index lookup returned error: %v", err)
1447 }
1448 if dataType != String || string(value) != "foo" {
1449 t.Fatalf("unexpected array lookup result: value=%s type=%v", string(value), dataType)
1450 }
1451 })
1452
1453 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

Used in the wild real call sites across dependent graphs

searching dependent graphs…