Parcourir la source

Extend ctkTesting(Utilities|Macro) to support QStringList

Jean-Christophe Fillion-Robin il y a 9 ans
Parent
commit
7286ef817f

+ 25 - 0
Libs/Core/Testing/Cpp/ctkCoreTestingMacrosTest.cpp

@@ -60,6 +60,9 @@ int TestCheckStdStringDifferentFailure();
 int TestCheckQStringDifferentSuccess();
 int TestCheckQStringDifferentFailure();
 
+int TestCheckQStringListSuccess();
+int TestCheckQStringListFailure();
+
 //----------------------------------------------------------------------------
 #define TestMacro(MACRO_NAME) \
   if (Test##MACRO_NAME##Success() != EXIT_SUCCESS) \
@@ -86,6 +89,7 @@ int ctkCoreTestingMacrosTest(int , char * [])
   TestMacro(CheckQString)
   TestMacro(CheckStdStringDifferent)
   TestMacro(CheckQStringDifferent)
+  TestMacro(CheckQStringList)
   return EXIT_SUCCESS;
 }
 
@@ -335,3 +339,24 @@ int TestCheckQStringDifferentFailure()
   return EXIT_SUCCESS;
 }
 
+//----------------------------------------------------------------------------
+// Test CHECK_QSTRINGLIST
+
+//----------------------------------------------------------------------------
+int TestCheckQStringListSuccess()
+{
+  QStringList actual = QStringList() << "a" << "b" << "c";
+  QStringList expected = actual;
+  CHECK_QSTRINGLIST(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckQStringListFailure()
+{
+  QStringList actual = QStringList() << "a" << "b" << "c";
+  QStringList expected = QStringList() << "a" << "x" << "c";
+  CHECK_QSTRINGLIST(actual, expected);
+  return EXIT_SUCCESS;
+}
+

+ 19 - 0
Libs/Core/Testing/Cpp/ctkCoreTestingUtilitiesTest.cpp

@@ -29,6 +29,7 @@ bool TestCheckNotNull();
 bool TestCheckNull();
 bool TestCheckPointer();
 bool TestCheckString();
+bool TestCheckStringList();
 
 //----------------------------------------------------------------------------
 int ctkCoreTestingUtilitiesTest(int , char * [])
@@ -39,6 +40,7 @@ int ctkCoreTestingUtilitiesTest(int , char * [])
   res = res && TestCheckNull();
   res = res && TestCheckPointer();
   res = res && TestCheckString();
+  res = res && TestCheckStringList();
   return res ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
@@ -110,3 +112,20 @@ bool TestCheckString()
   return true;
 }
 
+//----------------------------------------------------------------------------
+bool TestCheckStringList()
+{
+  QStringList abc = QStringList() << "a" << "b" << "c";
+  QStringList axc = QStringList() << "a" << "x" << "c";
+  QStringList abcd = QStringList() << "a" << "b" << "c" << "d";
+  if (!CheckStringList(__LINE__, "TestCheckStringList", QStringList(), QStringList())
+      ||!CheckStringList(__LINE__, "TestCheckStringList", abc, abc)
+      || CheckStringList(__LINE__, "TestCheckStringList Expected Failure", abc, axc)
+      || CheckStringList(__LINE__, "TestCheckStringList Expected Failure", abc, abcd))
+    {
+    qWarning() << "Line " << __LINE__ << " - TestCheckString failed";
+    return false;
+    }
+  return true;
+}
+

+ 11 - 0
Libs/Core/ctkCoreTestingMacros.h

@@ -183,5 +183,16 @@
     } \
   }
 
+/// Verifies if actual QStringList is the same as expected.
+#define CHECK_QSTRINGLIST(actual, expected) \
+  { \
+  QStringList a = (actual); \
+  QStringList e = (expected); \
+  if (!ctkCoreTestingUtilities::CheckStringList(__LINE__,#actual " != " #expected, a, e)) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
 #endif
 

+ 7 - 0
Libs/Core/ctkCoreTestingUtilities.cpp

@@ -71,5 +71,12 @@ bool CheckString(int line, const QString& description,
   return true;
 }
 
+//----------------------------------------------------------------------------
+bool CheckStringList(int line, const QString& description,
+                     const QStringList& current, const QStringList& expected)
+{
+  return CheckList<QString>(line, description, current, expected, "CheckStringList");
+}
+
 } // namespace ctkCoreTestingUtilities
 

+ 5 - 0
Libs/Core/ctkCoreTestingUtilities.h

@@ -26,6 +26,7 @@
 
 // Qt includes
 #include <QString>
+#include <QStringList>
 
 /// This module provides functions to facilitate writing tests.
 ///
@@ -69,6 +70,10 @@ CTK_CORE_EXPORT
 bool CheckString(int line, const QString& description,
                  const char* current, const char* expected, bool errorIfDifferent = true );
 
+CTK_CORE_EXPORT
+bool CheckStringList(int line, const QString& description,
+                     const QStringList& current, const QStringList& expected);
+
 } // namespace ctkCoreTestingUtilities
 
 #include "ctkCoreTestingUtilities.tpp"

+ 33 - 0
Libs/Core/ctkCoreTestingUtilities.tpp

@@ -57,5 +57,38 @@ bool Check(int line, const QString& description,
   return true;
 }
 
+//----------------------------------------------------------------------------
+template<typename TYPE>
+bool CheckList(int line, const QString& description,
+               const QList<TYPE>& current, const QList<TYPE>& expected,
+               const QString& testName)
+{
+  QString msg;
+  if (current.count() != expected.count())
+    {
+    qWarning() << "\nLine " << line << " - " << description
+               << " : " << testName << " failed"
+               << "\nCompared lists have different sizes."
+               << "\n\tcurrent size :" << current.count()
+               << "\n\texpected size:" << expected.count()
+               << "\n\tcurrent:" << current
+               << "\n\texpected:" << expected;
+    return false;
+    }
+  for (int idx = 0; idx < current.count(); ++idx)
+    {
+    if (current.at(idx) != expected.at(idx))
+      {
+      qWarning() << "\nLine " << line << " - " << description
+                 << " : " << testName << " failed"
+                 << "\nCompared lists differ at index " << idx
+                 << "\n\tcurrent[" << idx << "] :" << current.at(idx)
+                 << "\n\texpected[" << idx << "]:" << expected.at(idx);
+      return false;
+      }
+    }
+  return true;
+}
+
 } // namespace ctkCoreTestingUtilities