aboutsummaryrefslogtreecommitdiff
path: root/spec/testUtils.cpp
blob: c359e915e2064046056554ce494fafa57e20769a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <QtTest/QtTest>
#include <QtTest/qtestcase.h>
#include <cstdio>

#include "LuaRuntime.hpp"

std::vector<std::function<QObject *()>> &get_qtest_registry() {
  static std::vector<std::function<QObject *()>> registry;
  return registry;
}

int run_app_tests() {
  int exit_code = 0;

  QString test_name = getenv("TEST_NAME");
  if (test_name.startsWith("lua:"))
    return 0;

  for (const auto &run_test : get_qtest_registry()) {
    auto *test = run_test();
    if (test_name.isEmpty() || test->objectName().contains(test_name))
      exit_code |= QTest::qExec(test);
    delete test;
  }

  get_qtest_registry().clear();
  return exit_code;
}

int run_lua_tests() {
  QString test_name = getenv("TEST_NAME");
  if (test_name.startsWith("lua:")) {
    test_name = test_name.remove(0, 4);
  } else if (!test_name.isEmpty())
    return 0;

  QDir dir("../spec/lua"); // TODO: relative to root instead of build/?
  QStringList spec_files = dir.entryList(QDir::Files);
  auto &lua = LuaRuntime::instance();

  lua.start_event_loop();
  lua.require_module("null-browser.test-utils");

  for (auto &file : spec_files) {
    auto is_spec = file.endsWith("_spec.lua");
    auto should_run_spec = test_name.isEmpty() || file.contains(test_name);
    if (is_spec && should_run_spec) {
      qDebug() << "Running suite: " << file;
      lua.load_file_sync(dir.filePath(file));
    }
  }

  lua.stop_event_loop();

  qDebug() << "Tests ran successfully";
  return 0;
}

bool wait_for_lua_to_be_true(QString lua_code) {
  return QTest::qWaitFor([&lua_code]() {
    auto &lua = LuaRuntime::instance();
    QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);
    lua.evaluate(lua_code);
    evaluation_completed_spy.wait();
    return evaluation_completed_spy.first().first().toBool();
  });
}