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

#include "TestBase.h"
#include "TestTypes.h"

#include "LuaBridge/Vector.h"

#include <vector>

template <class T>
struct VectorTest : TestBase
{
};

TYPED_TEST_CASE_P (VectorTest);

TYPED_TEST_P (VectorTest, LuaRef)
{
  using Traits = TypeTraits <TypeParam>;

  this->runLua ("result = {" + Traits::list () + "}");

  std::vector <TypeParam> expected (Traits::values ());
  std::vector <TypeParam> actual = this->result ();
  ASSERT_EQ (expected, actual);
}

REGISTER_TYPED_TEST_CASE_P (VectorTest, LuaRef);

INSTANTIATE_TYPED_TEST_CASE_P(VectorTest, VectorTest, TestTypes);


namespace {

struct Data
{
  /* explicit */ Data (int i) : i (i) {}

  int i;
};

bool operator== (const Data& lhs, const Data& rhs)
{
  return lhs.i == rhs.i;
}

std::ostream& operator<< (std::ostream& lhs, const Data& rhs)
{
  lhs << "{" << rhs.i << "}";
  return lhs;
}

std::vector <Data> processValues(const std::vector <Data>& data)
{
  return data;
}

std::vector <Data> processPointers(const std::vector <const Data*>& data)
{
  std::vector <Data> result;
  for (const auto* item : data)
  {
    result.emplace_back (*item);
  }
  return result;
}

} // namespace

struct VectorTests : TestBase
{
};

TEST_F (VectorTests, PassFromLua)
{
  luabridge::getGlobalNamespace (L)
    .beginClass <Data> ("Data")
    .addConstructor <void (*) (int)> ()
    .endClass ()
    .addFunction ("processValues", &processValues)
    .addFunction ("processPointers", &processPointers);

  resetResult ();
  runLua ("result = processValues ({Data (-1), Data (2)})");

  ASSERT_EQ (
    std::vector <Data> ({-1, 2}),
    result <std::vector <Data>>());

  resetResult ();
  runLua ("result = processValues ({Data (-3), Data (4)})");

  ASSERT_EQ(
    std::vector <Data> ({-3, 4}),
    result <std::vector <Data>> ());
}