#!/usr/bin/env bash
#clear
#export PS4='\e[90m+${LINENO} in ${#BASH_SOURCE[@]}>${FUNCNAME[0]}:${BASH_SOURCE[@]##*/} \e[0m'
#set -x

source test.suite $*

log.init.colors

echo -e "${BOLD_CYAN}test starting: $0 <LOG_LEVEL=$level>${NORMAL}"
log.level $level



RESULT="result not loaded"
test.case - "direct call with parameter and functions: 3 f g 1 a b c" \
  mycmd 3 f g 1 a b c

RESULT="result not loaded"
expect 0 "result not loaded" "$RESULT after a direct call"

result.load
expect 0 "result not loaded" "$RESULT: currently a bug...scope level is already swapped out"

source $CONFIG_PATH/result.env
expect 0 "c should be \"c\"" "but it works with the same command directly
$RESULT"

RESULT="result not loaded"
console.log "
Test 2 
===================================================================="
mycmd log log.level $level log warn.log test log important.log cool 2 d e 3 f g 1 a b c
source $CONFIG_PATH/result.env
expect 0 "c should be \"c\"" "result loaded...
$RESULT"

RESULT="result not loaded"
test.case - "2a)  same as 2 but through test.case" \
  mycmd log log.level $level log warn.log test log important.log cool 2 d e 3 f g 1 a b c
source $CONFIG_PATH/result.env
expect 0 "c should be \"c\"" "result loaded...
$RESULT"

RESULT="result not loaded"
test.case - "show usage: since here init never was sourced, log levels is default on 3!" \
  mycmd
expect 0 "*" "if you can see the usage message"


console.log "
Test 4a init and call non sourced function mycmd.3 fails as expected 
===================================================================="
source init
log.level $level

expect.error 127
mycmd.3 e f
console.log "RETURN: $RETURN_VALUE  Result: $RESULT 
====================================================================

"
expect 127 "*"

console.log "
Test 4b sourcing and calling mycmd.2 and printing results 
===================================================================="

this.call mycmd mycmd.2 d e
console.log "RETURN: $RETURN_VALUE  Result: $RESULT 
====================================================================

"


console.log "Test 5  re-sourcing and calling mycmd.3 and getting an error 
===================================================================="

mycmd.3 e f
console.log "RETURN: $RETURN_VALUE  Result: $RESULT 
====================================================================

"

console.log "Test 6  getting the absolute path to this script ./mycmd 
===================================================================="

this.absolutePath ./mycmd
console.log "RETURN: $RETURN_VALUE  Result: $RESULT 
====================================================================

"
this.absolutePath
console.log "RETURN: $RETURN_VALUE  Result: $RESULT 
====================================================================

"

test.case - "this.caller" this.caller
expect 0 "test.mycmd" "$RESULT"

test.case - "this.caller.function" this.caller.function
expect 0 "test.case" "$RESULT"

this.caller.function
expect 0 "main" "$RESULT"