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"
28template <
typename TT,
typename T>
29class IEnumeratorIterator :
public Details::BaseIterator<std::input_iterator_tag, TT> {
36 IEnumeratorIterator(
const IEnumeratorIterator& o) : m_data(o.m_data) {};
40 IEnumeratorIterator& operator++() {
if (!m_data->MoveNext()) m_data =
nullptr;
return *
this; }
43 TT
operator*() {
return TT(m_data->get_Current()); };
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; }
63template <
typename TT,
typename T>
64class IEnumerableAdapter
68 using value_type = TT;
70 using const_iterator = IEnumeratorIterator<TT, T>;
73 IEnumerableAdapter() : m_data(nullptr) {}
80 IEnumeratorIterator<TT, T> begin()
const
82 return m_data->MoveNext() ? IEnumeratorIterator<TT, T>(m_data) : IEnumeratorIterator<TT,
T>(nullptr);
86 IEnumeratorIterator<TT, T> end()
const {
return IEnumeratorIterator<TT, T>(
nullptr); }
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>
106 IEnumerableAdapter<TT,
T>(data)
115class IEnumerableDefaultAdapter :
public IEnumerableAdapter<T, T>
121 IEnumerableAdapter<
T,
T>(data)
138static auto IsNull(
const T& obj) ->
decltype(obj ==
nullptr)
140 return obj ==
nullptr;
165 return collection->get_Count() == 0;
175 return collection ==
nullptr || collection->get_Count() == 0;
221 template <
typename T1,
typename T2>
224 std::list<std::pair<T1, int32_t>> diff;
226 auto ChangeDiff = [&](
const T1& key, int32_t delta)
237 diff.emplace_back(key, delta);
240 auto e_enumerator = (expected)->GetEnumerator();
241 while (e_enumerator->MoveNext())
243 ChangeDiff(e_enumerator->get_Current(), 1);
246 auto a_enumerator = (actual)->GetEnumerator();
247 while (a_enumerator->MoveNext())
249 ChangeDiff(
static_cast<T1
>(a_enumerator->get_Current()), -1);
252 auto result = System::MakeObject<System::Collections::Generic::Dictionary<T1, int32_t>>();
257 result->Add(d.first, d.second);
269 auto v_enumerator = (values)->GetEnumerator();
270 while (v_enumerator->MoveNext())
272 auto&& v = v_enumerator->get_Current();
286 auto v_enumerator = (values)->GetEnumerator();
287 while (v_enumerator->MoveNext())
289 auto&& v = v_enumerator->get_Current();
302 template <
typename T>
305 if (ie ==
nullptr || !ie->GetEnumerator()->MoveNext())
312 auto e_enumerator = (ie)->GetEnumerator();
313 while (e_enumerator->MoveNext())
315 auto&& e = e_enumerator->get_Current();
319 sb->Remove(sb->get_Length() - 2, 2);
321 return sb->ToString();
332 template <
typename T1,
typename T2>
337 return u
"\r\nExpected: " + extra_msg + u
"< " +
IEnumerableToStr(expected) + u
" >" +
346 return System::String(u
"Message: ") + (message.IsEmpty() ? u
"<empty>" : message);
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)))
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();\
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();\
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();\
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();\
397#define TEST_IF_CASE_HAS(member, name) \
398 template<typename T> struct has_##member \
401 static int detect(...); \
402 template<typename U> static decltype(void(std::declval<U>().name)) detect(const U&); \
404 static constexpr bool value = std::is_same<void, decltype(detect(std::declval<T>()))>::value; \
410#define TEST_IF_STATIC_METHOD(name) \
411 template<typename T> struct is_static_method \
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&); \
417 static constexpr bool value = std::is_same<bool, decltype(detect(std::declval<T>()))>::value; \
422#define TEST_IF_STATIC_METHOD_NAMED(name) \
423 template<typename T> struct is_static_method_##name \
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&); \
429 static constexpr bool value = std::is_same<bool, decltype(detect(std::declval<T>()))>::value; \
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