#!/bin/bash

. common


#---------------------------------------------
# test MST_TESTDEP suite

test_initialize
test_result_hook result_hook

function test_testdep_setup
{

    #---------------
    # Ensure MST_TESTDEP_SETUP is automatically called from MST_TESTDEP_TEST

    test_begin_subtests

    prefix='MST_TESTDEP_TEST: AC_REQUIRE'               \
    config_str "$prefix"                                \
        'MST_TESTDEP_TEST([true],[success])'            \
        'test "$MST_REQUIRE_TESTDEPS" = yes || exit 1'

    run_test "$prefix"

    test_end_subtests

    #---------------
    # Ensure MST_TESTDEP_SETUP is automatically called from MST_TESTDEP_FLAG

    test_begin_subtests

    prefix='MST_TESTDEP_FLAG: AC_REQUIRE'
    config_str "$prefix"                                \
        'MST_TESTDEP_FLAG([yes],[success])'             \
        'test "$MST_REQUIRE_TESTDEPS" = yes || exit 1'

    run_test "$prefix"

    test_end_subtests


    #---------------
    # Test that MST_TESTDEP_STATUS([mandatory_flag]) works

    test_begin_subtests
    prefix='MST_TESTDEP_SETUP(): pass'
    config_str "$prefix"                                \
        'MST_TESTDEP_SETUP'                             \
        'MST_TESTDEP_FLAG([yes],[success])'             \
        'test "$MST_HAVE_TESTDEPS" = yes || exit 1'

    run_test "$prefix"
    test_end_subtests

    test_begin_subtests
    prefix='MST_TESTDEP_SETUP(): fail'
    config_str "$prefix"                                \
        'MST_TESTDEP_SETUP'                             \
        'MST_TESTDEP_FLAG([no],[failure])'              \
        'MST_TESTDEP_STATUS'

    run_test "$prefix" '' nok

    typeset pfx=$(testpfx)
    test_file_grep_is 'Use --disable-testdeps to ignore'        \
          $pfx.cfg.err                                          \
          "$prefix: expected configure errors"

    test_end_subtests

    test_begin_subtests
    prefix='MST_TESTDEP_SETUP([no]): pass'
    config_str "$prefix"                                \
        'MST_TESTDEP_SETUP([no])'                       \
        'MST_TESTDEP_FLAG([yes],[success])'             \
        'MST_TESTDEP_STATUS'

    run_test "$prefix"
    test_end_subtests


    test_begin_subtests
    prefix='MST_TESTDEP_SETUP([no]): fail'
    config_str "$prefix"                                \
        'MST_TESTDEP_SETUP([no])'                       \
        'MST_TESTDEP_FLAG([no],[failure])'              \
        'MST_TESTDEP_STATUS'

    run_test "$prefix" '' nok

    typeset pfx=$(testpfx)
    test_file_grep_is 'Use --disable-testdeps to ignore'        \
          $pfx.cfg.err                                          \
          "$prefix: expected configure errors"

    test_end_subtests


    test_begin_subtests
    prefix='MST_TESTDEP_SETUP([yes]): pass'
    config_str "$prefix"                                \
        'MST_TESTDEP_SETUP([yes])'                      \
        'MST_TESTDEP_FLAG([yes],[success])'             \
        'MST_TESTDEP_STATUS'

    run_test "$prefix"
    test_end_subtests


    test_begin_subtests
    prefix='MST_TESTDEP_SETUP([yes]): fail'
    config_str "$prefix"                                \
        'MST_TESTDEP_SETUP([yes])'                      \
        'MST_TESTDEP_FLAG([no],[failure])'              \
        'MST_TESTDEP_STATUS'

    run_test "$prefix" '' nok

    typeset pfx=$(testpfx)
    test_file_grep_is 'Please install them and try again'       \
          $pfx.cfg.err                                          \
          "$prefix: expected configure errors"

    test_end_subtests


    test_begin_subtests
    prefix='MST_TESTDEP_SETUP([illegal]): fail'
    config_str "$prefix"                        \
        'MST_TESTDEP_SETUP([illegal])'

    typeset pfx=$(testpfx)
    autoconf -B $srcdir -o configure configure.ac 2> $pfx.ac.err
    test_nok $? "$prefix: configure"

    test_file_grep_is 'unknown value for mandatory flag'        \
          $pfx.ac.err                                           \
          "$prefix: expected configure errors"

    test_end_subtests
}

#---------------
# MST_TESTDEP_TEST

function test_test
{
  test_begin_subtests

  result=$1
  shift;

  prefix="MST_TESTDEP_TEST: ($result) $@"

  echo AC_INIT          >  configure.ac

  for flag in "$@" ; do
    echo  "MST_TESTDEP_TEST([test yes = $flag],[$flag])" >> configure.ac
  done

  echo test '"$MST_HAVE_TESTDEPS"' = $result '|| exit 1' >> configure.ac
  echo AC_OUTPUT        >> configure.ac
  test_ok $? "$prefix: create autoconf input"

  run_test "$prefix" '' ok yes

  typeset pfx=$(testpfx)

  if [ $result = yes ] ;then

      test -s $pfx.cfg.err                                              \
          && test_fail "$prefix: configure -- unexpected error output"
  else

      typeset nlines=$(wc -l $pfx.cfg.err | perl -ane 'print $F[0]')
      test_num_is "$nlines" 1 "$prefix: number of expected configure errors"

      test_file_grep_is 'Missing.*dependency'   \
          $pfx.cfg.err                          \
          "$prefix: expected configure errors"

  fi

  test_end_subtests

}

function _run_test_test
{

    test_test no    no
    test_test yes   yes
    test_test no    no yes
    test_test no    yes no
    test_test no    yes no yes

}

#---------------
# MST_TESTDEP_FLAG


function test_flags
{
  test_begin_subtests

  result=$1
  shift;

  prefix="MST_TESTDEP_FLAG: ($result) $@"

  echo AC_INIT          >  configure.ac

  for flag in "$@" ; do
    echo  "MST_TESTDEP_FLAG([$flag],[$flag])" >> configure.ac
  done

  echo test '"$MST_HAVE_TESTDEPS"' = $result '|| exit 1' >> configure.ac
  echo AC_OUTPUT        >> configure.ac
  test_ok $? "$prefix: create autoconf input"

  run_test "$prefix" '' ok yes

  typeset pfx=$(testpfx)

  if [ $result = yes ] ;then

      test -s $pfx.cfg.err                                              \
          && test_fail "$prefix: configure -- unexpected error output"
  else

      typeset nlines=$(wc -l $pfx.cfg.err | perl -ane 'print $F[0]')
      test_num_is "$nlines" 1 "$prefix: number of expected configure errors"

      test_file_grep_is 'Missing.*dependency'   \
          $pfx.cfg.err                          \
          "$prefix: expected configure errors"

  fi

  test_end_subtests

}

function _run_test_flags
{

    test_flags no   no
    test_flags yes  yes
    test_flags no   no yes
    test_flags no   yes no
    test_flags no   yes no yes

}

#---------------
# MST_TESTDEP_FLAG
#  blank message should not result in a warning

function test_flag_msg
{
  test_begin_subtests

  result=$1
  flag=$2
  msg=$3

  prefix="MST_TESTDEP_FLAG message: ($result) flag=$flag msg=$msg"

  echo AC_INIT          >  configure.ac

  echo  "MST_TESTDEP_FLAG([$flag],[$msg])" >> configure.ac

  echo test '"$MST_HAVE_TESTDEPS"' = $result '|| exit 1' >> configure.ac
  echo AC_OUTPUT        >> configure.ac
  test_ok $? "$prefix: create autoconf input"

  # have run_test check error output if test is expected to fail or msg is blank; will
  # check below in other cases

  { [ "$result" = ok -o -z "$msg" ] && ignore_err_log=no ; } ||  ignore_err_log=yes

  run_test "$prefix" '' ok $ignore_err_log

  if [ "$ignore_err_log" = yes ]; then

      typeset pfx=$(testpfx)

      typeset nlines=$(wc -l $pfx.cfg.err | perl -ane 'print $F[0]')
      test_num_is "$nlines" 1 "$prefix: number of expected configure errors"


      test_file_grep_is                                 \
            "WARNING: $msg: Missing test dependency"    \
            $(testpfx).cfg.err                          \
            "$prefix: non-blank message"

  fi

  test_end_subtests

}

function _run_test_flag_msg
{

    test_flag_msg no   no  msg
    test_flag_msg yes  yes
    test_flag_msg no   no
    test_flag_msg yes  yes

}

#---------------
# MST_CHECK_TESTPROG

function test_prog
{
  test_begin_subtests

  result=$1
  shift;

  prefix="MST_CHECK_TESTPROG: ($result) $@"

  echo AC_INIT          >  configure.ac

  for prog in "$@" ; do
    echo  "MST_CHECK_TESTPROG( [$prog] )" >> configure.ac
  done

  echo test '"$MST_HAVE_TESTDEPS"' = $result '|| exit 1' >> configure.ac
  echo AC_OUTPUT        >> configure.ac
  test_ok $? "$prefix: create autoconf input"

  run_test "$prefix"

  test_end_subtests
}

function _run_test_prog
{

    test_prog yes   ' true '
    test_prog yes   ' true ' 'false'
    test_prog no    ' does-not-exist'
    test_prog no    ' does_not_exist'
    test_prog no    ' does_not_exist' 'true'
    test_prog no    ' true' 'does_not_exist'

}

#---------------
# MST_CHECK_TEST_PKG_MODULES

function test_pkg_modules {

    test_begin_subtests

    result=$1
    module=$2

    prefix="MST_CHECK_TEST_PKG_MODULES: ($result) $module"

    echo AC_INIT		>  configure.ac

    echo  "MST_CHECK_TEST_PKG_MODULES([TESTMODULE],[$module])" >> configure.ac

    echo test '"$MST_HAVE_TESTDEPS"' = $result '|| exit 1' >> configure.ac
    echo AC_OUTPUT	>> configure.ac
    test_ok $? "$prefix: create autoconf input"

    run_test "$prefix"

    test_end_subtests
}


test_pkg_modules yes testlib_sh
test_pkg_modules no  package_does_not_exist


#---------------
# MST_TESTDEP_STATUS

function test_status
{
  test_begin_subtests

  result=$1
  flag=$2
  config_args="$3"
  ignore_err_log="${4:-no}"


  prefix="MST_TESTDEP_STATUS: ($result) input: $flag"

  if [ -n "$config_args" ]; then
    prefix="$prefix config args: $config_args"
  else
    prefix="$prefix config args: (none)"
  fi

  config_str "$prefix"                                  \
             "MST_TESTDEP_FLAG([$flag],[$flag])"        \
             "MST_TESTDEP_STATUS"


  run_test "$prefix" "$config_args" $result $ignore_err_log

  test_end_subtests
}

function _run_test_status
{

    test_status ok yes
    test_status nok no
    test_status ok yes --disable-testdeps
    test_status ok no  --disable-testdeps yes

}

test_testdep_setup
_run_test_test
_run_test_flags
_run_test_flag_msg
_run_test_prog
_run_test_status

test_summary
test_exit
