Unverified Commit 6a535174 authored by Kaitlyn Lee's avatar Kaitlyn Lee Committed by GitHub
Browse files

Added gtest for ID and split TestUtilities into a cpp and h file to fix...

Added gtest for ID and split TestUtilities into a cpp and h file to fix duplicate symbol error when building. (#2824)

Code reviewed and tested
parent 55e7ea22
Loading
Loading
Loading
Loading

isis/tests/IDTests.cpp

0 → 100644
+83 −0
Original line number Diff line number Diff line
#include "ID.h"
#include "IException.h"
#include "TestUtilities.h"
#include "gtest/gtest.h"

#include <QString>

TEST(ID, ConstructorDefaultBaseNum) {
  Isis::ID pid("ABC?");
  EXPECT_PRED_FORMAT2(Isis::AssertQStringsEqual, pid.Next(), "ABC1");
  EXPECT_PRED_FORMAT2(Isis::AssertQStringsEqual, pid.Next(), "ABC2");
}


TEST(ID, ConstructorSetBaseNum) {
  Isis::ID pid("ABC?", 2);
  EXPECT_PRED_FORMAT2(Isis::AssertQStringsEqual, pid.Next(), "ABC2");
  EXPECT_PRED_FORMAT2(Isis::AssertQStringsEqual, pid.Next(), "ABC3");
}


TEST(ID, ConstructorNoReplacement) {
  QString message = "No replacement set in string";
  try {
    Isis::ID pid("ABC");
      FAIL() << "Expected an IException";
  }
  catch(Isis::IException &e) {
    EXPECT_PRED_FORMAT2(Isis::AssertIExceptionMessage, e, message);
  }
  catch(...) {
    FAIL() << "Expected error message: \"" << message.toStdString() << "\"";
  }
}


TEST(ID, ConstructorMultipleReplacements) {
  QString message = "contains more than one replacement set";
  try {
    Isis::ID pid("A?B?C");
    FAIL() << "Expected an IException";
  }
  catch(Isis::IException &e) {
    EXPECT_PRED_FORMAT2(Isis::AssertIExceptionMessage, e, message);
  }
  catch(...) {
    FAIL() << "Expected error message: \"" << message.toStdString() << "\"";
  }
}


TEST(ID, Next) {
  Isis::ID pid("ABC??");
  QString idString;

  for (int i = 1; i < 20; i++) {
    if (i < 10) {
      idString = "ABC0" + QString::number(i);
    }
    else {
      idString = "ABC" + QString::number(i);
    }
    EXPECT_PRED_FORMAT2(Isis::AssertQStringsEqual, pid.Next(), idString);
  }
}

TEST(ID, NextMaximumReached) {
  QString message = "Maximum number reached for string";
  try {
    Isis::ID pid("ABC?");
    for (int i = 0; i < 11; i++) {
      pid.Next();
    } 
    FAIL() << "Expected an IException";
  }
  catch(Isis::IException &e) {
    EXPECT_PRED_FORMAT2(Isis::AssertIExceptionMessage, e, message);
  }
  catch(...) {
    FAIL() << "Expected error message: \"" << message.toStdString() << "\"";
  }
}
+56 −0
Original line number Diff line number Diff line
#include "TestUtilities.h"

namespace Isis {

  /**
   * Custom IException assertion that checks that the exception message contains
   * a substring.
   */
  ::testing::AssertionResult AssertIExceptionMessage(
          const char* e_expr,
          const char* contents_expr,
          IException &e,
          QString contents) {
    if (e.toString().contains(contents)) return ::testing::AssertionSuccess();

    return ::testing::AssertionFailure() << "IExeption "<< e_expr << "\'s error message (\"" 
       << e.toString().toStdString() << "\") does not contain " << contents_expr << " (\"" 
       << contents.toStdString() << "\").";
  }
  
  
  /**
   * Custom IException assertion that checks that the exception is the expected 
   * IException::ErrorType.
   */
  ::testing::AssertionResult AssertIExceptionError(
          const char* e_expr,
          const char* errorType_expr,
          IException &e,
          IException::ErrorType errorType) {
    if (e.errorType() == errorType) return ::testing::AssertionSuccess();

    return ::testing::AssertionFailure() << "IExeption "<< e_expr << "\'s error type (" 
        << std::to_string(e.errorType()) << ") does not match expected error type (" 
        << std::to_string(errorType) << ").";
  }


  /**
   * Custom QString assertion that properly outputs them as string if they
   * are different.
   */
  ::testing::AssertionResult AssertQStringsEqual(
        const char* string1_expr,
        const char* string2_expr,
        QString string1,
        QString string2) {
    if (string1 != string2) {
      return ::testing::AssertionFailure() << "QStrings " << string1_expr
          << " (" << string1.toStdString() << ") and " << string2_expr
          << " (" << string2.toStdString() << ") are not the same.";
    }

    return ::testing::AssertionSuccess();
  }
}
+3 −37
Original line number Diff line number Diff line
@@ -11,57 +11,23 @@

namespace Isis {

  /**
   * Custom IException assertion that checks that the exception message contains
   * a substring.
   */
  ::testing::AssertionResult AssertIExceptionMessage(
          const char* e_expr,
          const char* contents_expr,
          IException &e,
          QString contents) {
    if (e.toString().contains(contents)) return ::testing::AssertionSuccess();
          QString contents);

    return ::testing::AssertionFailure() << "IExeption "<< e_expr << "\'s error message (\"" 
       << e.toString().toStdString() << "\") does not contain " << contents_expr << " (\"" 
       << contents.toStdString() << "\").";
  }
  
  
  /**
   * Custom IException assertion that checks that the exception is the expected 
   * IException::ErrorType.
   */
  ::testing::AssertionResult AssertIExceptionError(
          const char* e_expr,
          const char* errorType_expr,
          IException &e,
          IException::ErrorType errorType) {
    if (e.errorType() == errorType) return ::testing::AssertionSuccess();

    return ::testing::AssertionFailure() << "IExeption "<< e_expr << "\'s error type (" 
        << std::to_string(e.errorType()) << ") does not match expected error type (" 
        << std::to_string(errorType) << ").";
  }
          IException::ErrorType errorType);


  /**
   * Custom QString assertion that properly outputs them as string if they
   * are different.
   */
  ::testing::AssertionResult AssertQStringsEqual(
        const char* string1_expr,
        const char* string2_expr,
        QString string1,
        QString string2) {
    if (string1 != string2) {
      return ::testing::AssertionFailure() << "QStrings " << string1_expr
          << " (" << string1.toStdString() << ") and " << string2_expr
          << " (" << string2.toStdString() << ") are not the same.";
    }

    return ::testing::AssertionSuccess();
  }
        QString string2);
}

#endif