summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/ctest.cc
diff options
context:
space:
mode:
authorDarshan Shaligram <dshaligram@users.sourceforge.net>2010-01-09 01:21:44 +0530
committerDarshan Shaligram <dshaligram@users.sourceforge.net>2010-01-09 01:47:09 +0530
commit9841ce45ea91c1ad394d706fd748ffc1fd9d9de0 (patch)
tree047cbb69276a6b8c55d9b22c1e01442a8bd7d173 /crawl-ref/source/ctest.cc
parent915b92e5527c1300fa701a31e3dcbc070b48d177 (diff)
downloadcrawl-ref-9841ce45ea91c1ad394d706fd748ffc1fd9d9de0.tar.gz
crawl-ref-9841ce45ea91c1ad394d706fd748ffc1fd9d9de0.zip
Add -script option to Crawl to run a Lua script. Scripts are similar to tests, but can be parameterised.
Add a script to generate 150 level at a named place and report on all the monsters generated there.
Diffstat (limited to 'crawl-ref/source/ctest.cc')
-rw-r--r--crawl-ref/source/ctest.cc57
1 files changed, 44 insertions, 13 deletions
diff --git a/crawl-ref/source/ctest.cc b/crawl-ref/source/ctest.cc
index cfff5edecb..dcfd78fe98 100644
--- a/crawl-ref/source/ctest.cc
+++ b/crawl-ref/source/ctest.cc
@@ -17,10 +17,13 @@
#if DEBUG_DIAGNOSTICS || DEBUG_TESTS
#include "clua.h"
+#include "cluautil.h"
#include "dlua.h"
#include "files.h"
+#include "libutil.h"
#include "maps.h"
#include "message.h"
+#include "ng-init.h"
#include "state.h"
#include "stuff.h"
@@ -30,9 +33,11 @@
namespace crawl_tests
{
const std::string test_dir = "test";
+ const std::string script_dir = "scripts";
const std::string test_player_name = "Superbug99";
const species_type test_player_species = SP_HUMAN;
const job_type test_player_job = JOB_FIGHTER;
+ const char *activity = "test";
int ntests = 0;
int nsuccess = 0;
@@ -52,22 +57,31 @@ namespace crawl_tests
int crawl_begin_test(lua_State *ls)
{
- mprf(MSGCH_PROMPT, "Starting tests: %s", luaL_checkstring(ls, 1));
+ mprf(MSGCH_PROMPT, "Starting %s: %s",
+ activity,
+ luaL_checkstring(ls, 1));
lua_pushnumber(ls, ++ntests);
return (1);
}
int crawl_test_success(lua_State *ls)
{
- mprf(MSGCH_PROMPT, "Test success: %s", luaL_checkstring(ls, 1));
+ if (!crawl_state.script)
+ mprf(MSGCH_PROMPT, "Test success: %s", luaL_checkstring(ls, 1));
lua_pushnumber(ls, ++nsuccess);
return (1);
}
+ int crawl_script_args(lua_State *ls)
+ {
+ return clua_stringtable(ls, crawl_state.script_args);
+ }
+
static const struct luaL_reg crawl_test_lib[] =
{
{ "begin_test", crawl_begin_test },
{ "test_success", crawl_test_success },
+ { "script_args", crawl_script_args },
{ NULL, NULL }
};
@@ -76,6 +90,7 @@ namespace crawl_tests
lua_stack_cleaner clean(dlua);
luaL_openlib(dlua, "crawl", crawl_test_lib, 0);
dlua.execfile("clua/test.lua", true, true);
+ initialise_branch_depths();
}
bool is_test_selected(const std::string &testname)
@@ -98,11 +113,12 @@ namespace crawl_tests
return;
++ntests;
- mprf(MSGCH_DIAGNOSTICS, "Running test %d: %s",
- ntests, file.c_str());
+ mprf(MSGCH_DIAGNOSTICS, "Running %s %d: %s",
+ activity, ntests, file.c_str());
flush_prev_message();
- const std::string path(catpath(test_dir, file));
+ const std::string path(
+ catpath(crawl_state.script? script_dir : test_dir, file));
dlua.execfile(path.c_str(), true, false);
if (dlua.error.empty())
++nsuccess;
@@ -113,6 +129,9 @@ namespace crawl_tests
// Assumes curses has already been initialized.
bool run_tests(bool exit_on_complete)
{
+ if (crawl_state.script)
+ activity = "script";
+
flush_prev_message();
run_map_preludes();
@@ -120,9 +139,10 @@ namespace crawl_tests
init_test_bindings();
- if (crawl_state.tests_selected.empty()
- || (crawl_state.tests_selected[0].find("makeitem") !=
- std::string::npos))
+ if ((crawl_state.tests_selected.empty()
+ || (crawl_state.tests_selected[0].find("makeitem") !=
+ std::string::npos))
+ && !crawl_state.script)
{
makeitem_tests();
}
@@ -130,22 +150,33 @@ namespace crawl_tests
// Get a list of Lua files in test. Order of execution of
// tests should be irrelevant.
const std::vector<std::string> tests(
- get_dir_files_ext(test_dir, ".lua"));
+ get_dir_files_ext(crawl_state.script? script_dir : test_dir,
+ ".lua"));
std::for_each(tests.begin(), tests.end(),
run_test);
+ if (failures.empty() && !ntests && crawl_state.script)
+ failures.push_back(
+ file_error(
+ "Script setup",
+ "No scripts found matching " +
+ comma_separated_line(crawl_state.tests_selected.begin(),
+ crawl_state.tests_selected.end(),
+ ", ",
+ ", ")));
+
if (exit_on_complete)
{
cio_cleanup();
for (int i = 0, size = failures.size(); i < size; ++i)
{
const file_error &fe(failures[i]);
- fprintf(stderr, "Test error: %s\n",
- fe.second.c_str());
+ fprintf(stderr, "%s error: %s\n",
+ activity, fe.second.c_str());
}
const int code = failures.empty() ? 0 : 1;
- end(code, false, "%d tests, %d succeeded, %d failed",
- ntests, nsuccess, failures.size());
+ end(code, false, "%d %ss, %d succeeded, %d failed",
+ ntests, activity, nsuccess, failures.size());
}
return (failures.empty());
}