|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +function bashunit::assert_json::require_jq() { |
| 4 | + if ! command -v jq >/dev/null 2>&1; then |
| 5 | + bashunit::skip "jq is required for JSON assertions" |
| 6 | + return 1 |
| 7 | + fi |
| 8 | + return 0 |
| 9 | +} |
| 10 | + |
| 11 | +function assert_json_key_exists() { |
| 12 | + bashunit::assert::should_skip && return 0 |
| 13 | + bashunit::assert_json::require_jq || return 0 |
| 14 | + |
| 15 | + local key="$1" |
| 16 | + local json="$2" |
| 17 | + |
| 18 | + local result |
| 19 | + if ! result=$(printf '%s' "$json" | jq -e "$key" 2>/dev/null) || [ "$result" = "null" ]; then |
| 20 | + local test_fn |
| 21 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 22 | + local label |
| 23 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 24 | + bashunit::assert::mark_failed |
| 25 | + bashunit::console_results::print_failed_test "${label}" "${json}" "to have key" "${key}" |
| 26 | + return |
| 27 | + fi |
| 28 | + |
| 29 | + bashunit::state::add_assertions_passed |
| 30 | +} |
| 31 | + |
| 32 | +function assert_json_contains() { |
| 33 | + bashunit::assert::should_skip && return 0 |
| 34 | + bashunit::assert_json::require_jq || return 0 |
| 35 | + |
| 36 | + local key="$1" |
| 37 | + local expected="$2" |
| 38 | + local json="$3" |
| 39 | + |
| 40 | + local result |
| 41 | + if ! result=$(printf '%s' "$json" | jq -e -r "$key" 2>/dev/null) || [ "$result" = "null" ]; then |
| 42 | + local test_fn |
| 43 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 44 | + local label |
| 45 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 46 | + bashunit::assert::mark_failed |
| 47 | + bashunit::console_results::print_failed_test "${label}" "${json}" "to have key" "${key}" |
| 48 | + return |
| 49 | + fi |
| 50 | + |
| 51 | + if [ "$result" != "$expected" ]; then |
| 52 | + local test_fn |
| 53 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 54 | + local label |
| 55 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 56 | + bashunit::assert::mark_failed |
| 57 | + bashunit::console_results::print_failed_test "${label}" "${expected}" "but got " "${result}" |
| 58 | + return |
| 59 | + fi |
| 60 | + |
| 61 | + bashunit::state::add_assertions_passed |
| 62 | +} |
| 63 | + |
| 64 | +function assert_json_equals() { |
| 65 | + bashunit::assert::should_skip && return 0 |
| 66 | + bashunit::assert_json::require_jq || return 0 |
| 67 | + |
| 68 | + local expected="$1" |
| 69 | + local actual="$2" |
| 70 | + |
| 71 | + local expected_sorted |
| 72 | + expected_sorted=$(printf '%s' "$expected" | jq -S '.' 2>/dev/null) |
| 73 | + local actual_sorted |
| 74 | + actual_sorted=$(printf '%s' "$actual" | jq -S '.' 2>/dev/null) |
| 75 | + |
| 76 | + if [ "$expected_sorted" != "$actual_sorted" ]; then |
| 77 | + local test_fn |
| 78 | + test_fn="$(bashunit::helper::find_test_function_name)" |
| 79 | + local label |
| 80 | + label="$(bashunit::helper::normalize_test_function_name "$test_fn")" |
| 81 | + bashunit::assert::mark_failed |
| 82 | + bashunit::console_results::print_failed_test "${label}" "${expected}" "but got " "${actual}" |
| 83 | + return |
| 84 | + fi |
| 85 | + |
| 86 | + bashunit::state::add_assertions_passed |
| 87 | +} |
0 commit comments