Added tests, fixed implementation

This commit is contained in:
Matthias Koefferlein 2021-12-15 23:18:42 +01:00
parent 07515cd716
commit 533a72b7bd
4 changed files with 34 additions and 0 deletions

View File

@ -172,6 +172,8 @@ IncludeExpander::from_string (const std::string &s)
} else if (*ex == '@') {
++ex;
while (! ex.at_end ()) {
int ln = 0;

View File

@ -108,3 +108,31 @@ TEST(4_multi_include_interpolate)
EXPECT_EQ (ie.translate_to_original (6).second, 3);
}
TEST(5_issue946)
{
std::string fn = tl::testdata () + "/tl/x_inc4.txt";
std::string et;
tl::IncludeExpander ie = tl::IncludeExpander::expand (fn, tl::InputStream (fn).read_all (), et);
EXPECT_EQ (et, "A line\nincluded.4\nAnother line\n");
EXPECT_EQ (np (ie.to_string ()), np ("@1*" + tl::testdata () + "/tl/x_inc4.txt*0;2*'" + tl::testdata () + "/tl/inc 4.txt'*-1;3*" + tl::testdata () + "/tl/x_inc4.txt*0;"));
EXPECT_EQ (tl::IncludeExpander::from_string (ie.to_string ()).to_string (), ie.to_string ());
EXPECT_EQ (ie.translate_to_original (1).first, fn);
EXPECT_EQ (ie.translate_to_original (1).second, 1);
EXPECT_EQ (np (ie.translate_to_original (2).first), np (tl::testdata () + "/tl/inc 4.txt"));
EXPECT_EQ (ie.translate_to_original (2).second, 1);
EXPECT_EQ (ie.translate_to_original (3).first, fn);
EXPECT_EQ (ie.translate_to_original (3).second, 3);
fn = tl::testdata () + "/tl/inc 4.txt";
et.clear ();
ie = tl::IncludeExpander::expand (fn, tl::InputStream (fn).read_all (), et);
EXPECT_EQ (et, "included.4\n");
// no quotes here so this string can be used as the original file name if there is no include
EXPECT_EQ (np (ie.to_string ()), np (tl::testdata () + "/tl/inc 4.txt"));
}

1
testdata/tl/inc 4.txt vendored Normal file
View File

@ -0,0 +1 @@
included.4

3
testdata/tl/x_inc4.txt vendored Normal file
View File

@ -0,0 +1,3 @@
A line
# %include 'inc 4.txt'
Another line