File: test-lua-utils.cpp

package info (click to toggle)
osm2pgsql 2.1.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,652 kB
  • sloc: cpp: 59,934; python: 1,039; ansic: 763; sh: 25; makefile: 14
file content (111 lines) | stat: -rw-r--r-- 3,316 bytes parent folder | download | duplicates (2)
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
/**
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * This file is part of osm2pgsql (https://osm2pgsql.org/).
 *
 * Copyright (C) 2006-2025 by the osm2pgsql developer community.
 * For a full list of authors see the git log.
 */

#include <catch.hpp>

#include "lua-utils.hpp"

#include <lua.hpp>

// Run the Lua code in "code" and then execute the function "func".
template <typename FUNC>
void test_lua(lua_State *lua_state, char const *code, FUNC&& func) {
    REQUIRE(lua_gettop(lua_state) == 0);
    REQUIRE(luaL_dostring(lua_state, code) == 0);
    REQUIRE(lua_gettop(lua_state) == 1);
    std::forward<FUNC>(func)();
    REQUIRE(lua_gettop(lua_state) == 1);
    lua_pop(lua_state, 1); // result from executing the Lua code
    REQUIRE(lua_gettop(lua_state) == 0);
}

TEST_CASE("check luaX_is_empty_table", "[NoDB]")
{
    std::shared_ptr<lua_State> lua_state{
        luaL_newstate(), [](lua_State *state) { lua_close(state); }};

    test_lua(lua_state.get(), "return {}", [&](){
        REQUIRE(luaX_is_empty_table(lua_state.get()));
    });

    test_lua(lua_state.get(), "return { 1 }", [&](){
        REQUIRE_FALSE(luaX_is_empty_table(lua_state.get()));
    });

    test_lua(lua_state.get(), "return { a = 'b' }", [&](){
        REQUIRE_FALSE(luaX_is_empty_table(lua_state.get()));
    });
}

TEST_CASE("check luaX_is_array with arrays", "[NoDB]")
{
    std::shared_ptr<lua_State> lua_state{
        luaL_newstate(), [](lua_State *state) { lua_close(state); }};

    test_lua(lua_state.get(), "return { 1, 2, 3 }", [&](){
        REQUIRE(luaX_is_array(lua_state.get()));
    });

    test_lua(lua_state.get(), "return { }", [&](){
        REQUIRE(luaX_is_array(lua_state.get()));
    });

    test_lua(lua_state.get(), "return { 1 }", [&](){
        REQUIRE(luaX_is_array(lua_state.get()));
    });

    test_lua(lua_state.get(), "return { [1] = 1, [2] = 2 }", [&](){
        REQUIRE(luaX_is_array(lua_state.get()));
    });
}

TEST_CASE("check luaX_is_array with non-arrays", "[NoDB]")
{
    std::shared_ptr<lua_State> lua_state{
        luaL_newstate(), [](lua_State *state) { lua_close(state); }};

    test_lua(lua_state.get(), "return { 1, nil, 3 }", [&](){
        REQUIRE_FALSE(luaX_is_array(lua_state.get()));
    });

    test_lua(lua_state.get(), "return { a = 'foo' }", [&](){
        REQUIRE_FALSE(luaX_is_array(lua_state.get()));
    });

    test_lua(lua_state.get(), "return { [1] = 'foo', ['bar'] = 2 }", [&](){
        REQUIRE_FALSE(luaX_is_array(lua_state.get()));
    });
}

TEST_CASE("luaX_for_each should call function n times", "[NoDB]")
{
    std::shared_ptr<lua_State> lua_state{
        luaL_newstate(), [](lua_State *state) { lua_close(state); }};

    test_lua(lua_state.get(), "return { 3, 4, 5 }", [&](){
        lua_Number sum = 0;
        luaX_for_each(lua_state.get(), [&]() {
            sum += lua_tonumber(lua_state.get(), -1);
        });
        REQUIRE(sum == 12);
    });
}

TEST_CASE("luaX_for_each should not call the function for empty arrays",
          "[NoDB]")
{
    std::shared_ptr<lua_State> lua_state{
        luaL_newstate(), [](lua_State *state) { lua_close(state); }};

    bool called = false;
    test_lua(lua_state.get(), "return {}", [&]() {
        luaX_for_each(lua_state.get(), [&]() { called = true; });
    });
    REQUIRE_FALSE(called);
}