我运行以下命令:
$> ctest -V -R path_unittest
并获得以下失败消息:
2: /path_to_spec/path_unittest.cpp:80: ERROR: CHECK( lines[0] == "test,test1,test2" ) is NOT correct!
== test,test1,test2 )st,test1,test2
2:
2: /path_to_spec/path_unittest.cpp:81: ERROR: CHECK( lines[1] == "1,2,3" ) is NOT correct!
== 1,2,3 ): CHECK( 1,2,3
2:
它还告诉我:
Output from these tests are in: /path_to_log/LastTest.log
但是当我对文件进行cat时,失败消息显示如下:
/path_to_spec/path_unittest.cpp:80: ERROR: CHECK( lines[0] == "test,test1,test2" ) is NOT correct!
== test,test1,test2 )test1,test2
/path_to_spec/path_unittest.cpp:81: ERROR: CHECK( lines[1] == "1,2,3" ) is NOT correct!
== 1,2,3 )HECK( 1,2,3
这里发生了什么?请注意,在测试输出中,它显示为== test,test1,test2 )st,test1,test2
和== 1,2,3 ): CHECK( 1,2,3
,然后被截断。类似地,在日志输出中,它显示为== 1,2,3 )HECK( 1,2,3
。为什么在日志文件中,单词CHECK
被截断?为什么在ctest输出中,结果看起来像是提前结束了?
结果真的很奇怪,因为预期数据和实际数据似乎是一样的,所以我希望有某种标志或东西可以传递给ctest,这将给予我更好地了解为什么测试失败。
编辑以添加最小可重现示例:
# include "../common.hpp"
# include <base/exception.hpp>
# include <base/paths.hpp>
# include <cstdlib>
TEST_CASE("test path functions") {
SUBCASE("test get file contents") {
SUBCASE("test valid file") {
String path_str =
base::path::unittest_resource_path() + "base/path/test.csv";
Strings lines;
base::path::get_lines_from_file(path_str, lines);
CHECK(lines.size() == 2);
CHECK(lines[0] == "test,test1,test2");
CHECK(lines[1] == "1,2,3");
}
}
}
下面是正在测试的函数,我假设您不需要include来理解它:
void get_lines_from_file(const String &fname, Strings &lines) {
if (!std::filesystem::exists(fname)) {
String msg = "The file " + fname + " does not exist!";
base::log_and_throw<base::InputException>(msg);
}
String line;
std::ifstream input;
input.open(fname);
std::stringstream buffer;
buffer << input.rdbuf();
lines = base::string::split(buffer.str(), "\n");
input.close();
}
1条答案
按热度按时间pw136qt21#
我猜你的输入文件的行尾是“\r\n”。你在“\n”处拆分,所以你得到了以“\r”结尾的字符串。当这些字符串被打印出来时,它们会导致你看到的奇怪输出,因为“\r”会导致输出从当前行的开头继续,从而覆盖了以前的输出。