Commit 6006ccb8 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

better unit test output

parent 2a19fcc0
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/invalid.csv");
  //EXPECT_EXCEPTION(errmsg, [] () {
    auto csv_file = CSVInputStream::openFile("test/fixtures/invalidx.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(RuntimeException, "error opening file '%s'", fp);
    RAISE_EXCEPTION(RuntimeException("error opening file '%s'", fp));
  }

  auto csv_file = new FileInputStream(fd);
+32 −8
Original line number Diff line number Diff line
@@ -15,12 +15,10 @@ namespace util {
RuntimeException::RuntimeException(
    const char* message,
    ...) :
    namespace_id_(0),
    type_id_(0),
    type_human_(NULL),
    file_(NULL),
    type_name_(nullptr),
    file_(nullptr),
    line_(0),
    func_(NULL) {
    func_(nullptr) {
  va_list args;
  va_start(args, message);
  int pos = vsnprintf(message_, sizeof(message_), message, args);
@@ -38,14 +36,40 @@ RuntimeException::RuntimeException(
  }
}

RuntimeException::RuntimeException(
    const RuntimeException& other) :
    type_name_(other.type_name_),
    file_(other.file_),
    line_(other.line_),
    func_(other.func_) {
  strncpy(message_, other.message_, sizeof(message_));
}

RuntimeException RuntimeException::setSource(
    const char* file,
    int line,
    const char* func) {
  file_ = file;
  line_ = line;
  func_ = func;
  return *this;
}

RuntimeException RuntimeException::setTypeName(const char* type_name) {
  type_name_ = type_name;
  return *this;
}

void RuntimeException::debugPrint() const {
  const char* type_name =
      type_name_ == nullptr ? "RuntimeException" : type_name_;

  fprintf(
      stderr,
      "RuntimeException[%s] (%i): %s\n"
      "\n%s: %s\n"
      "    in %s\n"
      "    in %s:%i\n",
      type_human_,
      type_id_,
      type_name,
      message_,
      func_,
      file_,
+10 −20
Original line number Diff line number Diff line
@@ -11,14 +11,10 @@
#include <string>

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

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

#define __RUNTIME_EXCEPTION(N, T, E, ...) \
    fnordmetric::util::RuntimeException( \
@@ -35,26 +31,20 @@ namespace util {

class RuntimeException : public std::exception {
public:

/*
      const void* namespace_id,
      int type_id,
      const char* type_human,
      const char* file,
      int line,
      const char* func,
      int posix_errno,
*/
  RuntimeException(const char* message, ...);
  RuntimeException(const RuntimeException& other);
  RuntimeException& operator=(const RuntimeException& other) = delete;

  void debugPrint() const;
  std::string getMessage() const;

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

private:
  const void* namespace_id_;
  const int type_id_;
  const char* type_human_;
  const char* type_name_;
  const char* file_;
  const int line_;
  int line_;
  const char* func_;
  char message_[1024];
};
+14 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
#define _FNORDMETRIC_UTIL_UNITTEST_H
#include "runtimeexception.h"
#include <functional>
#include <unordered_map>

namespace fnordmetric {
namespace util {
@@ -85,6 +86,8 @@ public:

    const TestCase* current_test_case = nullptr;
    int num_tests_passed = 0;
    std::unordered_map<const TestCase*, RuntimeException> errors;

    for (auto test_case : cases_) {
      printf("    %s::%s", name_, test_case->name_);
      current_test_case = test_case;
@@ -92,8 +95,8 @@ public:
      try {
        test_case->lambda_();
      } catch (fnordmetric::util::RuntimeException e) {
        printf(" [FAIL]\n\n");
        e.debugPrint();
        printf(" [FAIL]\n");
        errors.emplace(test_case, e);
        continue;
      }

@@ -105,6 +108,15 @@ public:
      printf("\n[SUCCESS] All tests passed :)\n");
      return 0;
    } else {
      for (auto test_case : cases_) {
        const auto& err = errors.find(test_case);

        if (err != errors.end()) {
          printf("\n[FAIL] %s::%s", name_, test_case->name_);
          err->second.debugPrint();
        }
      }

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