CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
test_tools.h
1
2#ifndef _TestTools_h_
3#define _TestTools_h_
4
5#include <functional>
6#include <type_traits>
7#include <iterator>
8
9#include "system/object.h"
10#include "system/object_ext.h"
11#include "system/string.h"
12#include "system/constraints.h"
13#include "system/text/string_builder.h"
14#include "system/collections/icollection.h"
15#include "system/collections/ienumerable.h"
16#include "system/collections/dictionary.h"
17#include "system/collections/keyvalue_pair.h"
18#include "system/test_tools/compare.h"
19
20namespace System {
21
23
28template <typename TT, typename T>
29class IEnumeratorIterator : public Details::BaseIterator<std::input_iterator_tag, TT> {
30public:
33 IEnumeratorIterator(const System::SharedPtr<System::Collections::Generic::IEnumerator<T>>& data) : m_data(data) {};
36 IEnumeratorIterator(const IEnumeratorIterator& o) : m_data(o.m_data) {};
37
40 IEnumeratorIterator& operator++() { if (!m_data->MoveNext()) m_data = nullptr; return *this; }
43 TT operator*() { return TT(m_data->get_Current()); };
44
48 bool operator==(const IEnumeratorIterator& o) const { return m_data == o.m_data; }
52 bool operator!=(const IEnumeratorIterator& o) const { return m_data != o.m_data; }
53
54private:
57};
58
63template <typename TT, typename T>
64class IEnumerableAdapter
65{
66public:
68 using value_type = TT;
70 using const_iterator = IEnumeratorIterator<TT, T>;
71
73 IEnumerableAdapter() : m_data(nullptr) {}
76 IEnumerableAdapter(const System::SharedPtr<System::Collections::Generic::IEnumerable<T>>& data) : m_data(data->GetEnumerator()) {};
77
80 IEnumeratorIterator<TT, T> begin() const
81 {
82 return m_data->MoveNext() ? IEnumeratorIterator<TT, T>(m_data) : IEnumeratorIterator<TT, T>(nullptr);
83 }
86 IEnumeratorIterator<TT, T> end() const { return IEnumeratorIterator<TT, T>(nullptr); }
87
88private:
91};
92
98template <typename TT, class Guard = typename std::enable_if<System::Constraints::IsStdTuple<TT>::value>::type,
99 typename T = typename std::tuple_element<0, TT>::type>
100class IEnumerableTupleAdapter : public IEnumerableAdapter<TT, T>
101{
102public:
105 IEnumerableTupleAdapter(const System::SharedPtr<System::Collections::Generic::IEnumerable<T>>& data) :
106 IEnumerableAdapter<TT, T>(data)
107 {
108 }
109};
110
114template <typename T>
115class IEnumerableDefaultAdapter : public IEnumerableAdapter<T, T>
116{
117public:
120 IEnumerableDefaultAdapter(const System::SharedPtr<System::Collections::Generic::IEnumerable<T>>& data) :
121 IEnumerableAdapter<T, T>(data)
122 {
123 }
124};
125
127
130{
131
137template <typename T>
138static auto IsNull(const T& obj) -> decltype(obj == nullptr)
139{
140 return obj == nullptr;
141}
142
146inline static bool IsNull(...)
147{
148 return false;
149}
150
154static bool IsEmpty(const System::String& str)
155{
156 return str.IsEmpty();
157}
162template <typename T>
163static bool IsEmpty(const SharedPtr<T>& collection)
164{
165 return collection->get_Count() == 0;
166}
167
172template <typename T>
173static bool IsNullOrEmpty(const SharedPtr<T>& collection)
174{
175 return collection == nullptr || collection->get_Count() == 0;
176}
180static bool IsNullOrEmpty(const System::String& str)
181{
182 return str.IsNullOrEmpty();
183}
184
188static bool IsNull(const System::String& str)
189{
190 return str.IsNull();
191}
192
196static bool AssertThrows(const std::function<void()>& func)
197{
198 try
199 {
200 func();
201 }
202 catch (...) {
203 return true;
204 }
205 return false;
206}
207
208}; // namespace TestTools
209
212{
221 template <typename T1, typename T2>
223 {
224 std::list<std::pair<T1, int32_t>> diff;
225
226 auto ChangeDiff = [&](const T1& key, int32_t delta)
227 {
228 for (auto& p : diff)
229 {
230 if (CompareDetails::AreEqual(p.first, key))
231 {
232 p.second += delta;
233 return;
234 }
235 }
236
237 diff.emplace_back(key, delta);
238 };
239
240 auto e_enumerator = (expected)->GetEnumerator();
241 while (e_enumerator->MoveNext())
242 {
243 ChangeDiff(e_enumerator->get_Current(), 1);
244 }
245
246 auto a_enumerator = (actual)->GetEnumerator();
247 while (a_enumerator->MoveNext())
248 {
249 ChangeDiff(static_cast<T1>(a_enumerator->get_Current()), -1);
250 }
251
252 auto result = System::MakeObject<System::Collections::Generic::Dictionary<T1, int32_t>>();
253 for (auto& d : diff)
254 {
255 if (d.second != 0)
256 {
257 result->Add(d.first, d.second);
258 }
259 }
260 return result;
261 }
262
267 static bool CheckDiffForAll(const std::function<bool(int)>& pred, const System::SharedPtr<System::Collections::Generic::ICollection<int32_t>>& values)
268 {
269 auto v_enumerator = (values)->GetEnumerator();
270 while (v_enumerator->MoveNext())
271 {
272 auto&& v = v_enumerator->get_Current();
273 if (!pred(v))
274 {
275 return false;
276 }
277 }
278 return true;
279 }
284 static bool CheckDiffForAny(const std::function<bool(int)>& pred, const System::SharedPtr<System::Collections::Generic::ICollection<int32_t>>& values)
285 {
286 auto v_enumerator = (values)->GetEnumerator();
287 while (v_enumerator->MoveNext())
288 {
289 auto&& v = v_enumerator->get_Current();
290 if (pred(v))
291 {
292 return true;
293 }
294 }
295 return false;
296 }
297
302 template <typename T>
304 {
305 if (ie == nullptr || !ie->GetEnumerator()->MoveNext())
306 {
307 return u"";
308 }
309
310 System::SharedPtr<System::Text::StringBuilder> sb = System::MakeObject<System::Text::StringBuilder>();
311
312 auto e_enumerator = (ie)->GetEnumerator();
313 while (e_enumerator->MoveNext())
314 {
315 auto&& e = e_enumerator->get_Current();
316 sb->Append(System::ObjectExt::ToString(e))->Append(u", ");
317 }
318
319 sb->Remove(sb->get_Length() - 2, 2);
320
321 return sb->ToString();
322 }
323
332 template <typename T1, typename T2>
336 {
337 return u"\r\nExpected: " + extra_msg + u"< " + IEnumerableToStr(expected) + u" >" +
338 u"\r\nActual: < " + IEnumerableToStr(actual) + u" >";
339 }
340
344 static System::String ToFullMessage(const System::String& message = u"")
345 {
346 return System::String(u"Message: ") + (message.IsEmpty() ? u"<empty>" : message);
347 }
348};
349
350} // namespace System
351
355#define COLLECTION_ASSERT_MAKE_DIFF(expected, actual) ((System::CollectionAssertHelper::MakeDiff<std::remove_const<std::remove_reference<decltype((expected)->GetEnumerator()->get_Current())>::type>::type, std::remove_const<std::remove_reference<decltype((actual)->GetEnumerator()->get_Current())>::type>::type>((expected), (actual)))->get_Values())
360#define COLLECTION_ASSERT_COLLECTIONS_TO_MSG(str, expected, actual) (System::CollectionAssertHelper::CollectionsToMsg<std::remove_const<std::remove_reference<decltype((expected)->GetEnumerator()->get_Current())>::type>::type, std::remove_const<std::remove_reference<decltype((actual)->GetEnumerator()->get_Current())>::type>::type>((str), (expected), (actual)))
361
365#define COLLECTION_ASSERT_ARE_EQUIVALENT(expected, actual, ...) {\
366 if (!System::CollectionAssertHelper::CheckDiffForAll([](int32_t v) {return v == 0;}, COLLECTION_ASSERT_MAKE_DIFF(expected, actual)))\
367 FAIL() << (System::CollectionAssertHelper::ToFullMessage(__VA_ARGS__) + COLLECTION_ASSERT_COLLECTIONS_TO_MSG(u"equivalent to ", expected, actual)).ToUtf8String();\
368}
369
373#define COLLECTION_ASSERT_ARE_NOT_EQUIVALENT(expected, actual, ...) {\
374 if (!System::CollectionAssertHelper::CheckDiffForAny([](int32_t v) {return v != 0;}, COLLECTION_ASSERT_MAKE_DIFF(expected, actual)))\
375 FAIL() << (System::CollectionAssertHelper::ToFullMessage(__VA_ARGS__) + COLLECTION_ASSERT_COLLECTIONS_TO_MSG(u"not equivalent to ", expected, actual)).ToUtf8String();\
376}
377
381#define COLLECTION_ASSERT_IS_SUBSET_OF(subset, superset, ...) {\
382 if (!System::CollectionAssertHelper::CheckDiffForAll([](int32_t v) {return v >= 0;}, COLLECTION_ASSERT_MAKE_DIFF(superset, subset)))\
383 FAIL() << (System::CollectionAssertHelper::ToFullMessage(__VA_ARGS__) + COLLECTION_ASSERT_COLLECTIONS_TO_MSG(u"subset of ", superset, subset)).ToUtf8String();\
384}
385
389#define COLLECTION_ASSERT_IS_NOT_SUBSET_OF(subset, superset, ...) {\
390 if (!System::CollectionAssertHelper::CheckDiffForAny([](int32_t v) {return v < 0;}, COLLECTION_ASSERT_MAKE_DIFF(superset, subset)))\
391 FAIL() << (System::CollectionAssertHelper::ToFullMessage(__VA_ARGS__) + COLLECTION_ASSERT_COLLECTIONS_TO_MSG(u"not subset of ", superset, subset)).ToUtf8String();\
392}
393
397#define TEST_IF_CASE_HAS(member, name) \
398 template<typename T> struct has_##member \
399 { \
400 private: \
401 static int detect(...); \
402 template<typename U> static decltype(void(std::declval<U>().name)) detect(const U&); \
403 public: \
404 static constexpr bool value = std::is_same<void, decltype(detect(std::declval<T>()))>::value; \
405 };
406
407
410#define TEST_IF_STATIC_METHOD(name) \
411 template<typename T> struct is_static_method \
412 { \
413 private: \
414 static int detect(...); \
415 template<typename U> static typename std::enable_if<std::is_function<decltype(U::name)>::value, bool>::type detect(const U&); \
416 public: \
417 static constexpr bool value = std::is_same<bool, decltype(detect(std::declval<T>()))>::value; \
418 };\
419
422#define TEST_IF_STATIC_METHOD_NAMED(name) \
423 template<typename T> struct is_static_method_##name \
424 { \
425 private: \
426 static int detect(...); \
427 template<typename U> static typename std::enable_if<std::is_function<decltype(U::name)>::value, bool>::type detect(const U&); \
428 public: \
429 static constexpr bool value = std::is_same<bool, decltype(detect(std::declval<T>()))>::value; \
430 };\
431
432#endif // _TestTools_h_
Interface of collection of elements. Objects of this class should only be allocated using System::Mak...
Definition: icollection.h:20
Interface of object providing enumerator on contained elements.
Definition: ienumerable.h:26
Interface of enumerator which can be used to iterate through some elements. Objects of this class sho...
Definition: ienumerator.h:63
static String ToString(const char_t *obj)
Substitution for C# ToString method to work on any C++ type.
Definition: object_ext.h:103
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:126
bool IsNullOrEmpty() const
Checks if string is empty or is considered null.
bool IsEmpty() const
Checks if string is both non-null and empty.
bool IsNull() const
Checks if string is considered null. String is null and only if it is constructed via String() constr...
Definition: string.h:292
bool AreEqual(const X &x, const Y &y)
Definition: compare.h:637
Definition: db_command.h:9
bool operator!=(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:173
Decimal operator*(const T &x, const Decimal &d)
Returns a new instance of Decimal class that represents a value that is a result of multiplication of...
Definition: decimal.h:556
bool operator==(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:167
Heler API for collection-related operations.
Definition: test_tools.h:212
static bool CheckDiffForAny(const std::function< bool(int)> &pred, const System::SharedPtr< System::Collections::Generic::ICollection< int32_t > > &values)
Checks that any collection element adheres the predicate.
Definition: test_tools.h:284
static System::SharedPtr< System::Collections::Generic::Dictionary< T1, int32_t > > MakeDiff(const System::SharedPtr< System::Collections::Generic::IEnumerable< T1 > > &expected, const System::SharedPtr< System::Collections::Generic::IEnumerable< T2 > > &actual)
Calculates 'diff' between two collections. For every element of each collection as key resulting valu...
Definition: test_tools.h:222
static System::String ToFullMessage(const System::String &message=u"")
Formats string to be used as message text.
Definition: test_tools.h:344
static bool CheckDiffForAll(const std::function< bool(int)> &pred, const System::SharedPtr< System::Collections::Generic::ICollection< int32_t > > &values)
Checks that all collection elements adhere the predicate.
Definition: test_tools.h:267
static System::String IEnumerableToStr(const System::SharedPtr< System::Collections::Generic::IEnumerable< T > > &ie)
Converts collection to string by joining string representations of elements.
Definition: test_tools.h:303
static System::String CollectionsToMsg(const System::String &extra_msg, const System::SharedPtr< System::Collections::Generic::IEnumerable< T1 > > &expected, const System::SharedPtr< System::Collections::Generic::IEnumerable< T2 > > &actual)
Serializes two collections for message representation.
Definition: test_tools.h:333
Provides a set of useful methods that check some basic properties of different types and functions.
Definition: test_tools.h:130
static bool IsNullOrEmpty(const SharedPtr< T > &collection)
Checks if collection is null or empty.
Definition: test_tools.h:173
static bool IsEmpty(const SharedPtr< T > &collection)
Checks if collection is empty.
Definition: test_tools.h:163
static bool IsNull(...)
Checks if specific value is null. Version for all other types.
Definition: test_tools.h:146
static bool AssertThrows(const std::function< void()> &func)
Checks if function throws exception of any type.
Definition: test_tools.h:196
static auto IsNull(const T &obj) -> decltype(obj==nullptr)
Checks if specific value is null. Version for all types which can be compared with nullptr.
Definition: test_tools.h:138
static bool IsNull(const System::String &str)
Checks if string is null.
Definition: test_tools.h:188
static bool IsEmpty(const System::String &str)
Checks if string is empty.
Definition: test_tools.h:154
static bool IsNullOrEmpty(const System::String &str)
Checks if string is null or empty.
Definition: test_tools.h:180