Skip to content
This repository has been archived by the owner on Dec 25, 2019. It is now read-only.

Commit

Permalink
Fix export tests by comparing HTML tree using nokogiri-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas E. Rabenau committed Mar 11, 2012
1 parent f20dcf4 commit cee2217
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -10,6 +10,7 @@ gem 'activesupport'
group :test do
gem "rake"
gem 'simplecov', :require => false
gem 'nokogiri-diff'
end

# Add dependencies to develop your gem here.
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -17,13 +17,18 @@ GEM
rdoc
json (1.6.5)
multi_json (1.1.0)
nokogiri (1.4.7)
nokogiri-diff (0.1.0)
nokogiri (~> 1.4.1)
tdiff (~> 0.3.2)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
simplecov (0.6.1)
multi_json (~> 1.0)
simplecov-html (~> 0.5.3)
simplecov-html (0.5.3)
tdiff (0.3.2)

PLATFORMS
ruby
Expand All @@ -34,6 +39,7 @@ DEPENDENCIES
commander
encryptor
jeweler (~> 1.8.3)
nokogiri-diff
rake
rdoc (~> 3.12)
simplecov
6 changes: 3 additions & 3 deletions templates/export.html.erb
Expand Up @@ -8,7 +8,7 @@
tr { page-break-inside:avoid; page-break-after:auto}
thead { display:table-header-group}
tfoot { display:table-footer-group; font-size: 0.85em;}

/* adapted from http://coding.smashingmagazine.com/2008/08/13/top-10-css-table-designs/ */
body{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
Expand Down Expand Up @@ -42,7 +42,7 @@
<tfoot>
<tr>
<td colspan="2">
This <a href="http://rdoc.info/github/nerab/pwm/master/frames">pwm</a> export was created on <%= DateTime.now.strftime('%F %R') %>.
This <a href="http://rdoc.info/github/nerab/pwm/master/frames">pwm</a> export was created on <%= DateTime.now.strftime('%F %R') %>.
<br/>
The database at <%= store.path %> was <%= "last modified #{store.last_modified.strftime('%F %R')}" rescue 'never modified' %>.
</td>
Expand All @@ -57,5 +57,5 @@
<% } %>
</tbody>
</table>
</body>
</body>
</html>
26 changes: 24 additions & 2 deletions test/acceptance/test_export.rb
@@ -1,10 +1,11 @@
require 'helper'
require 'nokogiri/diff'

# Tests `pwm export`
class TestExport < Test::Pwm::AppTestCase
def test_empty
fixture = fixture("test_empty.html").gsub('CREATED_STAMP', DateTime.now.strftime('%F %R')).gsub('MODIFIED_STAMP', 'never').gsub('DATABASE_FILE', store_file)
assert_successful(fixture, 'export')
assert_successful_html(fixture, 'export')
end

def test_all
Expand All @@ -15,6 +16,27 @@ def test_all

now = DateTime.now.strftime('%F %R')
fixture = fixture("test_all.html").gsub('CREATED_STAMP', now).gsub('MODIFIED_STAMP', now).gsub('DATABASE_FILE', store_file)
assert_successful(fixture, 'export')

assert_successful_html(fixture, 'export')
end

def assert_successful_html(expected_out, cmd, password = store_password)
out, err, rc = execute(cmd, password)
assert_equal(0, rc.exitstatus, "Expected exit status 0, but it was #{rc.exitstatus}. STDERR was: #{err}")
assert(err.empty?, "Expected empty STDERR, but it yielded #{err}")

actual = Nokogiri::HTML(out)
expected = Nokogiri::HTML(expected_out)

differences = actual.diff(expected, :added => true, :removed => true)

assert_equal(0, differences.count, "Unexpected differences in output. Diff:\n" << differences.collect{|change, node|
case change
when '+'
"Actual: #{node.to_html}"
when '-'
"Expected: #{node.to_html}"
end
}.join("\n"))
end
end
4 changes: 2 additions & 2 deletions test/fixtures/test_all.html
Expand Up @@ -43,7 +43,7 @@ <h1>Password Manager Export</h1>
<tr>
<td colspan="2">
This <a href="http://rdoc.info/github/nerab/pwm/master/frames">pwm</a> export was created on CREATED_STAMP.
<br/>
<br/>
The database at DATABASE_FILE was last modified MODIFIED_STAMP.
</td>
</tr>
Expand All @@ -67,5 +67,5 @@ <h1>Password Manager Export</h1>

</tbody>
</table>
</body>
</body>
</html>
2 changes: 1 addition & 1 deletion test/fixtures/test_empty.html
Expand Up @@ -52,5 +52,5 @@ <h1>Password Manager Export</h1>

</tbody>
</table>
</body>
</body>
</html>

0 comments on commit cee2217

Please sign in to comment.