[英]Catch2: Test all permutations of two lists of types
我需要在 Catch2 中編寫一些單元測試,其中每個測試用例都應該針對兩個類型列表的每個可能排列執行。 我喜歡類似的東西
// Some types defined in my project
class A;
class B;
PERMUTATION_TEST_CASE ("Foo", (A, B), (float, double))
{
TestTypeX x;
TestTypeY y;
}
測試用例將執行 4 次的地方
TestTypeX = A, TestTypeY = float
TestTypeX = A, TestTypeY = double
TestTypeX = B, TestTypeY = float
TestTypeX = B, TestTypeY = double
或者,這樣的事情也是可能的
constexpr A a;
constexpr B b;
TEMPLATE_TEST_CASE ("Foo", float, double)
{
auto x = GENERATE (a, b); // does not work because a and b have different types
TestType y;
}
據我所知,catch2 中沒有這樣的東西。 在type-parametrised-test-cases中, TEMPLATE_TEST_CASE
解決了單個類型列表的問題,但不是兩個列表的每個排列,並且TEMPLATE_PRODUCT_TEST_CASE
解決了第一個類型是模板的問題,然后用第二個列表的每種類型進行實例化——這也不是我需要的。
有沒有合適的 catch2 機制來解決我目前忽略的問題? 我正在使用 Catch2 版本 3.1.0。
我的實際需求是比這個 2x2 示例更大的組合集,因此手動指定所有排列不是我最喜歡的選擇。
您可以創建類型為 list 的笛卡爾積,然后:
using MyTypes = cross_product<type_list<A, B>, type_list<float, double>>::type;
// using MyTypes = type_list<std::pair<A, float>, std::pair<A, double>,
// std::pair<B, float>, std::pair<B, double>>;
TEMPLATE_LIST_TEST_CASE("some_name", "[xxx]", MyTypes)
{
using TestTypeX = typename TestType::first_type; // A or B
using TestTypeY = typename TestType::second_type; // float or double
// ...
}
同樣, TEMPLATE_PRODUCT_TEST_CASE
可能(ab)與包裝器類型一起使用:
template <typename T>
struct A_with
{
using first_type = A;
using second_type = T;
};
template <typename T>
struct B_with
{
using first_type = B;
using second_type = T;
};
TEMPLATE_PRODUCT_TEST_CASE("some name", "[xxx]", (A_with, B_with), (float, double))
{
using TestTypeX = typename TestType::first_type; // A or B
using TestTypeY = typename TestType::second_type; // float or double
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.