Add lots of tests

This commit is contained in:
Ilan Cosman 2020-05-08 22:48:35 -07:00
parent 41ee1233bf
commit a54a3dead5
9 changed files with 81 additions and 62 deletions

11
dev/bench.fish Normal file
View File

@ -0,0 +1,11 @@
function bench -a command times
set -l thingToRun
for i in (seq $times)
set -a thingToRun $command
end
eval (string join ';' $thingToRun) >>'/dev/null'
math $CMD_DURATION/$times
end

View File

@ -30,24 +30,19 @@ function _lean_status
end
function _lean_cmd_duration
set -l seconds (math --scale=$lean_cmd_duration_decimals $CMD_DURATION/1000)
if test $CMD_DURATION -gt $lean_cmd_duration_threshold
set -l seconds (math --scale=$lean_cmd_duration_decimals "$CMD_DURATION/1000" % 60)
set -l minutes (math -s0 "$seconds/60" % 60)
set -l hours (math -s0 "$minutes/60" % 60)
if test $seconds -gt $lean_cmd_duration_threshold
set -l secondsMod (math $seconds % 60)
set -l minutes (math -s0 $seconds/60)
set -l minutesMod (math $minutes % 60)
set -l hours (math -s0 $minutes/60)
for time in hours minutesMod secondsMod
for time in hours minutes seconds
if test $$time -eq 0
set -e $time
end
end
set_color $lean_cmd_duration
echo -n {$hours}'h' {$minutesMod}'m' {$gsecondsMod}'s'
set_color $lean_cmd_duration_color
echo -n {$hours}'h' {$minutes}'m' {$seconds}'s'
end
end

View File

@ -64,7 +64,7 @@ function lean_install
# --------------Cmd_Duration--------------
set -U lean_cmd_duration_color 87875F
set -U lean_cmd_duration_decimals 0
set -U lean_cmd_duration_threshold 3
set -U lean_cmd_duration_threshold 3000
# -------------Context-------------
set -U lean_context_ssh_color D7AF87
set -U lean_context_root_color D7AF00

View File

@ -247,7 +247,7 @@ function _displayRestartAndQuit
echo -e '(q) Quit and do nothing\n'
end
function _quit
function _quit --on-signal INT
functions -e fish_right_prompt
source $fishPrompt
clear

15
tests/cmd_duration.fish Normal file
View File

@ -0,0 +1,15 @@
# Fishtape test
source "$__fish_config_dir/functions/lean_right_prompt.fish"
function _cmd_duration -a duration threshold decimals
set -g CMD_DURATION $duration
set -g lean_cmd_duration_threshold $threshold
set -g lean_cmd_duration_decimals $decimals
lean_decolor (_lean_cmd_duration)
end
@test 'Less than threshold' (_cmd_duration 2000 3000 0) -z
@test 'Decimals' (_cmd_duration 4567 3000 3) = '4.567s'
@test 'Minutes' (_cmd_duration 456700 3000 0) = '7m 36s'
@test 'Hours' (_cmd_duration 4567000 3000 0) = '1h 16m 7s'

11
tests/context.fish Normal file
View File

@ -0,0 +1,11 @@
# Fishtape test
set -l sourceFile "$__fish_config_dir/functions/lean_right_prompt.fish"
source $sourceFile
function _context
lean_decolor (_lean_context)
end
@test 'None' (_context) -z
@test 'SSH' (set -g SSH_TTY 'true'; _context) = $USER'@'(prompt_hostname)
@test 'Root' (lean_decolor (su -c "source $sourceFile;_lean_context")) = 'root@'(prompt_hostname)

9
tests/jobs.fish Normal file
View File

@ -0,0 +1,9 @@
# Fishtape test
source "$__fish_config_dir/functions/lean_right_prompt.fish"
function _jobs
lean_decolor (_lean_jobs)
end
@test 'No Jobs' (_jobs) -z
@test 'Jobs' (sleep 60 &; _jobs) = "$lean_jobs_icon"

View File

@ -1,43 +1,21 @@
function test_pwd
set -l dB (set_color $lean_color_dark_blue)
set -l lB (set_color -o $lean_color_light_blue)
set -l l (set_color $lean_color_lilac)
set -l n (set_color normal)
# Fishtape test
function _pwd -a dir
cd $dir
lean_decolor (lean_pwd)
end
set -l unwr $dB"$lean_pwd_unwritable_icon "$n
set -l unwr "$lean_pwd_unwritable_icon "
echo 'Please perform a visual inspection.'
echo '------------'
echo 'OUTPUT'
echo 'ANSWER'
echo '------------'
_comparePwd '/' (_joinSlash $unwr '')
_comparePwd '/usr' (_joinSlash $unwr $lB'usr')
_comparePwd '/usr/share' (_joinSlash $unwr $lB'usr' $lB'share')
_comparePwd '/usr/share/fish' (_joinSlash $unwr $lB'usr' $dB'share' $lB'fish')
_comparePwd "$HOME" (_joinSlash $lB'~')
_comparePwd "$HOME/.config" (_joinSlash $lB'~' $lB'.config')
_comparePwd "$HOME/.config/fish" (_joinSlash $lB'~' $dB'.config' $lB'fish')
@test '/' (_pwd '/') = $unwr'/'
@test '/usr' (_pwd '/usr') = $unwr'/usr'
@test '/usr/share' (_pwd '/usr/share') = $unwr'/usr/share'
@test '~' (_pwd "$HOME") = '~'
@test '~/.config' (_pwd "$HOME/.config") = '~/.config'
@test '~/.config/fish' (_pwd "$HOME/.config/fish") = '~/.config/fish'
set -l longDir "$HOME/alfa/bravo/charlie/delta/echo/foxtrot/golf/hotel/inda/juliett/kilo/lima/mike/november/oscar/papa"
set -l longDirAnswerLilac $l{'a','b','c','d','e','f'}
set -l longDirAnswerDarkBlue $dB{'golf','hotel','inda','juliett','kilo','lima','mike','november','oscar'}
set -l longDirAnswer (_joinSlash $lB'~' $longDirAnswerLilac $longDirAnswerDarkBlue $lB'papa')
if not test -e $longDir
mkdir -p $longDir
end
_comparePwd $longDir $longDirAnswer
@test 'Long dir' (_pwd "$longDir") = '~/a/b/c/d/e/f/golf/hotel/inda/juliett/kilo/lima/mike/november/oscar/papa'
rm -r $longDir
end
function _comparePwd -a dir answer
cd $dir
echo (lean_pwd)
echo $answer
end
function _joinSlash
set -l slash (set_color $lean_color_dark_blue)'/'(set_color normal)
string join $slash {$argv}(set_color normal)
end

View File

@ -1,15 +1,15 @@
# Fishtape test
source "$__fish_config_dir/functions/lean_right_prompt.fish"
function _case
function _status
set -g last_pipestatus $pipestatus
set -g last_status $status
_lean_status
lean_decolor (_lean_status)
end
@test 'true' (true;_case) -z
@test 'false' (false;_case) -z
@test 'true|false' (true|false;_case) = '✘ 0|1'
@test 'true|true' (true|true;_case) -z
@test 'false|true' (false|true;_case) = '✔ 1|0'
@test 'false|false' (false|false;_case) = '✘ 1|1'
@test 'true' (true; _status) -z
@test 'false' (false; _status) -z
@test 'true|false' (true|false; _status) = "$lean_status_failure_icon 0|1"
@test 'true|true' (true|true; _status) -z
@test 'false|true' (false|true; _status) = "$lean_status_success_icon 1|0"
@test 'false|false' (false|false; _status) = "$lean_status_failure_icon 1|1"