Skip to content

Commit a47a266

Browse files
committed
Make string difference more readable
With this patch: ``` assertEquals "deadbeef" "deadbuff" ``` will output like this ``` ASSERT: expected:<deadbeef> ---------------^ but was :<deadbuff> ``` set env variable SHUNIT_DIFF_ALIGN=[always/never/auto]
1 parent 47be8b2 commit a47a266

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

shunit2

+60-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ SHUNIT_CMD_TPUT=${SHUNIT_CMD_TPUT:-${__SHUNIT_CMD_TPUT}}
4949

5050
# Enable color output. Options are 'auto', 'always', or 'never'.
5151
SHUNIT_COLOR=${SHUNIT_COLOR:-auto}
52+
# Enable difference alignment output. Options are 'auto', 'always', or 'never'.
53+
SHUNIT_DIFF_ALIGN=${SHUNIT_DIFF_ALIGN:-never}
5254

5355
#
5456
# Internal constants.
@@ -647,6 +649,35 @@ fail() {
647649
# shellcheck disable=SC2016,SC2034
648650
_FAIL_='eval fail --lineno "${LINENO:-}"'
649651

652+
_shunit_different() {
653+
expected="$1"
654+
actually="$2"
655+
len1=${#expected}
656+
len2=${#actually}
657+
min_len=$len1
658+
if ${__SHUNIT_BUILTIN} [ "$min_len" -gt "$len2" ]; then
659+
min_len=$len2;
660+
fi
661+
662+
i=1;
663+
while ${__SHUNIT_BUILTIN} [ "$i" -le "$min_len" ];
664+
do
665+
charA=$(echo "$expected"|cut -c$i);
666+
charB=$(echo "$actually"|cut -c$i);
667+
if ${__SHUNIT_BUILTIN} [ "$charA" = "$charB" ]; then
668+
printf "%c" "-";
669+
else
670+
printf "%c" "^";
671+
break;
672+
fi
673+
i=$((i + 1));
674+
done
675+
if ${__SHUNIT_BUILTIN} [ "$i" -gt "$min_len" ]; then
676+
printf "%c" "^";
677+
fi
678+
679+
}
680+
650681
# Records a test failure, stating two values were not equal.
651682
#
652683
# Args:
@@ -675,9 +706,14 @@ failNotEquals() {
675706
shunit_actual_=$2
676707

677708
shunit_message_=${shunit_message_%% }
709+
if ${__SHUNIT_BUILTIN} [ ${__shunit_diff_align} -eq ${SHUNIT_TRUE} ]; then
710+
diff_position_=$(_shunit_different "${shunit_expected_}" "${shunit_actual_})")
711+
_shunit_assertFail "${shunit_message_:+${shunit_message_} }\\nexpected:<${shunit_expected_}>\\n----------${diff_position_}\\n but was:<${shunit_actual_}>"
712+
return
713+
fi
678714
_shunit_assertFail "${shunit_message_:+${shunit_message_} }expected:<${shunit_expected_}> but was:<${shunit_actual_}>"
679715

680-
unset shunit_message_ shunit_expected_ shunit_actual_
716+
unset shunit_message_ shunit_expected_ shunit_actual_ diff_position_
681717
return ${SHUNIT_FALSE}
682718
}
683719
# shellcheck disable=SC2016,SC2034
@@ -1043,6 +1079,26 @@ _shunit_cleanup() {
10431079
unset _shunit_name_ _shunit_signal_
10441080
}
10451081

1082+
# configureDiff difference alignment.
1083+
#
1084+
# Args:
1085+
# alignment: string: align state (one of `always`, `auto`, or `never`).
1086+
_shunit_configureDiff() {
1087+
_shunit_diff_conf_=${SHUNIT_FALSE}
1088+
case $1 in
1089+
'always') _shunit_diff_conf_=${SHUNIT_TRUE} ;;
1090+
'auto')
1091+
if which cut|grep cut >/dev/null 2>&1; then
1092+
_shunit_diff_conf_=${SHUNIT_TRUE}
1093+
fi
1094+
;;
1095+
'never'|'none') _shunit_diff_conf_=${SHUNIT_FALSE} ;;
1096+
*) _shunit_fatal "unrecognized difference alignment option '$1'" ;;
1097+
esac
1098+
__shunit_diff_align=${_shunit_diff_conf_}
1099+
unset _shunit_diff_conf_
1100+
}
1101+
10461102
# configureColor based on user color preference.
10471103
#
10481104
# Args:
@@ -1329,6 +1385,9 @@ fi
13291385
# Configure default output coloring behavior.
13301386
_shunit_configureColor "${SHUNIT_COLOR}"
13311387

1388+
# Configure default difference alignment behavior.
1389+
_shunit_configureDiff "${SHUNIT_DIFF_ALIGN}"
1390+
13321391
# Execute the oneTimeSetUp function (if it exists).
13331392
if ! oneTimeSetUp; then
13341393
_shunit_fatal "oneTimeSetUp() returned non-zero return code."

shunit2_misc_test.sh

+30
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ EOF
5858
testIssue7() {
5959
# Disable coloring so 'ASSERT:' lines can be matched correctly.
6060
_shunit_configureColor 'none'
61+
# Disable difference alignment so 'ASSERT:' lines can be matched correctly.
62+
_shunit_configureDiff 'none'
6163

6264
# Ignoring errors with `|| :` as we only care about the message in this test.
6365
( assertEquals 'Some message.' 1 2 >"${stdoutF}" 2>"${stderrF}" ) || :
@@ -165,6 +167,34 @@ EOF
165167
fi
166168
}
167169

170+
testIssue89() {
171+
tempdiff="${SHUNIT_TMPDIR}/difference.diff"
172+
_shunit_configureColor none
173+
# test new aligned difference
174+
_shunit_configureDiff always
175+
( assertEquals "deadbeef" "deadbuff" >"${stdoutF}" 2>"${stderrF}" ) || :
176+
diff -u "${stdoutF}" - >"${tempdiff}" 2>&1 <<EOF
177+
ASSERT:
178+
expected:<deadbeef>
179+
---------------^
180+
but was:<deadbuff>
181+
EOF
182+
rtrn=$?
183+
[ "${rtrn}" -eq "${SHUNIT_TRUE}" ] || cat "${tempdiff}" >&2
184+
rm "${tempdiff}"
185+
# test does it breaks original diff implementation
186+
_shunit_configureColor none
187+
_shunit_configureDiff none
188+
( assertEquals "deadbeef" "deadbuff" >"${stdoutF}" 2>"${stderrF}" ) || :
189+
diff -u "${stdoutF}" - >"${tempdiff}" 2>&1 <<EOF
190+
ASSERT:expected:<deadbeef> but was:<deadbuff>
191+
EOF
192+
rtrn=$?
193+
[ "${rtrn}" -eq "${SHUNIT_TRUE}" ] || cat "${tempdiff}" >&2
194+
rm "${tempdiff}"
195+
}
196+
197+
168198
# Demonstrate that asserts are no longer executed in subshells.
169199
# https://github.com/kward/shunit2/issues/123
170200
#

0 commit comments

Comments
 (0)