diff options
author | Dominik Ritter <dritter03@googlemail.com> | 2016-02-12 02:30:48 +0300 |
---|---|---|
committer | Dominik Ritter <dritter03@googlemail.com> | 2016-02-12 03:18:35 +0300 |
commit | c0c611c21d9913bf333bc54b71af67b70107d0f9 (patch) | |
tree | 8fead4a567f566a64074759ae5180e3a31ae57a2 /test | |
parent | d7a0f493912ecdfe117c7e67fac0a355f34bb4a4 (diff) |
Add tests for functions/utilities.zsh
In particular: `defined`, `set_default`, `getRelevantItem` and
`segmentShouldBeJoined`
Diffstat (limited to 'test')
-rwxr-xr-x | test/functions/utilities.spec | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/test/functions/utilities.spec b/test/functions/utilities.spec index 12501348..ad0f4efb 100755 --- a/test/functions/utilities.spec +++ b/test/functions/utilities.spec @@ -10,6 +10,24 @@ function oneTimeSetUp() { source functions/utilities.zsh } +function testDefinedFindsDefinedVariable() { + my_var='X' + + assertTrue "defined 'my_var'" + unset my_var +} + +function testDefinedDoesNotFindUndefinedVariable() { + assertFalse "defined 'my_var'" +} + +function testSetDefaultSetsVariable() { + set_default 'my_var' 'x' + + assertEquals 'x' "$my_var" + unset my_var +} + function testPrintSizeHumanReadableWithBigNumber() { # Interesting: Currently we can't support numbers bigger than that. assertEquals '0.87E' "$(printSizeHumanReadable 1000000000000000000)" @@ -19,4 +37,72 @@ function testPrintSizeHumanReadableWithExabytesAsBase() { assertEquals '9.77Z' "$(printSizeHumanReadable 10000 'E')" } +function testGetRelevantItem() { + typeset -a list + list=(a b c) + local callback='[[ "$item" == "b" ]] && echo "found"' + + local result=$(getRelevantItem "$list" "$callback") + assertEquals 'found' "$result" + + unset list +} + +function testGetRelevantItemDoesNotReturnNotFoundItems() { + typeset -a list + list=(a b c) + local callback='[[ "$item" == "d" ]] && echo "found"' + + local result=$(getRelevantItem "$list" "$callback") + assertEquals '' '' + + unset list +} + +function testSegmentShouldBeJoinedIfDirectPredecessingSegmentIsJoined() { + typeset -a segments + segments=(a b_joined c_joined) + # Look at the third segment + local current_index=3 + local last_element_index=2 + + local joined + segmentShouldBeJoined $current_index $last_element_index "$segments" && joined=true || joined=false + assertTrue "$joined" + + unset segments +} + +function testSegmentShouldBeJoinedIfPredecessingSegmentIsJoinedTransitivley() { + typeset -a segments + segments=(a b_joined c_joined) + # Look at the third segment + local current_index=3 + # The last printed segment was the first one, + # the second segmend was conditional. + local last_element_index=1 + + local joined + segmentShouldBeJoined $current_index $last_element_index "$segments" && joined=true || joined=false + assertTrue "$joined" + + unset segments +} + +function testSegmentShouldNotBeJoinedIfPredecessingSegmentIsNotJoinedButConditional() { + typeset -a segments + segments=(a b_joined c d_joined) + # Look at the fourth segment + local current_index=4 + # The last printed segment was the first one, + # the second segmend was conditional. + local last_element_index=1 + + local joined + segmentShouldBeJoined $current_index $last_element_index "$segments" && joined=true || joined=false + assertFalse "$joined" + + unset segments +} + source shunit2/source/2.1/src/shunit2 |