Commit 239812ad authored by Paul Asmuth's avatar Paul Asmuth
Browse files

RAISE_ERRNO, use RAISE in EXPECTs

parent 6006ccb8
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -30,10 +30,10 @@ TEST_CASE(CSVInputStreamTest, TestInvalidFileName, [] () {
  auto errmsg = "error opening file 'test/fixtures/invalid.csv': "
      "No such file or directory";

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

TEST_CASE(CSVInputStreamTest, TestReadHeaders, [] () {
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ std::unique_ptr<FileInputStream> FileInputStream::openFile(
  int fd = open(fp, O_RDONLY);

  if (fd < 1) {
    RAISE_EXCEPTION(RuntimeException("error opening file '%s'", fp));
    RAISE_ERRNO(RuntimeException, "error opening file '%s'", fp);
  }

  auto csv_file = new FileInputStream(fd);
+11 −11
Original line number Diff line number Diff line
@@ -23,17 +23,6 @@ RuntimeException::RuntimeException(
  va_start(args, message);
  int pos = vsnprintf(message_, sizeof(message_), message, args);
  va_end(args);

  if (pos < 0) {
    pos = 0;
  }

  int posix_errno = -1;
  if (posix_errno > 0) {
    snprintf(message_ + pos, sizeof(message_) - pos, ": ");
    pos += 2;
    strerror_r(posix_errno, message_ + pos, sizeof(message_) - pos);
  }
}

RuntimeException::RuntimeException(
@@ -60,6 +49,17 @@ RuntimeException RuntimeException::setTypeName(const char* type_name) {
  return *this;
}

RuntimeException RuntimeException::setErrno(int posix_errno) {
  if (posix_errno > 0) {
    size_t pos = strlen(message_);
    snprintf(message_ + pos, sizeof(message_) - pos, ": ");
    pos += 2;
    strerror_r(posix_errno, message_ + pos, sizeof(message_) - pos);
  }

  return *this;
}

void RuntimeException::debugPrint() const {
  const char* type_name =
      type_name_ == nullptr ? "RuntimeException" : type_name_;
+8 −11
Original line number Diff line number Diff line
@@ -11,20 +11,16 @@
#include <string>

#define RAISE_EXCEPTION(E) \
    throw E.setSource(__FILE__, __LINE__, __PRETTY_FUNCTION__);
    throw (E).setSource(__FILE__, __LINE__, __PRETTY_FUNCTION__); while(0) {}

#define RAISE(E, M, ...) \
    RAISE_EXCEPTION(E((M), __VA_ARGS__).setTypeName(#E)); \
    RAISE_EXCEPTION(E((M), __VA_ARGS__).setTypeName(#E)); while(0) {}

#define __RUNTIME_EXCEPTION(N, T, E, ...) \
    fnordmetric::util::RuntimeException( \
        __VA_ARGS__)

#define RUNTIME_EXCEPTION(N, T, ...) \
    __RUNTIME_EXCEPTION(N, T, -1, __VA_ARGS__)

#define RUNTIME_EXCEPTION_ERRNO(N, T, ...) \
    __RUNTIME_EXCEPTION(N, T, errno, __VA_ARGS__)
#define RAISE_ERRNO(E, M, ...) \
    { \
      int e = errno; \
      RAISE_EXCEPTION(E((M), __VA_ARGS__).setTypeName(#E).setErrno(e)); \
    }

namespace fnordmetric {
namespace util {
@@ -40,6 +36,7 @@ public:

  RuntimeException setSource(const char* file, int line, const char* func);
  RuntimeException setTypeName(const char* type_name);
  RuntimeException setErrno(int posix_errno);

private:
  const char* type_name_;
+14 −11
Original line number Diff line number Diff line
@@ -26,9 +26,8 @@ namespace util {

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

@@ -41,16 +40,14 @@ namespace util {
        raised = true; \
        auto msg = e.getMessage().c_str(); \
        if (strcmp(msg, E) != 0) { \
          throw RUNTIME_EXCEPTION( \
              &typeid(fnordmetric::util::UnitTest), \
              fnordmetric::util::UnitTest::kExpectationFailed, \
          RAISE( \
              fnordmetric::util::UnitTest::ExpectationFailed, \
              "excepted exception '%s' but got '%s'", E, msg); \
        } \
      } \
      if (!raised) { \
        throw RUNTIME_EXCEPTION( \
            &typeid(fnordmetric::util::UnitTest), \
            fnordmetric::util::UnitTest::kExpectationFailed, \
        RAISE( \
            fnordmetric::util::UnitTest::ExpectationFailed, \
            "excepted exception '%s' but got no exception", E); \
      } \
    }
@@ -58,6 +55,12 @@ namespace util {
class UnitTest {
public:
  static const int kExpectationFailed = 0;
  struct ExpectationFailed : public RuntimeException {
    template <typename... T>
    ExpectationFailed(
        const char* message, T... args) :
        RuntimeException(message, args...) {}
  };

  class TestCase {
  public:
@@ -119,8 +122,8 @@ public:

      printf(
          "\n[FAIL] %i/%i tests failed :(\n",
          cases_.size() - num_tests_passed,
          cases_.size());
          (int) cases_.size() - num_tests_passed,
          (int) cases_.size());
      return 1;
    }
  }