Changeset View
Changeset View
Standalone View
Standalone View
autotests/src/testutils.cpp
Context not available. | |||||
30 | #include "kateconfig.h" | 30 | #include "kateconfig.h" | ||
---|---|---|---|---|---|
31 | #include "katedocument.h" | 31 | #include "katedocument.h" | ||
32 | #include "katescripthelpers.h" | 32 | #include "katescripthelpers.h" | ||
33 | #include "ktexteditor/cursor.h" | ||||
34 | #include "ktexteditor/range.h" | ||||
33 | 35 | | |||
34 | #include <QtScript/QScriptEngine> | 36 | #include <QJSEngine> | ||
37 | #include <QObject> | ||||
38 | #include <QQmlEngine> | ||||
35 | #include <QTest> | 39 | #include <QTest> | ||
36 | 40 | | |||
37 | //END Includes | 41 | //END Includes | ||
38 | 42 | | |||
39 | //BEGIN TestScriptEnv | 43 | //BEGIN TestScriptEnv | ||
40 | 44 | | |||
41 | //BEGIN conversion functions for Cursors and Ranges | | |||
42 | /** Converstion function from KTextEditor::Cursor to QtScript cursor */ | | |||
43 | static QScriptValue cursorToScriptValue(QScriptEngine *engine, const KTextEditor::Cursor &cursor) | | |||
44 | { | | |||
45 | QString code = QStringLiteral("new Cursor(%1, %2);").arg(cursor.line()) | | |||
46 | .arg(cursor.column()); | | |||
47 | return engine->evaluate(code); | | |||
48 | } | | |||
49 | | ||||
50 | /** Converstion function from QtScript cursor to KTextEditor::Cursor */ | | |||
51 | static void cursorFromScriptValue(const QScriptValue &obj, KTextEditor::Cursor &cursor) | | |||
52 | { | | |||
53 | cursor.setPosition(obj.property(QStringLiteral("line")).toInt32(), | | |||
54 | obj.property(QStringLiteral("column")).toInt32()); | | |||
55 | } | | |||
56 | | ||||
57 | /** Converstion function from QtScript range to KTextEditor::Range */ | | |||
58 | static QScriptValue rangeToScriptValue(QScriptEngine *engine, const KTextEditor::Range &range) | | |||
59 | { | | |||
60 | QString code = QStringLiteral("new Range(%1, %2, %3, %4);").arg(range.start().line()) | | |||
61 | .arg(range.start().column()) | | |||
62 | .arg(range.end().line()) | | |||
63 | .arg(range.end().column()); | | |||
64 | return engine->evaluate(code); | | |||
65 | } | | |||
66 | | ||||
67 | /** Converstion function from QtScript range to KTextEditor::Range */ | | |||
68 | static void rangeFromScriptValue(const QScriptValue &obj, KTextEditor::Range &range) | | |||
69 | { | | |||
70 | range.setStart(KTextEditor::Cursor( | | |||
71 | obj.property(QStringLiteral("start")).property(QStringLiteral("line")).toInt32(), | | |||
72 | obj.property(QStringLiteral("start")).property(QStringLiteral("column")).toInt32() | | |||
73 | )); | | |||
74 | range.setEnd(KTextEditor::Cursor( | | |||
75 | obj.property(QStringLiteral("end")).property(QStringLiteral("line")).toInt32(), | | |||
76 | obj.property(QStringLiteral("end")).property(QLatin1String("column")).toInt32() | | |||
77 | )); | | |||
78 | } | | |||
79 | //END | | |||
80 | | ||||
81 | TestScriptEnv::TestScriptEnv(KTextEditor::DocumentPrivate *part, bool &cflag) | 45 | TestScriptEnv::TestScriptEnv(KTextEditor::DocumentPrivate *part, bool &cflag) | ||
82 | : m_engine(nullptr), m_viewObj(nullptr), m_docObj(nullptr), m_output(nullptr) | 46 | : m_engine(nullptr), m_viewObj(nullptr), m_docObj(nullptr), m_output(nullptr) | ||
83 | { | 47 | { | ||
84 | m_engine = new QScriptEngine(this); | 48 | m_engine = new QJSEngine(this); | ||
85 | 49 | | |||
86 | qScriptRegisterMetaType(m_engine, cursorToScriptValue, cursorFromScriptValue); | 50 | // QJSValue cursorClass = m_engine->newQMetaObject(&KTextEditor::Cursor::staticMetaObject); | ||
87 | qScriptRegisterMetaType(m_engine, rangeToScriptValue, rangeFromScriptValue); | 51 | // m_engine->globalObject().setProperty(QStringLiteral("Cursor"), cursorClass); | ||
52 | // QJSValue rangeClass = m_engine->newQMetaObject(&KTextEditor::Range::staticMetaObject); | ||||
53 | // m_engine->globalObject().setProperty(QStringLiteral("Range"), rangeClass); | ||||
88 | 54 | | |||
89 | // export read & require function and add the require guard object | 55 | // export read & require function and add the require guard object | ||
90 | m_engine->globalObject().setProperty(QStringLiteral("read"), m_engine->newFunction(Kate::Script::read)); | 56 | QJSValue functions = m_engine->newQObject(new Kate::ScriptHelper(m_engine)); | ||
91 | m_engine->globalObject().setProperty(QStringLiteral("require"), m_engine->newFunction(Kate::Script::require)); | 57 | m_engine->globalObject().setProperty(QStringLiteral("functions"), functions); | ||
58 | m_engine->globalObject().setProperty(QStringLiteral("read"), functions.property(QStringLiteral("read"))); | ||||
59 | m_engine->globalObject().setProperty(QStringLiteral("require"), functions.property(QStringLiteral("require"))); | ||||
92 | m_engine->globalObject().setProperty(QStringLiteral("require_guard"), m_engine->newObject()); | 60 | m_engine->globalObject().setProperty(QStringLiteral("require_guard"), m_engine->newObject()); | ||
93 | 61 | | |||
94 | // export debug function | 62 | // export debug function | ||
95 | m_engine->globalObject().setProperty(QStringLiteral("debug"), m_engine->newFunction(Kate::Script::debug)); | 63 | m_engine->globalObject().setProperty(QStringLiteral("debug"), functions.property(QStringLiteral("debug"))); | ||
96 | 64 | | |||
97 | // export translation functions | 65 | // export translation functions | ||
98 | m_engine->globalObject().setProperty(QStringLiteral("i18n"), m_engine->newFunction(Kate::Script::i18n)); | 66 | m_engine->globalObject().setProperty(QStringLiteral("i18n"), functions.property(QStringLiteral("_i18n"))); | ||
99 | m_engine->globalObject().setProperty(QStringLiteral("i18nc"), m_engine->newFunction(Kate::Script::i18nc)); | 67 | m_engine->globalObject().setProperty(QStringLiteral("i18nc"), functions.property(QStringLiteral("_i18nc"))); | ||
100 | m_engine->globalObject().setProperty(QStringLiteral("i18ncp"), m_engine->newFunction(Kate::Script::i18ncp)); | 68 | m_engine->globalObject().setProperty(QStringLiteral("i18np"), functions.property(QStringLiteral("_i18np"))); | ||
101 | m_engine->globalObject().setProperty(QStringLiteral("i18np"), m_engine->newFunction(Kate::Script::i18np)); | 69 | m_engine->globalObject().setProperty(QStringLiteral("i18ncp"), functions.property(QStringLiteral("_i18ncp"))); | ||
102 | 70 | | |||
103 | KTextEditor::ViewPrivate *view = qobject_cast<KTextEditor::ViewPrivate *>(part->widget()); | 71 | KTextEditor::ViewPrivate *view = qobject_cast<KTextEditor::ViewPrivate *>(part->widget()); | ||
104 | 72 | | |||
105 | m_viewObj = new KateViewObject(view); | 73 | m_viewObj = new KateViewObject(m_engine, view); | ||
106 | QScriptValue sv = m_engine->newQObject(m_viewObj); | 74 | QJSValue sv = m_engine->newQObject(m_viewObj); | ||
107 | 75 | | |||
108 | m_engine->globalObject().setProperty(QStringLiteral("view"), sv); | 76 | m_engine->globalObject().setProperty(QStringLiteral("view"), sv); | ||
109 | m_engine->globalObject().setProperty(QStringLiteral("v"), sv); | 77 | m_engine->globalObject().setProperty(QStringLiteral("v"), sv); | ||
110 | 78 | | |||
111 | m_docObj = new KateDocumentObject(view->doc()); | 79 | m_docObj = new KateDocumentObject(m_engine, view->doc()); | ||
112 | QScriptValue sd = m_engine->newQObject(m_docObj); | 80 | QJSValue sd = m_engine->newQObject(m_docObj); | ||
113 | 81 | | |||
114 | m_engine->globalObject().setProperty(QStringLiteral("document"), sd); | 82 | m_engine->globalObject().setProperty(QStringLiteral("document"), sd); | ||
115 | m_engine->globalObject().setProperty(QStringLiteral("d"), sd); | 83 | m_engine->globalObject().setProperty(QStringLiteral("d"), sd); | ||
116 | 84 | | |||
117 | m_output = new OutputObject(view, cflag); | 85 | m_output = new OutputObject(view, cflag); | ||
118 | QScriptValue so = m_engine->newQObject(m_output); | 86 | QJSValue so = m_engine->newQObject(m_output); | ||
119 | 87 | | |||
120 | m_engine->globalObject().setProperty(QStringLiteral("output"), so); | 88 | m_engine->globalObject().setProperty(QStringLiteral("output"), so); | ||
121 | m_engine->globalObject().setProperty(QStringLiteral("out"), so); | 89 | m_engine->globalObject().setProperty(QStringLiteral("out"), so); | ||
Context not available. | |||||
139 | 107 | | |||
140 | //BEGIN KateViewObject | 108 | //BEGIN KateViewObject | ||
141 | 109 | | |||
142 | KateViewObject::KateViewObject(KTextEditor::ViewPrivate *view) | 110 | KateViewObject::KateViewObject(QJSEngine *engine, KTextEditor::ViewPrivate *view) | ||
143 | : KateScriptView() | 111 | : KateScriptView(engine) | ||
144 | { | 112 | { | ||
145 | setView(view); | 113 | setView(view); | ||
146 | } | 114 | } | ||
Context not available. | |||||
234 | 202 | | |||
235 | //BEGIN KateDocumentObject | 203 | //BEGIN KateDocumentObject | ||
236 | 204 | | |||
237 | KateDocumentObject::KateDocumentObject(KTextEditor::DocumentPrivate *doc) | 205 | KateDocumentObject::KateDocumentObject(QJSEngine *engine, KTextEditor::DocumentPrivate *doc) | ||
238 | : KateScriptDocument() | 206 | : KateScriptDocument(engine) | ||
239 | { | 207 | { | ||
240 | setDocument(doc); | 208 | setDocument(doc); | ||
241 | } | 209 | } | ||
Context not available. | |||||
247 | //END KateDocumentObject | 215 | //END KateDocumentObject | ||
248 | 216 | | |||
249 | //BEGIN OutputObject | 217 | //BEGIN OutputObject | ||
250 | | ||||
251 | OutputObject::OutputObject(KTextEditor::ViewPrivate *v, bool &cflag) | 218 | OutputObject::OutputObject(KTextEditor::ViewPrivate *v, bool &cflag) | ||
252 | : view(v), cflag(cflag) | 219 | : view(v), cflag(cflag) | ||
253 | { | 220 | { | ||
Context not available. | |||||
261 | void OutputObject::output(bool cp, bool ln) | 228 | void OutputObject::output(bool cp, bool ln) | ||
262 | { | 229 | { | ||
263 | QString str; | 230 | QString str; | ||
264 | for (int i = 0; i < context()->argumentCount(); ++i) { | 231 | // for (int i = 0; i < context()->argumentCount(); ++i) { | ||
265 | QScriptValue arg = context()->argument(i); | 232 | // QJSValue arg = context()->argument(i); | ||
266 | str += arg.toString(); | 233 | // str += arg.toString(); | ||
267 | } | 234 | // } | ||
268 | 235 | | |||
269 | if (cp) { | 236 | if (cp) { | ||
270 | KTextEditor::Cursor c = view->cursorPosition(); | 237 | KTextEditor::Cursor c = view->cursorPosition(); | ||
Context not available. | |||||
349 | { | 316 | { | ||
350 | output(true, true); | 317 | output(true, true); | ||
351 | } | 318 | } | ||
352 | | ||||
353 | //END OutputObject | 319 | //END OutputObject | ||
320 | | ||||
321 | #include "testutils.moc" | ||||
Context not available. |