Skip to content

Commit 2279bae

Browse files
authored
Merge pull request #195 from cybertk/0.5.0
0.5.0
2 parents 565d7a8 + 76bd805 commit 2279bae

24 files changed

+284
-93
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,15 @@ Options:
221221
[boolean]
222222
--grep, -g Only run tests matching <pattern> [string]
223223
--invert, -i Invert --grep matches [boolean]
224+
--sorted Sorts requests in a sensible way so that objects are not
225+
modified before they are created. Order: CONNECT, OPTIONS,
226+
POST, GET, HEAD, PUT, PATCH, DELETE, TRACE. [boolean]
224227
--timeout, -t Set test-case timeout in milliseconds
225228
[number] [default: 2000]
226229
--template Specify the template file to use for generating hooks
227-
[string]
230+
[string]
228231
--names, -n List names of requests and exit [boolean]
232+
--generate-hooks Output hooks generated from template file and exit [boolean]
229233
--reporters Display available reporters and exit [boolean]
230234
--help Show usage information and exit [boolean]
231235
--version Show version number and exit [boolean]

lib/abao.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Abao
4242
if !config.options.server
4343
if raml.baseUri
4444
config.options.server = raml.baseUri
45-
addTests raml, tests, hooks, callback, factory
45+
addTests raml, tests, hooks, callback, factory, config.options.sorted
4646
,
4747
# Run tests
4848
(callback) ->

lib/add-tests.coffee

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
async = require 'async'
2-
_ = require 'underscore'
2+
_ = require 'lodash'
33
csonschema = require 'csonschema'
44

55

@@ -21,11 +21,11 @@ parseHeaders = (raml) ->
2121

2222
headers
2323

24-
# addTests(raml, tests, [parent], callback, config)
25-
addTests = (raml, tests, hooks, parent, callback, testFactory) ->
24+
addTests = (raml, tests, hooks, parent, callback, testFactory, sortFirst) ->
2625

2726
# Handle 4th optional param
2827
if _.isFunction(parent)
28+
sortFirst = testFactory
2929
testFactory = callback
3030
callback = parent
3131
parent = null
@@ -41,7 +41,7 @@ addTests = (raml, tests, hooks, parent, callback, testFactory) ->
4141
# Apply parent properties
4242
if parent
4343
path = parent.path + path
44-
params = _.clone parent.params
44+
params = _.clone parent.params # shallow copy
4545

4646
# Setup param
4747
if resource.uriParameters
@@ -52,6 +52,49 @@ addTests = (raml, tests, hooks, parent, callback, testFactory) ->
5252
# In case of issue #8, resource does not define methods
5353
resource.methods ?= []
5454

55+
if sortFirst && resource.methods.length > 1
56+
methodTests = [
57+
method: 'CONNECT', tests: []
58+
,
59+
method: 'OPTIONS', tests: []
60+
,
61+
method: 'POST', tests: []
62+
,
63+
method: 'GET', tests: []
64+
,
65+
method: 'HEAD', tests: []
66+
,
67+
method: 'PUT', tests: []
68+
,
69+
method: 'PATCH', tests: []
70+
,
71+
method: 'DELETE', tests: []
72+
,
73+
method: 'TRACE', tests: []
74+
]
75+
76+
# Group endpoint tests by method name
77+
_.each methodTests, (methodTest) ->
78+
isSameMethod = (test) ->
79+
return methodTest.method == test.method.toUpperCase()
80+
81+
ans = _.partition resource.methods, isSameMethod
82+
if ans[0].length != 0
83+
_.each ans[0], (test) -> methodTest.tests.push test
84+
resource.methods = ans[1]
85+
86+
# Shouldn't happen unless new HTTP method introduced...
87+
leftovers = resource.methods
88+
if leftovers.length > 1
89+
console.error 'unknown method calls present!', leftovers
90+
91+
# Now put them back, but in order of methods listed above
92+
sortedTests = _.map methodTests, (methodTest) -> return methodTest.tests
93+
leftoverTests = _.map leftovers, (leftover) -> return leftover
94+
reassembled = _.flattenDeep [_.reject sortedTests, _.isEmpty,
95+
_.reject leftoverTests, _.isEmpty]
96+
resource.methods = reassembled
97+
5598
# Iterate response method
5699
async.each resource.methods, (api, callback) ->
57100
method = api.method.toUpperCase()
@@ -108,7 +151,7 @@ addTests = (raml, tests, hooks, parent, callback, testFactory) ->
108151
return callback(err) if err
109152

110153
# Recursive
111-
addTests resource, tests, hooks, {path, params}, callback, testFactory
154+
addTests resource, tests, hooks, {path, params}, callback, testFactory, sortFirst
112155
, callback
113156

114157

lib/apply-configuration.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ applyConfiguration = (config) ->
3333
grep: ''
3434
invert: false
3535
'hooks-only': false
36+
sorted: false
3637

3738
# Normalize options and config
3839
for own key, value of config

lib/options.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ options =
4040
description: 'Invert --grep matches'
4141
type: 'boolean'
4242

43+
sorted:
44+
description: 'Sorts requests in a sensible way so that objects are not ' +
45+
'modified before they are created.\nOrder: ' +
46+
'CONNECT, OPTIONS, POST, GET, HEAD, PUT, PATCH, DELETE, TRACE.'
47+
type: 'boolean'
48+
4349
timeout:
4450
alias: 't'
4551
description: 'Set test-case timeout in milliseconds'

lib/test.coffee

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class TestFactory
1818
if schemaLocation
1919

2020
files = glob.sync schemaLocation
21-
console.error 'Found JSON ref schemas: ' + files
22-
console.error ''
21+
console.log '\tJSON ref schemas: ' + files.join(', ')
2322

2423
tv4.banUnknown = true
2524

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "abao",
3-
"version": "0.4.1",
3+
"version": "0.5.0",
44
"description": "RAML testing tool",
55
"bin": "bin/abao",
66
"main": "lib/index.js",
@@ -60,6 +60,7 @@
6060
"source-map-support": "^0.4.0",
6161
"tv4": "^1.2.7",
6262
"underscore": "^1.8.2",
63+
"lodash": "^4.16.4",
6364
"yargs": "~6.0.0"
6465
}
6566
}

test/cli-test.coffee

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ execCommand = (cmd, callback) ->
4747

4848
describe 'Command line interface', ->
4949

50-
describe 'When run with "one and done" options', (done) ->
50+
describe 'when run with "one and done" options', (done) ->
5151

5252
describe 'when RAML argument unnecessary', () ->
5353

@@ -161,7 +161,7 @@ describe 'Command line interface', ->
161161
assert.include stderr, 'template -> generate-hooks'
162162

163163

164-
describe 'When RAML file not found', (done) ->
164+
describe 'when RAML file not found', (done) ->
165165
before (done) ->
166166
ramlFile = "#{RAML_DIR}/nonexistent_path.raml"
167167
cmd = "#{ABAO_BIN} #{ramlFile} --server #{SERVER}"
@@ -177,9 +177,9 @@ describe 'Command line interface', ->
177177
assert.include stderr, 'Error: ENOENT'
178178

179179

180-
describe 'Arguments with existing RAML and responding server', () ->
180+
describe 'arguments with existing RAML and responding server', () ->
181181
describe 'when invoked without "--server" option', () ->
182-
describe 'when RAML file hasn\'t correct baseUri', () ->
182+
describe 'when RAML file does not specify "baseUri"', () ->
183183
before (done) ->
184184
ramlFile = "#{RAML_DIR}/no-base-uri.raml"
185185
cmd = "#{ABAO_BIN} #{ramlFile} --reporter json"
@@ -192,7 +192,7 @@ describe 'Command line interface', ->
192192
it 'should print error message to stderr', ->
193193
assert.include stderr, 'no API endpoint specified'
194194

195-
describe 'when RAML file has correct baseUri', () ->
195+
describe 'when RAML file does specify "baseUri"', () ->
196196

197197
before (done) ->
198198
ramlFile = "#{RAML_DIR}/single-get.raml"

test/fixtures/1-get-1-post.raml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#%RAML 0.8
22

3-
title: World Music API
3+
title: Machines API
44
baseUri: http://example.api.com/{version}
55
version: v1
66

@@ -34,3 +34,4 @@ version: v1
3434
name: 'string'
3535
example: |
3636
{ "type": "Kulu", "name": "Mike" }
37+

test/fixtures/contacts.raml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#%RAML 0.8
2+
3+
title: Address Book API
4+
baseUri: http://example.api.com/{version}
5+
version: v1
6+
7+
/contacts:
8+
post:
9+
body:
10+
application/json:
11+
schema: |
12+
type: 'string'
13+
name: 'string'
14+
example: |
15+
{ "type": "Kulu", "name": "Mike" }
16+
responses:
17+
201:
18+
body:
19+
application/json:
20+
schema: |
21+
type: 'string'
22+
name: 'string'
23+
example: |
24+
{ "type": "Kulu", "name": "Mike" }
25+
/contacts/{id}
26+
delete:
27+
responses:
28+
204:
29+
put:
30+
body:
31+
application/json:
32+
schema: |
33+
type: 'string'
34+
name: 'string'
35+
example: |
36+
{ "type": "Kulu", "name": "Mike" }
37+
responses:
38+
201:
39+
body:
40+
application/json:
41+
schema: |
42+
type: 'string'
43+
name: 'string'
44+
example: |
45+
{ "type": "Kulu", "name": "Mike" }
46+
get:
47+
responses:
48+
200:
49+
body:
50+
application/json:
51+
schema: |
52+
[
53+
type: 'string'
54+
name: 'string'
55+
phone: 'string'
56+
]
57+
example: |
58+
{
59+
"type": "contact",
60+
"name": "Jenny",
61+
"phone": "867-5309"
62+
}
63+

0 commit comments

Comments
 (0)