Pārlūkot izejas kodu

Add ctkCoreTestingUtilities module

This module facilitate the writing of tests that do not rely on
QTestLib unit testing framework offered by Qt.

It has been adapted from Slicer/Libs/MRML/Core/vtkMRMLCoreTestingUtilities
Jean-Christophe Fillion-Robin 9 gadi atpakaļ
vecāks
revīzija
ff9db34fe5

+ 3 - 0
Libs/Core/CMakeLists.txt

@@ -37,6 +37,9 @@ set(KIT_SRCS
   ctkCallback.h
   ctkCommandLineParser.cpp
   ctkCommandLineParser.h
+  ctkCoreTestingUtilities.cpp
+  ctkCoreTestingUtilities.tpp
+  ctkCoreTestingUtilities.h
   ctkDependencyGraph.cpp
   ctkDependencyGraph.h
   ctkErrorLogAbstractMessageHandler.cpp

+ 2 - 0
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -26,6 +26,7 @@ set(KITTests_SRCS
   ctkBooleanMapperTest.cpp
   ctkCallbackTest1.cpp
   ctkCommandLineParserTest1.cpp
+  ctkCoreTestingUtilitiesTest.cpp
   ctkExceptionTest.cpp
   ctkFileLoggerTest.cpp
   ctkHighPrecisionTimerTest.cpp
@@ -142,6 +143,7 @@ endif()
 SIMPLE_TEST( ctkBooleanMapperTest )
 SIMPLE_TEST( ctkCallbackTest1 )
 SIMPLE_TEST( ctkCommandLineParserTest1 )
+SIMPLE_TEST( ctkCoreTestingUtilitiesTest )
 SIMPLE_TEST( ctkDependencyGraphTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest2 )
 SIMPLE_TEST( ctkExceptionTest )

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

@@ -0,0 +1,112 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkCoreTestingUtilities.h"
+
+using namespace ctkCoreTestingUtilities;
+
+//----------------------------------------------------------------------------
+bool TestCheckInt();
+bool TestCheckNotNull();
+bool TestCheckNull();
+bool TestCheckPointer();
+bool TestCheckString();
+
+//----------------------------------------------------------------------------
+int ctkCoreTestingUtilitiesTest(int , char * [])
+{
+  bool res = true;
+  res = res && TestCheckInt();
+  res = res && TestCheckNotNull();
+  res = res && TestCheckNull();
+  res = res && TestCheckPointer();
+  res = res && TestCheckString();
+  return res ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+//----------------------------------------------------------------------------
+bool TestCheckInt()
+{
+  if (!CheckInt(__LINE__, "TestCheckInt", 1, 1)
+      || CheckInt(__LINE__, "TestCheckInt Expected Failure", 1, -1))
+    {
+    qWarning() << "Line " << __LINE__ << " - TestCheckInt failed";
+    return false;
+    }
+  return true;
+}
+
+//----------------------------------------------------------------------------
+bool TestCheckNotNull()
+{
+  int foo = 1;
+  if (!CheckNotNull(__LINE__, "TestCheckNotNull", &foo)
+      || CheckNotNull(__LINE__, "TestCheckNotNull Expected Failure", 0))
+    {
+    qWarning() << "Line " << __LINE__ << " - TestCheckNotNull failed";
+    return false;
+    }
+  return true;
+}
+
+//----------------------------------------------------------------------------
+bool TestCheckNull()
+{
+  int foo = 1;
+  if (!CheckNull(__LINE__, "TestCheckNull", 0)
+      || CheckNull(__LINE__, "TestCheckNull Expected Failure", &foo))
+    {
+    qWarning() << "Line " << __LINE__ << " - TestCheckNull failed";
+    return false;
+    }
+  return true;
+}
+
+//----------------------------------------------------------------------------
+bool TestCheckPointer()
+{
+  int foo = 1;
+  int bar = 1;
+  if (!CheckPointer(__LINE__, "TestCheckPointer", &foo, &foo)
+      || CheckPointer(__LINE__, "TestCheckPointer Expected Failure", &foo, &bar))
+    {
+    qWarning() << "Line " << __LINE__ << " - TestCheckPointer failed";
+    return false;
+    }
+  return true;
+}
+
+//----------------------------------------------------------------------------
+bool TestCheckString()
+{
+  const char* foo = "foo";
+  const char* bar = "bar";
+  if (!CheckString(__LINE__, "TestCheckString", 0, 0)
+      ||!CheckString(__LINE__, "TestCheckString", foo, foo)
+      || CheckString(__LINE__, "TestCheckString Expected Failure", foo, bar)
+      || CheckString(__LINE__, "TestCheckString Expected Failure", foo, 0))
+    {
+    qWarning() << "Line " << __LINE__ << " - TestCheckString failed";
+    return false;
+    }
+  return true;
+}
+

+ 75 - 0
Libs/Core/ctkCoreTestingUtilities.cpp

@@ -0,0 +1,75 @@
+
+#include "ctkCoreTestingUtilities.h"
+
+namespace ctkCoreTestingUtilities
+{
+
+//----------------------------------------------------------------------------
+bool CheckInt(int line, const QString& description,
+              int current, int expected)
+{
+  return Check<int>(line, description, current, expected, "CheckInt");
+}
+
+//----------------------------------------------------------------------------
+bool CheckNotNull(int line, const QString& description,
+                  const void* pointer)
+{
+  if(!pointer)
+    {
+    qWarning() << "\nLine " << line << " - " << description
+               << " : CheckNotNull failed"
+               << "\n\tpointer:" << pointer;
+    return false;
+    }
+  return true;
+}
+
+//----------------------------------------------------------------------------
+bool CheckNull(int line, const QString& description, const void* pointer)
+{
+  if(pointer)
+    {
+    qWarning() << "\nLine " << line << " - " << description
+               << " : CheckNull failed"
+               << "\n\tpointer:" << pointer;
+    return false;
+    }
+  return true;
+}
+
+//----------------------------------------------------------------------------
+bool CheckPointer(int line, const QString& description,
+                  void* current, void* expected, bool errorIfDifferent /* = true */)
+{
+  return Check<void*>(line, description, current, expected, "CheckPointer", errorIfDifferent);
+}
+
+//----------------------------------------------------------------------------
+bool CheckString(int line, const QString& description,
+                 const char* current, const char* expected, bool errorIfDifferent /* = true */)
+{
+  QString testName = "CheckString";
+
+  bool different = true;
+  if (current == 0 || expected == 0)
+    {
+    different = !(current == 0 && expected == 0);
+    }
+  else if(strcmp(current, expected) == 0)
+    {
+    different = false;
+    }
+  if(different == errorIfDifferent)
+    {
+    qWarning() << "\nLine " << line << " - " << description
+               << " : " << testName << "  failed"
+               << "\n\tcurrent :" << (current ? current : "<null>")
+               << "\n\texpected:" << (expected ? expected : "<null>");
+    return false;
+    }
+  return true;
+}
+
+} // namespace ctkCoreTestingUtilities
+

+ 74 - 0
Libs/Core/ctkCoreTestingUtilities.h

@@ -0,0 +1,74 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkCoreTestingUtilities_h
+#define __ctkCoreTestingUtilities_h
+
+// CTK includes
+#include <ctkCoreExport.h>
+
+// Qt includes
+#include <QString>
+
+/// This module provides functions to facilitate writing tests.
+///
+/// Before using this module, first consider the QTestLib
+/// unit testing framework available in Qt.
+///
+/// Example:
+///
+/// \code{.cpp}
+/// int current = 40 + 2;
+/// int expected = 43;
+/// if (!CheckInt(__LINE__, "40 + 2", current, expected))
+///   {
+///   return false;
+///   }
+/// \endcode
+///
+
+namespace ctkCoreTestingUtilities
+{
+
+CTK_CORE_EXPORT
+bool CheckInt(int line, const QString& description,
+              int current, int expected);
+
+CTK_CORE_EXPORT
+bool CheckNotNull(int line, const QString& description,
+                  const void* pointer);
+
+CTK_CORE_EXPORT
+bool CheckNull(int line, const QString& description,
+               const void* pointer);
+
+CTK_CORE_EXPORT
+bool CheckPointer(int line, const QString& description,
+                  void* current, void* expected, bool errorIfDifferent = true);
+
+CTK_CORE_EXPORT
+bool CheckString(int line, const QString& description,
+                 const char* current, const char* expected, bool errorIfDifferent = true );
+
+} // namespace ctkCoreTestingUtilities
+
+#include "ctkCoreTestingUtilities.tpp"
+
+#endif

+ 61 - 0
Libs/Core/ctkCoreTestingUtilities.tpp

@@ -0,0 +1,61 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QDebug>
+
+namespace ctkCoreTestingUtilities
+{
+
+//----------------------------------------------------------------------------
+template<typename TYPE>
+bool Check(int line, const QString& description,
+           TYPE current, TYPE expected,
+           const QString& _testName,
+           bool errorIfDifferent = true)
+{
+  QString testName = _testName.isEmpty() ? "Check" : _testName;
+  if (errorIfDifferent)
+    {
+    if(current != expected)
+      {
+      qWarning() << "\nLine " << line << " - " << description
+                 << " : " << testName << " failed"
+                 << "\n\tcurrent :" << current
+                 << "\n\texpected:" << expected;
+      return false;
+      }
+    }
+  else
+    {
+    if(current == expected)
+      {
+      qWarning() << "\nLine " << line << " - " << description
+                 << " : " << testName << " failed"
+                 << "\n\tcurrent :" << current
+                 << "\n\texpected to be different from:" << expected;
+      return false;
+      }
+    }
+  return true;
+}
+
+} // namespace ctkCoreTestingUtilities
+