#
# Unit tests with libcheck
#

AC_DEFUN([RNA_ENABLE_UNIT_TESTS],[

  RNA_ADD_FEATURE([unittests],
                  [Unit tests],
                  [yes])

  RNA_ADD_PACKAGE([check],
                  [C-library Unit tests],
                  [yes])

  RNA_ADD_FEATURE([check_executables],
                  [Unit tests for executables],
                  [yes])

  RNA_ADD_FEATURE([check_perl],
                  [Perl interface Unit tests],
                  [yes])

  RNA_ADD_FEATURE([check_python],
                  [Python 3.x interface Unit tests],
                  [yes])

  RNA_ADD_FEATURE([check_python2],
                  [Python 2.x interface Unit tests],
                  [yes])

  RNA_FEATURE_IF_ENABLED([unittests],[

  AC_REQUIRE_AUX_FILE([tap-driver.sh])

    # check prerequisties for unit testing support
    RNA_PACKAGE_IF_ENABLED([check],[
      PKG_CHECK_MODULES([CHECK], [check >= 0.9.4], [],[
        AC_MSG_WARN([check not found -- will not build C-library Unit tests])
        with_check="no"
        enabled_but_failed_check="(check framework not found)"
      ])
    ])

    RNA_PACKAGE_IF_ENABLED([check],[
      AC_DEFINE([WITH_CHECK], [1], [Include C-library Unit tests])
    ])

    RNA_FEATURE_IF_ENABLED([check_executables],[
      AC_ARG_VAR([DIFF],[the 'diff' program to use for test output comparison])
      AC_PATH_PROG([DIFF],[diff],[])
      if test "x$DIFF" = "x"; then
        AC_MSG_WARN([diff not found -- deactivating check for executables!])
        AC_MSG_NOTICE([==> Set DIFF environment variable if present in non-standard path!])
        enable_check_executables="no"
      fi
    ])

    RNA_FEATURE_IF_ENABLED([check_perl],[
      ## switch off if perl interface is not going to be build
      if test "x$with_perl" != "xyes" ; then
        enable_check_perl="no"
      fi
    ])

    RNA_FEATURE_IF_ENABLED([check_python],[
      ## switch off if python interface is not going to be build
      if test "x$with_python" != "xyes" ; then
        enable_check_python="no"
      fi
    ])

    RNA_FEATURE_IF_ENABLED([check_python2],[
      ## switch off if python2 interface is not going to be build
      if test "x$with_python2" != "xyes" ; then
        enable_check_python2="no"
      fi
    ])

  ])

  AS_IF([test "x$enable_unittests" = "xno"],[
    with_check="no"
    enable_check_perl="no"
    enable_check_python="no"
    enable_check_python2="no"
  ])

  AM_CONDITIONAL(WITH_UNIT_TESTS, test "x$enable_unittests" != "xno")
  AM_CONDITIONAL(WITH_CHECK, test "x$with_check" != "xno")
  AM_CONDITIONAL(WITH_EXECUTABLE_TESTS, test "x$with_check_executables" != "xno")
  AM_CONDITIONAL(WITH_PERL_TESTS, test "x$enable_check_perl" != "xno")
  AM_CONDITIONAL(WITH_PYTHON_TESTS, test "x$enable_check_python" != "xno")
  AM_CONDITIONAL(WITH_PYTHON2_TESTS, test "x$enable_check_python2" != "xno")

  AC_CONFIG_FILES([tests/Makefile tests/RNApath.py tests/RNAHelpers.pm tests/test-env.sh])
])

