|
| 1 | +Feature: Generate Slate documentation from test examples |
| 2 | + |
| 3 | + Background: |
| 4 | + Given a file named "app.rb" with: |
| 5 | + """ |
| 6 | + require 'sinatra' |
| 7 | +
|
| 8 | + class App < Sinatra::Base |
| 9 | + get '/orders' do |
| 10 | + content_type :json |
| 11 | +
|
| 12 | + [200, { |
| 13 | + :page => 1, |
| 14 | + :orders => [ |
| 15 | + { name: 'Order 1', amount: 9.99, description: nil }, |
| 16 | + { name: 'Order 2', amount: 100.0, description: 'A great order' } |
| 17 | + ] |
| 18 | + }.to_json] |
| 19 | + end |
| 20 | +
|
| 21 | + get '/orders/:id' do |
| 22 | + content_type :json |
| 23 | +
|
| 24 | + [200, { order: { name: 'Order 1', amount: 100.0, description: 'A great order' } }.to_json] |
| 25 | + end |
| 26 | +
|
| 27 | + post '/orders' do |
| 28 | + 201 |
| 29 | + end |
| 30 | +
|
| 31 | + put '/orders/:id' do |
| 32 | + 200 |
| 33 | + end |
| 34 | +
|
| 35 | + delete '/orders/:id' do |
| 36 | + 200 |
| 37 | + end |
| 38 | +
|
| 39 | + get '/help' do |
| 40 | + [200, 'Welcome Henry !'] |
| 41 | + end |
| 42 | + end |
| 43 | + """ |
| 44 | + And a file named "app_spec.rb" with: |
| 45 | + """ |
| 46 | + require "rspec_api_documentation" |
| 47 | + require "rspec_api_documentation/dsl" |
| 48 | +
|
| 49 | + RspecApiDocumentation.configure do |config| |
| 50 | + config.app = App |
| 51 | + config.api_name = "Example API" |
| 52 | + config.format = :slate |
| 53 | + config.request_headers_to_include = %w[Content-Type Host] |
| 54 | + config.response_headers_to_include = %w[Content-Type Content-Length] |
| 55 | + end |
| 56 | +
|
| 57 | + resource 'Orders' do |
| 58 | + get '/orders' do |
| 59 | + response_field :page, "Current page" |
| 60 | +
|
| 61 | + example_request 'Getting a list of orders' do |
| 62 | + status.should eq(200) |
| 63 | + response_body.should eq('{"page":1,"orders":[{"name":"Order 1","amount":9.99,"description":null},{"name":"Order 2","amount":100.0,"description":"A great order"}]}') |
| 64 | + end |
| 65 | + end |
| 66 | +
|
| 67 | + get '/orders/:id' do |
| 68 | + let(:id) { 1 } |
| 69 | +
|
| 70 | + example_request 'Getting a specific order' do |
| 71 | + status.should eq(200) |
| 72 | + response_body.should == '{"order":{"name":"Order 1","amount":100.0,"description":"A great order"}}' |
| 73 | + end |
| 74 | + end |
| 75 | +
|
| 76 | + post '/orders' do |
| 77 | + parameter :name, 'Name of order', :required => true |
| 78 | + parameter :amount, 'Amount paid', :required => true |
| 79 | + parameter :description, 'Some comments on the order' |
| 80 | +
|
| 81 | + let(:name) { "Order 3" } |
| 82 | + let(:amount) { 33.0 } |
| 83 | +
|
| 84 | + example_request 'Creating an order' do |
| 85 | + status.should == 201 |
| 86 | + end |
| 87 | + end |
| 88 | +
|
| 89 | + put '/orders/:id' do |
| 90 | + parameter :name, 'Name of order', :required => true |
| 91 | + parameter :amount, 'Amount paid', :required => true |
| 92 | + parameter :description, 'Some comments on the order' |
| 93 | +
|
| 94 | + let(:id) { 2 } |
| 95 | + let(:name) { "Updated name" } |
| 96 | +
|
| 97 | + example_request 'Updating an order' do |
| 98 | + status.should == 200 |
| 99 | + end |
| 100 | + end |
| 101 | +
|
| 102 | + delete "/orders/:id" do |
| 103 | + let(:id) { 1 } |
| 104 | +
|
| 105 | + example_request "Deleting an order" do |
| 106 | + status.should == 200 |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | +
|
| 111 | + resource 'Help' do |
| 112 | + get '/help' do |
| 113 | + example_request 'Getting welcome message' do |
| 114 | + status.should eq(200) |
| 115 | + response_body.should == 'Welcome Henry !' |
| 116 | + end |
| 117 | + end |
| 118 | +
|
| 119 | + end |
| 120 | + """ |
| 121 | + When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter` |
| 122 | + |
| 123 | + Scenario: Output helpful progress to the console |
| 124 | + Then the output should contain: |
| 125 | + """ |
| 126 | + Generating API Docs |
| 127 | + Orders |
| 128 | + GET /orders |
| 129 | + * Getting a list of orders |
| 130 | + GET /orders/:id |
| 131 | + * Getting a specific order |
| 132 | + POST /orders |
| 133 | + * Creating an order |
| 134 | + PUT /orders/:id |
| 135 | + * Updating an order |
| 136 | + DELETE /orders/:id |
| 137 | + * Deleting an order |
| 138 | + Help |
| 139 | + GET /help |
| 140 | + * Getting welcome message |
| 141 | + """ |
| 142 | + And the output should contain "6 examples, 0 failures" |
| 143 | + And the exit status should be 0 |
| 144 | + |
| 145 | + Scenario: Index file should look like we expect |
| 146 | + Then the file "doc/api/index.markdown" should contain exactly: |
| 147 | + """ |
| 148 | + # Example API |
| 149 | +
|
| 150 | + ## Help |
| 151 | +
|
| 152 | + * [Getting welcome message](help/getting_welcome_message.markdown) |
| 153 | +
|
| 154 | + ## Orders |
| 155 | +
|
| 156 | + * [Creating an order](orders/creating_an_order.markdown) |
| 157 | + * [Deleting an order](orders/deleting_an_order.markdown) |
| 158 | + * [Getting a list of orders](orders/getting_a_list_of_orders.markdown) |
| 159 | + * [Getting a specific order](orders/getting_a_specific_order.markdown) |
| 160 | + * [Updating an order](orders/updating_an_order.markdown) |
| 161 | + """ |
| 162 | + |
| 163 | + Scenario: Example 'Getting a list of orders' file should look like we expect |
| 164 | + Then the file "doc/api/orders/getting_a_list_of_orders.markdown" should contain exactly: |
| 165 | + """ |
| 166 | + ## Getting a list of orders |
| 167 | +
|
| 168 | + ### Request |
| 169 | +
|
| 170 | + #### Endpoint |
| 171 | +
|
| 172 | + ``` |
| 173 | + GET /orders |
| 174 | + Host: example.org |
| 175 | + ``` |
| 176 | +
|
| 177 | + `GET /orders` |
| 178 | +
|
| 179 | +
|
| 180 | + #### Parameters |
| 181 | +
|
| 182 | +
|
| 183 | + None known. |
| 184 | +
|
| 185 | +
|
| 186 | + ### Response |
| 187 | +
|
| 188 | + ``` |
| 189 | + Content-Type: application/json |
| 190 | + Content-Length: 137 |
| 191 | + 200 OK |
| 192 | + ``` |
| 193 | +
|
| 194 | +
|
| 195 | + ```json |
| 196 | + { |
| 197 | + "page": 1, |
| 198 | + "orders": [ |
| 199 | + { |
| 200 | + "name": "Order 1", |
| 201 | + "amount": 9.99, |
| 202 | + "description": null |
| 203 | + }, |
| 204 | + { |
| 205 | + "name": "Order 2", |
| 206 | + "amount": 100.0, |
| 207 | + "description": "A great order" |
| 208 | + } |
| 209 | + ] |
| 210 | + } |
| 211 | + ``` |
| 212 | +
|
| 213 | +
|
| 214 | +
|
| 215 | + #### Fields |
| 216 | +
|
| 217 | + | Name | Description | |
| 218 | + |:-----------|:--------------------| |
| 219 | + | page | Current page | |
| 220 | +
|
| 221 | +
|
| 222 | + """ |
| 223 | + |
| 224 | + Scenario: Example 'Creating an order' file should look like we expect |
| 225 | + Then the file "doc/api/orders/creating_an_order.markdown" should contain exactly: |
| 226 | + """ |
| 227 | + ## Creating an order |
| 228 | +
|
| 229 | + ### Request |
| 230 | +
|
| 231 | + #### Endpoint |
| 232 | +
|
| 233 | + ``` |
| 234 | + POST /orders |
| 235 | + Host: example.org |
| 236 | + Content-Type: application/x-www-form-urlencoded |
| 237 | + ``` |
| 238 | +
|
| 239 | + `POST /orders` |
| 240 | +
|
| 241 | +
|
| 242 | + #### Parameters |
| 243 | +
|
| 244 | +
|
| 245 | + ```json |
| 246 | + name=Order+3&amount=33.0 |
| 247 | + ``` |
| 248 | +
|
| 249 | +
|
| 250 | + | Name | Description | |
| 251 | + |:-----|:------------| |
| 252 | + | name *required* | Name of order | |
| 253 | + | amount *required* | Amount paid | |
| 254 | + | description | Some comments on the order | |
| 255 | +
|
| 256 | +
|
| 257 | +
|
| 258 | + ### Response |
| 259 | +
|
| 260 | + ``` |
| 261 | + Content-Type: text/html;charset=utf-8 |
| 262 | + Content-Length: 0 |
| 263 | + 201 Created |
| 264 | + ``` |
| 265 | +
|
| 266 | +
|
| 267 | +
|
| 268 | +
|
| 269 | + """ |
| 270 | + |
| 271 | + Scenario: Example 'Deleting an order' file should be created |
| 272 | + Then a file named "doc/api/orders/deleting_an_order.markdown" should exist |
| 273 | + |
| 274 | + Scenario: Example 'Getting a list of orders' file should be created |
| 275 | + Then a file named "doc/api/orders/getting_a_list_of_orders.markdown" should exist |
| 276 | + |
| 277 | + Scenario: Example 'Getting a specific order' file should be created |
| 278 | + Then a file named "doc/api/orders/getting_a_specific_order.markdown" should exist |
| 279 | + |
| 280 | + Scenario: Example 'Updating an order' file should be created |
| 281 | + Then a file named "doc/api/orders/updating_an_order.markdown" should exist |
| 282 | + |
| 283 | + Scenario: Example 'Getting welcome message' file should be created |
| 284 | + Then a file named "doc/api/help/getting_welcome_message.markdown" should exist |
0 commit comments