From 3b842d5a62fdf1938edfd6acdef527bb3bb9a628 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sun, 22 Nov 2015 17:34:46 +0100 Subject: [PATCH] Add unit test for InsetListings::getCaption() The regex in InsetListings::getCaption() does not work with C++11 and std::regex. This can now be checked easily by this new unit test. --- src/Makefile.am | 13 +++++++- src/insets/InsetListings.cpp | 4 +++ src/tests/CMakeLists.txt | 22 +++++++++++++ src/tests/check_ListingsCaption.cpp | 48 +++++++++++++++++++++++++++++ src/tests/regfiles/ListingsCaption | 8 +++++ src/tests/test_ListingsCaption | 7 +++++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/tests/check_ListingsCaption.cpp create mode 100644 src/tests/regfiles/ListingsCaption create mode 100755 src/tests/test_ListingsCaption diff --git a/src/Makefile.am b/src/Makefile.am index c10b8e51cb..9446d17be2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -698,12 +698,14 @@ endif EXTRA_DIST += \ tests/test_ExternalTransforms \ + tests/test_ListingsCaption \ tests/regfiles/ExternalTransforms \ tests/regfiles/Length \ + tests/regfiles/ListingsCaption \ tests/test_layout \ tests/test_Length -TESTS = tests/test_ExternalTransforms tests/test_Length +TESTS = tests/test_ExternalTransforms tests/test_Length tests/test_ListingsCaption alltests: check alltests-recursive @@ -722,6 +724,7 @@ updatetests: check_PROGRAMS = \ check_ExternalTransforms \ check_Length \ + check_ListingsCaption \ check_layout if INSTALL_MACOSX @@ -770,4 +773,12 @@ check_Length_SOURCES = \ tests/boost.cpp \ tests/dummy_functions.cpp +check_ListingsCaption_CPPFLAGS = $(AM_CPPFLAGS) +check_ListingsCaption_LDADD = support/liblyxsupport.a $(LIBICONV) $(BOOST_LIBS) @LIBS@ $(QT_LIB) $(LIBSHLWAPI) +check_ListingsCaption_LDFLAGS = $(QT_LDFLAGS) $(ADD_FRAMEWORKS) +check_ListingsCaption_SOURCES = \ + tests/check_ListingsCaption.cpp \ + tests/boost.cpp \ + tests/dummy_functions.cpp + .PHONY: alltests alltests-recursive updatetests diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp index ac31d28a05..9c7daf2219 100644 --- a/src/insets/InsetListings.cpp +++ b/src/insets/InsetListings.cpp @@ -400,6 +400,10 @@ docstring InsetListings::getCaption(OutputParams const & runparams) const otexstream os(ods, texrow); ins->getArgs(os, runparams); ins->getArgument(os, runparams); + + // TODO: The code below should be moved to support, and then the test + // in ../tests should be moved there as well. + // the caption may contain \label{} but the listings // package prefer caption={}, label={} docstring cap = ods.str(); diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index afcceb578c..daf4a06174 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -104,3 +104,25 @@ add_test(NAME "check_Length" -P "${TOP_SRC_DIR}/src/support/tests/supporttest.cmake") add_dependencies(lyx_run_tests check_Length) +include_directories(${TOP_SRC_DIR}/src/tests) +set(check_ListingsCaption_SOURCES) +foreach(_f tests/check_ListingsCaption.cpp tests/boost.cpp tests/dummy_functions.cpp) + list(APPEND check_ListingsCaption_SOURCES ${TOP_SRC_DIR}/src/${_f}) +endforeach() +add_executable(check_ListingsCaption ${check_ListingsCaption_SOURCES}) + +target_link_libraries(check_ListingsCaption support + ${Lyx_Boost_Libraries} ${QT_QTGUI_LIBRARY} ${QT_QTCORE_LIBRARY}) +lyx_target_link_libraries(check_ListingsCaption Magic) + +add_dependencies(lyx_run_tests check_ListingsCaption) +set_target_properties(check_ListingsCaption PROPERTIES FOLDER "tests/src") +target_link_libraries(check_ListingsCaption ${ICONV_LIBRARY}) + +add_test(NAME "check_ListingsCaption" + COMMAND ${CMAKE_COMMAND} -DCommand=$ + "-DInput=${TOP_SRC_DIR}/src/tests/regfiles/ListingsCaption" + "-DOutput=${CMAKE_CURRENT_BINARY_DIR}/ListingsCaption_data" + -P "${TOP_SRC_DIR}/src/support/tests/supporttest.cmake") +add_dependencies(lyx_run_tests check_ListingsCaption) + diff --git a/src/tests/check_ListingsCaption.cpp b/src/tests/check_ListingsCaption.cpp new file mode 100644 index 0000000000..2df43e8cdc --- /dev/null +++ b/src/tests/check_ListingsCaption.cpp @@ -0,0 +1,48 @@ +#include + +#include "../support/debug.h" +#include "../support/regex.h" + +#include + + +using namespace lyx; +using namespace std; + + +// This function is unfortunately copied from ../insets/InsetListing.cpp, so we +// should try to make sure to keep the two in sync. +string test_ListingsCaption(string const & cap) +{ + // convert from + // blah1\label{blah2} blah3 + // to + // blah1 blah3},label={blah2 + // to form options + // caption={blah1 blah3},label={blah2} + // + // NOTE that } is not allowed in blah2. + regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)"); + string const new_cap("\\1\\3},label={\\2"); + return regex_replace(cap, reg, new_cap); +} + +void test_test_ListingsCaptions() +{ + cout << test_ListingsCaption("\\label{}") << endl; + cout << test_ListingsCaption("\\label{blah2}") << endl; + cout << test_ListingsCaption("\\label{blah2} blah3") << endl; + cout << test_ListingsCaption("\\label{} blah3") << endl; + cout << test_ListingsCaption("blah1\\label{}") << endl; + cout << test_ListingsCaption("blah1\\label{} blah3") << endl; + cout << test_ListingsCaption("blah1\\label{blah2}") << endl; + cout << test_ListingsCaption("blah1\\label{blah2} blah3") << endl; +} + + +int main(int, char **) +{ + // Connect lyxerr with cout instead of cerr to catch error output + lyx::lyxerr.setStream(cout); + test_test_ListingsCaptions(); +} diff --git a/src/tests/regfiles/ListingsCaption b/src/tests/regfiles/ListingsCaption new file mode 100644 index 0000000000..f9fea934e7 --- /dev/null +++ b/src/tests/regfiles/ListingsCaption @@ -0,0 +1,8 @@ +},label={ +},label={blah2 + blah3},label={blah2 + blah3},label={ +blah1},label={ +blah1 blah3},label={ +blah1},label={blah2 +blah1 blah3},label={blah2 diff --git a/src/tests/test_ListingsCaption b/src/tests/test_ListingsCaption new file mode 100755 index 0000000000..cbfcf87a26 --- /dev/null +++ b/src/tests/test_ListingsCaption @@ -0,0 +1,7 @@ +#!/bin/sh + +regfile=`cat ${srcdir}/tests/regfiles/ListingsCaption` +output=`./check_ListingsCaption` + +test "$regfile" = "$output" +exit $?