| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #ifndef TST_QSCRIPTVALUE_H |
| 30 | #define TST_QSCRIPTVALUE_H |
| 31 | |
| 32 | #include <QtCore/qobject.h> |
| 33 | #include <QtCore/qnumeric.h> |
| 34 | #include <QtScript/qscriptclass.h> |
| 35 | #include <QtScript/qscriptengine.h> |
| 36 | #include <QtScript/qscriptvalue.h> |
| 37 | #include <QtTest/QtTest> |
| 38 | |
| 39 | #define DEFINE_TEST_VALUE(expr) m_values.insert(QString::fromLatin1(#expr), expr) |
| 40 | |
| 41 | Q_DECLARE_METATYPE(QVariant) |
| 42 | Q_DECLARE_METATYPE(QScriptValue) |
| 43 | |
| 44 | class tst_QScriptValueGenerated : public QObject |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | |
| 48 | public: |
| 49 | tst_QScriptValueGenerated(); |
| 50 | virtual ~tst_QScriptValueGenerated(); |
| 51 | |
| 52 | private slots: |
| 53 | // Generated test functions |
| 54 | void isArray_data(); |
| 55 | void isArray(); |
| 56 | |
| 57 | void isBool_data(); |
| 58 | void isBool(); |
| 59 | |
| 60 | void isBoolean_data(); |
| 61 | void isBoolean(); |
| 62 | |
| 63 | void isDate_data(); |
| 64 | void isDate(); |
| 65 | |
| 66 | void isError_data(); |
| 67 | void isError(); |
| 68 | |
| 69 | void isFunction_data(); |
| 70 | void isFunction(); |
| 71 | |
| 72 | void isNull_data(); |
| 73 | void isNull(); |
| 74 | |
| 75 | void isNumber_data(); |
| 76 | void isNumber(); |
| 77 | |
| 78 | void isObject_data(); |
| 79 | void isObject(); |
| 80 | |
| 81 | void isQMetaObject_data(); |
| 82 | void isQMetaObject(); |
| 83 | |
| 84 | void isQObject_data(); |
| 85 | void isQObject(); |
| 86 | |
| 87 | void isRegExp_data(); |
| 88 | void isRegExp(); |
| 89 | |
| 90 | void isString_data(); |
| 91 | void isString(); |
| 92 | |
| 93 | void isUndefined_data(); |
| 94 | void isUndefined(); |
| 95 | |
| 96 | void isValid_data(); |
| 97 | void isValid(); |
| 98 | |
| 99 | void isVariant_data(); |
| 100 | void isVariant(); |
| 101 | |
| 102 | void toBool_data(); |
| 103 | void toBool(); |
| 104 | |
| 105 | void toBoolean_data(); |
| 106 | void toBoolean(); |
| 107 | |
| 108 | // void toDateTime_data(); |
| 109 | // void toDateTime(); |
| 110 | |
| 111 | void toInt32_data(); |
| 112 | void toInt32(); |
| 113 | |
| 114 | void toInteger_data(); |
| 115 | void toInteger(); |
| 116 | |
| 117 | void toNumber_data(); |
| 118 | void toNumber(); |
| 119 | |
| 120 | // void toQMetaObject_data(); |
| 121 | // void toQMetaObject(); |
| 122 | |
| 123 | // void toQObject_data(); |
| 124 | // void toQObject(); |
| 125 | |
| 126 | // void toRegExp_data(); |
| 127 | // void toRegExp(); |
| 128 | |
| 129 | void toString_data(); |
| 130 | void toString(); |
| 131 | |
| 132 | void toUInt16_data(); |
| 133 | void toUInt16(); |
| 134 | |
| 135 | void toUInt32_data(); |
| 136 | void toUInt32(); |
| 137 | |
| 138 | // void toVariant_data(); |
| 139 | // void toVariant(); |
| 140 | |
| 141 | void equals_data(); |
| 142 | void equals(); |
| 143 | |
| 144 | void strictlyEquals_data(); |
| 145 | void strictlyEquals(); |
| 146 | |
| 147 | void lessThan_data(); |
| 148 | void lessThan(); |
| 149 | |
| 150 | void instanceOf_data(); |
| 151 | void instanceOf(); |
| 152 | |
| 153 | void assignAndCopyConstruct_data(); |
| 154 | void assignAndCopyConstruct(); |
| 155 | |
| 156 | void qscriptvalue_castQString_data(); |
| 157 | void qscriptvalue_castQString(); |
| 158 | |
| 159 | void qscriptvalue_castqsreal_data(); |
| 160 | void qscriptvalue_castqsreal(); |
| 161 | |
| 162 | void qscriptvalue_castbool_data(); |
| 163 | void qscriptvalue_castbool(); |
| 164 | |
| 165 | void qscriptvalue_castqint32_data(); |
| 166 | void qscriptvalue_castqint32(); |
| 167 | |
| 168 | void qscriptvalue_castquint32_data(); |
| 169 | void qscriptvalue_castquint32(); |
| 170 | |
| 171 | void qscriptvalue_castquint16_data(); |
| 172 | void qscriptvalue_castquint16(); |
| 173 | |
| 174 | private: |
| 175 | typedef void (tst_QScriptValueGenerated::*InitDataFunction)(); |
| 176 | typedef void (tst_QScriptValueGenerated::*DefineDataFunction)(const char *); |
| 177 | void dataHelper(InitDataFunction init, DefineDataFunction define); |
| 178 | QTestData &newRow(const char *tag); |
| 179 | |
| 180 | typedef void (tst_QScriptValueGenerated::*TestFunction)(const char *, const QScriptValue &); |
| 181 | void testHelper(TestFunction fun); |
| 182 | |
| 183 | // Generated functions |
| 184 | |
| 185 | void initScriptValues(); |
| 186 | |
| 187 | void isArray_initData(); |
| 188 | void isArray_makeData(const char *expr); |
| 189 | void isArray_test(const char *expr, const QScriptValue &value); |
| 190 | |
| 191 | void isBool_initData(); |
| 192 | void isBool_makeData(const char *expr); |
| 193 | void isBool_test(const char *expr, const QScriptValue &value); |
| 194 | |
| 195 | void isBoolean_initData(); |
| 196 | void isBoolean_makeData(const char *expr); |
| 197 | void isBoolean_test(const char *expr, const QScriptValue &value); |
| 198 | |
| 199 | void isDate_initData(); |
| 200 | void isDate_makeData(const char *expr); |
| 201 | void isDate_test(const char *expr, const QScriptValue &value); |
| 202 | |
| 203 | void isError_initData(); |
| 204 | void isError_makeData(const char *expr); |
| 205 | void isError_test(const char *expr, const QScriptValue &value); |
| 206 | |
| 207 | void isFunction_initData(); |
| 208 | void isFunction_makeData(const char *expr); |
| 209 | void isFunction_test(const char *expr, const QScriptValue &value); |
| 210 | |
| 211 | void isNull_initData(); |
| 212 | void isNull_makeData(const char *expr); |
| 213 | void isNull_test(const char *expr, const QScriptValue &value); |
| 214 | |
| 215 | void isNumber_initData(); |
| 216 | void isNumber_makeData(const char *expr); |
| 217 | void isNumber_test(const char *expr, const QScriptValue &value); |
| 218 | |
| 219 | void isObject_initData(); |
| 220 | void isObject_makeData(const char *expr); |
| 221 | void isObject_test(const char *expr, const QScriptValue &value); |
| 222 | |
| 223 | void isQMetaObject_initData(); |
| 224 | void isQMetaObject_makeData(const char *expr); |
| 225 | void isQMetaObject_test(const char *expr, const QScriptValue &value); |
| 226 | |
| 227 | void isQObject_initData(); |
| 228 | void isQObject_makeData(const char *expr); |
| 229 | void isQObject_test(const char *expr, const QScriptValue &value); |
| 230 | |
| 231 | void isRegExp_initData(); |
| 232 | void isRegExp_makeData(const char *expr); |
| 233 | void isRegExp_test(const char *expr, const QScriptValue &value); |
| 234 | |
| 235 | void isString_initData(); |
| 236 | void isString_makeData(const char *expr); |
| 237 | void isString_test(const char *expr, const QScriptValue &value); |
| 238 | |
| 239 | void isUndefined_initData(); |
| 240 | void isUndefined_makeData(const char *expr); |
| 241 | void isUndefined_test(const char *expr, const QScriptValue &value); |
| 242 | |
| 243 | void isValid_initData(); |
| 244 | void isValid_makeData(const char *expr); |
| 245 | void isValid_test(const char *expr, const QScriptValue &value); |
| 246 | |
| 247 | void isVariant_initData(); |
| 248 | void isVariant_makeData(const char *expr); |
| 249 | void isVariant_test(const char *expr, const QScriptValue &value); |
| 250 | |
| 251 | void toBool_initData(); |
| 252 | void toBool_makeData(const char *); |
| 253 | void toBool_test(const char *, const QScriptValue &value); |
| 254 | |
| 255 | void toBoolean_initData(); |
| 256 | void toBoolean_makeData(const char *); |
| 257 | void toBoolean_test(const char *, const QScriptValue &value); |
| 258 | |
| 259 | void toDateTime_initData(); |
| 260 | void toDateTime_makeData(const char *); |
| 261 | void toDateTime_test(const char *, const QScriptValue &value); |
| 262 | |
| 263 | void toInt32_initData(); |
| 264 | void toInt32_makeData(const char *); |
| 265 | void toInt32_test(const char *, const QScriptValue &value); |
| 266 | |
| 267 | void toInteger_initData(); |
| 268 | void toInteger_makeData(const char *); |
| 269 | void toInteger_test(const char *, const QScriptValue &value); |
| 270 | |
| 271 | void toNumber_initData(); |
| 272 | void toNumber_makeData(const char *); |
| 273 | void toNumber_test(const char *, const QScriptValue &value); |
| 274 | |
| 275 | void toQMetaObject_initData(); |
| 276 | void toQMetaObject_makeData(const char *); |
| 277 | void toQMetaObject_test(const char *, const QScriptValue &value); |
| 278 | |
| 279 | void toQObject_initData(); |
| 280 | void toQObject_makeData(const char *); |
| 281 | void toQObject_test(const char *, const QScriptValue &value); |
| 282 | |
| 283 | void toRegExp_initData(); |
| 284 | void toRegExp_makeData(const char *); |
| 285 | void toRegExp_test(const char *, const QScriptValue &value); |
| 286 | |
| 287 | void toString_initData(); |
| 288 | void toString_makeData(const char *); |
| 289 | void toString_test(const char *, const QScriptValue &value); |
| 290 | |
| 291 | void toUInt16_initData(); |
| 292 | void toUInt16_makeData(const char *); |
| 293 | void toUInt16_test(const char *, const QScriptValue &value); |
| 294 | |
| 295 | void toUInt32_initData(); |
| 296 | void toUInt32_makeData(const char *); |
| 297 | void toUInt32_test(const char *, const QScriptValue &value); |
| 298 | |
| 299 | void toVariant_initData(); |
| 300 | void toVariant_makeData(const char *); |
| 301 | void toVariant_test(const char *, const QScriptValue &value); |
| 302 | |
| 303 | void equals_initData(); |
| 304 | void equals_makeData(const char *); |
| 305 | void equals_test(const char *, const QScriptValue &value); |
| 306 | |
| 307 | void strictlyEquals_initData(); |
| 308 | void strictlyEquals_makeData(const char *); |
| 309 | void strictlyEquals_test(const char *, const QScriptValue &value); |
| 310 | |
| 311 | void lessThan_initData(); |
| 312 | void lessThan_makeData(const char *); |
| 313 | void lessThan_test(const char *, const QScriptValue &value); |
| 314 | |
| 315 | void instanceOf_initData(); |
| 316 | void instanceOf_makeData(const char *); |
| 317 | void instanceOf_test(const char *, const QScriptValue &value); |
| 318 | |
| 319 | void assignAndCopyConstruct_initData(); |
| 320 | void assignAndCopyConstruct_makeData(const char *); |
| 321 | void assignAndCopyConstruct_test(const char *, const QScriptValue &value); |
| 322 | |
| 323 | void qscriptvalue_castQString_initData(); |
| 324 | void qscriptvalue_castQString_makeData(const char *); |
| 325 | void qscriptvalue_castQString_test(const char *, const QScriptValue &value); |
| 326 | |
| 327 | void qscriptvalue_castqsreal_initData(); |
| 328 | void qscriptvalue_castqsreal_makeData(const char *); |
| 329 | void qscriptvalue_castqsreal_test(const char *, const QScriptValue &value); |
| 330 | |
| 331 | void qscriptvalue_castbool_initData(); |
| 332 | void qscriptvalue_castbool_makeData(const char *); |
| 333 | void qscriptvalue_castbool_test(const char *, const QScriptValue &value); |
| 334 | |
| 335 | void qscriptvalue_castqint32_initData(); |
| 336 | void qscriptvalue_castqint32_makeData(const char *); |
| 337 | void qscriptvalue_castqint32_test(const char *, const QScriptValue &value); |
| 338 | |
| 339 | void qscriptvalue_castquint32_initData(); |
| 340 | void qscriptvalue_castquint32_makeData(const char *); |
| 341 | void qscriptvalue_castquint32_test(const char *, const QScriptValue &value); |
| 342 | |
| 343 | void qscriptvalue_castquint16_initData(); |
| 344 | void qscriptvalue_castquint16_makeData(const char *); |
| 345 | void qscriptvalue_castquint16_test(const char *, const QScriptValue &value); |
| 346 | |
| 347 | private: |
| 348 | QScriptEngine *engine; |
| 349 | QHash<QString, QScriptValue> m_values; |
| 350 | QString m_currentExpression; |
| 351 | }; |
| 352 | |
| 353 | static inline QSet<QString> charArrayToQStringSet(const char **array, int size) |
| 354 | { |
| 355 | QSet<QString> result; |
| 356 | result.reserve(asize: size); |
| 357 | for (int i = 0; i < size; ++i) |
| 358 | result.insert(value: QLatin1String(array[i])); |
| 359 | return result; |
| 360 | } |
| 361 | |
| 362 | static inline QHash<QString, QString> charArraysToStringHash(const char **keys, const char **values, int size) |
| 363 | { |
| 364 | QHash<QString, QString > result; |
| 365 | result.reserve(asize: size); |
| 366 | for (int i = 0; i < size; ++i) |
| 367 | result.insert(akey: QLatin1String(keys[i]), avalue: QLatin1String(values[i])); |
| 368 | return result; |
| 369 | } |
| 370 | |
| 371 | template <class Value> |
| 372 | QHash<QString, Value> charValueArraysToHash(const char **keys, const Value *values, int size) |
| 373 | { |
| 374 | QHash<QString, Value> result; |
| 375 | result.reserve(size); |
| 376 | for (int i = 0; i < size; ++i) |
| 377 | result.insert(QLatin1String(keys[i]), values[i]); |
| 378 | return result; |
| 379 | } |
| 380 | |
| 381 | #define DEFINE_TEST_FUNCTION(name) \ |
| 382 | void tst_QScriptValueGenerated::name##_data() { dataHelper(&tst_QScriptValueGenerated::name##_initData, &tst_QScriptValueGenerated::name##_makeData); } \ |
| 383 | void tst_QScriptValueGenerated::name() { testHelper(&tst_QScriptValueGenerated::name##_test); } |
| 384 | |
| 385 | #endif |
| 386 | |