11async = require ' async'
2- _ = require ' underscore '
2+ _ = require ' lodash '
33csonschema = 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
0 commit comments