#!/bin/bash

. common


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

test_initialize
test_result_hook result_hook

#---------------
# Ensure MST_CHECK_SETUP is automatically called from MST_CHECK_TEST

test_begin_subtests

prefix='MST_CHECK_TEST: AC_REQUIRE'
config_str "$prefix"					\
    'MST_CHECK_TEST([true],[failure])'	\
    'test "$MST_CHECKS_OK" = yes || exit 1'

run_test "$prefix"

test_end_subtests

#---------------
# Ensure MST_CHECK_SETUP is automatically called from MST_CHECK_FLAG

test_begin_subtests

prefix='MST_CHECK_FLAG: AC_REQUIRE'
config_str "$prefix"	\
    'MST_CHECK_FLAG([yes],[true],[false])'	\
    'test "$MST_CHECKS_OK" = yes || exit 1'

run_test "$prefix"

test_end_subtests

#---------------
# MST_CHECK_TEST

function test_test
{
  test_begin_subtests

  typeset result=$1
  shift;

  typeset prefix="MST_CHECK_TEST: ($result) $@"

  echo AC_INIT		>  configure.ac

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

  echo test '"$MST_CHECKS_OK"' = $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
}


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_CHECK_PROG

function test_test
{
  test_begin_subtests

  result=$1
  shift;

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

  echo AC_INIT		>  configure.ac

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

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

  run_test "$prefix"

  test_end_subtests
}


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


#---------------
# MST_CHECK_FLAG


function test_flags
{
  test_begin_subtests

  result=$1
  shift;

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

  echo AC_INIT		>  configure.ac

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

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

  run_test "$prefix"

  test_end_subtests
}


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_CHECK_PKG_MODULES

function test_pkg_modules {

    test_begin_subtests

    result=$1
    module=$2

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

    echo AC_INIT		>  configure.ac

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

    echo test '"$MST_CHECKS_OK"' = $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_CHECK_STATUS

function test_status
{
  test_begin_subtests

  result=$1
  flag=$2

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

  config_str "$prefix"	\
             "MST_CHECK_FLAG([$flag])" \
   	     "MST_CHECK_STATUS"

  run_test "$prefix" '' $result

  test_end_subtests
}

test_status ok yes
test_status nok no

test_summary
test_exit
