| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
|
| 3 |
#include "test_utils.h" |
| 4 |
#include "tests.h" |
| 5 |
#include "tests_common.h" |
| 6 |
|
| 7 |
#include "common/appconfig.h" |
| 8 |
|
| 9 |
void CoreTests::classByteArray() |
| 10 |
{ |
| 11 |
RUN("ByteArray", ""); |
| 12 |
RUN("ByteArray('test')", "test"); |
| 13 |
RUN("ByteArray(ByteArray('test'))", "test"); |
| 14 |
RUN("typeof(ByteArray('test'))", "object\n"); |
| 15 |
RUN("ByteArray('test') instanceof ByteArray", "true\n"); |
| 16 |
RUN("b = ByteArray('0123'); b.chop(2); b", "01"); |
| 17 |
RUN("ByteArray('0123').equals(ByteArray('0123'))", "true\n"); |
| 18 |
RUN("ByteArray('0123').left(3)", "012"); |
| 19 |
RUN("ByteArray('0123').mid(1, 2)", "12"); |
| 20 |
RUN("ByteArray('0123').mid(1)", "123"); |
| 21 |
RUN("ByteArray('0123').right(3)", "123"); |
| 22 |
RUN("ByteArray('0123').remove(1, 2)", "03"); |
| 23 |
RUN("ByteArray(' 01 23 ').simplified()", "01 23"); |
| 24 |
RUN("ByteArray('0123').toBase64()", "MDEyMw=="); |
| 25 |
RUN("ByteArray('ABCd').toLower()", "abcd"); |
| 26 |
RUN("ByteArray('abcD').toUpper()", "ABCD"); |
| 27 |
RUN("ByteArray(' 01 23 ').trimmed()", "01 23"); |
| 28 |
RUN("b = ByteArray('0123'); b.truncate(2); b", "01"); |
| 29 |
RUN("ByteArray('0123').toLatin1String() == '0123'", "true\n"); |
| 30 |
RUN("ByteArray('0123').valueOf() == '0123'", "true\n"); |
| 31 |
RUN("ByteArray(8).size()", "8\n"); |
| 32 |
RUN("b = ByteArray(); b.length = 10; b.length", "10\n"); |
| 33 |
|
| 34 |
// ByteArray implicitly converts to str. |
| 35 |
RUN("ByteArray('test') == 'test'", "true\n"); |
| 36 |
RUN("ByteArray('test1') == 'test2'", "false\n"); |
| 37 |
RUN("ByteArray('test') === 'test'", "false\n"); |
| 38 |
RUN("ByteArray('a') + 'b'", "ab\n"); |
| 39 |
} |
| 40 |
|
| 41 |
void CoreTests::classFile() |
| 42 |
{ |
| 43 |
RUN("var f = new File('/copyq_missing_file'); f.exists()", "false\n"); |
| 44 |
} |
| 45 |
|
| 46 |
void CoreTests::classDir() |
| 47 |
{ |
| 48 |
RUN("var d = new Dir('/missing_directory/')" |
| 49 |
"; d.exists()" |
| 50 |
, "false\n" |
| 51 |
); |
| 52 |
|
| 53 |
const auto home = QDir::homePath(); |
| 54 |
RUN("Dir().homePath()", home + "\n"); |
| 55 |
RUN("Dir().home().path()", home + "\n"); |
| 56 |
|
| 57 |
const auto root = QDir::rootPath(); |
| 58 |
RUN("Dir().rootPath()", root + "\n"); |
| 59 |
RUN("Dir().root().path()", root + "\n"); |
| 60 |
|
| 61 |
const auto temp = QDir::tempPath(); |
| 62 |
RUN("Dir().tempPath()", temp + "\n"); |
| 63 |
RUN("Dir().temp().path()", temp + "\n"); |
| 64 |
|
| 65 |
RUN("Dir().separator()", QString(QDir::separator()) + "\n"); |
| 66 |
|
| 67 |
QTemporaryDir tmpDir; |
| 68 |
QVERIFY(tmpDir.isValid()); |
| 69 |
QDir dir(tmpDir.path()); |
| 70 |
const auto path = dir.path(); |
| 71 |
const auto args = QString::fromLatin1("var d = new Dir('%1')").arg(path); |
| 72 |
|
| 73 |
RUN(args << "d.exists()", "true\n"); |
| 74 |
RUN(args << "d.isReadable()", "true\n"); |
| 75 |
RUN(args << "d.isAbsolute()", "true\n"); |
| 76 |
RUN(args << "d.isRelative()", "false\n"); |
| 77 |
RUN(args << "d.absolutePath()", path + "\n"); |
| 78 |
RUN(args << "d.path()", path + "\n"); |
| 79 |
RUN(args << "d.makeAbsolute()", "true\n"); |
| 80 |
|
| 81 |
RUN(args << "d.mkdir('test')", "true\n"); |
| 82 |
QVERIFY( QDir(dir.filePath("test")).exists() ); |
| 83 |
RUN(args << "d.exists('test')", "true\n"); |
| 84 |
|
| 85 |
RUN(args << "d.mkpath('a/b/c')", "true\n"); |
| 86 |
QVERIFY( QDir(dir.filePath("a/b/c")).exists() ); |
| 87 |
RUN(args << "d.exists('a/b/c')", "true\n"); |
| 88 |
RUN(args << "d.filePath('a/b/c')", dir.filePath("a/b/c") + "\n"); |
| 89 |
RUN(args << "d.relativeFilePath('" + path + "/a/b/c')", "a/b/c\n"); |
| 90 |
RUN("Dir('" + path + "/test/../a//b/c/..').canonicalPath()", QDir(path + "/a/b").canonicalPath() + "\n"); |
| 91 |
RUN(args << "d.setPath('" + path + "/a/b/c')" << "d.path()", dir.filePath("a/b/c") + "\n"); |
| 92 |
|
| 93 |
RUN(args << "d.cd('a')" << "d.cd('b')", "true\n"); |
| 94 |
RUN(args << "d.cd('x')", "false\n"); |
| 95 |
RUN(args << "d.cd('a')" << "d.cd('b')" << "d.path()", dir.filePath("a/b") + "\n"); |
| 96 |
RUN(args << "d.cd('a')" << "d.cd('..')" << "d.path()", path + "\n"); |
| 97 |
RUN(args << "d.cd('a')" << "d.cdUp()" << "d.path()", path + "\n"); |
| 98 |
|
| 99 |
RUN(args << "d.count()", QString::fromLatin1("%1\n").arg(dir.count())); |
| 100 |
RUN(args << "d.dirName()", QString::fromLatin1("%1\n").arg(dir.dirName())); |
| 101 |
|
| 102 |
RUN(args << "d.match(['a*'], 'test')", "false\n"); |
| 103 |
RUN(args << "d.match(['t*'], 'test')", "true\n"); |
| 104 |
RUN(args << "d.entryList()", ".\n..\na\ntest\n"); |
| 105 |
RUN(args << "d.entryList(['t*'])", "test\n"); |
| 106 |
RUN(args << "d.entryList(['t?st', 'a*'])", "a\ntest\n"); |
| 107 |
|
| 108 |
RUN(args << "d.setNameFilters(['t?st', 'a*'])" << "d.nameFilters()", "t?st\na*\n"); |
| 109 |
|
| 110 |
QFile f(dir.filePath("test.txt")); |
| 111 |
QVERIFY( f.open(QIODevice::WriteOnly) ); |
| 112 |
f.close(); |
| 113 |
RUN(args << "d.exists('test.txt')", "true\n"); |
| 114 |
RUN(args << "d.absoluteFilePath('test.txt')", dir.filePath("test.txt") + "\n"); |
| 115 |
RUN(args << "d.rename('test.txt', 'test2.txt')", "true\n"); |
| 116 |
RUN(args << "d.exists('test2.txt')", "true\n"); |
| 117 |
RUN(args << "d.remove('test2.txt')", "true\n"); |
| 118 |
RUN(args << "d.exists('test2.txt')", "false\n"); |
| 119 |
|
| 120 |
RUN(args << "d.rmdir('test')", "true\n"); |
| 121 |
RUN(args << "d.exists('test')", "false\n"); |
| 122 |
|
| 123 |
RUN("Dir().cleanPath('/a//b/../c/')", QDir::cleanPath("/a//b/../c/") + "\n"); |
| 124 |
} |
| 125 |
|
| 126 |
void CoreTests::classTemporaryFile() |
| 127 |
{ |
| 128 |
RUN("var f = new TemporaryFile(); f.open()", "true\n"); |
| 129 |
|
| 130 |
QByteArray err; |
| 131 |
|
| 132 |
for ( const auto autoRemove : {true, false} ) { |
| 133 |
QByteArray fileName; |
| 134 |
const auto script = |
| 135 |
QString(R"( |
| 136 |
var f = new TemporaryFile() |
| 137 |
if (!f.open()) |
| 138 |
throw 'Failed to open temporary file' |
| 139 |
|
| 140 |
f.setAutoRemove(%1) |
| 141 |
print(f.fileName()) |
| 142 |
)").arg(autoRemove); |
| 143 |
run(Args() << script, &fileName, &err); |
| 144 |
QVERIFY2( testStderr(err), err ); |
| 145 |
|
| 146 |
QFile f( QString::fromUtf8(fileName) ); |
| 147 |
QVERIFY( f.exists() != autoRemove ); |
| 148 |
|
| 149 |
if (!autoRemove) |
| 150 |
f.remove(); |
| 151 |
} |
| 152 |
|
| 153 |
QByteArray fileName; |
| 154 |
const auto scriptWrite = R"( |
| 155 |
var f = new TemporaryFile() |
| 156 |
if (!f.open()) |
| 157 |
throw 'Failed to open temporary file' |
| 158 |
|
| 159 |
if (!f.write('LINE')) |
| 160 |
throw 'Failed to write to temporary file' |
| 161 |
|
| 162 |
f.setAutoRemove(false) |
| 163 |
print(f.fileName()) |
| 164 |
)"; |
| 165 |
run(Args() << scriptWrite, &fileName); |
| 166 |
QVERIFY2( testStderr(err), err ); |
| 167 |
QVERIFY( QFile::exists(QString::fromUtf8(fileName)) ); |
| 168 |
|
| 169 |
QByteArray out; |
| 170 |
const auto scriptRead = R"( |
| 171 |
var f = new File(str(input())) |
| 172 |
if (!f.openReadOnly()) |
| 173 |
throw 'Failed to open file' |
| 174 |
|
| 175 |
print('' |
| 176 |
+ ' exists()=' + str(f.exists()) |
| 177 |
+ ' isOpen()=' + str(f.isOpen()) |
| 178 |
+ ' isReadable()=' + str(f.isReadable()) |
| 179 |
+ ' isWritable()=' + str(f.isWritable()) |
| 180 |
+ ' size()=' + str(f.size()) |
| 181 |
+ ' readAll()=' + str(f.readAll()) |
| 182 |
+ ' atEnd()=' + str(f.atEnd()) |
| 183 |
+ ' seek(0)=' + str(f.seek(0)) |
| 184 |
+ ' read(1)=' + str(f.read(1)) |
| 185 |
+ ' pos()=' + str(f.pos()) |
| 186 |
+ ' peek(1)=' + str(f.peek(1)) |
| 187 |
+ ' readLine()=' + str(f.readLine()) |
| 188 |
) |
| 189 |
)"; |
| 190 |
const QByteArray expectedOut = |
| 191 |
" exists()=true" |
| 192 |
" isOpen()=true" |
| 193 |
" isReadable()=true" |
| 194 |
" isWritable()=false" |
| 195 |
" size()=4" |
| 196 |
" readAll()=LINE" |
| 197 |
" atEnd()=true" |
| 198 |
" seek(0)=true" |
| 199 |
" read(1)=L" |
| 200 |
" pos()=1" |
| 201 |
" peek(1)=I" |
| 202 |
" readLine()=INE"; |
| 203 |
run(Args() << scriptRead, &out, &err, fileName); |
| 204 |
QVERIFY2( testStderr(err), err ); |
| 205 |
QCOMPARE(out, expectedOut); |
| 206 |
|
| 207 |
const auto scriptRemove = R"( |
| 208 |
var f = new File(str(input())) |
| 209 |
if (!f.remove()) |
| 210 |
throw 'Failed to remove file' |
| 211 |
)"; |
| 212 |
run(Args() << scriptRemove, &out, &err, fileName); |
| 213 |
QVERIFY2( testStderr(err), err ); |
| 214 |
QCOMPARE(QByteArray(), out); |
| 215 |
QVERIFY( !QFile::exists(QString::fromUtf8(fileName)) ); |
| 216 |
|
| 217 |
RUN("TemporaryFile().autoRemove()", "true\n"); |
| 218 |
RUN("TemporaryFile().fileTemplate()", QDir::temp().filePath(QStringLiteral("copyq-%1.XXXXXX").arg(sessionName)) + "\n"); |
| 219 |
} |
| 220 |
|
| 221 |
void CoreTests::classItemSelection() |
| 222 |
{ |
| 223 |
const auto tab1 = testTab(1); |
| 224 |
const Args args = Args("tab") << tab1 << "separator" << ","; |
| 225 |
const QString outRows("ItemSelection(tab=\"" + tab1 + "\", rows=[%1])\n"); |
| 226 |
|
| 227 |
RUN(args << "add" << "C" << "B" << "A", ""); |
| 228 |
RUN(args << "ItemSelection().length", "0\n"); |
| 229 |
RUN("ItemSelection('" + tab1 + "').length", "0\n"); |
| 230 |
RUN(args << "ItemSelection().selectAll().length", "3\n"); |
| 231 |
RUN("ItemSelection('" + tab1 + "').selectAll().length", "3\n"); |
| 232 |
|
| 233 |
RUN(args << "a = ItemSelection(); b = a; a === b", "true\n"); |
| 234 |
RUN(args << "a = ItemSelection(); b = a.selectAll(); a === b", "true\n"); |
| 235 |
|
| 236 |
RUN(args << "ItemSelection().selectAll().str()", outRows.arg("0..2")); |
| 237 |
RUN(args << "ItemSelection().selectRemovable().str()", outRows.arg("0..2")); |
| 238 |
RUN(args << "ItemSelection().selectRemovable().removeAll().str()", outRows.arg("")); |
| 239 |
RUN(args << "read(0,1,2)", ",,"); |
| 240 |
|
| 241 |
RUN(args << "add" << "C" << "B" << "A", ""); |
| 242 |
RUN(args << "ItemSelection().select(/A|C/).str()", outRows.arg("0,2")); |
| 243 |
RUN(args << "ItemSelection().select(/a|c/i).str()", outRows.arg("0,2")); |
| 244 |
RUN(args << "ItemSelection().select(/A/).select(/C/).str()", outRows.arg("0,2")); |
| 245 |
RUN(args << "ItemSelection().select(/C/).select(/A/).str()", outRows.arg("2,0")); |
| 246 |
RUN(args << "ItemSelection().select(/A|C/).invert().str()", outRows.arg("1")); |
| 247 |
|
| 248 |
RUN(args << "ItemSelection().select(/A|C/).deselectIndexes([0]).str()", outRows.arg("2")); |
| 249 |
RUN(args << "ItemSelection().select(/A|C/).deselectIndexes([1]).str()", outRows.arg("0")); |
| 250 |
RUN(args << "ItemSelection().select(/A|C/).deselectIndexes([0,1]).str()", outRows.arg("")); |
| 251 |
RUN(args << "ItemSelection().select(/A|C/).deselectSelection(ItemSelection().select(/A/)).str()", outRows.arg("2")); |
| 252 |
RUN(args << "ItemSelection().select(/A|C/).deselectSelection(ItemSelection().select(/C/)).str()", outRows.arg("0")); |
| 253 |
RUN(args << "ItemSelection().select(/A|C/).deselectSelection(ItemSelection().selectAll()).str()", outRows.arg("")); |
| 254 |
|
| 255 |
RUN(args << "a = ItemSelection().select(/a/i); b = a.copy(); a !== b", "true\n"); |
| 256 |
RUN(args << "a = ItemSelection().select(/a/i); b = a.copy(); a.str() == b.str()", "true\n"); |
| 257 |
RUN(args << "a = ItemSelection().select(/a|b/i); b = a.copy(); b.select(/C/); [a.rows(), '', b.rows()]", "0\n1\n\n0\n1\n2\n"); |
| 258 |
|
| 259 |
RUN(args << "s = ItemSelection().selectAll(); insert(1, 'X'); insert(3, 'Y'); s.invert().str()", outRows.arg("1,3")); |
| 260 |
|
| 261 |
RUN(args << "ItemSelection().select(/a/i).invert().removeAll().str()", outRows.arg("")); |
| 262 |
RUN(args << "read(0,1,2)", "A,,"); |
| 263 |
|
| 264 |
RUN(args << "ItemSelection().selectAll().removeAll().str()", outRows.arg("")); |
| 265 |
RUN(args << "read(0,1,2)", ",,"); |
| 266 |
|
| 267 |
RUN(args << "write('application/x-tst', 'ghi')", ""); |
| 268 |
RUN(args << "write('application/x-tst', 'def')", ""); |
| 269 |
RUN(args << "write('application/x-tst', 'abc')", ""); |
| 270 |
RUN(args << "read('application/x-tst',0,1,2)", "abc,def,ghi"); |
| 271 |
RUN(args << "ItemSelection().select(/e/, 'application/x-tst').str()", outRows.arg("1")); |
| 272 |
RUN(args << "ItemSelection().select(/e/, 'application/x-tst').removeAll().str()", outRows.arg("")); |
| 273 |
RUN(args << "read('application/x-tst',0,1,2)", "abc,ghi,"); |
| 274 |
|
| 275 |
RUN(args << "ItemSelection().selectAll().itemAtIndex(0)['application/x-tst']", "abc"); |
| 276 |
RUN(args << "ItemSelection().selectAll().itemAtIndex(1)['application/x-tst']", "ghi"); |
| 277 |
RUN(args << "ItemSelection().select(/h/, 'application/x-tst').itemAtIndex(0)['application/x-tst']", "ghi"); |
| 278 |
RUN(args << "ItemSelection().select(/h/, 'application/x-tst').itemAtIndex(1)['application/x-tst'] == undefined", "true\n"); |
| 279 |
|
| 280 |
RUN(args << "ItemSelection().select(/ghi/, 'application/x-tst').setItemAtIndex(0, {'application/x-tst': 'def'}).str()", |
| 281 |
outRows.arg("1")); |
| 282 |
RUN(args << "read('application/x-tst',0,1,2)", "abc,def,"); |
| 283 |
|
| 284 |
RUN(args << "d = ItemSelection().selectAll().items(); [d.length, str(d[0]['application/x-tst']), str(d[1]['application/x-tst'])]", "2\nabc\ndef\n"); |
| 285 |
RUN(args << "ItemSelection().selectAll().setItems([{'application/x-tst': 'xyz'}]).str()", outRows.arg("0,1")); |
| 286 |
RUN(args << "read('application/x-tst',0,1,2)", "xyz,def,"); |
| 287 |
|
| 288 |
RUN(args << "ItemSelection().selectAll().setItemsFormat(mimeItemNotes, 'test1').str()", outRows.arg("0,1")); |
| 289 |
RUN(args << "read(mimeItemNotes,0,1,2)", "test1,test1,"); |
| 290 |
RUN(args << "read('application/x-tst',0,1,2)", "xyz,def,"); |
| 291 |
|
| 292 |
RUN(args << "ItemSelection().selectAll().setItems([{'application/x-tst': ByteArray('abc')}]).str()", outRows.arg("0,1")); |
| 293 |
RUN(args << "read('application/x-tst',0,1,2)", "abc,def,"); |
| 294 |
|
| 295 |
RUN(args << "ItemSelection().selectAll().setItemsFormat(mimeItemNotes, ByteArray('test2')).str()", outRows.arg("0,1")); |
| 296 |
RUN(args << "read(mimeItemNotes,0,1,2)", "test2,test2,"); |
| 297 |
RUN(args << "read('application/x-tst',0,1,2)", "abc,def,"); |
| 298 |
|
| 299 |
RUN(args << "ItemSelection().selectAll().itemsFormat(mimeItemNotes).map(str)", "test2\ntest2\n"); |
| 300 |
RUN(args << "ItemSelection().selectAll().itemsFormat('application/x-tst').map(str)", "abc\ndef\n"); |
| 301 |
RUN(args << "ItemSelection().selectAll().itemsFormat(ByteArray('application/x-tst')).map(str)", "abc\ndef\n"); |
| 302 |
|
| 303 |
RUN(args << "ItemSelection().selectAll().setItemsFormat(mimeItemNotes, undefined).str()", outRows.arg("0,1")); |
| 304 |
RUN(args << "read(mimeItemNotes,0,1,2)", ",,"); |
| 305 |
RUN(args << "read('application/x-tst',0,1,2)", "abc,def,"); |
| 306 |
|
| 307 |
RUN(args << "ItemSelection().selectAll().removeAll().str()", outRows.arg("")); |
| 308 |
RUN(args << "add" << "C" << "B" << "A", ""); |
| 309 |
RUN(args << "ItemSelection().select(/C/).move(1).str()", outRows.arg("1")); |
| 310 |
RUN(args << "read(0,1,2)", "A,C,B"); |
| 311 |
RUN(args << "ItemSelection().select(/B/).select(/C/).move(1).str()", outRows.arg("2,1")); |
| 312 |
RUN(args << "read(0,1,2)", "A,C,B"); |
| 313 |
RUN(args << "ItemSelection().select(/A/).move(2).str()", outRows.arg("1")); |
| 314 |
RUN(args << "read(0,1,2)", "C,A,B"); |
| 315 |
RUN(args << "ItemSelection().select(/C/).select(/B/).move(2).str()", outRows.arg("1,2")); |
| 316 |
RUN(args << "read(0,1,2)", "A,C,B"); |
| 317 |
|
| 318 |
RUN(args << "change(1, mimeItemNotes, 'NOTE'); read(mimeItemNotes,0,1,2)", ",NOTE,"); |
| 319 |
RUN(args << "ItemSelection().select(/.*/, mimeItemNotes).str()", outRows.arg("1")); |
| 320 |
RUN(args << "ItemSelection().select(undefined, mimeItemNotes).str()", outRows.arg("0,2")); |
| 321 |
|
| 322 |
// Match nothing if select() argument is not a regular expression. |
| 323 |
RUN(args << "add" << "", ""); |
| 324 |
RUN(args << "ItemSelection().select('A').str()", outRows.arg("")); |
| 325 |
} |
| 326 |
|
| 327 |
void CoreTests::classItemSelectionGetCurrent() |
| 328 |
{ |
| 329 |
const auto tab1 = testTab(1); |
| 330 |
const Args args = Args("tab") << tab1 << "separator" << ","; |
| 331 |
|
| 332 |
RUN("ItemSelection().tab", "CLIPBOARD\n"); |
| 333 |
RUN(args << "ItemSelection().tab", tab1 + "\n"); |
| 334 |
|
| 335 |
RUN(args << "ItemSelection().current().tab", tab1 + "\n"); |
| 336 |
RUN(args << "ItemSelection().current().str()", "ItemSelection(tab=\"" + tab1 + "\", rows=[])\n"); |
| 337 |
RUN("setCurrentTab" << tab1, ""); |
| 338 |
RUN(args << "ItemSelection().current().tab", tab1 + "\n"); |
| 339 |
RUN(args << "ItemSelection().current().str()", "ItemSelection(tab=\"" + tab1 + "\", rows=[])\n"); |
| 340 |
|
| 341 |
RUN(args << "add" << "C" << "B" << "A", ""); |
| 342 |
RUN(args << "ItemSelection().current().str()", "ItemSelection(tab=\"" + tab1 + "\", rows=[0])\n"); |
| 343 |
|
| 344 |
RUN("setCommands([{name: 'test', inMenu: true, shortcuts: ['Ctrl+F1'], cmd: 'copyq: add(ItemSelection().current().str())'}])", ""); |
| 345 |
KEYS("CTRL+F1"); |
| 346 |
WAIT_ON_OUTPUT(args << "read(0)", "ItemSelection(tab=\"" + tab1 + "\", rows=[0])"); |
| 347 |
KEYS("END" << "SHIFT+UP" << "CTRL+F1"); |
| 348 |
WAIT_ON_OUTPUT(args << "read(0)", "ItemSelection(tab=\"" + tab1 + "\", rows=[3,2])"); |
| 349 |
} |
| 350 |
|
| 351 |
void CoreTests::classItemSelectionByteArray() |
| 352 |
{ |
| 353 |
const auto tab1 = testTab(1); |
| 354 |
const Args args = Args("tab") << tab1 << "separator" << ","; |
| 355 |
RUN("setCurrentTab" << tab1, ""); |
| 356 |
|
| 357 |
RUN(args << "add" << "C" << "B" << "A", ""); |
| 358 |
RUN(args << "ByteArray(ItemSelection().selectAll().itemAtIndex(0)[mimeText])", "A"); |
| 359 |
RUN(args << "str(ItemSelection().selectAll().itemAtIndex(0)[mimeText])", "A\n"); |
| 360 |
RUN(args << "write(0, [ItemSelection().selectAll().itemAtIndex(2)])" |
| 361 |
<< "read(mimeText, 0)", "C"); |
| 362 |
} |
| 363 |
|
| 364 |
void CoreTests::classItemSelectionSort() |
| 365 |
{ |
| 366 |
const auto tab1 = testTab(1); |
| 367 |
const Args args = Args("tab") << tab1 << "separator" << ","; |
| 368 |
const QString outRows("ItemSelection(tab=\"" + tab1 + "\", rows=[%1])\n"); |
| 369 |
RUN("setCurrentTab" << tab1, ""); |
| 370 |
|
| 371 |
const QString initScript = R"( |
| 372 |
add( |
| 373 |
{[mimeText]: 2, [mimeHtml]: "two"}, |
| 374 |
{[mimeText]: 5, [mimeHtml]: "five"}, |
| 375 |
{[mimeText]: 1, [mimeHtml]: "one"}, |
| 376 |
{[mimeText]: 3, [mimeHtml]: "three"}, |
| 377 |
{[mimeText]: 4, [mimeHtml]: "four"}, |
| 378 |
); |
| 379 |
read(0,1,2,3,4); |
| 380 |
)"; |
| 381 |
RUN(args << initScript, "4,3,1,5,2"); |
| 382 |
|
| 383 |
const auto script = R"( |
| 384 |
var sel = ItemSelection().selectAll(); |
| 385 |
const texts = sel.itemsFormat(mimeText); |
| 386 |
sel.sort(function(i,j){ |
| 387 |
return texts[i] < texts[j]; |
| 388 |
}); |
| 389 |
sel.str(); |
| 390 |
)"; |
| 391 |
RUN(args << script, outRows.arg("3,2,0,4,1")); |
| 392 |
RUN(args << "read(0,1,2,3,4)", "1,2,3,4,5"); |
| 393 |
RUN(args << "read(mimeHtml,0,1,2,3,4)", "one,two,three,four,five"); |
| 394 |
RUN(args << "size", "5\n"); |
| 395 |
} |
| 396 |
|
| 397 |
void CoreTests::classSettings() |
| 398 |
{ |
| 399 |
TemporaryFile configFile; |
| 400 |
const QString fileName = configFile.fileName(); |
| 401 |
|
| 402 |
RUN("eval" << "s=Settings(str(arguments[1])); print(s.fileName())" << fileName, fileName); |
| 403 |
RUN("eval" << "s=Settings(str(arguments[1])); s.isWritable() === true" << fileName, "true\n"); |
| 404 |
|
| 405 |
RUN("eval" << "s=Settings(str(arguments[1])); s.contains('o1')" << fileName, "false\n"); |
| 406 |
RUN("eval" << "s=Settings(str(arguments[1])); s.setValue('o1', 1); s.sync(); s.contains('o1')" << fileName, "true\n"); |
| 407 |
RUN("eval" << "s=Settings(str(arguments[1])); s.value('o1')" << fileName, "1\n"); |
| 408 |
|
| 409 |
RUN("eval" << "s=Settings(str(arguments[1])); s.setValue('o2', 2)" << fileName, ""); |
| 410 |
RUN("eval" << "s=Settings(str(arguments[1])); s.value('o2')" << fileName, "2\n"); |
| 411 |
|
| 412 |
RUN("eval" << "s=Settings(str(arguments[1])); s.setValue('o2', [1,2,3])" << fileName, ""); |
| 413 |
RUN("eval" << "s=Settings(str(arguments[1])); s.value('o2')[0]" << fileName, "1\n"); |
| 414 |
RUN("eval" << "s=Settings(str(arguments[1])); s.value('o2')[1]" << fileName, "2\n"); |
| 415 |
RUN("eval" << "s=Settings(str(arguments[1])); s.value('o2')[2]" << fileName, "3\n"); |
| 416 |
|
| 417 |
RUN("eval" << "s=Settings(str(arguments[1])); s.setValue('g1/o3', true)" << fileName, ""); |
| 418 |
RUN("eval" << "s=Settings(str(arguments[1])); s.value('g1/o3')" << fileName, "true\n"); |
| 419 |
|
| 420 |
RUN("eval" << "s=Settings(str(arguments[1])); s.childKeys()" << fileName, "o1\no2\n"); |
| 421 |
RUN("eval" << "s=Settings(str(arguments[1])); s.allKeys()" << fileName, "g1/o3\no1\no2\n"); |
| 422 |
|
| 423 |
RUN("eval" << "s=Settings(str(arguments[1])); s.beginGroup('g1'); s.group()" << fileName, "g1\n"); |
| 424 |
RUN("eval" << "s=Settings(str(arguments[1])); s.beginGroup('g1'); s.setValue('g1.2/o4', 'test')" << fileName, ""); |
| 425 |
RUN("eval" << "s=Settings(str(arguments[1])); s.beginGroup('g1'); s.childGroups()" << fileName, "g1.2\n"); |
| 426 |
RUN("eval" << "s=Settings(str(arguments[1])); s.beginGroup('g1'); s.endGroup(); s.childGroups()" << fileName, "g1\n"); |
| 427 |
RUN("eval" << "s=Settings(str(arguments[1])); s.value('g1/g1.2/o4')" << fileName, "test\n"); |
| 428 |
RUN("eval" << "s=Settings(str(arguments[1])); s.allKeys()" << fileName, "g1/g1.2/o4\ng1/o3\no1\no2\n"); |
| 429 |
RUN("eval" << "s=Settings(str(arguments[1])); s.remove('g1/g1.2/o4'); s.allKeys()" << fileName, "g1/o3\no1\no2\n"); |
| 430 |
|
| 431 |
RUN("eval" << "s=Settings(str(arguments[1])); s.beginWriteArray('a1', 3); s.setArrayIndex(1); s.setValue('o1', 'v1'); s.endArray()" << fileName, ""); |
| 432 |
RUN("eval" << "s=Settings(str(arguments[1])); s.beginReadArray('a1')" << fileName, "3\n"); |
| 433 |
RUN("eval" << "s=Settings(str(arguments[1])); s.beginReadArray('a1'); s.setArrayIndex(1); s.value('o1');" << fileName, "v1\n"); |
| 434 |
|
| 435 |
RUN("eval" << "s=Settings(str(arguments[1])); s.clear(); s.allKeys()" << fileName, ""); |
| 436 |
|
| 437 |
QVERIFY(QFile::remove(fileName)); |
| 438 |
QVERIFY(!QFile::exists(fileName)); |
| 439 |
RUN("eval" << "s=Settings(str(arguments[1])); s.setValue('o1', 1); s.sync(); File(str(arguments[1])).exists()" << fileName, "true\n"); |
| 440 |
QVERIFY(QFile::exists(fileName)); |
| 441 |
|
| 442 |
QVERIFY(QFile::remove(fileName)); |
| 443 |
QVERIFY(!QFile::exists(fileName)); |
| 444 |
RUN("eval" << "s=Settings(str(arguments[1])); s.setValue('o1', 1)" << fileName, ""); |
| 445 |
QVERIFY(QFile::exists(fileName)); |
| 446 |
|
| 447 |
const QString appConfigFileName = AppConfig().settings().fileName(); |
| 448 |
RUN("Settings().fileName()", QStringLiteral("%1\n").arg(appConfigFileName)); |
| 449 |
RUN("Settings().value('Options/tabs')", QStringLiteral("%1\n").arg(clipboardTabName)); |
| 450 |
} |
| 451 |
|
| 452 |
void CoreTests::calledWithInstance() |
| 453 |
{ |
| 454 |
// These would fail with the old deprecated Qt Script module. |
| 455 |
RUN("f=ByteArray().size; f()", "0\n"); |
| 456 |
RUN("f=Dir().path; f()", ".\n"); |
| 457 |
RUN("f=File('test').fileName; f()", "test\n"); |
| 458 |
RUN("f=TemporaryFile().autoRemove; f()", "true\n"); |
| 459 |
} |