• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KJS-API

  • kjs
  • api
kjsapitest.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2008 Harri Porten (porten@kde.org)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "kjsobject.h"
23#include "kjsprototype.h"
24#include "kjsarguments.h"
25#include "kjsinterpreter.h"
26
27#include "qtest_kde.h"
28
29class KJSApiTest : public QObject
30{
31 Q_OBJECT
32
33private Q_SLOTS:
34 void objectConstruction();
35 void interpreterEvaluate();
36 void interpreterNormalizeCode();
37 void objectProperties();
38 void prototypeConstants();
39 void prototypeProperties();
40 void prototypeFunctions();
41 void globalObject();
42};
43
44void KJSApiTest::objectConstruction()
45{
46 KJSInterpreter ip;
47 KJSContext* ctx = ip.globalContext();
48
49 // Object
50 QVERIFY2(KJSObject().isObject(), "Broken default object");
51
52 // undefined
53 QVERIFY2(KJSUndefined().isUndefined(),
54 "Undefined object is not undefined");
55 // null
56 QVERIFY2(KJSNull().isNull(),
57 "Null object is not null");
58
59 // Boolean
60 KJSBoolean boolObject(true);
61 QVERIFY2(boolObject.isBoolean(), "Boolean object is not of boolean type");
62 QVERIFY2(boolObject.toBoolean(ctx), "Boolean object has wrong value");
63 QVERIFY2(!ctx->hasException(), "Boolean conversion threw exception");
64
65 // Number
66 KJSNumber numObject(42.0);
67 QVERIFY2(numObject.isNumber(), "Number object is not of number type");
68 QCOMPARE(numObject.toNumber(ctx), 42.0);
69 QCOMPARE(numObject.toInt32(ctx), 42);
70 QVERIFY2(!ctx->hasException(), "Number conversion threw exception");
71
72 // String
73 KJSString stringObject("Trunk");
74 QVERIFY2(stringObject.isString(), "String object is not of string type");
75 QCOMPARE(stringObject.toString(ctx), QLatin1String("Trunk"));
76 QVERIFY2(!ctx->hasException(), "String conversion threw exception");
77
78 // Array
79 KJSArray arrayObject(ctx, 3);
80 QVERIFY2(arrayObject.isObject(), "Array object is not of object type");
81 QCOMPARE(arrayObject.property(ctx, "length").toNumber(ctx), 3.0);
82 QCOMPARE(arrayObject.toString(ctx), QLatin1String(",,"));
83 QVERIFY2(!ctx->hasException(), "Array conversion threw exception");
84
85 // copying
86 KJSObject copy(stringObject);
87 QCOMPARE(copy.toString(ctx), QLatin1String("Trunk"));
88 copy = numObject;
89 QCOMPARE(copy.toNumber(ctx), 42.0);
90}
91
92void KJSApiTest::interpreterEvaluate()
93{
94 KJSInterpreter ip;
95 KJSContext* ctx = ip.globalContext();
96 KJSResult res;
97
98 // syntax error
99 res = ip.evaluate(")(");
100 QVERIFY2(res.isException(), "Syntax error not caught");
101
102 res = ip.evaluate("11+22");
103 QVERIFY2(!res.isException(), "Evaluation returned non-number object");
104 QCOMPARE(res.value().toNumber(ctx), 33.0);
105}
106
107void KJSApiTest::interpreterNormalizeCode()
108{
109 int errLine = -1;
110 QString errMsg;
111 QString norm;
112 bool ok;
113
114 // syntax error case
115 ok = KJSInterpreter::normalizeCode(")(", &norm, &errLine, &errMsg);
116 QVERIFY(!ok);
117 QVERIFY(!errMsg.isEmpty());
118 QVERIFY(errLine >= 0 && errLine <= 2); // ### imprecise
119
120 // success case
121 ok = KJSInterpreter::normalizeCode("foo(); bar();", &norm);
122 QVERIFY(ok);
123 QVERIFY(!norm.isEmpty());
124 QStringList lines = norm.split('\n');
125 QVERIFY(lines.size() >= 2); // ### imprecise
126 int fooLine = lines.indexOf(QRegExp(" *foo\\(\\);"));
127 int barLine = lines.indexOf(QRegExp(" *bar\\(\\);"));
128 QVERIFY(fooLine >= 0);
129 QVERIFY(barLine > fooLine);
130}
131
132void KJSApiTest::objectProperties()
133{
134 KJSInterpreter ip;
135 KJSContext* ctx = ip.globalContext();
136
137 KJSObject global = ip.globalObject();
138 KJSObject v;
139
140 // bool
141 global.setProperty(ctx, "myprop", true);
142 v = global.property(ctx, "myprop");
143 QVERIFY(v.isBoolean());
144 QCOMPARE(v.toBoolean(ctx), true);
145
146 // double
147 global.setProperty(ctx, "myprop", 21.0);
148 v = global.property(ctx, "myprop");
149 QVERIFY(v.isNumber());
150 QCOMPARE(v.toNumber(ctx), 21.0);
151
152 // int
153 global.setProperty(ctx, "myprop", 22);
154 v = global.property(ctx, "myprop");
155 QVERIFY(v.isNumber());
156 QCOMPARE(v.toNumber(ctx), 22.0);
157
158 // string (8-bit)
159 global.setProperty(ctx, "myprop", "myvalue8");
160 v = global.property(ctx, "myprop");
161 QVERIFY(v.isString());
162 QCOMPARE(v.toString(ctx), QLatin1String("myvalue8"));
163
164 // string (Unicode)
165 global.setProperty(ctx, "myprop", QLatin1String("myvalue16"));
166 v = global.property(ctx, "myprop");
167 QVERIFY(v.isString());
168 QCOMPARE(v.toString(ctx), QLatin1String("myvalue16"));
169}
170
171void KJSApiTest::prototypeConstants()
172{
173 KJSInterpreter ip;
174 KJSContext* ctx = ip.globalContext();
175
176 KJSPrototype proto;
177
178 proto.defineConstant("d0", 44.4);
179 proto.defineConstant("s0", QLatin1String("XYZ"));
180
181 KJSObject obj = proto.constructObject(ctx, 0);
182
183 QCOMPARE(obj.property(ctx, "d0").toNumber(ctx), 44.4);
184 QCOMPARE(obj.property(ctx, "s0").toString(ctx), QLatin1String("XYZ"));
185}
186
187static struct O { int x; } o0 = { 42 };
188
189static KJSObject getX(KJSContext* /*context*/, void* object)
190{
191 O* o = reinterpret_cast<O*>(object);
192 int x = o->x;
193 return KJSNumber(x);
194}
195
196static void setX(KJSContext* context, void* object, KJSObject value)
197{
198 O* o = reinterpret_cast<O*>(object);
199 double n = value.toNumber(context);
200 o->x = n;
201}
202
203void KJSApiTest::prototypeProperties()
204{
205 KJSInterpreter ip;
206 KJSContext* ctx = ip.globalContext();
207
208 KJSPrototype proto;
209
210 proto.defineProperty(ctx, "x", getX, setX);
211 proto.defineProperty(ctx, "readOnlyX", getX);
212
213 KJSObject obj = proto.constructObject(ctx, &o0);
214
215 // read/write property
216 QCOMPARE(obj.property(ctx, "x").toNumber(ctx), 42.0);
217 obj.setProperty(ctx, "x", KJSNumber(43));
218 QCOMPARE(obj.property(ctx, "x").toNumber(ctx), 43.0);
219
220 QCOMPARE(obj.property(ctx, "readOnlyX").toNumber(ctx), 43.0);
221 obj.setProperty(ctx, "readOnlyX", KJSNumber(44));
222 QVERIFY2(ctx->hasException(), "Write access caused no exception");
223 QCOMPARE(obj.property(ctx, "readOnlyX").toNumber(ctx), 43.0);
224}
225
226static KJSObject multiply(KJSContext* context, void* object,
227 const KJSArguments& arguments)
228{
229 double factor = *reinterpret_cast<double*>(object);
230
231 // test number of arguments
232 if (arguments.count() != 1)
233 return context->throwException("Missing argument");
234
235 KJSObject a0 = arguments.at(0);
236 if (!a0.isNumber())
237 return KJSNumber(-2);
238
239 double v0 = a0.toNumber(context);
240
241 return KJSNumber(factor * v0);
242}
243
244void KJSApiTest::prototypeFunctions()
245{
246 KJSInterpreter ip;
247 KJSContext* ctx = ip.globalContext();
248
249 KJSPrototype proto;
250
251 proto.defineFunction(ctx, "multiply", multiply);
252
253 double factor = 3.0;
254 KJSObject obj = proto.constructObject(ctx, &factor);
255 ip.globalObject().setProperty(ctx, "obj", obj);
256
257 KJSResult res = ip.evaluate("obj.multiply(4)");
258 QCOMPARE(res.value().toNumber(ctx), 12.0);
259
260 // expect exception
261 res = ip.evaluate("obj.multiply()");
262 QVERIFY2(res.isException(), "Exception did not work");
263}
264
265void KJSApiTest::globalObject()
266{
267 KJSPrototype proto;
268 proto.defineConstant("g0", 55.5);
269
270 KJSGlobalObject glob = proto.constructGlobalObject(0);
271
272 KJSInterpreter ip(glob);
273 KJSResult res = ip.evaluate("2 * g0");
274 QCOMPARE(res.value().toNumber(ip.globalContext()), 111.0);
275}
276
277QTEST_KDEMAIN_CORE(KJSApiTest)
278
279#include "kjsapitest.moc"
KJSArguments
A class representing a list of JavaScript arguments.
Definition: kjsarguments.h:37
KJSArguments::count
int count() const
Returns the number of arguments.
Definition: kjsarguments.cpp:25
KJSArguments::at
KJSObject at(int idx) const
Returns the argument at the specified index.
Definition: kjsarguments.cpp:31
KJSArray
A class representing a JavaScript array object.
Definition: kjsobject.h:253
KJSBoolean
A class representing a boolean JavaScript value.
Definition: kjsobject.h:203
KJSContext
A class representing a JavaScript execution context.
Definition: kjscontext.h:40
KJSContext::throwException
KJSObject throwException(const QString &message) const
Throws a general exception with the specified error message.
Definition: kjscontext.cpp:41
KJSContext::hasException
bool hasException() const
Returns true if this context has an unhandled exception.
Definition: kjscontext.cpp:36
KJSGlobalObject
A class representing a global object of an execution environment.
Definition: kjsobject.h:281
KJSInterpreter
A class representing a JavaScript interpreter.
Definition: kjsinterpreter.h:83
KJSInterpreter::evaluate
KJSResult evaluate(const QString &sourceURL, int startingLineNumber, const QString &code, KJSObject *thisValue=0)
Evaluates a piece of code with a "this" set to (optionally set) value.
Definition: kjsinterpreter.cpp:163
KJSInterpreter::globalContext
KJSContext * globalContext()
Returns a handle to the global execution context.
Definition: kjsinterpreter.cpp:148
KJSInterpreter::globalObject
KJSObject globalObject()
Returns the object that is used as the global object during all script execution performed by this in...
Definition: kjsinterpreter.cpp:156
KJSInterpreter::normalizeCode
static bool normalizeCode(const QString &codeIn, QString *codeOut, int *errLine=0, QString *errMsg=0)
Reformat the given script code to an easy to read format with only one statement per line.
Definition: kjsinterpreter.cpp:203
KJSNull
A class representing a JavaScript null value.
Definition: kjsobject.h:175
KJSNumber
A class representing a JavaScript number value.
Definition: kjsobject.h:217
KJSObject
A class representing a JavaScript value.
Definition: kjsobject.h:49
KJSObject::setProperty
void setProperty(KJSContext *ctx, const QString &name, const KJSObject &value)
Sets the specified property on this object.
Definition: kjsobject.cpp:206
KJSObject::isBoolean
bool isBoolean() const
Returns whether this object is a boolean.
Definition: kjsobject.cpp:140
KJSObject::toBoolean
bool toBoolean(KJSContext *ctx)
Returns this value converted to a boolean.
Definition: kjsobject.cpp:160
KJSObject::property
KJSObject property(KJSContext *ctx, const QString &name)
Reads the specified property from this object.
Definition: kjsobject.cpp:188
KJSObject::toString
QString toString(KJSContext *ctx)
Returns this value converted to a string.
Definition: kjsobject.cpp:181
KJSObject::toNumber
double toNumber(KJSContext *ctx)
Returns this value converted to a number.
Definition: kjsobject.cpp:167
KJSObject::isNumber
bool isNumber() const
Returns whether this object is a number.
Definition: kjsobject.cpp:145
KJSObject::isString
bool isString() const
Returns whether this object is a string.
Definition: kjsobject.cpp:150
KJSPrototype
A class representing a JavaScript prototype object.
Definition: kjsprototype.h:39
KJSPrototype::constructObject
KJSObject constructObject(KJSContext *ctx, void *internalValue=0)
Construct an object with this prototype and the specified internal value.
Definition: kjsprototype.cpp:263
KJSPrototype::defineProperty
void defineProperty(KJSContext *ctx, const QString &name, PropertyGetter getter, PropertySetter setter=0)
Defines a property of this prototype with C++ callback functions for getting and setting the property...
Definition: kjsprototype.cpp:286
KJSPrototype::defineConstant
void defineConstant(const QString &name, double value)
Add a read-only numerical property to this object.
Definition: kjsprototype.cpp:239
KJSPrototype::constructGlobalObject
KJSGlobalObject constructGlobalObject(void *internalValue=0)
Similar to constructObject() but specialized on the construction of global objects.
Definition: kjsprototype.cpp:278
KJSPrototype::defineFunction
void defineFunction(KJSContext *ctx, const QString &name, FunctionCall callback)
Define a function.
Definition: kjsprototype.cpp:299
KJSResult
A class representing the result of a script evaluation.
Definition: kjsinterpreter.h:38
KJSResult::isException
bool isException() const
Returns true if the script evaluation has caused an exception.
Definition: kjsinterpreter.cpp:73
KJSResult::value
KJSObject value() const
Definition: kjsinterpreter.cpp:83
KJSString
A class representing a JavaScript string value.
Definition: kjsobject.h:231
KJSUndefined
A class representing an undefined JavaScript value.
Definition: kjsobject.h:189
o0
static struct O o0
multiply
static KJSObject multiply(KJSContext *context, void *object, const KJSArguments &arguments)
Definition: kjsapitest.cpp:226
setX
static void setX(KJSContext *context, void *object, KJSObject value)
Definition: kjsapitest.cpp:196
getX
static KJSObject getX(KJSContext *, void *object)
Definition: kjsapitest.cpp:189
kjsarguments.h
kjsinterpreter.h
kjsobject.h
kjsprototype.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KJS-API

Skip menu "KJS-API"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal