?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2018, Dmitry Tarakanov
  3. // SPDX-License-Identifier: MIT
  4.  
  5. #include "TestBase.h"
  6.  
  7. #include "LuaBridge/detail/Iterator.h"
  8.  
  9. struct IteratorTests : TestBase
  10. {
  11. };
  12.  
  13. TEST_F(IteratorTests, DictionaryIteration)
  14. {
  15.     runLua("result = {"
  16.            "  bool = true,"
  17.            "  int = 5,"
  18.            "  c = 3.14,"
  19.            "  [true] = 'D',"
  20.            "  [8] = 'abc',"
  21.            "  fn = function (i)"
  22.            "    result = i + 1"
  23.            "  end"
  24.            "}");
  25.  
  26.     std::map<luabridge::LuaRef, luabridge::LuaRef> expected{
  27.         {{L, "bool"}, {L, true}},
  28.         {{L, "int"}, {L, 5}},
  29.         {{L, 'c'}, {L, 3.14}},
  30.         {{L, true}, {L, 'D'}},
  31.         {{L, 8}, {L, "abc"}},
  32.         {{L, "fn"}, {L, result()["fn"]}},
  33.     };
  34.  
  35.     std::map<luabridge::LuaRef, luabridge::LuaRef> actual;
  36.  
  37.     for (luabridge::Iterator iterator(result()); !iterator.isNil(); ++iterator)
  38.     {
  39.         actual.emplace(iterator.key(), iterator.value());
  40.     }
  41.  
  42.     ASSERT_EQ(expected, actual);
  43.  
  44.     actual.clear();
  45.  
  46.     for (auto&& pair : pairs(result()))
  47.     {
  48.         actual.emplace(pair.first, pair.second);
  49.     }
  50.  
  51.     ASSERT_EQ(expected, actual);
  52. }
  53.  
  54. TEST_F(IteratorTests, SequenceIteration)
  55. {
  56.     runLua("result = {"
  57.            "  true,"
  58.            "  5,"
  59.            "  3.14,"
  60.            "  'D',"
  61.            "  'abc',"
  62.            "  function (i)"
  63.            "    result = i + 1"
  64.            "  end"
  65.            "}");
  66.  
  67.     std::map<luabridge::LuaRef, luabridge::LuaRef> expected{
  68.         {{L, 1}, {L, true}},
  69.         {{L, 2}, {L, 5}},
  70.         {{L, 3}, {L, 3.14}},
  71.         {{L, 4}, {L, 'D'}},
  72.         {{L, 5}, {L, "abc"}},
  73.         {{L, 6}, {L, result()[6]}},
  74.     };
  75.  
  76.     std::map<luabridge::LuaRef, luabridge::LuaRef> actual;
  77.  
  78.     for (luabridge::Iterator iterator(result()); !iterator.isNil(); ++iterator)
  79.     {
  80.         actual.emplace(iterator.key(), iterator.value());
  81.     }
  82.  
  83.     ASSERT_EQ(expected, actual);
  84.  
  85.     actual.clear();
  86.  
  87.     for (auto&& pair : pairs(result()))
  88.     {
  89.         actual.emplace(pair.first, pair.second);
  90.     }
  91.  
  92.     ASSERT_EQ(expected, actual);
  93. }
  94.