Merge branch 'threaded3'
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeexpression / tst_qdeclarativeexpression.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <QtDeclarative/qdeclarativeexpression.h>
46 #include <QtDeclarative/qdeclarativescriptstring.h>
47
48 #ifdef Q_OS_SYMBIAN
49 // In Symbian OS test data is located in applications private dir
50 #define SRCDIR "."
51 #endif
52
53 class tst_qdeclarativeexpression : public QObject
54 {
55     Q_OBJECT
56 public:
57     tst_qdeclarativeexpression() {}
58
59 private slots:
60     void scriptString();
61 };
62
63 class TestObject : public QObject
64 {
65     Q_OBJECT
66     Q_PROPERTY(QDeclarativeScriptString scriptString READ scriptString WRITE setScriptString)
67     Q_PROPERTY(QDeclarativeScriptString scriptStringError READ scriptStringError WRITE setScriptStringError)
68 public:
69     TestObject(QObject *parent = 0) : QObject(parent) {}
70
71     QDeclarativeScriptString scriptString() const { return m_scriptString; }
72     void setScriptString(QDeclarativeScriptString scriptString) { m_scriptString = scriptString; }
73
74     QDeclarativeScriptString scriptStringError() const { return m_scriptStringError; }
75     void setScriptStringError(QDeclarativeScriptString scriptString) { m_scriptStringError = scriptString; }
76
77 private:
78     QDeclarativeScriptString m_scriptString;
79     QDeclarativeScriptString m_scriptStringError;
80 };
81
82 QML_DECLARE_TYPE(TestObject)
83
84 void tst_qdeclarativeexpression::scriptString()
85 {
86     qmlRegisterType<TestObject>("Test", 1, 0, "TestObject");
87
88     QDeclarativeEngine engine;
89     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/scriptString.qml"));
90     TestObject *testObj = qobject_cast<TestObject*>(c.create());
91     QVERIFY(testObj != 0);
92
93     QDeclarativeScriptString script = testObj->scriptString();
94     QCOMPARE(script.script(), QLatin1String("value1 + value2"));
95
96     QDeclarativeExpression expression(script);
97     QVariant value = expression.evaluate();
98     QCOMPARE(value.toInt(), 15);
99
100     QDeclarativeScriptString scriptError = testObj->scriptStringError();
101     QCOMPARE(scriptError.script(), QLatin1String("value3 * 5"));
102
103     //verify that the expression has the correct error location information
104     QDeclarativeExpression expressionError(scriptError);
105     QVariant valueError = expressionError.evaluate();
106     QVERIFY(!valueError.isValid());
107     QVERIFY(expressionError.hasError());
108     QDeclarativeError error = expressionError.error();
109     QCOMPARE(error.url(), c.url());
110     QCOMPARE(error.line(), 8);
111 }
112
113 QTEST_MAIN(tst_qdeclarativeexpression)
114
115 #include "tst_qdeclarativeexpression.moc"