now also supports optional else after command if

This commit is contained in:
Marc Wäckerlin
2015-05-11 09:54:11 +00:00
parent 9335d5f6bb
commit 13b8752c7a
3 changed files with 24 additions and 14 deletions

View File

@@ -111,7 +111,7 @@ class Command: public QObject {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
protected:
void subScript(std::shared_ptr<Script> script,
void runScript(std::shared_ptr<Script> script,
Script* parent, QWebFrame* frame,
QStringList vars = QStringList(),
QStringList args = QStringList());
@@ -1843,7 +1843,7 @@ class Function: public Command {
bool call(QStringList args, Script* script, QWebFrame* frame) {
Logger log(this, script);
try {
subScript(_script, script, frame, _vars, args);
runScript(_script, script, frame, _vars, args);
} catch (const std::exception& x) {
throw FunctionCallFailed(_name, _vars, args, x);
}
@@ -1902,12 +1902,17 @@ class If: public Command {
" <command1>\n"
" <command2>\n"
" <...>\n"
"else\n"
" <command3>\n"
" <command4>\n"
" <...>\n"
"\n\n"
"Execute commands conditionally. "
"The comparision <cmp> can be = ^ ~ < >, "
"which means equal, different, match, "
"less (as integer), bigger (as integer). "
"Match allows a regular expression.";
"Match allows a regular expression. "
"There is an optional else part.";
}
QString command() const {
return tag()+" "+_variable+" "+QString(_cmp)+" "+_value
@@ -1923,6 +1928,11 @@ class If: public Command {
cmd->_value = args.mid(pos+1).trimmed();
cmd->_script = std::shared_ptr<Script>(new Script);
cmd->_script->parse(subCommandBlock(in));
if (in.size() && in.first().contains(QRegularExpression("^else *$"))) {
in.removeFirst();
cmd->_else = std::shared_ptr<Script>(new Script);
cmd->_else->parse(subCommandBlock(in));
}
return cmd;
}
bool execute(Script* script, QWebFrame* frame) {
@@ -1943,7 +1953,8 @@ class If: public Command {
break;
default:;
}
if (check) subScript(_script, script, frame);
if (check) runScript(_script, script, frame);
else if (_else) runScript(_else, script, frame);
return true;
}
private:
@@ -1951,6 +1962,7 @@ class If: public Command {
char _cmp;
QString _value;
std::shared_ptr<Script> _script;
std::shared_ptr<Script> _else;
};
class TestSuite: public Command {
@@ -2067,7 +2079,7 @@ inline Logger::~Logger() {
_script->log("---------------------");
}
inline void Command::subScript(std::shared_ptr<Script> script,
inline void Command::runScript(std::shared_ptr<Script> script,
Script* parent, QWebFrame* frame,
QStringList vars,
QStringList args) {