about summary refs log blame commit diff stats
path: root/lua/chupacabra/test_chupacabra.lua
blob: 1eb5a48d7d3c1d1097a7b73e86c5087dab0844b4 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

                                         












                                 

                                                



                                                                                                                                        


   

                            

                                    


                                   
                                     



                                                         
                                                          
local chupacabra = require("chupacabra") 

-- convert a table to a string
local function table_to_string(t)
    local str = "{"
    for i, v in ipairs(t) do
        if i > 1 then
            str = str .. ", "
        end
        str = str .. tostring(v)
    end
    str = str .. "}"
    return str
end

local function test_case(input, expected_output)
    local output = chupacabra.run(input, {})
    local expected_output_str = type(expected_output) == "table" and table_to_string(expected_output) or tostring(expected_output)
    local output_str = type(output) == "table" and table_to_string(output) or tostring(output)
    assert(output_str == expected_output_str, "Test failed: " .. input .. " => " .. output_str .. ", expected: " .. expected_output_str)
    print("Test passed: " .. input .. " => " .. output_str)
end


test_case("1", 1)  -- 1
test_case("2 1 pop", 2) -- 2
test_case("[1 1]", {1, 1})
test_case("[1 2 3] map+", {2, 3, 4})
test_case("3 4 +", 7)  -- 3 + 4 = 7
test_case("5 2 -", 3)  -- 5 - 2 = 3
test_case("2 3 *", 6)  -- 2 * 3 = 6
test_case("8 2 /", 4.0)  -- 8 / 2 = 4
test_case("2 3 4 + *", 14)  -- 2 * (3 + 4) = 14
test_case("5 2 3 + 4 + +", 14)  -- 5 + 2 + 3 + 4 = 14
test_case("7 2 + 3 4 + +", 16)  -- 7 + 2 + 3 + 4 = 16
test_case("5 2 - 3 4 * +", 15)  -- (5 - 2) + (3 * 4) = 15
test_case("8 2 / 3 4 * +", 16.0)  -- (8 / 2) + (3 * 4) = 16