File indexing completed on 2024-11-24 03:41:03

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2021 Slava Aseev <nullptrnine@basealt.ru>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #ifndef __TESTHELPERS_HPP__
0008 #define __TESTHELPERS_HPP__
0009 
0010 #include <../kwalletfreedesktopservice.h>
0011 #include <QTest>
0012 #include <string>
0013 #include <tuple>
0014 #include <vector>
0015 
0016 template<typename InputT, typename OutputT>
0017 using Testset = std::vector<std::pair<InputT, OutputT>>;
0018 
0019 template<typename T, typename Enable = void>
0020 struct EasyFormater {
0021     std::string operator()(const T &v) const
0022     {
0023         return std::string(v.toStdString());
0024     }
0025 };
0026 
0027 template<typename T>
0028 struct EasyFormater<T, typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value>::type> {
0029     std::string operator()(T v) const
0030     {
0031         return std::to_string(v);
0032     }
0033 };
0034 
0035 template<>
0036 struct EasyFormater<FdoUniqueLabel> {
0037     std::string operator()(const FdoUniqueLabel &v) const
0038     {
0039         return "{" + v.label.toStdString() + ", " + std::to_string(v.copyId) + "}";
0040     }
0041 };
0042 
0043 template<>
0044 struct EasyFormater<EntryLocation> {
0045     std::string operator()(const EntryLocation &v) const
0046     {
0047         return "{" + v.folder.toStdString() + ", " + v.key.toStdString() + "}";
0048     }
0049 };
0050 
0051 template<bool Reverse>
0052 struct TestsetCmpHelper {
0053     template<typename F, typename T>
0054     bool cmp(F &&function, const T &pair)
0055     {
0056         return function(pair.first) == pair.second;
0057     }
0058     template<typename F, typename T>
0059     std::string format(F &&function, const T &pair)
0060     {
0061         using RetT = decltype(function(pair.first));
0062         return EasyFormater<typename T::first_type>()(pair.first) + " (evaluates to " + EasyFormater<RetT>()(function(pair.first)) + ") not equal with "
0063             + EasyFormater<typename T::second_type>()(pair.second);
0064     }
0065 };
0066 template<>
0067 struct TestsetCmpHelper<true> {
0068     template<typename F, typename T>
0069     bool cmp(F &&function, const T &pair)
0070     {
0071         return function(pair.second) == pair.first;
0072     }
0073     template<typename F, typename T>
0074     std::string format(F &&function, const T &pair)
0075     {
0076         using RetT = decltype(function(pair.second));
0077         return EasyFormater<typename T::second_type>()(pair.second) + " (evaluates to " + EasyFormater<RetT>()(function(pair.second)) + ") not equal with "
0078             + EasyFormater<typename T::first_type>()(pair.first);
0079     }
0080 };
0081 
0082 template<bool Reverse, typename F, typename I, typename O>
0083 void runTestsetTmpl(F &&function, const Testset<I, O> &labelMap)
0084 {
0085     for (auto &pair : labelMap) {
0086         bool ok = TestsetCmpHelper<Reverse>().cmp(function, pair);
0087         if (!ok) {
0088             std::string str = TestsetCmpHelper<Reverse>().format(function, pair);
0089             QVERIFY2(ok, str.c_str());
0090         }
0091     }
0092 }
0093 
0094 template<typename F, typename I, typename O>
0095 void runTestset(F &&function, const Testset<I, O> &labelMap)
0096 {
0097     return runTestsetTmpl<false>(std::forward<F>(function), labelMap);
0098 }
0099 
0100 template<typename F, typename I, typename O>
0101 void runRevTestset(F &&function, const Testset<I, O> &labelMap)
0102 {
0103     return runTestsetTmpl<true>(std::forward<F>(function), labelMap);
0104 }
0105 
0106 #endif // __TESTHELPERS_HPP__