#!/usr/local/bin/bash



source this
if (this.isNumber $1); then
  level=$1
else 
  # remove the level parameter
  shift
fi
if [ -z "$level" ]; then
  level=3
fi
echo "starting: $0 <LOG_LEVEL=$level>"
source test.suite

log.level $level


RESULT="log"
test.case - "start sourced log function directly" \
  warn.log "test.case warned you as expected"
expect 0 "log"

test.case - "call sourced log function" \
  this.call warn.log "test.call warned you as expected"
expect 0 "log"


test.save.result() {
  create.result 0 "test case $TEST_COUNTER: $testName" save
}
test.case - "save a result"  test.save.result
expect 0 "test case $TEST_COUNTER: $testName"

create.result 0 "do not affect current env"
test.case - "source mycmd for one time and call feature3"  \
  this.call mycmd feature3 hello world
expect 0 "do not affect current env"

test.call.recall() 
{
  #suppresses the error on log level 2 and less
  if [ "$LOG_LEVEL" -lt "2" ]; then
    expect.error 127
  fi
  mycmd.feature3 hello world
}
test.case - "catch \e[1;31mERROR\e[1;32m: re-call mycmd feature3 produces an \e[1;31mERROR\e[1;32m because it was one time sourced" \
  test.call.recall
expect 127 "*"

# test.case - "re-call mycmd feature3 produces an error because it was one time sourced" \
#   this.call mycmd scope

test.case - "source mycmd for one time and call feature2"  \
  this.call mycmd feature2 hello world
expect 0 "do not affect current env"


test.case - "source mycmd permanently" \
  source mycmd 
expect 0 "*"

test.case - "use sourced mycmd and call feature3 directly" \
  mycmd.feature3 hello world
expect 0 "*"

#log.level 6

test.case - "re-call mycmd feature3 without sourcing it again" \
  this.call mycmd feature3 hello world
expect 0 "*"

test.case - "directly call soureced mycmd.feature2 without sourcing it again" \
  this.call mycmd.feature2 hello world
expect 0 "*"

expect.error 127
test.case - "call config.v and throw and error (and catch it) because it is not sourced" \
  this.call config.v 
expect 127 "*"

test.case - "call config v again but source it" \
  this.call config v
expect 0 "*"