11#! /usr/bin/env bash
22# shellcheck disable=SC2329
33
4- function set_up_before_script() {
5- if ! command -v jq > /dev/null 2>&1 ; then
6- bashunit::skip " jq is required for JSON assertions"
7- return
8- fi
9- }
4+ _JQ_AVAILABLE=false
5+ if command -v jq > /dev/null 2>&1 ; then
6+ _JQ_AVAILABLE=true
7+ fi
108
119function test_successful_assert_json_key_exists() {
10+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
1211 assert_empty " $( assert_json_key_exists " .name" ' {"name":"bashunit","version":"1.0"}' ) "
1312}
1413
1514function test_successful_assert_json_key_exists_nested() {
15+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
1616 assert_empty " $( assert_json_key_exists " .data.id" ' {"data":{"id":42}}' ) "
1717}
1818
1919function test_unsuccessful_assert_json_key_exists() {
20+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
2021 local json=' {"name":"bashunit"}'
2122
2223 assert_same \
@@ -27,14 +28,17 @@ function test_unsuccessful_assert_json_key_exists() {
2728}
2829
2930function test_successful_assert_json_contains() {
31+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
3032 assert_empty " $( assert_json_contains " .name" " bashunit" ' {"name":"bashunit","version":"1.0"}' ) "
3133}
3234
3335function test_successful_assert_json_contains_numeric() {
36+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
3437 assert_empty " $( assert_json_contains " .count" " 42" ' {"count":42}' ) "
3538}
3639
3740function test_unsuccessful_assert_json_contains_wrong_value() {
41+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
3842 local json=' {"name":"bashunit"}'
3943
4044 assert_same \
@@ -45,6 +49,7 @@ function test_unsuccessful_assert_json_contains_wrong_value() {
4549}
4650
4751function test_unsuccessful_assert_json_contains_missing_key() {
52+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
4853 local json=' {"name":"bashunit"}'
4954
5055 assert_same \
@@ -55,10 +60,12 @@ function test_unsuccessful_assert_json_contains_missing_key() {
5560}
5661
5762function test_successful_assert_json_equals() {
63+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
5864 assert_empty " $( assert_json_equals ' {"b":2,"a":1}' ' {"a":1,"b":2}' ) "
5965}
6066
6167function test_unsuccessful_assert_json_equals() {
68+ if [ " $_JQ_AVAILABLE " = false ]; then bashunit::skip " jq required" ; return ; fi
6269 local expected=' {"a":1}'
6370 local actual=' {"a":2}'
6471
0 commit comments