1 Star 0 Fork 0

via/Catch2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.conan
.github
CMake
data/artwork
docs
examples
010-TestCase.cpp
020-TestCase-1.cpp
020-TestCase-2.cpp
030-Asn-Require-Check.cpp
100-Fix-Section.cpp
110-Fix-ClassFixture.cpp
120-Bdd-ScenarioGivenWhenThen.cpp
210-Evt-EventListeners.cpp
231-Cfg-OutputStreams.cpp
232-Cfg-CustomMain.cpp
300-Gen-OwnGenerator.cpp
301-Gen-MapTypeConversion.cpp
302-Gen-Table.cpp
310-Gen-VariablesInGenerators.cpp
311-Gen-CustomCapture.cpp
CMakeLists.txt
extras
fuzzing
src
tests
third_party
tools
.bazelrc
.clang-format
.clang-tidy
.gitattributes
.gitignore
BUILD.bazel
CMakeLists.txt
CMakePresets.json
CODE_OF_CONDUCT.md
Doxyfile
LICENSE.txt
MODULE.bazel
README.md
SECURITY.md
WORKSPACE.bazel
appveyor.yml
codecov.yml
conanfile.py
mdsnippets.json
meson.build
meson_options.txt
克隆/下载
302-Gen-Table.cpp 2.37 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
// 302-Gen-Table.cpp
// Shows how to use table to run a test many times with different inputs. Lifted from examples on
// issue #850.
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <string>
struct TestSubject {
// this is the method we are going to test. It returns the length of the
// input string.
size_t GetLength( const std::string& input ) const { return input.size(); }
};
TEST_CASE("Table allows pre-computed test inputs and outputs", "[example][generator]") {
using std::make_tuple;
// do setup here as normal
TestSubject subj;
SECTION("This section is run for each row in the table") {
std::string test_input;
size_t expected_output;
std::tie( test_input, expected_output ) =
GENERATE( table<std::string, size_t>(
{ /* In this case one of the parameters to our test case is the
* expected output, but this is not required. There could be
* multiple expected values in the table, which can have any
* (fixed) number of columns.
*/
make_tuple( "one", 3 ),
make_tuple( "two", 3 ),
make_tuple( "three", 5 ),
make_tuple( "four", 4 ) } ) );
// run the test
auto result = subj.GetLength(test_input);
// capture the input data to go with the outputs.
CAPTURE(test_input);
// check it matches the pre-calculated data
REQUIRE(result == expected_output);
} // end section
}
/* Possible simplifications where less legacy toolchain support is needed:
*
* - With libstdc++6 or newer, the make_tuple() calls can be omitted
* (technically C++17 but does not require -std in GCC/Clang). See
* https://stackoverflow.com/questions/12436586/tuple-vector-and-initializer-list
*
* - In C++17 mode std::tie() and the preceding variable declarations can be
* replaced by structured bindings: auto [test_input, expected] = GENERATE(
* table<std::string, size_t>({ ...
*/
// Compiling and running this file will result in 4 successful assertions
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/chooosky/Catch2.git
git@gitee.com:chooosky/Catch2.git
chooosky
Catch2
Catch2
devel

搜索帮助