aboutsummaryrefslogtreecommitdiff
path: root/spec/LuaRuntimeSpec.cpp
blob: 924681a2e526ff1eff8b27e6681c1691270bc5a1 (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
#include <QtCore>
#include <uv.h>

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

class LuaRuntimeSpec : public QObject {
  Q_OBJECT

private slots:
  void beforeTestCase() { LuaRuntime::instance()->startEventLoop(); }

  void cleanupTestCase() {
    LuaRuntime::instance()->stopEventLoop();
    uv_library_shutdown();
  }

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

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

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

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

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

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

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

      lua->evaluate("local num = 5; return 5 == num");

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

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

      lua->evaluate("return nil");

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

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

      lua->queueTask([&wasTaskCalled]() { wasTaskCalled = true; });
      QVERIFY(NOT wasTaskCalled);

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

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

      lua->evaluate(R"(
        _G.wasTimerCalled = false;
        local timer = uv.new_timer();
        timer:start(1000, 0, function()
          _G.wasTimerCalled = true;
          timer:close()
        end)
      )");
      QVERIFY(evaluationCompletedSpy.wait());
      QVERIFY(NOT lua->evaluateSync("return _G.wasTimerCalled").toBool());

      QVERIFY(QTest::qWaitFor([&lua]() {
        return lua->evaluateSync("return _G.wasTimerCalled").toBool();
      }));
    }
  }
};

QTEST_REGISTER(LuaRuntimeSpec)
#include "LuaRuntimeSpec.moc"