Commit e6c2e1d9 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

unit test abstraction WIP

parent 9f0020d0
Loading
Loading
Loading
Loading
+20 −21
Original line number Diff line number Diff line
@@ -13,34 +13,32 @@
#include "csvfile.h"
//#include "csv_backend.h"
#include "../../util/runtimeexception.h"
#include "../../util/unittest.h"

namespace fnordmetric {
namespace csv_backend {
UNIT_TEST(CSVFileTest);

class CSVBackendTest {
public:
  CSVBackendTest() {}
TEST_CASE(CSVFileTest, TestOpenFile, [] () {
  //printf("run test!\n");
});

  void run() {
    testOpenFile();
  }

  void testOpenFile() {
    auto csv_file = CSVFile::openFile("test/fixtures/gbp_per_country.csxv");
    assert(csv_file.get() != nullptr);
    assert(csv_file->fd_ > 0);
  }
/*
static fnordmetric::util::UnitTest::TestCase __case(
    &csv_file_test,
    "testOpenFile",
    [] () {
    });
*/

};

}
}
  //void testOpenFile() {
  //  auto csv_file = CSVFile::openFile("test/fixtures/gbp_per_country.csv");
  //  assert(csv_file.get() != nullptr);
  //  assert(csv_file->fd_ > 0);
  //}

/*
int main() {
  fnordmetric::csv_backend::CSVBackendTest test;

  try {
    test.run();
    csv_file_test.run();
  } catch (fnordmetric::util::RuntimeException e) {
    printf("test fail :(\n\n");
    e.debugPrint();
@@ -49,3 +47,4 @@ int main() {

  printf("all tests passed! :)\n");
}
*/
+1 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ namespace fnordmetric {
namespace csv_backend {

class CSVFile {
  friend class CSVBackendTest;
  friend class CSVFileTest;
public:

  enum ErrorCodes {

src/util/unittest.h

0 → 100644
+71 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * Licensed under the MIT license (see LICENSE).
 */
#ifndef _FNORDMETRIC_UTIL_UNITTEST_H
#define _FNORDMETRIC_UTIL_UNITTEST_H
#include <functional>

namespace fnordmetric {
namespace util {

#define UNIT_TEST(T) \
  static fnordmetric::util::UnitTest T(#T); \
  int main() { \
    auto& t = T; \
    return t.run(); \
  }

#define TEST_CASE(T, N, L) \
  static fnordmetric::util::UnitTest::TestCase __case(&T, #N, L);

class UnitTest {
public:

  class TestCase {
  public:
    TestCase(
        UnitTest* test,
        const char* name,
        std::function<void ()> lambda) :
        name_(name),
        lambda_(lambda) {
      test->addTestCase(this);
    }

    const char* name_;
    const std::function<void ()> lambda_;
  };

  UnitTest(const char* name) : name_(name) {}

  void addTestCase(const TestCase* test_case) {
    cases_.push_back(test_case);
  }

  int run() {
    setbuf(stdout, NULL);
    printf("%s\n", name_);

    const TestCase* current_test_case = nullptr;
    for (auto test_case : cases_) {
      printf("    %s::%s", name_, test_case->name_);
      current_test_case = test_case;
      test_case->lambda_();
      printf(" [PASS]\n");
    }

    printf("All tests passed :)\n");
    return 0;
  }

protected:
  const char* name_;
  std::vector<const TestCase*> cases_;
};

}
}
#endif