/* This file is part of the KDE libraries SPDX-FileCopyrightText: 2021 Slava Aseev SPDX-License-Identifier: LGPL-2.0-or-later */ #ifndef __TESTHELPERS_HPP__ #define __TESTHELPERS_HPP__ #include <../kwalletfreedesktopservice.h> #include #include #include #include template using Testset = std::vector>; template struct EasyFormater { std::string operator()(const T &v) const { return std::string(v.toStdString()); } }; template struct EasyFormater::value || std::is_floating_point::value>::type> { std::string operator()(T v) const { return std::to_string(v); } }; template<> struct EasyFormater { std::string operator()(const FdoUniqueLabel &v) const { return "{" + v.label.toStdString() + ", " + std::to_string(v.copyId) + "}"; } }; template<> struct EasyFormater { std::string operator()(const EntryLocation &v) const { return "{" + v.folder.toStdString() + ", " + v.key.toStdString() + "}"; } }; template struct TestsetCmpHelper { template bool cmp(F &&function, const T &pair) { return function(pair.first) == pair.second; } template std::string format(F &&function, const T &pair) { using RetT = decltype(function(pair.first)); return EasyFormater()(pair.first) + " (evaluates to " + EasyFormater()(function(pair.first)) + ") not equal with " + EasyFormater()(pair.second); } }; template<> struct TestsetCmpHelper { template bool cmp(F &&function, const T &pair) { return function(pair.second) == pair.first; } template std::string format(F &&function, const T &pair) { using RetT = decltype(function(pair.second)); return EasyFormater()(pair.second) + " (evaluates to " + EasyFormater()(function(pair.second)) + ") not equal with " + EasyFormater()(pair.first); } }; template void runTestsetTmpl(F &&function, const Testset &labelMap) { for (auto &pair : labelMap) { bool ok = TestsetCmpHelper().cmp(function, pair); if (!ok) { std::string str = TestsetCmpHelper().format(function, pair); QVERIFY2(ok, str.c_str()); } } } template void runTestset(F &&function, const Testset &labelMap) { return runTestsetTmpl(std::forward(function), labelMap); } template void runRevTestset(F &&function, const Testset &labelMap) { return runTestsetTmpl(std::forward(function), labelMap); } #endif // __TESTHELPERS_HPP__