aboutsummaryrefslogtreecommitdiff
path: root/spec/LuaRuntimeSpec.cpp
blob: 4d9270c509816027ebfc57c7cfa87f779879b14a (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <QtCore>
#include <qtestcase.h>
#include <uv.h>

#include "LuaRuntime.hpp"
#include "lua.h"
#include "testUtils.h"

// NOLINTBEGIN
class LuaRuntimeSpec : public QObject {
  Q_OBJECT

private slots:
  void beforeTestCase() { LuaRuntime::instance().start_event_loop(); }

  void cleanupTestCase() { LuaRuntime::instance().stop_event_loop(); }

  void test_evaluate() {
    describe("#evaluate");

    context("when given an expression returning a number");
    it("emits evaluation_completed with the result") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate("return 20 + 1 * 5");

      evaluation_completed_spy.wait();
      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.takeFirst().at(0);
      QCOMPARE(result, 25);
    }

    context("when given an expression returning a string");
    it("emits evaluation_completed with the result") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate("local name = 'world'; return 'hello ' .. name");

      evaluation_completed_spy.wait();
      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.takeFirst().at(0);
      QCOMPARE(result, "hello world");
    }

    context("when given an expression returning a boolean");
    it("emits evaluation_completed with the result") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate("local num = 5; return 5 == num");

      evaluation_completed_spy.wait();
      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.takeFirst().at(0);
      QCOMPARE(result, true);
    }

    context("when given an expression returning nil");
    it("emits evaluation_completed with the result") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate("return nil");

      QVERIFY(evaluation_completed_spy.wait());
      QCOMPARE(evaluation_completed_spy.count(), 1);
      QVariant result = evaluation_completed_spy.takeFirst().at(0);
      QCOMPARE(result, 0);
    }
  }

  void test_queue_task() {
    describe("#queue_task");

    context("when task is queued");
    it("evaluates task asynchronously") {
      auto &lua = LuaRuntime::instance();
      bool was_task_called = false;

      lua.queue_task([&was_task_called]() { was_task_called = true; });
      QVERIFY(NOT was_task_called);

      QVERIFY(QTest::qWaitFor([&was_task_called]() { return was_task_called; }));
    }
  }

  void test_sanity_check_uv_timer() {
    describe("uv timer");

    context("when a 1 second timer is set");
    it("calls callback after 1 second") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate(R"(
        _G.was_timer_called = false;
        local timer = uv.new_timer();
        timer:start(1000, 0, function()
          _G.was_timer_called = true;
          timer:close()
        end)
      )");
      QVERIFY(evaluation_completed_spy.wait());

      QVERIFY(wait_for_lua_to_be_true("return _G.was_timer_called"));
    }
  }

  void test_sanity_check_uv_spawn() {
    describe("uv spawn");

    it("calls exit callback when process exists") {
      auto &lua = LuaRuntime::instance();
      QSignalSpy evaluation_completed_spy(&lua, &LuaRuntime::evaluation_completed);

      lua.evaluate(R"(
        _G.spawn_exit_code = -1;
        local handle, pid = uv.spawn('ls', {}, function(code)
          _G.spawn_exit_code = code;
        end)
      )");
      QVERIFY(evaluation_completed_spy.wait());

      QVERIFY(wait_for_lua_to_be_true("return _G.spawn_exit_code == 0"));
    }
  }

  void test_push_qvariant() {
    describe("LuaRuntime::push_qvariant");

    context("when called with a QString");
    it("pushes lua string") {
      auto &lua = LuaRuntime::instance();
      auto *state = lua.get_state();

      LuaRuntime::push_qvariant(state, QString("hello"));

      QVERIFY(lua_isstring(state, 1));
      QCOMPARE(lua_tostring(state, 1), "hello");
      lua_pop(state, 1);
    }

    context("when called with a QVariant int");
    it("pushes lua number") {
      auto &lua = LuaRuntime::instance();
      auto *state = lua.get_state();

      LuaRuntime::push_qvariant(state, QVariant(42));

      QVERIFY(lua_isnumber(state, 1));
      QCOMPARE(lua_tointeger(state, 1), 42);
      lua_pop(state, 1);
    }

    context("when called with a QVariant double");
    it("pushes lua number") {
      auto &lua = LuaRuntime::instance();
      auto *state = lua.get_state();

      LuaRuntime::push_qvariant(state, QVariant(42.69));

      QVERIFY(lua_isnumber(state, 1));
      QCOMPARE(lua_tonumber(state, 1), 42.69);
      lua_pop(state, 1);
    }

    context("when called with a QVariant bool");
    it("pushes lua boolean") {
      auto &lua = LuaRuntime::instance();
      auto *state = lua.get_state();

      LuaRuntime::push_qvariant(state, QVariant(true));

      QVERIFY(lua_isboolean(state, 1));
      QCOMPARE(lua_toboolean(state, 1), true);
      lua_pop(state, 1);
    }
  }
};

QTEST_REGISTER(LuaRuntimeSpec)
#include "LuaRuntimeSpec.moc"
// NOLINTEND