diff --git a/package.json b/package.json index f8421799f..9a2feba45 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "chai": "^3.4.1", "clone": "^1.0.0", "coffee-script": "^1.9.1", - "dredd-transactions": "0.0.4", + "dredd-transactions": "^0.0.5", "file": "~0.2.2", "gavel": "0.5.2", "glob": "^6.0.1", diff --git a/scripts/test-hooks-handlers.coffee b/scripts/test-hooks-handlers.coffee index ccf67b234..9cc62990d 100644 --- a/scripts/test-hooks-handlers.coffee +++ b/scripts/test-hooks-handlers.coffee @@ -59,7 +59,7 @@ yaml = require 'js-yaml' unless process.env.CI - console.error ''' \ + console.error '''\ This script is meant to be ran on Travis CI. It is not optimized (yet) for local usage. It could mess up your Git repository. ''' diff --git a/src/expand-uri-template-with-parameters.coffee b/src/expand-uri-template-with-parameters.coffee deleted file mode 100644 index 6795c180f..000000000 --- a/src/expand-uri-template-with-parameters.coffee +++ /dev/null @@ -1,66 +0,0 @@ -ut = require 'uri-template' - -expandUriTemplateWithParameters = (uriTemplate, parameters) -> - result = - errors: [] - warnings: [] - uri: null - try - parsed = ut.parse uriTemplate - catch e - text = "\n Failed to parse URI template: #{uriTemplate}" - text += "\n Error: #{e}" - result['errors'].push text - return result - - # get parameters from expression object - uriParameters = [] - for expression in parsed['expressions'] - for param in expression['params'] - uriParameters.push param['name'] - - # check if all parameters from blueprint have an expression in URI - for parameter in Object.keys(parameters) - if uriParameters.indexOf(parameter) == -1 - text = "\nURI template: #{uriTemplate}\nDoesn\'t contain expression for parameter" + \ - " '" + parameter + "'" - result['warnings'].push text - - if parsed['expressions'].length == 0 - result['uri'] = uriTemplate - else - ambigous = false - - for uriParameter in uriParameters - if Object.keys(parameters).indexOf(uriParameter) == -1 - ambigous = true - text = "\nAmbiguous URI parameter in template: #{uriTemplate} " + \ - "\nParameter not defined in blueprint:" + \ - "'" + uriParameter + "'" - result['warnings'].push text - - if ambigous == false - toExpand = {} - for uriParameter in uriParameters - param = parameters[uriParameter] - if param['required'] == true - if param['example'] == undefined or param['example'] == "" - ambigous = true - text = "\nAmbiguous URI parameter in template: #{uriTemplate} " + \ - "\nNo example value for required parameter in blueprint:" + \ - "'" + uriParameter + "'" - result['warnings'].push text - else - toExpand[uriParameter] = param['example'] - else - if param['example'] != undefined and param['example'] != "" - toExpand[uriParameter] = param['example'] - else if param['default'] != undefined and param['default'] != "" - toExpand[uriParameter] = param['default'] - - if ambigous == false - result['uri'] = parsed.expand toExpand - - return result - -module.exports = expandUriTemplateWithParameters diff --git a/src/handle-runtime-problems.coffee b/src/handle-runtime-problems.coffee index 9a6b7baf0..ff73fb0e2 100644 --- a/src/handle-runtime-problems.coffee +++ b/src/handle-runtime-problems.coffee @@ -19,7 +19,7 @@ handleRuntimeProblems = (blueprintData) -> if annotation.component is 'apiDescriptionParser' ranges = blueprintUtils.warningLocationToRanges(annotation.location, apiDescriptionDocument) - message = """ \ + message = """\ Parser #{annotation.type} in file '#{filename}': \ (#{annotation.type} code #{annotation.code}) #{annotation.message} \ """ @@ -32,12 +32,11 @@ handleRuntimeProblems = (blueprintData) -> annotation.origin.actionName ].join(' > ') - log(""" \ + log("""\ Compilation #{annotation.type} in file '#{filename}': \ - #{annotation.message} on #{transactionName} \ + #{annotation.message} (#{transactionName}) \ """) - return new Error('Error when processing API description.') if error diff --git a/src/hooks-worker-client.coffee b/src/hooks-worker-client.coffee index 3cfdba404..7fa0a09e8 100644 --- a/src/hooks-worker-client.coffee +++ b/src/hooks-worker-client.coffee @@ -81,7 +81,7 @@ class HooksWorkerClient if @language == 'ruby' @handlerCommand = 'dredd-hooks-ruby' unless which.which @handlerCommand - msg = """ \ + msg = """\ Ruby hooks handler server command not found: #{@handlerCommand} Install ruby hooks handler by running: $ gem install dredd_hooks @@ -93,7 +93,7 @@ class HooksWorkerClient else if @language == 'python' @handlerCommand = 'dredd-hooks-python' unless which.which @handlerCommand - msg = """ \ + msg = """\ Python hooks handler server command not found: #{@handlerCommand} Install python hooks handler by running: $ pip install dredd_hooks @@ -105,7 +105,7 @@ class HooksWorkerClient else if @language == 'php' @handlerCommand = 'dredd-hooks-php' unless which.which @handlerCommand - msg = """ \ + msg = """\ PHP hooks handler server command not found: #{@handlerCommand} Install php hooks handler by running: $ composer require ddelnano/dredd-hooks-php --dev @@ -117,7 +117,7 @@ class HooksWorkerClient else if @language == 'perl' @handlerCommand = 'dredd-hooks-perl' unless which.which @handlerCommand - msg = """ \ + msg = """\ Perl hooks handler server command not found: #{@handlerCommand} Install perl hooks handler by running: $ cpanm Dredd::Hooks @@ -127,7 +127,7 @@ class HooksWorkerClient callback() else if @language == 'nodejs' - msg = ''' \ + msg = '''\ Hooks handler should not be used for nodejs. \ Use Dredds' native node hooks instead. ''' diff --git a/src/inherit-parameters.coffee b/src/inherit-parameters.coffee deleted file mode 100644 index 0bc9e8e5a..000000000 --- a/src/inherit-parameters.coffee +++ /dev/null @@ -1,8 +0,0 @@ -inheritParameters = (actualParameters, inheritingParameters) -> - for name, params of inheritingParameters - if actualParameters[name] == undefined - actualParameters[name] = params - - return actualParameters - -module.exports = inheritParameters diff --git a/src/validate-parameters.coffee b/src/validate-parameters.coffee deleted file mode 100644 index 39bd8b10d..000000000 --- a/src/validate-parameters.coffee +++ /dev/null @@ -1,31 +0,0 @@ -validateParameters = (params) -> - result = { - warnings: [] - errors: [] - } - - for paramName, param of params - if param['required'] == true and (param['example'] == '' or param['example'] == undefined) - text = "Required URI parameter '#{paramName}' has no example value." - result['errors'].push text - - switch param['type'] - when 'number' - if isNaN(parseFloat(param['example'])) - text = "URI parameter '#{paramName}' is declared as 'number' but it is a string." - result['errors'].push text - when 'boolean' - if param['example'] != 'true' and param['example'] != 'false' - text = "URI parameter '#{paramName}' is declared as 'boolean' but it is not. " - result['errors'].push text - - if param['values'].length > 0 - values = param['values'].map (value) -> value['value'] - unless values.indexOf(param['example']) > -1 - text = "URI parameter '#{paramName}' example value is not one of enum values." - result['errors'].push text - - return result - -module.exports = validateParameters - diff --git a/test/fixtures/apiary.apib b/test/fixtures/apiary.apib index efc26abcd..bde3ddcf4 100644 --- a/test/fixtures/apiary.apib +++ b/test/fixtures/apiary.apib @@ -1,4 +1,4 @@ -FORMAT: X-1A +FORMAT: 1A # Machines API diff --git a/test/fixtures/warning-ambigous.apib b/test/fixtures/warning-ambigous.apib deleted file mode 100644 index 43ec755ea..000000000 --- a/test/fixtures/warning-ambigous.apib +++ /dev/null @@ -1,40 +0,0 @@ -FORMAT: X-1A - -# Machines API -# Group Machines - -# Machine [/machines/{name}] - - -## Update a Machine [PUT] - -+ Request (application/json) - - { - "name": "waldo" - } - -+ Response 200 (application/json) - - { - "type": "bulldozer", - "name": "waldo", - "_id": "5229c6e8e4b0bd7dbb07e29c" - } - -## Retrieve a Machine [GET] -+ Parameters - + name (required,`waldo`) - -+ Response 200 (text/plain) - - { - "type": "bulldozer", - "name": "waldo", - "_id": "5229c6e8e4b0bd7dbb07e29c" - } - -## Delete Message [DELETE] -+ Parameters - + name (required,`waldo`) - diff --git a/test/fixtures/warning-ambiguous.apib b/test/fixtures/warning-ambiguous.apib new file mode 100644 index 000000000..b4f92c143 --- /dev/null +++ b/test/fixtures/warning-ambiguous.apib @@ -0,0 +1,38 @@ +FORMAT: 1A + +# Machines API +# Group Machines +# Machine [/machines/{name}] +## Retrieve Single Machine [GET] + ++ Response 200 (application/json; charset=utf-8) + + { + "type": "bulldozer", + "name": "willy" + } + +# Machines [/machines/{?sort}] +## Retrieve All Machines [GET] + ++ Response 200 (application/json; charset=utf-8) + + [{ + "type": "bulldozer", + "name": "willy" + }] + +# Machine Types [/machine-types/] +## Create a New Machine Type [POST] + ++ Request (application/json; charset=utf-8) + + { + "name": "bulldozer" + } + ++ Response 200 (application/json; charset=utf-8) + + [{ + "name": "bulldozer" + }] diff --git a/test/integration/cli/api-description-cli-test.coffee b/test/integration/cli/api-description-cli-test.coffee index fc6a20d1a..5aae4345f 100644 --- a/test/integration/cli/api-description-cli-test.coffee +++ b/test/integration/cli/api-description-cli-test.coffee @@ -29,6 +29,9 @@ describe 'CLI - API Description Document', -> app.get '/machines/caterpillar', (req, res) -> res.send {type: 'bulldozer', name: 'caterpillar'} + app.post '/machine-types', (req, res) -> + res.send [{name: 'bulldozer'}] + beforeEach (done) -> startServer configureServer, PORT, (err, serverInfo) -> server = serverInfo @@ -102,6 +105,40 @@ describe 'CLI - API Description Document', -> it 'should print error message to stderr', -> assert.include dreddCommand.stderr, 'Error when reading file' + describe 'When API description is loaded with errors', -> + dreddCommand = undefined + args = [ + './test/fixtures/error-blueprint.apib' + "http://localhost:#{PORT}" + ] + + beforeEach (done) -> + execDredd args, (err, commandInfo) -> + dreddCommand = commandInfo + done(err) + + it 'should exit with status 1', -> + assert.equal dreddCommand.exitStatus, 1 + it 'should print error message to stderr', -> + assert.include dreddCommand.stderr, 'Error when processing API description' + + describe 'When API description is loaded with warnings', -> + dreddCommand = undefined + args = [ + './test/fixtures/warning-ambiguous.apib' + "http://localhost:#{PORT}" + ] + + beforeEach (done) -> + execDredd args, (err, commandInfo) -> + dreddCommand = commandInfo + done(err) + + it 'should exit with status 0', -> + assert.equal dreddCommand.exitStatus, 0 + it 'should print warning to stdout', -> + assert.include dreddCommand.stdout, 'warn: Compilation warning' + describe 'When loaded from URL', -> diff --git a/test/unit/dredd-test.coffee b/test/unit/dredd-test.coffee index 30a708695..593d3423a 100644 --- a/test/unit/dredd-test.coffee +++ b/test/unit/dredd-test.coffee @@ -19,21 +19,21 @@ Dredd = proxyquire '../../src/dredd', { './logger': loggerStub } -describe 'Dredd class', () -> +describe 'Dredd class', -> configuration = {} dredd = {} - beforeEach () -> + beforeEach -> sinon.spy ProtagonistStub, 'parse' sinon.spy fsStub, 'readFile' - afterEach () -> + afterEach -> ProtagonistStub.parse.restore() fsStub.readFile.restore() - describe 'with legacy configuration', () -> - before () -> + describe 'with legacy configuration', -> + before -> configuration = server: 'http://localhost:3000/' blueprintPath: './test/fixtures/apiary.apib' @@ -46,7 +46,7 @@ describe 'Dredd class', () -> it 'should not explode and run executeTransaction', (done) -> - fn = () -> + fn = -> dredd = new Dredd(configuration) sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() @@ -57,8 +57,8 @@ describe 'Dredd class', () -> assert.doesNotThrow fn - describe 'with valid configuration', () -> - before () -> + describe 'with valid configuration', -> + before -> configuration = server: 'http://localhost:3000/' options: @@ -69,7 +69,7 @@ describe 'Dredd class', () -> sorted: true path: ['./test/fixtures/apiary.apib'] - it 'should copy configuration on creation', () -> + it 'should copy configuration on creation', -> dredd = new Dredd(configuration) assert.ok(dredd.configuration.options.silent) assert.notOk(dredd.configuration.options['dry-run']) @@ -120,8 +120,8 @@ describe 'Dredd class', () -> dredd.runner.executeTransaction.restore() done() - describe 'when paths specified with glob paterns', () -> - before () -> + describe 'when paths specified with glob paterns', -> + before -> configuration = server: 'http://localhost:3000/' options: @@ -129,11 +129,11 @@ describe 'Dredd class', () -> path: ['./test/fixtures/multifile/*.apib', './test/fixtures/multifile/*.apib' ,'./test/fixtures/multifile/*.balony'] dredd = new Dredd(configuration) - beforeEach () -> + beforeEach -> sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.restore() it 'should expand all glob patterns and resolved paths should be unique', (done) -> @@ -170,8 +170,8 @@ describe 'Dredd class', () -> done() - describe 'when glob pattern does not match any files', () -> - before () -> + describe 'when glob pattern does not match any files', -> + before -> configuration = server: 'http://localhost:3000/' options: @@ -179,11 +179,11 @@ describe 'Dredd class', () -> path: ['./test/fixtures/multifile/*.balony'] dredd = new Dredd(configuration) - beforeEach () -> + beforeEach -> sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.restore() it 'should return error', (done) -> @@ -192,7 +192,7 @@ describe 'Dredd class', () -> done() - describe 'when configuration contains data object with "filename" as key, and an API Blueprint string as value', () -> + describe 'when configuration contains data object with "filename" as key, and an API Blueprint string as value', -> beforeEach -> configuration = server: 'http://localhost:3000/' @@ -221,7 +221,7 @@ describe 'Dredd class', () -> sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.restore() it 'should not expand any glob patterns', (done) -> @@ -279,7 +279,7 @@ describe 'Dredd class', () -> done() - describe 'when paths are specified as a mix of URLs and a glob path', () -> + describe 'when paths are specified as a mix of URLs and a glob path', -> blueprintCode = null before (done) -> configuration = @@ -292,11 +292,11 @@ describe 'Dredd class', () -> blueprintCode = content.toString() done err - beforeEach () -> + beforeEach -> sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.restore() describe 'when all URLs can be downloaded', -> @@ -379,7 +379,7 @@ describe 'Dredd class', () -> done() it 'should not execute any transaction', (done) -> - dredd.run () -> + dredd.run -> assert.notOk dredd.runner.executeTransaction.called done() @@ -403,7 +403,7 @@ describe 'Dredd class', () -> done() it 'should not execute any transaction', (done) -> - dredd.run () -> + dredd.run -> assert.notOk dredd.runner.executeTransaction.called done() @@ -416,11 +416,11 @@ describe 'Dredd class', () -> path: ['./test/fixtures/error-blueprint.apib'] dredd = new Dredd(configuration) - beforeEach () -> + beforeEach -> sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.restore() it 'should exit with an error', (done) -> @@ -429,7 +429,7 @@ describe 'Dredd class', () -> done() it 'should NOT execute any transaction', (done) -> - dredd.run () -> + dredd.run -> assert.notOk dredd.runner.executeTransaction.called done() @@ -439,25 +439,25 @@ describe 'Dredd class', () -> url: 'http://localhost:3000/' options: silent: true - path: ['./test/fixtures/warning-ambigous.apib'] + path: ['./test/fixtures/warning-ambiguous.apib'] dredd = new Dredd(configuration) - beforeEach () -> + beforeEach -> sinon.stub dredd.runner, 'run', (transaction, callback) -> callback() sinon.spy loggerStub, 'warn' - afterEach () -> + afterEach -> dredd.runner.run.restore() loggerStub.warn.restore() it 'should execute the runtime', (done) -> - dredd.run () -> + dredd.run -> assert.ok dredd.runner.run.called done() it 'should write warnings to warn logger', (done) -> - dredd.run () -> + dredd.run -> assert.ok loggerStub.warn.called done() @@ -472,7 +472,7 @@ describe 'Dredd class', () -> sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.reset() it 'should pass the error to the callback function', (done) -> @@ -485,8 +485,8 @@ describe 'Dredd class', () -> assert.notOk dredd.runner.executeTransaction.called done() - describe 'when runtime contains any error', () -> - beforeEach () -> + describe 'when runtime contains any error', -> + beforeEach -> configuration = server: 'http://localhost:3000/' options: @@ -497,7 +497,7 @@ describe 'Dredd class', () -> sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.reset() it 'should NOT execute any transaction', (done) -> @@ -516,13 +516,13 @@ describe 'Dredd class', () -> server: 'http://localhost:3000/' options: silent: true - path: ['./test/fixtures/warning-ambigous.apib'] + path: ['./test/fixtures/warning-ambiguous.apib'] sinon.spy loggerStub, 'warn' dredd = new Dredd(configuration) sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.reset() loggerStub.warn.restore() @@ -541,18 +541,18 @@ describe 'Dredd class', () -> assert.notOk error done() - describe 'when runtime is without errors and warnings', () -> - beforeEach () -> + describe 'when runtime is without errors and warnings', -> + beforeEach -> configuration = server: 'http://localhost:3000/' options: silent: true - path: ['./test/fixtures/warning-ambigous.apib'] + path: ['./test/fixtures/apiary.apib'] dredd = new Dredd(configuration) sinon.stub dredd.runner, 'executeTransaction', (transaction, hooks, callback) -> callback() - afterEach () -> + afterEach -> dredd.runner.executeTransaction.reset() it 'should execute the runtime', (done) -> @@ -560,9 +560,9 @@ describe 'Dredd class', () -> assert.ok dredd.runner.executeTransaction.called done() - describe "#emitStart", () -> + describe "#emitStart", -> - describe 'no error in reporter occurs', () -> + describe 'no error in reporter occurs', -> PORT = 9876 dredd = null apiaryServer = null @@ -600,7 +600,7 @@ describe 'Dredd class', () -> done() afterEach (done) -> - apiaryServer.close () -> + apiaryServer.close -> done() @@ -612,12 +612,12 @@ describe 'Dredd class', () -> dredd.emitStart callback - describe 'an error in the apiary reporter occurs', () -> + describe 'an error in the apiary reporter occurs', -> PORT = 9876 dredd = null apiaryServer = null - beforeEach () -> + beforeEach -> configuration = server: 'http://localhost:3000/' options: diff --git a/test/unit/expand-uri-template-with-parameters-test.coffee b/test/unit/expand-uri-template-with-parameters-test.coffee deleted file mode 100644 index 9ec834dea..000000000 --- a/test/unit/expand-uri-template-with-parameters-test.coffee +++ /dev/null @@ -1,336 +0,0 @@ -{assert} = require 'chai' - -expandUriTemplateWithParameters = require '../../src/expand-uri-template-with-parameters' - -describe 'expandUriTemplateWithParameters', () -> - data = null - uriTemplate = '' - parameters = '' - - before () -> - uriTemplate = '/machines{/name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'waldo' - default: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return an object', () -> - assert.isObject data - - describe 'returned obejct', () -> - [ - 'errors' - 'warnings' - 'uri' - ].forEach (key) -> - it 'should have key "' + key + '"', () -> - assert.include Object.keys(data), key - - describe 'when not parseable uri templeate privided', () -> - before () -> - uriTemplate = '/machines{{/name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'waldo' - default: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'it should return some errror', () -> - assert.notEqual data['errors'].length, 0 - - - describe 'when URI with no URI template expression given', () -> - before () -> - uriTemplate = '/machines/waldo' - parameters = {} - data = expandUriTemplateWithParameters uriTemplate, parameters - - describe 'with no parameters given', () -> - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return no warning', () -> - assert.equal data['warnings'].length, 0 - - it 'should return URI as it is', () -> - assert.equal data['uri'], uriTemplate - - - describe 'with some parameters given', () -> - before () -> - uriTemplate = '/machines/waldo' - parameters = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'waldo' - default: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return some warning', () -> - assert.notEqual data['warnings'].length, 0 - - describe 'returned warning', () -> - warning = '' - before () -> - warning = data['warnings'][data['warnings'].length - 1] - - it 'should contain paramter name', () -> - assert.include warning, Object.keys(parameters)[0] - - it 'sohuld contain proper text', () -> - text = 'Doesn\'t contain expression for parameter' - assert.include warning, text - - it 'should return URI as it is', () -> - assert.equal data['uri'], uriTemplate - - - describe 'when UriTemplate with some URI template expression given', () -> - describe 'when no matching parameters provided', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = {} - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return some warning', () -> - assert.notEqual data['warnings'].length, 0 - - describe 'returned warning', () -> - warning = '' - before () -> - warning = data['warnings'][data['warnings'].length - 1] - - it 'sohuld contain proper text', () -> - text = "Parameter not defined" - assert.include warning, text - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return no URI', () -> - assert.equal data['uri'], null - - describe 'with defined some parameters not matching any expression', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'waldo' - default: '' - fanny: - required: false - description: 'Machine fanny' - type: 'string' - required: true - example: 'wild' - default: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return some warning', () -> - assert.equal data['warnings'].length, 1 - - it 'should return expandend URI', () -> - assert.equal data['uri'], '/machines/waldo' - - describe 'when expression parameter is required', () -> - describe 'when example is not given', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: true - example: '' - default: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return some warning', () -> - assert.equal data['warnings'].length, 1 - - it 'should return no URI', () -> - assert.isNull data['uri'] - - describe 'returned warning', () -> - warning = '' - before () -> - warning = data['warnings'][data['warnings'].length - 1] - - it 'sohuld contain proper text', () -> - text = "No example value for required parameter" - assert.include warning, text - - describe 'when example value is given', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'example-one' - default: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return no warning', () -> - assert.equal data['warnings'].length, 0 - - it 'should use example value to URI parameter expansion', () -> - assert.include data['uri'], parameters['name']['example'] - - it 'should return URI', () -> - assert.isNotNull data['uri'] - - describe 'when example and default value is given', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'example-one' - default: 'default-one' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return no warning', () -> - assert.equal data['warnings'].length, 0 - - it 'should use example value to URI parameter expansion', () -> - assert.include data['uri'], parameters['name']['example'] - - it 'should return URI', () -> - assert.isNotNull data['uri'] - - describe 'when expression parameter is optional', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: false - example: 'example-one' - default: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return no warning', () -> - assert.equal data['warnings'].length, 0 - - it 'should use example value to URI parameter expansion', () -> - assert.include data['uri'], parameters['name']['example'] - - it 'should return URI', () -> - assert.isNotNull data['uri'] - - describe 'when default value is given and example is empty', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: false - default: 'default-one' - example: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return no warning', () -> - assert.equal data['warnings'].length, 0 - - it 'should use default value to URI parameter expansion', () -> - assert.include data['uri'], parameters['name']['default'] - - it 'should return URI', () -> - assert.isNotNull data['uri'] - - describe 'when example and default value is given', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: false - example: 'example-one' - default: 'default-one' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return no warning', () -> - assert.equal data['warnings'].length, 0 - - it 'should use example value to URI parameter expansion', () -> - assert.include data['uri'], parameters['name']['example'] - - it 'should return some URI', () -> - assert.isNotNull data['uri'] - - - describe 'when example and default value is not given', () -> - before () -> - uriTemplate = '/machines/{name}' - parameters = - name: - description: 'Machine name' - type: 'string' - required: false - default: '' - example: '' - - data = expandUriTemplateWithParameters uriTemplate, parameters - - it 'should return no error', () -> - assert.equal data['errors'].length, 0 - - it 'should return no warning', () -> - assert.equal data['warnings'].length, 0 - - it 'should return some URI', () -> - assert.isNotNull data['uri'] - diff --git a/test/unit/inherit-parameters-test.coffee b/test/unit/inherit-parameters-test.coffee deleted file mode 100644 index 9a730c263..000000000 --- a/test/unit/inherit-parameters-test.coffee +++ /dev/null @@ -1,42 +0,0 @@ -{assert} = require 'chai' - -inheritParameters = require '../../src/inherit-parameters' - -describe 'inheritParameters()', () -> - actual = - name: - description: '' - type: '' - required: true - default: '' - example: 'waldo' - values: '' - - inheriting = - name: - description: '' - type: '' - required: true - default: '' - example: 'willy' - values: '' - limit: - description: '' - type: '' - required: true - default: '' - example: 1 - values: '' - - - data = null - - describe 'its return', () -> - before () -> - data = inheritParameters actual, inheriting - - it 'inheriting parameters should be added', () -> - assert.include Object.keys(data), 'limit' - - it 'actual values should not be overwriten by inheriting', () -> - assert.equal data['name']['example'], 'waldo' \ No newline at end of file diff --git a/test/unit/validate-parameters-test.coffee b/test/unit/validate-parameters-test.coffee deleted file mode 100644 index 35546525e..000000000 --- a/test/unit/validate-parameters-test.coffee +++ /dev/null @@ -1,176 +0,0 @@ -{assert} = require 'chai' - -validateParameters = require '../../src/validate-parameters' - -describe 'validateParameters', () -> - - it 'should return an object', () -> - params = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'waldo' - default: '' - values: [] - - result = validateParameters params - assert.isObject result - - describe 'when type is string and example is a parseable float', () -> - it 'should set no error', () -> - params = - name: - description: 'Machine name' - type: 'string' - required: true - example: '1.1' - default: '' - values: [] - - result = validateParameters params - message = result['errors'][0] - assert.equal result['errors'].length, 0 - - # Based on bug report: - # https://github.com/apiaryio/dredd/issues/106 - describe 'when type is string and example is a string but starting with a number', () -> - it 'should set no error', () -> - params = - name: - description: 'Machine name' - type: 'string' - required: true - example: '6f7c1245' - default: '' - values: [] - - result = validateParameters params - - describe 'when type is string and example is a not a parseable float', () -> - it 'should set no error', () -> - params = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'waldo' - default: '' - values: [] - - result = validateParameters params - assert.equal result['errors'].length, 0 - - describe 'when type is number and example is a string', () -> - it 'should set descriptive error', () -> - params = - name: - description: 'Machine name' - type: 'number' - required: true - example: 'waldo' - default: '' - values: [] - - result = validateParameters params - message = result['errors'][0] - assert.include message, 'name' - assert.include message, 'number' - - describe 'when type is number and example is a parseable float', () -> - it 'should set no error', () -> - params = - name: - description: 'Machine name' - type: 'number' - required: true - example: '1.1' - default: '' - values: [] - - result = validateParameters params - assert.equal result['errors'].length, 0 - - describe 'when enum values are defined and example value is not one of enum values', () -> - it 'should set descirptive error', () -> - params = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'D' - default: '' - values: [ - { "value": "A" }, - { "value": "B" }, - { "value": "C" } - ] - - result = validateParameters params - message = result['errors'][0] - assert.include message, 'name' - assert.include message, 'enum' - - describe 'when enum values are defined and example value is one of enum values', () -> - it 'should set no errors', () -> - params = - name: - description: 'Machine name' - type: 'string' - required: true - example: 'A' - default: '' - values: [ - { "value": "A" }, - { "value": "B" }, - { "value": "C" } - ] - - result = validateParameters params - assert.equal result['errors'].length, 0 - - describe 'when type is boolean and example value is not parseable bool', () -> - it 'should set descirptive error', () -> - params = - name: - description: 'Machine name' - type: 'boolean' - required: true - example: 'booboo' - default: '' - values: [] - - result = validateParameters params - message = result['errors'][0] - assert.include message, 'name' - assert.include message, 'boolean' - - describe 'when type is boolean and example value is a parseable bool', () -> - it 'should set no error', () -> - params = - name: - description: 'Machine name' - type: 'boolean' - required: true - example: 'true' - default: '' - values: [] - - result = validateParameters params - assert.equal result['errors'].length, 0 - - describe 'when parameter is reqired example value is empty', () -> - it 'should set descirptive error', () -> - params = - name: - description: 'Machine name' - type: 'string' - required: true - example: '' - default: '' - values: [] - - result = validateParameters params - message = result['errors'][0] - assert.include message, 'name' - assert.include message, 'Required'