mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-06 11:31:14 +00:00
Add a substring function to the formula language.
Also added a unit test for the function.
This commit is contained in:
parent
c2b37db47b
commit
243f9fd87d
@ -3,6 +3,8 @@ Version 1.9.3+svn:
|
|||||||
* Delfadors Memoirs:
|
* Delfadors Memoirs:
|
||||||
* Fixed bug #17273: Made difficulty selection conform to the style of all
|
* Fixed bug #17273: Made difficulty selection conform to the style of all
|
||||||
other mainline campaigns
|
other mainline campaigns
|
||||||
|
* Formula language:
|
||||||
|
* Added substring function.
|
||||||
* Graphics:
|
* Graphics:
|
||||||
* Terrain: added transitions for the wood floor.
|
* Terrain: added transitions for the wood floor.
|
||||||
* Language and i18n:
|
* Language and i18n:
|
||||||
|
@ -794,6 +794,7 @@ if(ENABLE_TESTS)
|
|||||||
tests/utils/play_scenario.cpp
|
tests/utils/play_scenario.cpp
|
||||||
tests/test_config_cache.cpp
|
tests/test_config_cache.cpp
|
||||||
tests/test_formula_ai.cpp
|
tests/test_formula_ai.cpp
|
||||||
|
tests/test_formula_function.cpp
|
||||||
tests/test_lexical_cast.cpp
|
tests/test_lexical_cast.cpp
|
||||||
tests/test_network_worker.cpp
|
tests/test_network_worker.cpp
|
||||||
tests/test_team.cpp
|
tests/test_team.cpp
|
||||||
|
@ -450,6 +450,7 @@ test_SOURCES = \
|
|||||||
tests/utils/play_scenario.cpp \
|
tests/utils/play_scenario.cpp \
|
||||||
tests/test_config_cache.cpp \
|
tests/test_config_cache.cpp \
|
||||||
tests/test_formula_ai.cpp \
|
tests/test_formula_ai.cpp \
|
||||||
|
tests/formula_function.cpp \
|
||||||
tests/test_lexical_cast.cpp \
|
tests/test_lexical_cast.cpp \
|
||||||
tests/test_network_worker.cpp \
|
tests/test_network_worker.cpp \
|
||||||
tests/test_team.cpp \
|
tests/test_team.cpp \
|
||||||
|
@ -516,6 +516,7 @@ libtest_utils = test_env.Library("test_utils", test_utils_sources)
|
|||||||
test_sources = Split("""
|
test_sources = Split("""
|
||||||
tests/main.cpp
|
tests/main.cpp
|
||||||
tests/test_formula_ai.cpp
|
tests/test_formula_ai.cpp
|
||||||
|
tests/test_formula_function.cpp
|
||||||
tests/test_lexical_cast.cpp
|
tests/test_lexical_cast.cpp
|
||||||
tests/test_network_worker.cpp
|
tests/test_network_worker.cpp
|
||||||
tests/test_team.cpp
|
tests/test_team.cpp
|
||||||
|
@ -29,6 +29,8 @@ static lg::log_domain log_engine("engine");
|
|||||||
#define DBG_NG LOG_STREAM(debug, log_engine)
|
#define DBG_NG LOG_STREAM(debug, log_engine)
|
||||||
static lg::log_domain log_scripting_formula("scripting/formula");
|
static lg::log_domain log_scripting_formula("scripting/formula");
|
||||||
#define LOG_SF LOG_STREAM(info, log_scripting_formula)
|
#define LOG_SF LOG_STREAM(info, log_scripting_formula)
|
||||||
|
#define WRN_SF LOG_STREAM(warn, log_scripting_formula)
|
||||||
|
#define ERR_SF LOG_STREAM(err, log_scripting_formula)
|
||||||
|
|
||||||
namespace game_logic {
|
namespace game_logic {
|
||||||
|
|
||||||
@ -369,6 +371,55 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class substring_function
|
||||||
|
: public function_expression {
|
||||||
|
public:
|
||||||
|
explicit substring_function(const args_list& args)
|
||||||
|
: function_expression("substring", args, 2, 3)
|
||||||
|
{}
|
||||||
|
|
||||||
|
variant execute(const formula_callable& variables
|
||||||
|
, formula_debugger *fdb) const {
|
||||||
|
|
||||||
|
std::string result = args()[0]->evaluate(variables, fdb).as_string();
|
||||||
|
|
||||||
|
int offset = args()[1]->evaluate(variables, fdb).as_int();
|
||||||
|
if(offset < 0) {
|
||||||
|
offset += result.size();
|
||||||
|
if(offset < 0) {
|
||||||
|
WRN_SF << "[concatenate] Offset '"
|
||||||
|
<< args()[1]->evaluate(variables, fdb).as_int()
|
||||||
|
<< "' results in a negative start in string '"
|
||||||
|
<< result
|
||||||
|
<< "' and is reset at the beginning of the string.\n";
|
||||||
|
|
||||||
|
offset = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(static_cast<size_t>(offset) >= result.size()) {
|
||||||
|
WRN_SF << "[concatenate] Offset '" << offset
|
||||||
|
<< "' is larger as the size of '" << result
|
||||||
|
<< "' and results in an empty string.\n";
|
||||||
|
|
||||||
|
return variant(std::string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(args().size() > 2) {
|
||||||
|
const int size = args()[2]->evaluate(variables, fdb).as_int();
|
||||||
|
if(size < 0) {
|
||||||
|
ERR_SF << "[concatenate] Size is negative an "
|
||||||
|
<< "empty string is returned.\n";
|
||||||
|
|
||||||
|
return variant(std::string());
|
||||||
|
}
|
||||||
|
return variant(result.substr(offset, size));
|
||||||
|
} else {
|
||||||
|
return variant(result.substr(offset));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class index_of_function : public function_expression {
|
class index_of_function : public function_expression {
|
||||||
public:
|
public:
|
||||||
explicit index_of_function(const args_list& args)
|
explicit index_of_function(const args_list& args)
|
||||||
@ -1050,6 +1101,7 @@ functions_map& get_functions_map() {
|
|||||||
FUNCTION(values);
|
FUNCTION(values);
|
||||||
FUNCTION(tolist);
|
FUNCTION(tolist);
|
||||||
FUNCTION(tomap);
|
FUNCTION(tomap);
|
||||||
|
FUNCTION(substring);
|
||||||
#undef FUNCTION
|
#undef FUNCTION
|
||||||
}
|
}
|
||||||
|
|
||||||
|
91
src/tests/test_formula_function.cpp
Normal file
91
src/tests/test_formula_function.cpp
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/* $Id$ */
|
||||||
|
/*
|
||||||
|
Copyright (C) 2010 by Mark de Wever <koraq@xs4all.nl>
|
||||||
|
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
See the COPYING file for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define GETTEXT_DOMAIN "wesnoth-test"
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
#include "formula_function.hpp"
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(formula_function)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_formula_function_substring)
|
||||||
|
{
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', 0)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "hello world");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', 6)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "world");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', 11)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "");
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', -1)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "d");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', -5)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "world");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', -11)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "hello world");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', -12)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "hello world");
|
||||||
|
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', 0, 0)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', 0, -1)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', 5, 1)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, " ");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', 1, 9)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "ello worl");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(
|
||||||
|
game_logic::formula("substring('hello world', -10, 9)")
|
||||||
|
.evaluate().as_string()
|
||||||
|
, "ello worl");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user