首页 > 解决方案 > 使用 CppUTest 问题进行 Yaml-cpp 配置解析器测试“在 '(' 标记之前的预期类型说明符”

问题描述

我正在使用 yaml-cpp 库(v 0.6.0)来解析 yaml 配置文件。Yaml-cpp 作为系统库安装。

我创建了一个类Parser来检查 yaml 节点中的键值对。该类Parser被构建为ConfigParserLibrary静态库。

现在我想Parser使用CppUTest. 使用CMake. 示例代码:

CMakeLists:

cmake_minimum_required(VERSION 3.9)
project(yaml-parser VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ConfigParserLibrary lib
include_directories(${PROJECT_SOURCE_DIR}/include/config_parser_lib)
file(GLOB_RECURSE LIB_CONFIG_PARSER_SRC "config_parser_lib/Parser.cpp")
add_library(ConfigParserLibrary STATIC ${LIB_CONFIG_PARSER_SRC})
target_link_libraries(ConfigParserLibrary yaml-cpp)

# Executable that uses ConfigParserLibrary
add_executable(conf_reader "config_reader.cpp")
target_link_libraries(conf_reader yaml-cpp ConfigParserLibrary)

message(STATUS "Library tests will be built.")
add_library(ConfigParserTests STATIC tests/ConfigParserTests.cpp)
target_link_libraries(ConfigParserTests ConfigParserLibrary yaml-cpp)

add_executable(RunLibTests tests/RunTests.cpp)
target_link_libraries(RunLibTests CppUTest CppUTestExt
    ConfigParserTests ConfigParserLibrary yaml-cpp)
add_test(NAME test_library COMMAND RunLibTests)

当我在另一个可执行文件中使用 Parser 类时,它正在编译而没有错误。但是当我在 ConfigParserTests.cpp 文件中包含 config_parser_lib/Parser.hpp 文件时,它会引发:/usr/include/c++/8/optional:208: error: expected type-specifier before ‘(’ token ::new ((void *) std::__addressof(this->_M_payload)) ^. 在此处输入图像描述 解析器.hpp

#pragma once
#include <yaml-cpp/yaml.h>

class Parser{
public:
    int parseNode(YAML::Node configNode); 
};

解析器.cpp

#include "config_parser_lib/Parser.hpp"

int Parser::parseNode(YAML::Node configNode){
    return valueA = configNode["keyA"].as<int>();
}

ConfigParserTests.cpp

#include <CppUTest/TestHarness.h>
#include "config_parser_lib/Parser.hpp"

TEST_GROUP(ConfigParserTests) {
};

TEST(ConfigParserTests, ParseConfigNode){
}

运行测试.cpp

#include <CppUTest/CommandLineTestRunner.h>

IMPORT_TEST_GROUP(ConfigParserTests);

int main(int ac, const char** av)
{
    return CommandLineTestRunner::RunAllTests(ac, av);
}

CMakeLists.txt:当我将CMAKE_CXX_STANDARD17 更改为 11 时,我收到以下错误/usr/include/c++/8/bits/stl_tree.h:636: error: ‘__node’ does not name a type ::new(__node) _Rb_tree_node<_Val>; ^~~~~~

如果我注释掉 中的#include "config_parser_lib/Parser.hpp"ConfigParserTests.cpp,则没有错误。因此,我认为错误来自包含<yaml-cpp/yaml.h>文件。如果我包含<yaml-cpp/node/node.h>而不是yaml.h>,则没有错误,但我需要<yaml-cpp/yaml.h>包含我需要使用的其他头文件的文件。

gcc 版本 8.3.1 20190226。

标签: c++cmakecompiler-errorsyaml-cppcpputest

解决方案


更改包含顺序ConfigParserTests.cpp解决了这个问题。

#include "config_parser_lib/Parser.hpp"
#include <CppUTest/TestHarness.h>

我认为#include <yaml-cpp/yaml.h>应该在每个翻译单元的开头。


推荐阅读