From 6d15afd8e378d584a682e1546af4d30da220515a Mon Sep 17 00:00:00 2001 From: Niclas Thobaben Date: Wed, 7 Jul 2021 20:54:56 +0200 Subject: [PATCH] added tests for linter module --- src/linter.js | 23 ++++++++---- tests/{cmd.test.js => cmd.gen.test.js} | 0 tests/lint.test.js | 49 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 7 deletions(-) rename tests/{cmd.test.js => cmd.gen.test.js} (100%) create mode 100644 tests/lint.test.js diff --git a/src/linter.js b/src/linter.js index 4a0053f..db91004 100644 --- a/src/linter.js +++ b/src/linter.js @@ -5,25 +5,28 @@ const DESCRIPTION_REGEX = /^[A-Z]{1}.+\.$/ function required() { return (path, input, msgs) => { + input = input && input.trim() if(!input) { msgs.push(`Property '${path}' must be set! value=${input}`) } } } -function length(min, max=-1) { +function length(opts) { return (path, input, msgs) => { - if(min > 0 && input.length < min) { - msgs.push(`Property '${path}' must have at least ${min} characters! actual=${input.length}`) + input = input && input.trim() || '' + if(opts.min && input.length < opts.min) { + msgs.push(`Property '${path}' must have at least ${opts.min} characters! actual=${input.length}`) } - if(max > 0 && input.length > max) { - msgs.push(`Property '${path}' must not have more than ${max} characters! actual=${input.length}`) + if(opts.max && input.length > opts.max) { + msgs.push(`Property '${path}' must not have more than ${opts.max} characters! actual=${input.length}`) } } } function regex(regex) { return (path, input, msgs) => { + input = input && input.trim() if(!input.match(regex)) { msgs.push(`Property '${path}' must only comply to pattern ${regex}! value=${input}`) } @@ -32,8 +35,8 @@ function regex(regex) { const linterMappings = [ - { path: 'name', linters: [ required(), length(4), regex(NAME_REGEX) ] }, - { path: 'description', linters: [ required(), length(4), regex(DESCRIPTION_REGEX) ] } + { path: 'name', linters: [ required(), length({ min: 4 }), regex(NAME_REGEX) ] }, + { path: 'description', linters: [ required(), length({ min: 4 }), regex(DESCRIPTION_REGEX) ] } ] @@ -51,4 +54,10 @@ module.exports.lint = function(spec) { module.exports.regex = { NAME_REGEX, DESCRIPTION_REGEX +} + +module.exports.validators = { + required, + length, + regex, } \ No newline at end of file diff --git a/tests/cmd.test.js b/tests/cmd.gen.test.js similarity index 100% rename from tests/cmd.test.js rename to tests/cmd.gen.test.js diff --git a/tests/lint.test.js b/tests/lint.test.js new file mode 100644 index 0000000..c77ace6 --- /dev/null +++ b/tests/lint.test.js @@ -0,0 +1,49 @@ +const assert = require('assert').strict +const logging = require('../src/logging') +const expect = require('chai').expect +const linter = require('../src/linter') + +logging.init(-1) + +describe('linter validators', () => { + it('creates an error on required property', () => { + let msg = [] + linter.validators.required()('path', '', msg) + linter.validators.required()('path', null, msg) + linter.validators.required()('path', undefined, msg) + linter.validators.required()('path', ' ', msg) + + expect(msg).to.not.be.empty + expect(msg.length).to.equal(4) + }) + it('creates an error on to small strings for length validator', () => { + let msg = [] + linter.validators.length({ min: 4 })('path', '', msg) + linter.validators.length({ min: 4 })('path', null, msg) + linter.validators.length({ min: 4 })('path', undefined, msg) + linter.validators.length({ min: 4 })('path', '123', msg) + + expect(msg).to.not.be.empty + expect(msg.length).to.equal(4) + }) + it('creates an error on too large strings for length validator', () => { + let msg = [] + linter.validators.length({ max: 4 })('path', '', msg) + linter.validators.length({ max: 4 })('path', null, msg) + linter.validators.length({ max: 4 })('path', undefined, msg) + linter.validators.length({ min: 4 })('path', '12345', msg) + linter.validators.length({ min: 4 })('path', ' 1 ', msg) + + expect(msg).to.not.be.empty + expect(msg.length).to.equal(1) + }) + it('creates an error when string does not match regex', () => { + let msg = [] + linter.validators.regex(/[0-9]+/)('path', 'abcdef', msg) + linter.validators.regex(/[0-9]+/)('path', ' ', msg) + linter.validators.regex(/.+/)('path', ' ', msg) + + expect(msg).to.not.be.empty + expect(msg.length).to.equal(3) + }) +}) \ No newline at end of file