繁体   English   中英

Yaml-cpp配置解析器测试与CppUTest问题“期望类型说明符''('令牌'

[英]Yaml-cpp configuration parser testing with CppUTest issue “expected type-specifier before ‘(’ token”

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

我做了一个类Parser ,它检查yaml节点中的键值对。 Parser类构建为ConfigParserLibrary静态库。

现在我想使用CppUTest测试Parser类。 使用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)) ^ 在此输入图像描述 Parser.hpp

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

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

Parser.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){
}

RunTests.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_STANDARD从17更改为11时,我收到以下错误/usr/include/c++/8/bits/stl_tree.h:636: error: '__node' does not name a type ::new(__node) _Rb_tree_node<_Val>; ^~~~~~ /usr/include/c++/8/bits/stl_tree.h:636: error: '__node' does not name a type ::new(__node) _Rb_tree_node<_Val>; ^~~~~~

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

gcc版本8.3.1 20190226。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM