Added tests

master
Keith Irwin 2018-02-23 22:29:33 +00:00
parent ba339c587a
commit 1259b55154
No known key found for this signature in database
GPG Key ID: 378933C743E2BBC0
4 changed files with 117 additions and 5 deletions

View File

@ -59,3 +59,9 @@ Same as above, but without foreign characters:
console.log( froth(5,10,{foreign:false}) ) console.log( froth(5,10,{foreign:false}) )
// [ 'VP"""t¬mK²', '²L6)>\r\nV', 'v,→“', '*e8', '→' ] // [ 'VP"""t¬mK²', '²L6)>\r\nV', 'v,→“', '*e8', '→' ]
``` ```
## Testing
```sh
npm test
```

View File

@ -54,22 +54,31 @@ module.exports = function(num=10, max=20, opt={
// Ordinary letters and numbers // Ordinary letters and numbers
if (opt.alphanumeric!==false) chars = chars.concat( if (opt.alphanumeric!==false) chars = chars.concat(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split(''), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('')
) )
// Set minimum string length
const min = (opt.none!==false)? 0 : 1 const min = (opt.none!==false)? 0 : 1
// Add tests until we have enough tests // Add tests until we have enough tests
for (let n=0; n<num; n++) { while (tests.length<num) {
// Pick a random number from min to max // Pick a random number from min to max
const len = Math.floor(Math.random() * (max - min + 1)) + min const len = Math.floor(Math.random() * (max - min + 1)) + min
// Create a string of that length // Create a string of that length
let s = '' let s = ''
for (let l=0; l<len; l++) { while (s.length<len) {
s += chars[Math.floor(Math.random()*chars.length)] s += chars[Math.floor(Math.random()*chars.length)]
} }
// Make sure we didn't go over the max length
// (some chars have multiple characters)
while (s.length>len) s = s.substring(1)
// Add that string to the tests if not already // Add that string to the tests if not already
if (!tests.includes(s)) tests.push(s) if (!tests.includes(s)) tests.push(s)
} }
return tests return tests

View File

@ -4,7 +4,7 @@
"description": "Fuzzer for mocha testing", "description": "Fuzzer for mocha testing",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "mocha --reporter spec"
}, },
"keywords": [ "keywords": [
"fuzz", "fuzz",
@ -18,5 +18,9 @@
"url": "https://github.com/keith24/mocha-froth" "url": "https://github.com/keith24/mocha-froth"
}, },
"author": "Keith Irwin <mocha-froth@keithirwin.us> (https://keithirwin.us)", "author": "Keith Irwin <mocha-froth@keithirwin.us> (https://keithirwin.us)",
"license": "GPL-3.0" "license": "GPL-3.0",
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.0.1"
}
} }

93
test.js Normal file
View File

@ -0,0 +1,93 @@
'use strict'
const expect = require('chai').expect
const froth = require('./index')
describe('Default settings', () => {
it('should return a 10-item array of strings with 20 characters or less by default', () => {
const output = froth()
expect(output).to.be.an('array').and.to.have.lengthOf(10)
output.forEach( (item) => {
expect(item).to.be.a('string').and.not.to.have.lengthOf.above(20)
})
})
})
describe('Changing number of strings and maximum length', () => {
it('should return a different number of strings', () => {
// Generate a random number from 1 to 50
const test_num = Math.floor(Math.random()*51)
const output = froth(test_num)
expect(output).to.be.an('array').and.to.have.lengthOf(test_num)
output.forEach( (item) => {
expect(item).to.be.a('string').and.not.to.have.lengthOf.above(20)
})
})
it('should return strings with a different maximum length', () => {
// Generate a random number from 1 to 50
const test_max = Math.floor(Math.random()*51)
const output = froth(undefined, test_max)
expect(output).to.be.an('array').and.to.have.lengthOf(10)
output.forEach( (item) => {
expect(item).to.be.a('string').and.not.to.have.lengthOf.above(test_max)
})
})
it('should return a different number of strings with a different maximum length', () => {
// Generate two random number from 1 to 50
const test_num = Math.floor(Math.random()*51)
const test_max = Math.floor(Math.random()*51)
const output = froth(test_num, test_max)
expect(output).to.be.an('array').and.to.have.lengthOf(test_num)
output.forEach( (item) => {
expect(item).to.be.a('string').and.not.to.have.lengthOf.above(test_max)
})
})
})
describe('Changing options', () => {
it('should not return blank strings when \'none=false\' is specified', () => {
const output = froth(100, 100, {none:false})
expect(output).to.be.an('array').and.to.not.contain('')
})
it('should not return whitespaces when \'whitespace=false\' is specified', () => {
const output = froth(100, 100, {whitespace:false})
expect(output).to.be.an('array').and.to.not.contain(' ',' ','\n','\r\n','\r')
})
it('should not return quotations when \'quotes=false\' is specified', () => {
const output = froth(100, 100, {quotes:false})
expect(output).to.be.an('array').and.to.not.contain(
'\'', '\'\'', '\'\'\'', '"', '""', '"""', '`', '``', '```'
)
})
it('should not return symbols when \'symbols=false\' is specified', () => {
const output = froth(100, 100, {quotes:false})
expect(output).to.be.an('array').and.to.not.contain(
...'°~!@#$%€^&*()-_─=+[]{}|;:,./<>?¿¹²³¼½¬€¶←↓→»«¢„“”·…–'.split('')
)
})
it('should not return foreign characters when \'foreign=false\' is specified', () => {
const output = froth(100, 100, {quotes:false})
expect(output).to.be.an('array').and.to.not.contain(
...'ŧłßöäüñáóíúýéâêîôûŷàèìòùảẻỉỏỷÿïøþłĸŋđðſæµёйцукенгшщзхъэждлорпавыфячсмитьбюЁЙЦУКЕНГШЩЗХЪЭЖДЛОРПАВЫФЯЧСМИТЬБЮ'.split('')
)
})
it('should not return numbers or letters when \'alphanumeric=false\' is specified', () => {
const output = froth(100, 100, {quotes:false})
expect(output).to.be.an('array').and.to.not.contain(
...'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('')
)
})
})