Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Frantisek Boranek committed Sep 19, 2018
1 parent 5e3f50e commit 375f29e
Show file tree
Hide file tree
Showing 11 changed files with 411 additions and 5 deletions.
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
**/.deps
**/.libs/
**/debian/*.debhelper.log
**/debian/*.substvars
**/debian/autoreconf.*
Expand All @@ -6,7 +8,9 @@
*.debhelper
*.la
*.lo
*.log
*.o
*.trs
aclocal.m4
autom4te.cache
compile
Expand All @@ -27,13 +31,14 @@ Makefile.in
missing
python/.pybuild
python/debian/python-teng/**
src/.libs/
src/Makefile
src/Makefile.in
src/tengconfig.h
src/tenglex2.cc
src/tengsyntax.cc
src/tengsyntax.hh
test-driver
test_base
test_filesystem
version
ylwrap
src/.deps
4 changes: 2 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
# Created.
#

SUBDIRS = src
# directories temporarily out of order: 'test', 'doc'
SUBDIRS = . src tests
# directories temporarily out of order: 'doc'

#
# Removed due to this request:
Expand Down
16 changes: 16 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ AM_SILENT_RULES

# tell autoconf where to find sources
AC_CONFIG_SRCDIR([src/teng.cc])
AC_CONFIG_FILES([tests/Makefile])

# check for C++ compiler
AC_PROG_CXX
Expand Down Expand Up @@ -129,6 +130,14 @@ AC_ARG_ENABLE(lock-udf,[ --disable-udf-locks compile without thread safe UDF],[
AM_PROG_LEX
AC_PROG_YACC

AX_BOOST_BASE([1.48],, [AC_MSG_ERROR([tests needs Boost, but it was not found in your system])])
AX_BOOST_SYSTEM
AX_BOOST_FILESYSTEM
AX_BOOST_UNIT_TEST_FRAMEWORK
AC_SUBST(BOOST_FILESYSTEM_LIB)
AC_SUBST(BOOST_SYSTEM_LIB)
AC_SUBST(BOOST_UNIT_TEST_FRAMEWORK_LIB)

# check for special header files
AC_CHECK_HEADERS(fenv.h)

Expand All @@ -139,6 +148,13 @@ AC_REPLACE_FUNCS([trunc round])
AC_LANG_CPLUSPLUS
AC_CHECK_LIB([pcre++], [_init],,AC_MSG_ERROR([pcre++ not installed]))

AX_CHECK_COMPILE_FLAG([-std=c++14], [
check_cpp14="true"
AC_MSG_NOTICE([Compiler with c++14, test are enabled.])
],[AC_MSG_NOTICE([Compiler without c++14, test are disabled.])])

AM_CONDITIONAL([CPP14], [test x$check_cpp14 == xtrue])

AS_IF([test x$check_pthreads == xyes], [
# Check for nonglibc header from serr library. The library is not
# needed just this header.
Expand Down
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Source: libteng
Section: Seznam
Priority: optional
Maintainer: Seznam.cz a.s. <opensource@firma.seznam.cz>
Build-Depends: debhelper (>= 7.0.50), dh-autoreconf, bison, flex, libpcre++-dev, libpcre++0 | libpcre++0v5, libglib2.0-dev, pkg-config, libcurl-dev
Build-Depends: debhelper (>= 7.0.50), dh-autoreconf, bison, flex, libpcre++-dev, libpcre++0 | libpcre++0v5, libglib2.0-dev, pkg-config, libcurl-dev, libboost-test-dev
Standards-Version: 3.7.2.2
Vcs-Git: git://github.com/seznam/teng.git
Vcs-Browser: https://github.com/seznam/teng
Expand Down
42 changes: 42 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Teng -- a general purpose templating engine.
# Copyright (C) 2018 Seznam.cz, a.s.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Seznam.cz, a.s.
# Naskove 1, Praha 5, 15000, Czech Republic
# http://www.seznam.cz, mailto:teng@firma.seznam.cz
#

AM_LDFLAGS = -L@top_srcdir@/src $(BOOST_LDFLAGS)
AM_CPPFLAGS = -std=c++14 -I$(top_srcdir)/src $(BOOST_CPPFLAGS) -DBOOST_TEST_DYN_LINK
LDADD = -lteng ${BOOST_SYSTEM_LIB} ${BOOST_FILESYSTEM_LIB} ${BOOST_UNIT_TEST_FRAMEWORK_LIB}


# test programs
check_PROGRAMS =

test_base_SOURCES = test_base.cc
test_filesystem_SOURCES = test_filesystem.cc

if CPP14
check_PROGRAMS += test_base test_filesystem
else
check_PROGRAMS +=
endif !CPP14

AM_COLOR_TESTS = always
TESTS = $(check_PROGRAMS)
3 changes: 3 additions & 0 deletions tests/templates/common-head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<head>
<title>Example page: ${title}</title>
</head>
6 changes: 6 additions & 0 deletions tests/templates/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<?teng include file="common-head.html" ?>
<body>
<p><?teng include file="subdir/paragraph.html" ?></p>
</body>
</html>
1 change: 1 addition & 0 deletions tests/templates/subdir/paragraph.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Paragraph: ${title}
150 changes: 150 additions & 0 deletions tests/test_base.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#define BOOST_TEST_MODULE base_tests

#include "test_utils.h"

#include <boost/filesystem.hpp>

const std::string result_1 = R"__( <head>
<title>Example page: Title</title>
</head>)__";

const std::string result_2 = R"__( <head>
<title>Example page: </title>
</head>)__";

const std::string result_3 = R"__(<html>
<head>
<title>Example page: Title</title>
</head>
<body>
<p>Paragraph: Title</p>
</body>
</html>)__";

const std::string result_4 = R"__(<html>
<head>
<title>Example page: </title>
</head>
<body>
<p>Paragraph: </p>
</body>
</html>)__";

std::string createAbsPath(const std::string& root, const std::string& filename)
{
return (!root.empty() && root[0] == '/')
? (boost::filesystem::path(root) / filename).string()
: (boost::filesystem::current_path() / root / filename).string();
}

BOOST_AUTO_TEST_CASE(test_include_ok)
{
Teng::Teng_t teng("templates", 0);
Teng::Error_t err;

BOOST_CHECK_EQUAL(
generate(teng, "<?teng include file=\"common-head.html\" ?>", *createFragmentTitle(), err),
result_1);
BOOST_CHECK_EQUAL(err.count(), 0);
}

BOOST_AUTO_TEST_CASE(test_include_warning)
{
Teng::Teng_t teng("templates", 0);
Teng::Error_t err;

BOOST_CHECK_EQUAL(
generate(teng, "<?teng include file=\"common-head.html\" ?>", *createFragment(), err),
result_2);
BOOST_REQUIRE_EQUAL(err.count(), 1);
BOOST_CHECK_EQUAL(err.getEntries().front().getLogLine(),
"common-head.html(2,37) Warning: Runtime: Variable '.title' is undefined\n");
}

BOOST_AUTO_TEST_CASE(test_include_missing)
{
Teng::Teng_t teng("no-directory", 0);
Teng::Error_t err;

const std::string absPath = createAbsPath("no-directory", "common-head.html");

BOOST_CHECK_EQUAL(
generate(teng, "<?teng include file=\"common-head.html\" ?>", *createFragmentTitle(), err),
"");
BOOST_REQUIRE_EQUAL(err.count(), 2);
BOOST_CHECK_EQUAL(err.getEntries()[0].getLogLine(),
"(no file)(1,41) Error: Cannot open input file '" + absPath
+ "' (No such file or directory)\n");
BOOST_CHECK_EQUAL(err.getEntries()[1].getLogLine(),
"common-head.html(1,0) Error: Cannot stat file '" + absPath
+ "' (No such file or directory)\n");
}

BOOST_AUTO_TEST_CASE(test_file_ok)
{
Teng::Teng_t teng("templates", 0);
Teng::Error_t err;

BOOST_CHECK_EQUAL(generateFromFile(teng, "common-head.html", *createFragmentTitle(), err),
result_1);
BOOST_REQUIRE_EQUAL(err.count(), 0);
}

BOOST_AUTO_TEST_CASE(test_file_missing)
{
Teng::Teng_t teng("no-directory", 0);
Teng::Error_t err;

const std::string absPath = createAbsPath("no-directory", "common-head.html");

BOOST_CHECK_EQUAL(generateFromFile(teng, "common-head.html", *createFragmentTitle(), err), "");
BOOST_REQUIRE_EQUAL(err.count(), 2);
BOOST_CHECK_EQUAL(err.getEntries()[0].getLogLine(),
"(no file) Error: Cannot stat file '" + absPath
+ "' (No such file or directory)\n");
BOOST_CHECK_EQUAL(err.getEntries()[1].getLogLine(),
"(no file) Error: Cannot open input file '" + absPath
+ "' (No such file or directory)\n");
}

BOOST_AUTO_TEST_CASE(test_inner_include_ok)
{
Teng::Teng_t teng("templates", 0);
Teng::Error_t err;

BOOST_CHECK_EQUAL(
generate(teng, "<?teng include file=\"page.html\" ?>", *createFragmentTitle(), err),
result_3);
BOOST_CHECK_EQUAL(err.count(), 0);
}

BOOST_AUTO_TEST_CASE(test_inner_include_warning)
{
Teng::Teng_t teng("templates", 0);
Teng::Error_t err;

const std::string filename1 = "common-head.html";
const std::string filename2 = "subdir/paragraph.html";

BOOST_CHECK_EQUAL(
generate(teng, "<?teng include file=\"page.html\" ?>", *createFragment(), err), result_4);
BOOST_REQUIRE_EQUAL(err.count(), 2);
BOOST_CHECK_EQUAL(err.getEntries()[0].getLogLine(),
filename1 + "(2,37) Warning: Runtime: Variable '.title' is undefined\n");
BOOST_CHECK_EQUAL(err.getEntries()[1].getLogLine(),
filename2 + "(1,19) Warning: Runtime: Variable '.title' is undefined\n");
}

BOOST_AUTO_TEST_CASE(test_include_abs_ok)
{
Teng::Teng_t teng("templates", 0);
Teng::Error_t err;

const std::string path = createAbsPath("templates", "subdir/paragraph.html");
BOOST_REQUIRE(boost::filesystem::exists(path));

const std::string templ = "<?teng include file=\"" + path + "\" ?>";

BOOST_CHECK_EQUAL(generate(teng, templ, *createFragmentTitle(), err), "Paragraph: Title");
BOOST_CHECK_EQUAL(err.count(), 0);
}
Loading

0 comments on commit 375f29e

Please sign in to comment.