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
19namespace System {
20
22
27template <typename TT, typename T>
28class IEnumeratorIterator : public Details::BaseIterator<std::input_iterator_tag, TT> {
29public:
32 IEnumeratorIterator(const System::SharedPtr<System::Collections::Generic::IEnumerator<T>>& data) : m_data(data) {};
35 IEnumeratorIterator(const IEnumeratorIterator& o) : m_data(o.m_data) {};
36
39 IEnumeratorIterator& operator++() { if (!m_data->MoveNext()) m_data = nullptr; return *this; }
42 TT operator*() { return TT(m_data->get_Current()); };
43
47 bool operator==(const IEnumeratorIterator& o) const { return m_data == o.m_data; }
51 bool operator!=(const IEnumeratorIterator& o) const { return m_data != o.m_data; }
52
53private:
56};
57
62template <typename TT, typename T>
63class IEnumerableAdapter
64{
65public:
67 using value_type = TT;
69 using const_iterator = IEnumeratorIterator<TT, T>;
70
72 IEnumerableAdapter() : m_data(nullptr) {}
75 IEnumerableAdapter(const System::SharedPtr<System::Collections::Generic::IEnumerable<T>>& data) : m_data(data->GetEnumerator()) {};
76
79 IEnumeratorIterator<TT, T> begin() const
80 {
81 return m_data->MoveNext() ? IEnumeratorIterator<TT, T>(m_data) : IEnumeratorIterator<TT, T>(nullptr);
82 }
85 IEnumeratorIterator<TT, T> end() const { return IEnumeratorIterator<TT, T>(nullptr); }
86
87private:
90};
91
97template <typename TT, class Guard = typename std::enable_if<System::Constraints::IsStdTuple<TT>::value>::type,
98 typename T = typename std::tuple_element<0, TT>::type>
99class IEnumerableTupleAdapter : public IEnumerableAdapter<TT, T>
100{
101public:
104 IEnumerableTupleAdapter(const System::SharedPtr<System::Collections::Generic::IEnumerable<T>>& data) :
105 IEnumerableAdapter<TT, T>(data)
106 {
107 }
108};
109
113template <typename T>
114class IEnumerableDefaultAdapter : public IEnumerableAdapter<T, T>
115{
116public:
119 IEnumerableDefaultAdapter(const System::SharedPtr<System::Collections::Generic::IEnumerable<T>>& data) :
120 IEnumerableAdapter<T, T>(data)
121 {
122 }
123};
124
126
129{
130
136template <typename T>
137static typename std::enable_if<std::is_arithmetic<T>::value || std::is_enum<T>::value, bool>::type IsNull(T obj)
138{
139 return false;
140}
146template <typename T>
147static typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_enum<T>::value, bool>::type IsNull(const T &obj)
148{
149 return obj == nullptr;
150}
156template <typename T>
157static bool IsNull(const SharedPtr<T>& obj)
158{
159 return obj == nullptr;
160}
167template<typename K, typename V>
169{
170 return kvp == nullptr;
171}
172
176static bool IsEmpty(const System::String& str)
177{
178 return str.IsEmpty();
179}
184template <typename T>
185static bool IsEmpty(const SharedPtr<T>& collection)
186{
187 return collection->get_Count() == 0;
188}
189
194template <typename T>
195static bool IsNullOrEmpty(const SharedPtr<T>& collection)
196{
197 return collection == nullptr || collection->get_Count() == 0;
198}
202static bool IsNullOrEmpty(const System::String& str)
203{
204 return str.IsNullOrEmpty();
205}
206
210static bool IsNull(const System::String& str)
211{
212 return str.IsNull();
213}
214
218static bool AssertThrows(const std::function<void()>& func)
219{
220 try
221 {
222 func();
223 }
224 catch (...) {
225 return true;
226 }
227 return false;
228}
229
230}; // namespace TestTools
231
234{
243 template <typename T1, typename T2>
245 {
246 auto diff = System::MakeObject<System::Collections::Generic::Dictionary<T1, int32_t>>();
247
248 auto e_enumerator = (expected)->GetEnumerator();
249 while (e_enumerator->MoveNext())
250 {
251 auto&& e = e_enumerator->get_Current();
252 int32_t v;
253 if (diff->TryGetValue(e, v))
254 {
255 diff->idx_set(e, v + 1);
256 }
257 else
258 {
259 diff->Add(e, 1);
260 }
261 }
262
263 auto a_enumerator = (actual)->GetEnumerator();
264 while (a_enumerator->MoveNext())
265 {
266 auto&& a = a_enumerator->get_Current();
267 int32_t v;
268 const auto aa = static_cast<T1>(a);
269 if (diff->TryGetValue(aa, v))
270 {
271 diff->idx_set(aa, v - 1);
272 }
273 else
274 {
275 diff->Add(aa, -1);
276 }
277 }
278 return diff;
279 }
280
285 static bool CheckDiffForAll(const std::function<bool(int)>& pred, const System::SharedPtr<System::Collections::Generic::ICollection<int32_t>>& values)
286 {
287 auto v_enumerator = (values)->GetEnumerator();
288 while (v_enumerator->MoveNext())
289 {
290 auto&& v = v_enumerator->get_Current();
291 if (!pred(v))
292 {
293 return false;
294 }
295 }
296 return true;
297 }
302 static bool CheckDiffForAny(const std::function<bool(int)>& pred, const System::SharedPtr<System::Collections::Generic::ICollection<int32_t>>& values)
303 {
304 auto v_enumerator = (values)->GetEnumerator();
305 while (v_enumerator->MoveNext())
306 {
307 auto&& v = v_enumerator->get_Current();
308 if (pred(v))
309 {
310 return true;
311 }
312 }
313 return false;
314 }
315
320 template <typename T>
322 {
323 if (ie == nullptr || !ie->GetEnumerator()->MoveNext())
324 {
325 return u"";
326 }
327
328 System::SharedPtr<System::Text::StringBuilder> sb = System::MakeObject<System::Text::StringBuilder>();
329
330 auto e_enumerator = (ie)->GetEnumerator();
331 while (e_enumerator->MoveNext())
332 {
333 auto&& e = e_enumerator->get_Current();
334 sb->Append(System::ObjectExt::ToString(e))->Append(u", ");
335 }
336
337 sb->Remove(sb->get_Length() - 2, 2);
338
339 return sb->ToString();
340 }
341
350 template <typename T1, typename T2>
354 {
355 return u"\r\nExpected: " + extra_msg + u"< " + IEnumerableToStr(expected) + u" >" +
356 u"\r\nActual: < " + IEnumerableToStr(actual) + u" >";
357 }
358
362 static System::String ToFullMessage(const System::String& message = u"")
363 {
364 return System::String(u"Message: ") + (message.IsEmpty() ? u"<empty>" : message);
365 }
366};
367
368} // namespace System
369
373#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())
378#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)))
379
383#define COLLECTION_ASSERT_ARE_EQUIVALENT(expected, actual, ...) {\
384 if (!System::CollectionAssertHelper::CheckDiffForAll([](int32_t v) {return v == 0;}, COLLECTION_ASSERT_MAKE_DIFF(expected, actual)))\
385 FAIL() << (System::CollectionAssertHelper::ToFullMessage(__VA_ARGS__) + COLLECTION_ASSERT_COLLECTIONS_TO_MSG(u"equivalent to ", expected, actual)).ToUtf8String();\
386}
387
391#define COLLECTION_ASSERT_ARE_NOT_EQUIVALENT(expected, actual, ...) {\
392 if (!System::CollectionAssertHelper::CheckDiffForAny([](int32_t v) {return v != 0;}, COLLECTION_ASSERT_MAKE_DIFF(expected, actual)))\
393 FAIL() << (System::CollectionAssertHelper::ToFullMessage(__VA_ARGS__) + COLLECTION_ASSERT_COLLECTIONS_TO_MSG(u"not equivalent to ", expected, actual)).ToUtf8String();\
394}
395
399#define COLLECTION_ASSERT_IS_SUBSET_OF(subset, superset, ...) {\
400 if (!System::CollectionAssertHelper::CheckDiffForAll([](int32_t v) {return v >= 0;}, COLLECTION_ASSERT_MAKE_DIFF(superset, subset)))\
401 FAIL() << (System::CollectionAssertHelper::ToFullMessage(__VA_ARGS__) + COLLECTION_ASSERT_COLLECTIONS_TO_MSG(u"subset of ", superset, subset)).ToUtf8String();\
402}
403
407#define COLLECTION_ASSERT_IS_NOT_SUBSET_OF(subset, superset, ...) {\
408 if (!System::CollectionAssertHelper::CheckDiffForAny([](int32_t v) {return v < 0;}, COLLECTION_ASSERT_MAKE_DIFF(superset, subset)))\
409 FAIL() << (System::CollectionAssertHelper::ToFullMessage(__VA_ARGS__) + COLLECTION_ASSERT_COLLECTIONS_TO_MSG(u"not subset of ", superset, subset)).ToUtf8String();\
410}
411
415#define TEST_IF_CASE_HAS(member, name) \
416 template<typename T> struct has_##member \
417 { \
418 private: \
419 static int detect(...); \
420 template<typename U> static decltype(void(std::declval<U>().name)) detect(const U&); \
421 public: \
422 static constexpr bool value = std::is_same<void, decltype(detect(std::declval<T>()))>::value; \
423 };
424
425
428#define TEST_IF_STATIC_METHOD(name) \
429 template<typename T> struct is_static_method \
430 { \
431 private: \
432 static int detect(...); \
433 template<typename U> static typename std::enable_if<std::is_function<decltype(U::name)>::value, bool>::type detect(const U&); \
434 public: \
435 static constexpr bool value = std::is_same<bool, decltype(detect(std::declval<T>()))>::value; \
436 };\
437
440#define TEST_IF_STATIC_METHOD_NAMED(name) \
441 template<typename T> struct is_static_method_##name \
442 { \
443 private: \
444 static int detect(...); \
445 template<typename U> static typename std::enable_if<std::is_function<decltype(U::name)>::value, bool>::type detect(const U&); \
446 public: \
447 static constexpr bool value = std::is_same<bool, decltype(detect(std::declval<T>()))>::value; \
448 };\
449
450#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:25
Interface of enumerator which can be used to iterate through some elements. Objects of this class sho...
Definition: ienumerator.h:63
Pair of key and value. This type should be allocated on stack and passed to functions by value or by ...
Definition: keyvalue_pair.h:20
static String ToString(const char_t *obj)
Substitution for C# ToString method to work on any C++ type.
Definition: object_ext.h:96
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:122
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:280
Definition: db_command.h:9
bool operator!=(ArraySegment< T > a, ArraySegment< T > b)
Definition: array_segment.h:157
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:151
Heler API for collection-related operations.
Definition: test_tools.h:234
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:302
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:244
static System::String ToFullMessage(const System::String &message=u"")
Formats string to be used as message text.
Definition: test_tools.h:362
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:285
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:321
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:351
Provides a set of useful methods that check some basic properties of different types and functions.
Definition: test_tools.h:129
static bool IsNullOrEmpty(const SharedPtr< T > &collection)
Checks if collection is null or empty.
Definition: test_tools.h:195
static bool IsEmpty(const SharedPtr< T > &collection)
Checks if collection is empty.
Definition: test_tools.h:185
static std::enable_if< std::is_arithmetic< T >::value||std::is_enum< T >::value, bool >::type IsNull(T obj)
Checks if specific value is null. Version for arithmetic and enum types.
Definition: test_tools.h:137
static bool AssertThrows(const std::function< void()> &func)
Checks if function throws exception of any type.
Definition: test_tools.h:218
static std::enable_if<!std::is_arithmetic< T >::value &&!std::is_enum< T >::value, bool >::type IsNull(const T &obj)
Checks if specific value is null. Version for non-arithmetic and non-enum value types.
Definition: test_tools.h:147
static bool IsNull(const System::String &str)
Checks if string is null.
Definition: test_tools.h:210
static bool IsEmpty(const System::String &str)
Checks if string is empty.
Definition: test_tools.h:176
static bool IsNull(System::Collections::Generic::KeyValuePair< K, V > &kvp)
Checks if specific value is null. Version for key-value pairs.
Definition: test_tools.h:168
static bool IsNullOrEmpty(const System::String &str)
Checks if string is null or empty.
Definition: test_tools.h:202
static bool IsNull(const SharedPtr< T > &obj)
Checks if specific value is null. Version for non-arithmetic value types.
Definition: test_tools.h:157