@@ -49,6 +49,8 @@ SHUNIT_CMD_TPUT=${SHUNIT_CMD_TPUT:-${__SHUNIT_CMD_TPUT}}
4949
5050# Enable color output. Options are 'auto', 'always', or 'never'.
5151SHUNIT_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:
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).
13331392if ! oneTimeSetUp; then
13341393 _shunit_fatal " oneTimeSetUp() returned non-zero return code."
0 commit comments