From 22b573e0abf7c20c9733fe88eecc3ec4e7ffb436 Mon Sep 17 00:00:00 2001 From: Yuriy Skalko Date: Tue, 9 Feb 2021 00:30:59 +0200 Subject: [PATCH] Convert ExternalTransforms test --- src/insets/ExternalSupport.cpp | 14 ++++++--- src/insets/ExternalTransforms.cpp | 19 +++++------- src/test/insets/ExternalTransforms.cpp | 42 ++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 src/test/insets/ExternalTransforms.cpp diff --git a/src/insets/ExternalSupport.cpp b/src/insets/ExternalSupport.cpp index 33c14d38be..e5efdb2cf1 100644 --- a/src/insets/ExternalSupport.cpp +++ b/src/insets/ExternalSupport.cpp @@ -34,6 +34,7 @@ #include "support/Package.h" #include +#include using namespace std; using namespace lyx::support; @@ -529,10 +530,15 @@ string const substituteOption(InsetExternalParams const & params, { string opt = transformIt(params, input, format); - if (format == "LaTeX" || format == "PDFLaTeX") - return sanitizeLatexOption(opt); - if (format == "DocBook") - return sanitizeDocBookOption(opt); + try { + if (format == "LaTeX" || format == "PDFLaTeX") + return sanitizeLatexOption(opt); + if (format == "DocBook") + return sanitizeDocBookOption(opt); + } catch (invalid_argument & ex) { + lyxerr << ex.what(); + return string(); + } return opt; } diff --git a/src/insets/ExternalTransforms.cpp b/src/insets/ExternalTransforms.cpp index 76578b2ea0..261ae5456f 100644 --- a/src/insets/ExternalTransforms.cpp +++ b/src/insets/ExternalTransforms.cpp @@ -21,6 +21,7 @@ #include // abs #include #include +#include using namespace std; using namespace lyx::support; @@ -282,14 +283,12 @@ string const sanitizeLatexOption(string const & input) // "[,,,,foo..." -> "foo..." ("foo..." may be empty) string output; smatch what; + static regex const front("^( *\\[,*)(.*)$"); + if (!regex_match(it, end, what, front)) + throw invalid_argument("Unable to sanitize LaTeX option"); - if (!regex_match(it, end, what, front)) { - lyxerr << "Unable to sanitize LaTeX \"Option\": " - << input << '\n'; - return string(); - } - it = what[1].second; + it = what[1].second; // Replace any consecutive commas with a single one // "foo,,,,bar" -> "foo,bar" @@ -307,11 +306,9 @@ string const sanitizeLatexOption(string const & input) // Strip any trailing commas // "...foo,,,]" -> "...foo" ("...foo,,," may be empty) static regex const back("^(.*[^,])?,*\\] *$"); - if (!regex_match(output, what, back)) { - lyxerr << "Unable to sanitize LaTeX \"Option\": " - << output << '\n'; - return string(); - } + if (!regex_match(output, what, back)) + throw invalid_argument("Unable to sanitize LaTeX option"); + output = what.str(1); // Remove any surrounding whitespace diff --git a/src/test/insets/ExternalTransforms.cpp b/src/test/insets/ExternalTransforms.cpp new file mode 100644 index 0000000000..babfef8c92 --- /dev/null +++ b/src/test/insets/ExternalTransforms.cpp @@ -0,0 +1,42 @@ +#include +#include + +#include "insets/ExternalTransforms.h" + +using namespace lyx::external; +using namespace std; + + +TEST_CASE("sanitizeLatexOption") { + // invalid input + CHECK_THROWS(sanitizeLatexOption("")); + CHECK_THROWS(sanitizeLatexOption(",")); + CHECK_THROWS(sanitizeLatexOption(",,")); + CHECK_THROWS(sanitizeLatexOption("[")); + CHECK_THROWS(sanitizeLatexOption("]")); + CHECK_THROWS(sanitizeLatexOption("a,[,c]")); + CHECK_THROWS(sanitizeLatexOption("[a,],c")); + // valid input + CHECK(sanitizeLatexOption("[]") == ""); + CHECK(sanitizeLatexOption("[[]") == "[[]"); + CHECK(sanitizeLatexOption("[]]") == "[]]"); + CHECK(sanitizeLatexOption("[[]]") == "[[]]"); + CHECK(sanitizeLatexOption("[,]") == ""); + CHECK(sanitizeLatexOption("[,,]") == ""); + CHECK(sanitizeLatexOption("[,,,]") == ""); + CHECK(sanitizeLatexOption("[a]") == "[a]"); + CHECK(sanitizeLatexOption("[,a]") == "[a]"); + CHECK(sanitizeLatexOption("[,,a]") == "[a]"); + CHECK(sanitizeLatexOption("[,,,a]") == "[a]"); + CHECK(sanitizeLatexOption("[a,b]") == "[a,b]"); + CHECK(sanitizeLatexOption("[a,,b]") == "[a,b]"); + CHECK(sanitizeLatexOption("[a,,,b]") == "[a,b]"); + CHECK(sanitizeLatexOption("[a,[,c]") == "[a,[,c]"); + CHECK(sanitizeLatexOption("[a,],c]") == "[a,],c]"); + CHECK(sanitizeLatexOption("[a,[],c]") == "[a,[],c]"); + CHECK(sanitizeLatexOption("[a,,[],,c]") == "[a,[],c]"); + CHECK(sanitizeLatexOption("[a,,[,],,c]") == "[a,[,],c]"); + CHECK(sanitizeLatexOption("[a,]") == "[a]"); + CHECK(sanitizeLatexOption("[a,,]") == "[a]"); + CHECK(sanitizeLatexOption("[a,,,]") == "[a]"); +} -- 2.39.5