Skip to content

Commit e3332c6

Browse files
committed
Replace crummy plain-text HTML matching with awesome capybara!
1 parent c27e585 commit e3332c6

File tree

3 files changed

+74
-73
lines changed

3 files changed

+74
-73
lines changed

features/html_documentation.feature

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -55,67 +55,44 @@ Feature: Generate HTML documentation from test examples
5555
And the output should contain "1 example, 0 failures"
5656
And the exit status should be 0
5757

58-
Scenario: Create an index of all API examples, including all resources and examples
59-
Then the file "docs/index.html" should contain "<h2>Greetings</h2>"
60-
And the file "docs/index.html" should contain HTML:
61-
"""
62-
<h2>Greetings</h2>
63-
64-
<ul>
65-
<li>
66-
<a href="greetings/greeting_your_favorite_gem.html">Greeting your favorite gem</a>
67-
</li>
68-
</ul>
69-
"""
58+
Scenario: Create an index of all API examples, including all resources
59+
When I open the index
60+
Then I should see the following resources:
61+
| Greetings |
7062

7163
Scenario: Example HTML documentation includes the parameters
72-
Then the file "docs/greetings/greeting_your_favorite_gem.html" should contain HTML:
73-
"""
74-
<h3>Parameters</h3>
75-
<table>
76-
<thead>
77-
<tr>
78-
<th>Name</th>
79-
<th>Description</th>
80-
</tr>
81-
</thead>
82-
<tbody>
83-
<tr>
84-
<th>
85-
<span class="name">target</span>
86-
</th>
87-
<td>
88-
<span class="description">The thing you want to greet</span>
89-
</td>
90-
</tr>
91-
</tbody>
92-
</table>
93-
"""
64+
When I open the index
65+
And I navigate to "Greeting your favorite gem"
66+
Then I should see the following parameters:
67+
| name | description |
68+
| target | The thing you want to greet |
9469

9570
Scenario: Example HTML documentation includes the request information
96-
Then the file "docs/greetings/greeting_your_favorite_gem.html" should contain HTML:
71+
When I open the index
72+
And I navigate to "Greeting your favorite gem"
73+
Then I should see the route is "GET /greetings?target=rspec_api_documentation"
74+
And I should see the following request headers:
9775
"""
98-
<h3>Request</h3>
99-
<h4>Headers</h4>
100-
<pre class="headers">Host: example.org
101-
Cookie: </pre>
102-
<h4>Route</h4>
103-
<pre class="request highlight">GET /greetings?target=rspec_api_documentation</pre>
104-
<h4>Query Parameters</h4>
105-
<pre class="request highlight">target: rspec_api_documentation</pre>
76+
Host: example.org
77+
Cookie:
78+
"""
79+
And I should see the following query parameters:
80+
"""
81+
target: rspec_api_documentation
10682
"""
10783

10884
Scenario: Example HTML documentation includes the response information
109-
Then the file "docs/greetings/greeting_your_favorite_gem.html" should contain HTML:
85+
When I open the index
86+
And I navigate to "Greeting your favorite gem"
87+
Then I should see the response status is "200 OK"
88+
And I should see the following response headers:
89+
"""
90+
Content-Type: application/json
91+
Content-Length: 35
92+
"""
93+
And I should see the following response_body:
11094
"""
111-
<h3>Response</h3>
112-
<h4>Headers</h4>
113-
<pre class="headers">Content-Type: application/json
114-
Content-Length: 35</pre>
115-
<h4>Status</h4>
116-
<pre class="response_status">200 OK</pre>
117-
<h4>Body</h4>
118-
<pre class="response highlight">{
119-
&quot;hello&quot;: &quot;rspec_api_documentation&quot;
120-
}</pre>
95+
{
96+
"hello" => "rspec_api_documentation"
97+
}
12198
"""
Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1-
RSpec::Matchers.define :contain_ignoring_whitespace do |expected|
2-
match do |actual|
3-
insignificant_whitespace = /(^\s*)|(\s*$)/
4-
actual.gsub(insignificant_whitespace, "").should =~ regexp(expected.gsub(insignificant_whitespace, ""))
5-
end
6-
end
7-
8-
Then /^the file "([^"]*)" should contain HTML:$/ do |file, partial_html|
9-
prep_for_fs_check do
10-
content = IO.read(file)
11-
content.should contain_ignoring_whitespace(partial_html)
12-
end
1+
When /^I open the index$/ do
2+
visit "/index.html"
3+
end
4+
5+
When /^I navigate to "([^"]*)"$/ do |example|
6+
click_link example
7+
end
8+
9+
Then /^I should see the following resources:$/ do |table|
10+
all("h2").map(&:text).should == table.raw.flatten
11+
end
12+
13+
Then /^I should see the following parameters:$/ do |table|
14+
names = all(".parameters .name").map(&:text)
15+
descriptions = all(".parameters .description").map(&:text)
16+
names.zip(descriptions).should == table.rows
17+
end
18+
19+
Then /^I should see the following (request|response) headers:$/ do |part, headers|
20+
page.should have_css("pre.#{part}.headers", :content => headers)
21+
end
22+
23+
Then /^I should see the route is "([^"]*)"$/ do |route|
24+
page.should have_css(".request.route", :content => route)
25+
end
26+
27+
Then /^I should see the following query parameters:$/ do |query_parameters|
28+
page.should have_css("pre.request.query_parameters"), :content => query_parameters
29+
end
30+
31+
Then /^I should see the response status is "([^"]*)"$/ do |status|
32+
page.should have_css(".response.status", :content => status)
33+
end
34+
35+
Then /^I should see the following response_body:$/ do |response_body|
36+
page.should have_css("pre.response.body", :content => response_body)
1337
end

templates/rspec_api_documentation/html_example.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
{{# has_parameters? }}
3232
<h3>Parameters</h3>
33-
<table>
33+
<table class="parameters">
3434
<thead>
3535
<tr>
3636
<th>Name</th>
@@ -55,12 +55,12 @@
5555
{{# requests }}
5656
<h3>Request</h3>
5757
<h4>Headers</h4>
58-
<pre class="headers">{{ request_headers }}</pre>
58+
<pre class="request headers">{{ request_headers }}</pre>
5959
<h4>Route</h4>
60-
<pre class="request highlight">{{ method }} {{ route }}</pre>
60+
<pre class="request route highlight">{{ method }} {{ route }}</pre>
6161
{{# request_query_parameters }}
6262
<h4>Query Parameters</h4>
63-
<pre class="request highlight">{{ request_query_parameters }}</pre>
63+
<pre class="request query_parameters highlight">{{ request_query_parameters }}</pre>
6464
{{/ request_query_parameters }}
6565
{{# request_body }}
6666
<h4>Body</h4>
@@ -75,12 +75,12 @@
7575
{{# response_status }}
7676
<h3>Response</h3>
7777
<h4>Headers</h4>
78-
<pre class="headers">{{ response_headers }}</pre>
78+
<pre class="response headers">{{ response_headers }}</pre>
7979
<h4>Status</h4>
80-
<pre class="response_status">{{ response_status }} {{ response_status_text}}</pre>
80+
<pre class="response status">{{ response_status }} {{ response_status_text}}</pre>
8181
{{# response_body }}
8282
<h4>Body</h4>
83-
<pre class="response highlight">{{ response_body }}</pre>
83+
<pre class="response body highlight">{{ response_body }}</pre>
8484
{{/ response_body }}
8585
{{/ response_status }}
8686
{{/ requests }}

0 commit comments

Comments
 (0)