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