From 1259b55154bb53d915011137f2fa5ab1417a71b1 Mon Sep 17 00:00:00 2001 From: Keith Irwin Date: Fri, 23 Feb 2018 22:29:33 +0000 Subject: [PATCH] Added tests --- README.md | 6 ++++ index.js | 15 +++++++-- package.json | 8 +++-- test.js | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 test.js diff --git a/README.md b/README.md index 493bab4..5a29b2d 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,9 @@ Same as above, but without foreign characters: console.log( froth(5,10,{foreign:false}) ) // [ 'VP"""t¬mK²', '²L6)>\r\nV', 'v,→“', '*e8', '→' ] ``` + +## Testing + +```sh +npm test +``` diff --git a/index.js b/index.js index 3aaa5a4..ac797c2 100644 --- a/index.js +++ b/index.js @@ -54,22 +54,31 @@ module.exports = function(num=10, max=20, opt={ // Ordinary letters and numbers if (opt.alphanumeric!==false) chars = chars.concat( - 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split(''), + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('') ) + // Set minimum string length const min = (opt.none!==false)? 0 : 1 // Add tests until we have enough tests - for (let n=0; nlen) s = s.substring(1) + // Add that string to the tests if not already if (!tests.includes(s)) tests.push(s) + } return tests diff --git a/package.json b/package.json index 62ef3ab..20bcd71 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Fuzzer for mocha testing", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha --reporter spec" }, "keywords": [ "fuzz", @@ -18,5 +18,9 @@ "url": "https://github.com/keith24/mocha-froth" }, "author": "Keith Irwin (https://keithirwin.us)", - "license": "GPL-3.0" + "license": "GPL-3.0", + "devDependencies": { + "chai": "^4.1.2", + "mocha": "^5.0.1" + } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..b7083ce --- /dev/null +++ b/test.js @@ -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('') + ) + }) + +})