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

level=$1
if [ -z "$level" ]; then
  level=1
fi
echo "starting: ${BASH_SOURCE[@]##*/} <LOG_LEVEL=$1>"

#echo "sourcing init"
source this
source test.suite

log.level $level

competionArray=(once config list file ite)
source oo

test.line() 
{
((TEST_COUNTER++))
console.log "


Test 0: line \"$*\"
===================================================================="
line.start "$@"
console.log "RETURN: $RETURN_VALUE  Result: $RESULT 
===================================================================="
}

test.case - "line test start" \
   line usage
expect 0 "*" "Test start"

source line

# ============================================================================
# Regression: [args...] must be stripped from paramList (commit 58048e1)
# Without the fix, this generates: declare: '[args...]=addDefaultValue': not a valid identifier
# ============================================================================
test.case $level "completion: [args...] stripped from paramList" \
  echo "branch command [args...]" '|' line.parse.paramList.new

# Feed a parameter list containing [args...] through the parser
PARSE_OUTPUT=$(echo "branch command [args...]" | line.parse.paramList.new 2>&1)
PARSE_RC=$?

# Check no "not a valid identifier" error
if echo "$PARSE_OUTPUT" | grep -q 'not a valid identifier'; then
  expect.fail "paramList parser should not produce 'not a valid identifier' for [args...]"
else
  expect.pass "no invalid identifier error from [args...]"
fi

# Check that [args...] does not appear as a PARAM_ variable in the output
if echo "$PARSE_OUTPUT" | grep -q 'PARAM_\['; then
  expect.fail "paramList should strip [args...], not create PARAM_[args...]"
else
  expect.pass "[args...] stripped from paramList output"
fi

# Check that valid params (branch, command) are still parsed
PARAM_COUNT=$(echo "$PARSE_OUTPUT" | grep -c 'PARAM_' || true)
if [ "$PARAM_COUNT" -ge 1 ]; then
  expect.pass "valid PARAM_ variables generated ($PARAM_COUNT found)"
else
  expect.fail "should generate at least 1 PARAM_ variable"
fi

# ============================================================================
# T-FORMAT-1: line.format produces correct output with 3-arg format
# ============================================================================
test.case $level "line.format produces correct output with 3-arg format" \
  echo "checking"

FORMAT_TEST="[%s|%s|%s]"
RESULT=$(printf "'a'\n'b'\n'c'\n" | line.format FORMAT_TEST 2>&1)
if [ "$RESULT" = "[a|b|c]" ]; then
  create.result 0 "correct"
else
  create.result 1 "got: $RESULT"
fi
expect 0 "correct" "line.format with 3-arg format produces [a|b|c]"
unset FORMAT_TEST

test.suite.save.results
