mocha-froth/index.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-02-23 12:13:35 -07:00
'use strict'
2018-02-19 23:23:24 -07:00
2018-02-23 12:13:35 -07:00
/**
* Returns an array of random strings for fuzzing
* @param {number} num: number of test strings to return
* @param {number} max: maximum length of test string
* @param {object} opt: options
* @return {array}
2018-02-19 23:23:24 -07:00
*/
2018-02-23 12:13:35 -07:00
module.exports = function(num=10, max=20, opt={
2018-02-19 23:23:24 -07:00
// Set to true to include tests with...
none: true, // Empty string
whitespace: true, // Various whitespace chars
quotes: true, // Combinations of quotes
backslashing: true, // Combinations of backslashes
symbols: true, // Various symbols
foreign: true, // Foreign chars
alphanumeric: true, // Ordinary letters and numbers
} ){
2018-02-23 12:13:35 -07:00
let chars = []
let tests = []
2018-02-19 23:23:24 -07:00
// Whitespace characters
2018-02-23 12:13:35 -07:00
if (opt.whitespace!==false) chars = chars.concat([
2018-02-19 23:23:24 -07:00
' ', // Space
' ', // Tab
'\n', // Newline
'\r', // Return
'\r\n', // Carrage return
])
// Quotation characters
2018-02-23 12:13:35 -07:00
if (opt.quotes!==false) chars = chars.concat([
2018-02-19 23:23:24 -07:00
'\'', '\'\'', '\'\'\'', // Single quotes
'"', '""', '"""', // Double quotes
'`', '``', '```', // Backticks
])
// Backslashes
2018-02-23 12:13:35 -07:00
if (opt.backslashing!==false) chars = chars.concat([
2018-02-19 23:23:24 -07:00
'\\', '\\\\',
])
// Symbols
2018-02-23 12:13:35 -07:00
if (opt.symbols!==false) chars = chars.concat(
'°~!@#$%€^&*()-_─=+[]{}|;:,./<>?¿¹²³¼½¬€¶←↓→»«¢„“”·…–'.split('')
2018-02-19 23:23:24 -07:00
)
// Foreign characters
2018-02-23 12:13:35 -07:00
if (opt.foreign!==false) chars = chars.concat(
'ŧłßöäüñáóíúýéâêîôûŷàèìòùảẻỉỏỷÿïøþłĸŋđðſæµёйцукенгшщзхъэждлорпавыфячсмитьбюЁЙЦУКЕНГШЩЗХЪЭЖДЛОРПАВЫФЯЧСМИТЬБЮ'.split('')
2018-02-19 23:23:24 -07:00
)
// Ordinary letters and numbers
2018-02-23 12:13:35 -07:00
if (opt.alphanumeric!==false) chars = chars.concat(
2018-02-23 15:29:33 -07:00
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('')
2018-02-19 23:23:24 -07:00
)
2018-02-23 15:29:33 -07:00
// Set minimum string length
2018-02-23 12:13:35 -07:00
const min = (opt.none!==false)? 0 : 1
2018-02-19 23:23:24 -07:00
// Add tests until we have enough tests
2018-02-23 15:29:33 -07:00
while (tests.length<num) {
2018-02-19 23:23:24 -07:00
// Pick a random number from min to max
2018-02-23 12:13:35 -07:00
const len = Math.floor(Math.random() * (max - min + 1)) + min
2018-02-23 15:29:33 -07:00
2018-02-19 23:23:24 -07:00
// Create a string of that length
let s = ''
2018-02-23 15:29:33 -07:00
while (s.length<len) {
2018-02-19 23:23:24 -07:00
s += chars[Math.floor(Math.random()*chars.length)]
}
2018-02-23 15:29:33 -07:00
// Make sure we didn't go over the max length
// (some chars have multiple characters)
while (s.length>len) s = s.substring(1)
2018-02-23 12:13:35 -07:00
// Add that string to the tests if not already
if (!tests.includes(s)) tests.push(s)
2018-02-23 15:29:33 -07:00
2018-02-19 23:23:24 -07:00
}
return tests
}