• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
  • Tests
TestScriptEngine.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License as
7  published by the Free Software Foundation; either version 2 of
8  the License, or (at your option) any later version.
9 
10  This program 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
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "TestScriptEngine.h"
20 #include "DataStructure.h"
21 #include "Data.h"
22 #include "Pointer.h"
23 #include "Document.h"
24 #include "DocumentManager.h"
25 #include "QtScriptBackend.h"
26 
27 #include <qtest_kde.h>
28 
29 TestScriptEngine::TestScriptEngine()
30 {
31  DocumentManager::self().addDocument(new Document("test"));;
32 }
33 
34 // simple test if script starts and ends
35 void TestScriptEngine::simpleStart()
36 {
37  DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
38  DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
39  DataList dataList;
40 
41  Document* graphDoc = DocumentManager::self().activeDocument();
42  QtScriptBackend *engine = graphDoc->engineBackend();
43 
44  engine->setScript(QString("for (var i=0; i<10; i++) ;"), graphDoc);
45  //QScriptValue results =
46  engine->execute();
47 // QVERIFY2(results.toNumber() == 1, "Error: number of graphs is not 1.");
48 }
49 
50 // test basic operations on pointers and data elements
51 void TestScriptEngine::basicOperationsGraph()
52 {
53  Document* graphDoc = DocumentManager::self().activeDocument();
54  QtScriptBackend *engine = graphDoc->engineBackend();
55  DataStructurePtr ds;
56  QString result;
57 
58  // create test data structure
59  ds = graphDoc->addDataStructure("test");
60  DataPtr a = ds->createData("a", 0);
61  DataPtr b = ds->createData("b", 0);
62  DataPtr c = ds->createData("c", 0);
63  PointerPtr connection = ds->createPointer(a, b, 0);
64  int type1 = graphDoc->registerDataType("type1");
65  graphDoc->setActiveDataStructure(ds);
66 
67  // test name
68  engine->setScript(QString("test.name;"), graphDoc);
69  result = engine->execute();
70  QVERIFY(result == "test");
71 
72  // test type
73  engine->setScript(QString("test.list_nodes()[0].type();"), graphDoc);
74  result = engine->execute();
75  QVERIFY(result == QString::number(0));
76 
77  // change type
78  engine->setScript(QString("var a = test.list_nodes()[0]; a.set_type(%1); a.type();").arg(type1), graphDoc);
79  result = engine->execute();
80  QVERIFY(result == QString::number(type1));
81 
82  // add and remove pointer
83  engine->setScript(QString("var x = test.add_edge(test.list_nodes()[1], test.list_nodes()[2]); test.list_edges().length;"), graphDoc);
84  result = engine->execute();
85  QVERIFY(result == QString::number(2));
86  engine->setScript(QString("var x = test.list_edges()[1]; x.remove(); test.list_edges().length;"), graphDoc);
87  result = engine->execute();
88  QVERIFY(result == QString::number(1));
89 
90  // remove data elements
91  engine->setScript(QString("test.list_nodes()[0].remove(); test.list_nodes().length;"), graphDoc);
92  result = engine->execute();
93  QVERIFY(result == QString::number(2));
94 }
95 
96 // tests if stop action stops execution
97 void TestScriptEngine::startStop()
98 {
99  //FIXME; for 4.10 add threading support for this test
100 // DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
101 // DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
102 // DataList dataList;
103 //
104 // Document* graphDoc = DocumentManager::self().activeDocument();
105 // QtScriptBackend *engine = graphDoc->engineBackend();
106 //
107 // // start infinite loop
108 // engine->setScript(QString("while (true) ;"), graphDoc);
109 // engine->execute();
110 //
111 // // stop execution
112 // engine->stop();
113 }
114 
115 
116 void TestScriptEngine::ignoreComments()
117 {
118  Document* graphDoc = DocumentManager::self().activeDocument();
119  QtScriptBackend *engine = graphDoc->engineBackend();
120 
121  QString test;
122  QScriptValue result;
123 
124  DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
125  DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
126  DataList dataList;
127 
128  // start engine
129  test = QString("// broken");
130  result = engine->engine()->evaluate(test);
131  QVERIFY2(!result.toString().contains("Error"), "Comment was not ignored, script with syntax error was executed.");
132  result = engine->engine()->evaluate(engine->includeManager().include(test));
133  QVERIFY2(!result.toString().contains("Error"), "Comment in file with processed includes was not ignored, script with syntax error was executed.");
134 
135  test = QString("//broken");
136  result = engine->engine()->evaluate(test);
137  QVERIFY2(!result.toString().contains("Error"), "Comment was not ignored, script with syntax error was executed.");
138  result = engine->engine()->evaluate(engine->includeManager().include(test));
139  QVERIFY2(!result.toString().contains("Error"), "Comment in file with processed includes was not ignored, script with syntax error was executed.");
140 
141  test = QString("/* broken */");
142  result = engine->engine()->evaluate(test);
143  QVERIFY2(!result.toString().contains("Error"), "Comment was not ignored, script with syntax error was executed.");
144  result = engine->engine()->evaluate(engine->includeManager().include(test));
145  QVERIFY2(!result.toString().contains("Error"), "Comment in file with processed includes was not ignored, script with syntax error was executed.");
146 
147  test = QString("/*\n broken\n */");
148  result = engine->engine()->evaluate(test);
149  QVERIFY2(!result.toString().contains("Error"), "Comment was not ignored, script with syntax error was executed.");
150  result = engine->engine()->evaluate(engine->includeManager().include(test));
151  QVERIFY2(!result.toString().contains("Error"), "Comment in file with processed includes was not ignored, script with syntax error was executed.");
152 
153  test = QString("/**\n * broken\n */");
154  result = engine->engine()->evaluate(test);
155  QVERIFY2(!result.toString().contains("Error"), "Comment was not ignored, script with syntax error was executed.");
156  result = engine->engine()->evaluate(engine->includeManager().include(test));
157  QVERIFY2(!result.toString().contains("Error"), "Comment in file with processed includes was not ignored, script with syntax error was executed.");
158 }
159 
160 void TestScriptEngine::useOfDynamicProperties()
161 {
162  DocumentManager::self().addDocument(new Document("test"));
163  DataStructurePtr ds = DataStructure::create(DocumentManager::self().activeDocument());
164  DocumentManager::self().activeDocument()->setActiveDataStructure(ds);
165  Document* graphDoc = DocumentManager::self().activeDocument();
166 
167  DataStructurePtr d = graphDoc->activeDataStructure();
168  d->setName("myGraph");
169  DataPtr n1 =d->createData("Node1", 0);
170  DataPtr n2 = d->createData("Node2", 0);
171  PointerPtr e1 = d->createPointer(n1, n2, 0);
172 
173  QString test;
174  QScriptValue result;
175 
176  // start engine
177  test = QString("nodes = myGraph.list_nodes()\n"
178  "edges = myGraph.list_edges()\n"
179  "for (var i = 0; i < nodes.length; ++i ){\n"
180  " nodes[i].add_property(\"MyProperty\", 0)\n"
181  "}\n"
182  "for (var i = 0; i < edges.length; ++i ){\n"
183  " edges[i].add_property(\"MyProperty\", 0)\n"
184  "}\n"
185  "nodes[0].MyProperty = 2\n"
186  "nodes[1].MyProperty = nodes[0].MyProperty + 2\n"
187  "edges[0].MyProperty = 2\n"
188  "myGraph.add_property(\"MyProperty\", 2)\n");
189 
190  QtScriptBackend *engine = graphDoc->engineBackend();
191 
192  engine->setScript(test, graphDoc);
193  engine->execute();
194 
195  QVERIFY2 (n1->property("MyProperty").isValid(), "Node 1 property is invalid!");
196  QVERIFY2 (n2->property("MyProperty").isValid(), "Node 2 property is invalid!");
197  QVERIFY2 (e1->property("MyProperty").isValid(), "Edge 1 property is invalid!");
198  QVERIFY2 (d->property("MyProperty").isValid(), "DS property is invalid!");
199 
200  QCOMPARE (n1->property("MyProperty").toInt(), 2);
201  QCOMPARE (n2->property("MyProperty").toInt(), 4);
202  QCOMPARE (e1->property("MyProperty").toInt(), 2);
203  QCOMPARE (d->property("MyProperty").toInt(), 2);
204 }
205 
206 
207 QTEST_KDEMAIN_CORE(TestScriptEngine)
Document::engineBackend
QtScriptBackend * engineBackend() const
Definition: Document.cpp:239
Document::registerDataType
int registerDataType(const QString &name, int identifier=0)
Register new type for data elements.
Definition: Document.cpp:124
TestScriptEngine.h
DocumentManager.h
DocumentManager::self
static DocumentManager & self()
Definition: DocumentManager.cpp:57
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
DocumentManager::activeDocument
Document * activeDocument() const
Returns the currently active document, or 0 if there document list is empty.
Definition: DocumentManager.cpp:96
DataStructure::create
static DataStructurePtr create(Document *parent=0)
Definition: DataStructure.cpp:68
DataList
QList< boost::shared_ptr< Data > > DataList
Definition: CoreTypes.h:30
IncludeManager::include
QString include(const QString &script, const QString &actualPath=QString(), const QString &filename=QString())
Definition: IncludeManager.cpp:51
QtScriptBackend.h
Data.h
Document.h
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
DataStructure.h
Document::addDataStructure
DataStructurePtr addDataStructure(const QString &name=QString())
Add data structure to graph document with name name.
Definition: Document.cpp:333
Document::activeDataStructure
DataStructurePtr activeDataStructure() const
Definition: Document.cpp:431
TestScriptEngine
Definition: TestScriptEngine.h:23
Document::setActiveDataStructure
void setActiveDataStructure(int index)
Sets the active data structure of graph document with index index in the data structure list...
Definition: Document.cpp:315
QtScriptBackend
This class provides the script backend for script executions.
Definition: QtScriptBackend.h:40
QtScriptBackend::includeManager
IncludeManager & includeManager() const
Definition: QtScriptBackend.cpp:137
QtScriptBackend::execute
QString execute()
Execute the currently set script.
Definition: QtScriptBackend.cpp:164
Document
Definition: Document.h:41
TestScriptEngine::TestScriptEngine
TestScriptEngine()
Definition: TestScriptEngine.cpp:29
Pointer.h
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
QtScriptBackend::setScript
void setScript(const QString &script, Document *document)
Set the script script and the corresponding document document to be executed on next run...
Definition: QtScriptBackend.cpp:257
DocumentManager::addDocument
void addDocument(Document *document)
Add document to document list and set this document as active document.
Definition: DocumentManager.cpp:106
QtScriptBackend::engine
QScriptEngine * engine() const
Definition: QtScriptBackend.cpp:104
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



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