kwin/scripting/scriptingutils.cpp
Martin Gräßlin 1f97345e35 Asserts for KWin scripts
Further debugging functionality for KWin scripts. Added assert
methods validate the to be tested parameter and throw a script
error if the value is not valid.

Following methods are available:
* assert(value)
* assertTrue(boolean)
* assertFalse(boolean)
* assertEquals(expected, actual)
* assertNull(nullValue)
* assertNotNull(notNullValue)

All methods take an additional optional parameter which is used
as the error message if provided.

Methods to validate the number of arguments and types of the
parameters are added and throw syntax or type errors.

REVIEW: 104870
2012-05-10 10:00:54 +02:00

46 lines
1.7 KiB
C++

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2012 Martin Gräßlin <mgraesslin@kde.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; 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, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "scriptingutils.h"
namespace KWin
{
bool validateParameters(QScriptContext *context, int min, int max)
{
if (context->argumentCount() < min || context->argumentCount() > max) {
context->throwError(QScriptContext::SyntaxError,
i18nc("syntax error in KWin script", "Invalid number of arguments"));
return false;
}
return true;
}
template<>
bool validateArgumentType<QVariant>(QScriptContext *context, int argument)
{
const bool result =context->argument(argument).toVariant().isValid();
if (!result) {
context->throwError(QScriptContext::TypeError,
i18nc("KWin Scripting function received incorrect value for an expected type",
"%1 is not a variant type", context->argument(argument).toString()));
}
return result;
}
}