aboutsummaryrefslogtreecommitdiffstats
path: root/lib/LuaBridge/Tests/Source/IssueTests.cpp
blob: f6c65142cf1508a3142057e6116ad247f2b73c94 (plain)
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
// https://github.com/vinniefalco/LuaBridge
//
// Copyright 2019, Dmitry Tarakanov
// SPDX-License-Identifier: MIT

#include "TestBase.h"

struct IssueTests : TestBase
{
};

struct AbstractClass
{
  virtual int sum (int a, int b) = 0;
};

struct ConcreteClass : AbstractClass
{
  int sum (int a, int b) override
  {
    return a + b;
  }

  static AbstractClass& get ()
  {
    static ConcreteClass instance;
    return instance;
  }
};

TEST_F (IssueTests, Issue87)
{
  luabridge::getGlobalNamespace (L)
    .beginClass <AbstractClass> ("Class")
    .addFunction ("sum", &AbstractClass::sum)
    .endClass ()
    .addFunction ("getAbstractClass", &ConcreteClass::get);

  runLua ("result = getAbstractClass ():sum (1, 2)");
  ASSERT_TRUE (result ().isNumber ());
  ASSERT_EQ (3, result <int> ());
}

TEST_F (IssueTests, Issue121)
{
  runLua (R"(
    first = {
      second = {
        actual = "data"
      }
    }
  )");
  auto first = luabridge::getGlobal (L, "first");
  ASSERT_TRUE (first.isTable ());
  ASSERT_EQ (0, first.length ());
  ASSERT_TRUE (first ["second"].isTable ());
  ASSERT_EQ (0, first ["second"].length ());
}

void pushArgs (lua_State*)
{
}

template <class Arg, class... Args>
void pushArgs (lua_State* L, Arg arg, Args... args)
{
  luabridge::Stack <Arg>::push (L, arg);
  pushArgs (L, args...);
}

template <class... Args>
std::vector <luabridge::LuaRef> callFunction (const luabridge::LuaRef& function, Args... args)
{
  assert (function.isFunction ());

  lua_State* L = function.state ();
  int originalTop = lua_gettop (L);
  function.push (L);
  pushArgs (L, args...);

  luabridge::LuaException::pcall (L, sizeof... (args), LUA_MULTRET);

  std::vector <luabridge::LuaRef> results;
  int top = lua_gettop (L);
  results.reserve (top - originalTop);
  for (int i = originalTop + 1; i <= top; ++i)
  {
    results.push_back (luabridge::LuaRef::fromStack (L, i));
  }
  return results;
}

TEST_F (IssueTests, Issue160)
{
  runLua (
    "function isConnected (arg1, arg2) "
    " return 1, 'srvname', 'ip:10.0.0.1', arg1, arg2 "
    "end");

  luabridge::LuaRef f_isConnected = luabridge::getGlobal (L, "isConnected");

  auto v = callFunction (f_isConnected, 2, "abc");
  ASSERT_EQ (5u, v.size ());
  ASSERT_EQ (1, v [0].cast <int> ());
  ASSERT_EQ ("srvname", v [1].cast <std::string> ());
  ASSERT_EQ ("ip:10.0.0.1", v [2].cast <std::string> ());
  ASSERT_EQ (2, v [3].cast <int> ());
  ASSERT_EQ ("abc", v [4].cast <std::string> ());
}

struct Vector
{
  float getX () const
  {
    return x;
  }

  float x = 0;
};

struct WideVector : Vector
{
  WideVector (float, float, float, float w)
  {
    x = w;
  }
};

TEST_F (IssueTests, Issue178)
{
  luabridge::getGlobalNamespace (L)
    .beginClass <Vector> ("Vector")
    .addFunction ("getX", &Vector::getX)
    .addProperty ("X", &Vector::getX)
    .addData ("x", &Vector::x, true)
    .endClass ()
    .deriveClass <WideVector, Vector> ("WideVector")
    .addConstructor <void (*) (float, float, float, float)> ()
    .endClass ();

  runLua ("result = WideVector (0, 1, 2, 3).x");

  ASSERT_TRUE (result ().isNumber ());
  ASSERT_EQ (3.f, result <float> ());
}