@@ -49,6 +49,8 @@ SHUNIT_CMD_TPUT=${SHUNIT_CMD_TPUT:-${__SHUNIT_CMD_TPUT}}
49
49
50
50
# Enable color output. Options are 'auto', 'always', or 'never'.
51
51
SHUNIT_COLOR=${SHUNIT_COLOR:- auto}
52
+ # Enable difference alignment output. Options are 'auto', 'always', or 'never'.
53
+ SHUNIT_DIFF_ALIGN=${SHUNIT_DIFF_ALIGN:- never}
52
54
53
55
#
54
56
# Internal constants.
@@ -647,6 +649,35 @@ fail() {
647
649
# shellcheck disable=SC2016,SC2034
648
650
_FAIL_=' eval fail --lineno "${LINENO:-}"'
649
651
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
+
650
681
# Records a test failure, stating two values were not equal.
651
682
#
652
683
# Args:
@@ -675,9 +706,14 @@ failNotEquals() {
675
706
shunit_actual_=$2
676
707
677
708
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
678
714
_shunit_assertFail " ${shunit_message_: +${shunit_message_} } expected:<${shunit_expected_} > but was:<${shunit_actual_} >"
679
715
680
- unset shunit_message_ shunit_expected_ shunit_actual_
716
+ unset shunit_message_ shunit_expected_ shunit_actual_ diff_position_
681
717
return ${SHUNIT_FALSE}
682
718
}
683
719
# shellcheck disable=SC2016,SC2034
@@ -1043,6 +1079,26 @@ _shunit_cleanup() {
1043
1079
unset _shunit_name_ _shunit_signal_
1044
1080
}
1045
1081
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
+
1046
1102
# configureColor based on user color preference.
1047
1103
#
1048
1104
# Args:
1329
1385
# Configure default output coloring behavior.
1330
1386
_shunit_configureColor " ${SHUNIT_COLOR} "
1331
1387
1388
+ # Configure default difference alignment behavior.
1389
+ _shunit_configureDiff " ${SHUNIT_DIFF_ALIGN} "
1390
+
1332
1391
# Execute the oneTimeSetUp function (if it exists).
1333
1392
if ! oneTimeSetUp; then
1334
1393
_shunit_fatal " oneTimeSetUp() returned non-zero return code."
0 commit comments