2017-04-22 22:49:49 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2018-03-19 03:23:05 +00:00
|
|
|
const random = require('../random')
|
2018-03-19 04:54:40 +00:00
|
|
|
const make = require('../make')
|
|
|
|
const utils = require('../utils')
|
2018-03-19 05:34:18 +00:00
|
|
|
const {o} = require('./objects')
|
2018-03-19 03:23:05 +00:00
|
|
|
|
|
|
|
class script extends utils {
|
|
|
|
static methodHead (list, numOptional) {
|
2017-04-22 22:49:49 +00:00
|
|
|
if (isNaN(numOptional)) {
|
2017-04-25 22:21:31 +00:00
|
|
|
numOptional = 0
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-05-02 14:01:32 +00:00
|
|
|
let arity = list.length - random.number(numOptional)
|
|
|
|
let params = []
|
|
|
|
for (let i = 0; i < arity; i++) {
|
2017-04-25 22:21:31 +00:00
|
|
|
params.push(random.pick([list[i]]))
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
return `(${params.join(', ')})`
|
|
|
|
}
|
|
|
|
|
|
|
|
static methodCall (objectName, methodHash) {
|
2017-04-22 22:49:49 +00:00
|
|
|
if (!utils.common.getKeysFromHash(methodHash).length || !objectName) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return ''
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-05-02 14:01:32 +00:00
|
|
|
let methodName = random.key(methodHash)
|
|
|
|
let methodArgs = methodHash[methodName]
|
2017-04-25 22:21:31 +00:00
|
|
|
if (typeof (methodArgs) === 'function') { // Todo: Hmmmm..
|
|
|
|
return methodArgs()
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
return objectName + '.' + methodName + script.methodHead(methodArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
static setAttribute (objectName, attributeHash) {
|
2017-04-22 22:49:49 +00:00
|
|
|
if (!utils.common.getKeysFromHash(attributeHash).length || !objectName) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return ''
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-05-02 14:01:32 +00:00
|
|
|
let attributeName = random.key(attributeHash)
|
|
|
|
let attributeValue = random.pick(attributeHash[attributeName])
|
|
|
|
let operator = ' = '
|
2017-04-22 22:49:49 +00:00
|
|
|
/*
|
|
|
|
if (typeof(attributeValue) == "number" && Random.chance(8)) {
|
|
|
|
operator = " " + Make.randomAssignmentOperator() + " ";
|
|
|
|
}
|
|
|
|
if (typeof(attributeValue) == "string") {
|
|
|
|
attributeValue = "'" + attributeValue + "'";
|
|
|
|
}
|
|
|
|
*/
|
2017-04-25 22:21:31 +00:00
|
|
|
return objectName + '.' + attributeName + operator + attributeValue + ';'
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static makeConstraint (keys, values) {
|
2017-05-02 14:01:32 +00:00
|
|
|
let o = {}
|
|
|
|
let n = random.range(0, keys.length)
|
2017-04-22 22:49:49 +00:00
|
|
|
while (n--) {
|
2017-04-25 22:21:31 +00:00
|
|
|
o[random.pick(keys)] = random.pick(values)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return o
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static makeRandomOptions (baseObject) {
|
2017-05-02 14:01:32 +00:00
|
|
|
let o = {}
|
2017-06-08 02:27:26 +00:00
|
|
|
let unique = random.subset(Object.keys(baseObject))
|
2017-05-02 14:01:32 +00:00
|
|
|
for (let i = 0; i < unique.length; i++) {
|
2017-04-25 22:21:31 +00:00
|
|
|
o[unique[i]] = random.pick(baseObject[unique[i]])
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return JSON.stringify(o)
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 17:45:19 +00:00
|
|
|
static safely (obj) {
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
return obj.map(s => utils.script.safely(s)).join(' ')
|
|
|
|
} else {
|
|
|
|
return `try { ${obj} } catch(e) { }`
|
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static makeLoop (s, max) {
|
2017-06-10 05:23:38 +00:00
|
|
|
return 'for (let i = 0; i < ' + (max || make.number.range()) + '; i++) {' + s + '}'
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static makeArray (type, arrayLength, cb) {
|
2017-04-25 15:22:15 +00:00
|
|
|
if (type === null || type === undefined) {
|
2017-06-15 16:49:29 +00:00
|
|
|
type = random.item(['Uint8', 'Float32'])
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
switch (random.number(8)) {
|
|
|
|
case 0:
|
2017-05-02 14:01:32 +00:00
|
|
|
let src = 'function() { let buffer = new ' + type + 'Array(' + arrayLength + ');'
|
2018-03-19 03:23:05 +00:00
|
|
|
src += script.makeLoop('buffer[i] = ' + cb() + ';', arrayLength)
|
2017-04-25 22:21:31 +00:00
|
|
|
src += 'return buffer;}()'
|
|
|
|
return src
|
2017-04-22 22:49:49 +00:00
|
|
|
case 1:
|
2018-03-19 03:23:05 +00:00
|
|
|
return `new ${type}Array([${make.arrays.filledArray(cb, arrayLength)}])`
|
2017-04-22 22:49:49 +00:00
|
|
|
default:
|
2018-03-19 03:23:05 +00:00
|
|
|
return `new ${type}Array(${arrayLength})`
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static randListIndex (objName) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return random.number() + ' % ' + o.pick(objName) + '.length'
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static addElementToBody (name) {
|
|
|
|
return `(document.body || document.documentElement).appendChild${script.methodHead([name])}`
|
|
|
|
}
|
|
|
|
|
|
|
|
static getRandomElement () {
|
|
|
|
return `document.getElementsByTagName('*')[${random.number(document.getElementsByTagName('*').length)}]`
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
|
|
|
|
module.exports = script
|