aboutsummaryrefslogtreecommitdiff
path: root/spec/LuaRuntimeSpec.cpp
blob: 149e1e1b3e581387f863022452ba0ed789af6682 (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
68
69
70
71
72
73
74
75
76
#include "testUtils.h"
#include <QtCore>
#include <atomic>
#include <uv.h>

#include "LuaRuntime.hpp"

class LuaRuntimeSpec : public QObject {
  Q_OBJECT

private slots:
  void cleanupTestCase() { uv_library_shutdown(); }

  void testSanityCheckBuiltins() {
    auto lua = LuaRuntime::instance();

    it("evaluates simple expression") {
      lua->startEventLoop();
      std::atomic<int> foobar = 2;

      lua->queueTask([&foobar]() { foobar = 10; });

      lua->evaluate(R"(
        print('Hello -- ');
        local timer = uv.new_timer();
        timer:start(1000, 0, function()
          print('inside timer 1')
          print('inside timer 2')
          print('inside timer 3')
          print('inside timer 4')
          print('inside timer 5')
          timer:close()
        end);
        print('-- end');
      )");
      lua->queueTask([]() { qDebug() << "---- 1"; });
      lua->queueTask([]() { qDebug() << "---- 2"; });
      lua->queueTask([]() { qDebug() << "---- 3"; });
      std::this_thread::sleep_for(std::chrono::seconds(2));
      // std::this_thread::sleep_for(std::chrono::milliseconds(20));
      lua->stopEventLoop();

      qDebug() << "foobar" << foobar;

      QCOMPARE(1, 1);
    }
  }

  void testSanityCheckBuiltins2() {
    auto lua = LuaRuntime::instance();

    it("evaluates again") {
      lua->startEventLoop();

      lua->queueTask([]() { qDebug() << "---- 5"; });
      lua->evaluate(R"(
        print('Hello -- ');
        local timer = uv.new_timer();
        timer:start(1000, 0, function()
          print('%%%%% blagb')
          timer:close()
        end);
        print('-- end');
      )");
      lua->queueTask([]() { qDebug() << "---- 5"; });
      // TODO: Impl
      std::this_thread::sleep_for(std::chrono::seconds(2));
      lua->stopEventLoop();

      QCOMPARE(1, 1);
    }
  }
};

QTEST_REGISTER(LuaRuntimeSpec)
#include "LuaRuntimeSpec.moc"