Commit 2f6cc7aa authored by Paul Asmuth's avatar Paul Asmuth
Browse files

EXPECT_EXCEPTION

parent da33e3a1
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -25,9 +25,13 @@ TEST_CASE(CSVFileTest, TestOpenFile, [] () {
});

TEST_CASE(CSVFileTest, TestInvalidFileName, [] () {
  EXPECT(1 == 2);
  //auto csv_file = CSVFile::openFile("test/fixtures/invalid.csv");
  //assert(csv_file.get() != nullptr);
  auto errmsg = "error opening file 'test/fixtures/invalid.csv': "
      "No such file or directory";

  EXPECT_EXCEPTION(errmsg, [] () {
    auto csv_file = CSVFile::openFile("test/fixtures/invalid.csv");
    EXPECT(csv_file.get() != nullptr);
  });
});

/*
+4 −0
Original line number Diff line number Diff line
@@ -58,5 +58,9 @@ void RuntimeException::debugPrint() const {
      line_);
}

std::string RuntimeException::getMessage() const {
  return std::string(message_);
}

}
}
+3 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
#define _FNORDMETRIC_UTIL_RUNTIMEXCEPTION_H
#include <errno.h>
#include <exception>
#include <string>

#define __RUNTIME_EXCEPTION(N, T, E, ...) \
    fnordmetric::util::RuntimeException( \
@@ -45,6 +46,8 @@ public:

  void debugPrint() const;

  std::string getMessage() const;

private:
  const void* namespace_id_;
  const int type_id_;
+27 −4
Original line number Diff line number Diff line
@@ -23,13 +23,36 @@ namespace util {
    static fnordmetric::util::UnitTest::TestCase __##T##__case__##N(&T, #N, L);


#define EXPECT(E) \
    if (!(E)) { \
#define EXPECT(X) \
    if (!(X)) { \
      throw RUNTIME_EXCEPTION( \
          &typeid(fnordmetric::util::UnitTest), \
          fnordmetric::util::UnitTest::kExpectationFailed, \
          "expectation failed: %s", #E); \
          "expectation failed: %s", #X); \
    }

#define EXPECT_EXCEPTION(E, L) \
    { \
      bool raised = false; \
      try { \
        L(); \
      } catch (fnordmetric::util::RuntimeException e) { \
        raised = true; \
        auto msg = e.getMessage().c_str(); \
        if (strcmp(msg, E) != 0) { \
          throw RUNTIME_EXCEPTION( \
              &typeid(fnordmetric::util::UnitTest), \
              fnordmetric::util::UnitTest::kExpectationFailed, \
              "excepted exception '%s' but got '%s'", E, msg); \
        } \
      } \
      if (!raised) { \
        throw RUNTIME_EXCEPTION( \
            &typeid(fnordmetric::util::UnitTest), \
            fnordmetric::util::UnitTest::kExpectationFailed, \
            "excepted exception '%s' but got no exception", E); \
      } \
    }

class UnitTest {
public: