Skip to content

Commit

Permalink
Merge pull request #1553 from grumpycoders/lua-unit-tests
Browse files Browse the repository at this point in the history
Adding Lua unit testing.
  • Loading branch information
nicolasnoble authored Jan 25, 2024
2 parents 77e49e4 + 99eb152 commit ab7f2fd
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 6 deletions.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,13 @@ sstate.proto
comport.txt

# NVIDIA Nsight
GPUCache
GPUCache

# Lua tests results
luacov.stats.out

# luarocks stuff
/luarocks
/lua
/lua_modules
/.luarocks
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,6 @@
[submodule "third_party/luacov"]
path = third_party/luacov
url = https://github.com/grumpycoders/luacov.git
[submodule "third_party/luaunit"]
path = third_party/luaunit
url = https://github.com/bluebird75/luaunit.git
4 changes: 2 additions & 2 deletions src/main/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ int pcsxMain(int argc, char **argv) {
bool luacovEnabled = false;
if (args.get<bool>("luacov")) {
auto L = *emulator->m_lua;
L.load("package.path = package.path .. ';./lua_modules/share/lua/5.1/?.lua;./third_party/luacov/src/?.lua'",
L.load("package.path = package.path .. ';./lua_modules/share/lua/5.1/?.lua;../../../third_party/luacov/src/?.lua;./third_party/luacov/src/?.lua'",
"internal:package.path.lua");
try {
L.load(R"(
Expand Down Expand Up @@ -426,7 +426,7 @@ runner.init({
// Then run all of the Lua "exec" commands.
auto luaexecs = args.values("exec");
for (auto &luaexec : luaexecs) {
L->load(luaexec.data(), "cmdline:");
L->load(std::string(luaexec), "cmdline:");
}

system->m_inStartup = false;
Expand Down
33 changes: 33 additions & 0 deletions tests/lua/basic.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- Copyright (C) 2024 PCSX-Redux authors
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the
-- Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

local lu = require 'luaunit'

TestBasic = {}

function TestBasic:test_basic()
lu.assertEquals(1, 1)
end

function TestBasic:test_coroutine()
local testCoroutine = coroutine.running()
PCSX.nextTick(function()
coroutine.resume(testCoroutine, 42)
end)
local r = coroutine.yield()
lu.assertEquals(r, 42)
end
30 changes: 30 additions & 0 deletions tests/lua/file.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- Copyright (C) 2024 PCSX-Redux authors
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the
-- Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

local lu = require 'luaunit'

TestFile = {}

function TestFile:test_buffer()
local buf = Support.File.buffer()
buf:write('hello')
buf:write(' ')
buf:write('world')
lu.assertEquals(buf:size(), 11)
local r = buf:read(buf:size())
lu.assertEquals(tostring(r), 'hello world')
end
68 changes: 68 additions & 0 deletions tests/pcsxrunner/lua.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/***************************************************************************
* Copyright (C) 2024 PCSX-Redux authors *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
***************************************************************************/

#include "gtest/gtest.h"
#include "main/main.h"

static const char prefix[] = R"(
package.path = package.path
.. ';../../../third_party/luaunit/?.lua'
.. ';./third_party/luaunit/?.lua'
.. ';../../../?.lua'
.. ';./?.lua'
)";

static const char suffix[] = R"(
coroutine.resume(coroutine.create(function()
PCSX.quit(require('luaunit').LuaUnit.new():runSuite '--verbose')
end))
)";

template <typename... Args>
int runLuaInt(Args... args) {
MainInvoker invoker("-no-ui", "-cli", "-bios", "src/mips/openbios/openbios.bin", "-testmode", "-interpreter", "-luacov",
"-exec", prefix, args..., "-exec", suffix);
return invoker.invoke();
}

template <typename... Args>
int runLuaDyn(Args... args) {
MainInvoker invoker("-no-ui", "-cli", "-bios", "src/mips/openbios/openbios.bin", "-testmode", "-dynarec", "-luacov",
"-exec", prefix, args..., "-exec", suffix);
return invoker.invoke();
}

static int runLuaIntTest(const char* name) {
std::string req = "require '";
req += name;
req += "'";
return runLuaInt("-exec", req.c_str());
}

static int runLuaDynTest(const char* name) {
std::string req = "require '";
req += name;
req += "'";
return runLuaDyn("-exec", req.c_str());
}

TEST(LuaBasic, Interpreter) { EXPECT_EQ(runLuaIntTest("tests.lua.basic"), 0); }
TEST(LuaBasic, Dynarec) { EXPECT_EQ(runLuaDynTest("tests.lua.basic"), 0); }
TEST(LuaFile, Interpreter) { EXPECT_EQ(runLuaIntTest("tests.lua.file"), 0); }
TEST(LuaFile, Dynarec) { EXPECT_EQ(runLuaDynTest("tests.lua.file"), 0); }
4 changes: 2 additions & 2 deletions third_party/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

#include <algorithm>
#include <array>
#include <map>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

namespace CommandLine {
namespace detail {
using argument_map =
std::unordered_multimap<std::string_view, std::optional<std::string_view>>;
std::multimap<std::string_view, std::optional<std::string_view>>;

// Non-destructively parses the argv tokens.
// * If the token begins with a -, it will be considered an option.
Expand Down
1 change: 1 addition & 0 deletions third_party/luaunit
Submodule luaunit added at 86f682
9 changes: 8 additions & 1 deletion vsprojects/pcsx-redux.sln
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "supportpsx", "supportpsx\su
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lpeg", "lpeg\lpeg.vcxproj", "{CE54ED92-4645-4AE9-BDC8-C0B9607765F8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Lua", "Lua", "{394627A0-57EB-46B1-B768-E02ACFC798A8}"
ProjectSection(SolutionItems) = preProject
..\tests\lua\basic.lua = ..\tests\lua\basic.lua
..\tests\lua\file.lua = ..\tests\lua\file.lua
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -530,9 +536,10 @@ Global
{CDED480F-14EE-475E-97F5-97F2B62DB3CE} = {C6DD47BC-0C38-4AE6-B517-9675F3AC8A50}
{B2E2AD84-9D7F-4976-9572-E415819FFD7F} = {008A2872-432F-480B-828D-FF9AAA4846BC}
{CE54ED92-4645-4AE9-BDC8-C0B9607765F8} = {64A05F50-3203-42CC-B632-09D6EE6EA856}
{394627A0-57EB-46B1-B768-E02ACFC798A8} = {9D5A1DB2-E74D-4CDD-8377-9EA08CF4AADE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AC54A867-F976-4B3D-A6EF-F57EB764DCD4}
SolutionGuid = {284E91C1-764E-441A-854D-CFD3623A6501}
SolutionGuid = {AC54A867-F976-4B3D-A6EF-F57EB764DCD4}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions vsprojects/tests/pcsxrunner/pcsxrunner.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@
<ClCompile Include="..\..\..\tests\pcsxrunner\dma.cc" />
<ClCompile Include="..\..\..\tests\pcsxrunner\dumpproto.cc" />
<ClCompile Include="..\..\..\tests\pcsxrunner\libc.cc" />
<ClCompile Include="..\..\..\tests\pcsxrunner\lua.cc" />
<ClCompile Include="..\..\..\tests\pcsxrunner\pcdrv.cc" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions vsprojects/tests/pcsxrunner/pcsxrunner.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<ClCompile Include="..\..\..\tests\pcsxrunner\dma.cc">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\tests\pcsxrunner\lua.cc">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

0 comments on commit ab7f2fd

Please sign in to comment.