File: scriptengine_t.cpp

package info (click to toggle)
texstudio 2.8.4%2Bdebian-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 39,108 kB
  • ctags: 13,390
  • sloc: cpp: 84,770; xml: 10,109; ansic: 6,865; sh: 150; makefile: 82
file content (127 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

#ifndef QT_NO_DEBUG
#include "mostQtHeaders.h"
#include "scriptengine_t.h"
#include "scriptengine.h"
#include "latexeditorview.h"
#include "qdocumentcursor.h"
#include "qdocument.h"
#include "qeditor.h"
#include "testutil.h"
#include <QtTest/QtTest>
ScriptEngineTest::ScriptEngineTest(LatexEditorView* editor, bool all): edView(editor), all(all){
	edView->editor->setCursorPosition(0,0);
	edView->editor->document()->clear();
}

void ScriptEngineTest::script_data(){
	QTest::addColumn<QString>("script");
	QTest::addColumn<QString>("newText");
	
	//-------------cursor without selection--------------
	QTest::newRow("Setup Text")
		<< "editor.setText(\"Hallo\", false)"
		<< "Hallo";

	QTest::newRow("Insert Text")
		<< "cursor.insertText(\"a\");"
		<< "aHallo";

	QTest::newRow("Move Cursor")
		<< "cursor.moveTo(0,0);cursor.insertText(\"b\");"
		<< "baHallo";

	QTest::newRow("Delete Chars")
		<< "cursor.deletePreviousChar();cursor.deleteChar();"
		<< "Hallo";

	QTest::newRow("Undo")
		<< "editor.undo()"
		<< "baHallo";

	QTest::newRow("Redo")
		<< "editor.redo()"
		<< "Hallo";
	if (all) {
		QTest::newRow("Select All/copy/paste")
		<< "editor.selectAll();editor.copy();editor.selectNothing();editor.paste()"
		<< "HalloHallo";	
	} else 
		QTest::newRow("SKIP Select All/copy/paste ")
		<< "editor.setText(\"HalloHallo\");"
		<< "HalloHallo";
	
	QTest::newRow("remove Selection")
		<< "cursor.movePosition(1,cursorEnums.Start);cursor.movePosition(5,cursorEnums.Right,cursorEnums.KeepAnchor);cursor.removeSelectedText()"
		<< "Hallo";

	QTest::newRow("get selected Text")
		<< "cursor.movePosition(1,cursorEnums.Start);cursor.movePosition(2,cursorEnums.Right,cursorEnums.KeepAnchor);a=cursor.selectedText();cursor.replaceSelectedText(\"be\");cursor.clearSelection();cursor.insertText(a)"
		<< "beHallo";

	QTest::newRow("check cursor movement out of range")
		<< "cursor.movePosition(20,cursorEnums.Right);cursor.deleteChar();cursor.deletePreviousChar()"
		<< "bello";  // a movement out of range is canceled completely

	QTest::newRow("check cursor movement out of range")
		<< "cursor.moveTo(20,10);cursor.deleteChar();cursor.deletePreviousChar();cursor.eraseLine();cursor.insertText(\"as\")"
		<< "bello";  // invalid cursors are not executed

	QTest::newRow("Search/Replace Test 1")
		<< "editor.setText(\"Hallo1\\nHallo2\\nHallo3\", false); editor.replace(\"a\", \"b\"); "
		<< "Hbllo1\nHallo2\nHallo3";
	QTest::newRow("Search/Replace Test 2")
		<< "editor.replace(\"ll\", \"tt\", editor.document().cursor(1,0,1,6)); "
		<< "Hbllo1\nHatto2\nHallo3";
	QTest::newRow("Search/Replace Test 3")
		<< "editor.replace(/b..o/, function(c){return editor.search(c.selectedText());}); "
		<< "H11\nHatto2\nHallo3";
	QTest::newRow("Search/Replace Test 4 (no conversion)")
		<< "editor.replace(/[0-9]*/, function(c){return 17+c.selectedText();}); "
		<<  "H1711\nHatto2\nHallo3";
	QTest::newRow("Search/Replace Test 5")
		<< "editor.replace(/[0-9]*/, function(c){return 17+1*c.selectedText();}); "
		<< "H1728\nHatto2\nHallo3";

	if (all) {
		QTest::newRow("Search/Replace Test 1g")
			<< "editor.setText(\"Hallo1\\nHallo2\\nHallo3\", false); editor.replace(\"a\", \"g\", \"b\"); "
			<< "Hbllo1\nHbllo2\nHbllo3";
		QTest::newRow("Search/Replace Test 2g")
			<< "editor.replace(\"ll\", \"g\", \"tt\", editor.document().cursor(1,0,1,6)); "
			<< "Hbllo1\nHbtto2\nHbllo3";
		QTest::newRow("Search/Replace Test 3gg")
			<< "editor.replace(/b..o/g, function(c){return editor.search(c.selectedText(),\"g\");}); "
			<< "H21\nH12\nH13";
		QTest::newRow("Search/Replace Test 4g (no conversion)")
			<< "editor.replace(/[0-9]*/g, function(c){return 17+c.selectedText();}); "
			<<  "H1721\nH1712\nH1713";
		QTest::newRow("Search/Replace Test 5g")
			<< "editor.replace(/[0-9]*/g, function(c){return 17+1*c.selectedText();}); "
			<< "H1738\nH1729\nH1730";
		QTest::newRow("Search/Replace Test 6g")
			<< "editor.replace(/h/g, 'test'); "
			<< "H1738\nH1729\nH1730";
		QTest::newRow("Search/Replace Test 7gi")
			<< "editor.replace(/h/gi, 'test'); "
			<< "test1738\ntest1729\ntest1730";
		QTest::newRow("Search/Replace Test function replacing")
			  << "editor.setText(\"Hallo1\\nHallo2\\nHallo3\", false); editor.replace(\"a\", \"g\", function(c){return \">\"+c.selectedText()+\"<\";}); "
			<< "H>a<llo1\nH>a<llo2\nH>a<llo3";
		QTest::newRow("Search/Replace Test function replacing 2")
			  << "editor.setText(\"Hallo1\\nHamlo2\\nHallo3\", false); editor.replace(/a./, \"g\", function(c){return \">\"+c.selectedText()+\"<\";}); "
			<< "H>al<lo1\nH>am<lo2\nH>al<lo3";
	}
}
void ScriptEngineTest::script(){
	QFETCH(QString, script);
	QFETCH(QString, newText);
	scriptengine eng(0);
	eng.setEditorView(edView);
	eng.setScript(script);
	eng.run();

	QEQUAL(edView->editor->document()->text(), newText);	
}
#endif