From ca6cbaa0b663b580c62d3187616af4d0aed79758 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Tue, 9 Feb 2016 21:43:57 +0100 Subject: Add unit tests for printSizeHumanReadable --- test/functions/utilities.spec | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 test/functions/utilities.spec (limited to 'test/functions/utilities.spec') diff --git a/test/functions/utilities.spec b/test/functions/utilities.spec new file mode 100755 index 00000000..12501348 --- /dev/null +++ b/test/functions/utilities.spec @@ -0,0 +1,22 @@ +#!/usr/bin/env zsh +#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8 + +# Required for shunit2 to run correctly +setopt shwordsplit +SHUNIT_PARENT=$0 + +function oneTimeSetUp() { + # Load Powerlevel9k + source functions/utilities.zsh +} + +function testPrintSizeHumanReadableWithBigNumber() { + # Interesting: Currently we can't support numbers bigger than that. + assertEquals '0.87E' "$(printSizeHumanReadable 1000000000000000000)" +} + +function testPrintSizeHumanReadableWithExabytesAsBase() { + assertEquals '9.77Z' "$(printSizeHumanReadable 10000 'E')" +} + +source shunit2/source/2.1/src/shunit2 -- cgit v1.2.3 From c0c611c21d9913bf333bc54b71af67b70107d0f9 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Fri, 12 Feb 2016 00:30:48 +0100 Subject: Add tests for functions/utilities.zsh In particular: `defined`, `set_default`, `getRelevantItem` and `segmentShouldBeJoined` --- test/functions/utilities.spec | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'test/functions/utilities.spec') 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 -- cgit v1.2.3 From a3eaf4a4b0098918d3b39dbe705fa587032c5648 Mon Sep 17 00:00:00 2001 From: Dominik Ritter Date: Sat, 13 Feb 2016 17:35:06 +0100 Subject: Improve isolation of tests --- test/functions/colors.spec | 2 +- test/functions/utilities.spec | 3 ++- test/powerlevel9k.spec | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) (limited to 'test/functions/utilities.spec') diff --git a/test/functions/colors.spec b/test/functions/colors.spec index d1c5a6fc..61a40087 100755 --- a/test/functions/colors.spec +++ b/test/functions/colors.spec @@ -5,7 +5,7 @@ setopt shwordsplit SHUNIT_PARENT=$0 -function oneTimeSetUp() { +function setUp() { # Load Powerlevel9k source functions/colors.zsh } diff --git a/test/functions/utilities.spec b/test/functions/utilities.spec index ad0f4efb..a9bd67e3 100755 --- a/test/functions/utilities.spec +++ b/test/functions/utilities.spec @@ -5,8 +5,9 @@ setopt shwordsplit SHUNIT_PARENT=$0 -function oneTimeSetUp() { +function setUp() { # Load Powerlevel9k + source functions/icons.zsh source functions/utilities.zsh } diff --git a/test/powerlevel9k.spec b/test/powerlevel9k.spec index 7a85e516..06f0868e 100755 --- a/test/powerlevel9k.spec +++ b/test/powerlevel9k.spec @@ -5,7 +5,7 @@ setopt shwordsplit SHUNIT_PARENT=$0 -function oneTimeSetUp() { +function setUp() { # Load Powerlevel9k source powerlevel9k.zsh-theme source functions/* -- cgit v1.2.3