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

#include "TestBase.h"

#include "LuaBridge/detail/Iterator.h"

struct IteratorTests : TestBase
{
};

TEST_F (IteratorTests, DictionaryIteration)
{
  runLua (
    "result = {"
    "  bool = true,"
    "  int = 5,"
    "  c = 3.14,"
    "  [true] = 'D',"
    "  [8] = 'abc',"
    "  fn = function (i)"
    "    result = i + 1"
    "  end"
    "}");

  std::map <luabridge::LuaRef, luabridge::LuaRef> expected {
    {{L, "bool"}, {L, true}},
    {{L, "int"}, {L, 5}},
    {{L, 'c'}, {L, 3.14}},
    {{L, true}, {L, 'D'}},
    {{L, 8}, {L, "abc"}},
    {{L, "fn"}, {L, result () ["fn"]}},
  };

  std::map <luabridge::LuaRef, luabridge::LuaRef> actual;

  for (luabridge::Iterator iterator (result ()); !iterator.isNil (); ++iterator)
  {
    actual.emplace(iterator.key (), iterator.value ());
  }

  ASSERT_EQ (expected, actual);

  actual.clear ();

  for (auto&& pair : pairs (result ()))
  {
    actual.emplace (pair.first, pair.second);
  }

  ASSERT_EQ (expected, actual);
}

TEST_F (IteratorTests, SequenceIteration)
{
  runLua (
    "result = {"
    "  true,"
    "  5,"
    "  3.14,"
    "  'D',"
    "  'abc',"
    "  function (i)"
    "    result = i + 1"
    "  end"
    "}");

  std::map <luabridge::LuaRef, luabridge::LuaRef> expected {
    {{L, 1}, {L, true}},
    {{L, 2}, {L, 5}},
    {{L, 3}, {L, 3.14}},
    {{L, 4}, {L, 'D'}},
    {{L, 5}, {L, "abc"}},
    {{L, 6}, {L, result () [6]}},
  };

  std::map <luabridge::LuaRef, luabridge::LuaRef> actual;

  for (luabridge::Iterator iterator (result ()); !iterator.isNil (); ++iterator)
  {
    actual.emplace (iterator.key (), iterator.value ());
  }

  ASSERT_EQ (expected, actual);

  actual.clear ();

  for (auto&& pair : pairs (result ()))
  {
    actual.emplace (pair.first, pair.second);
  }

  ASSERT_EQ (expected, actual);
}