rspec-approvals 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +12 -0
- data/lib/rspec/approvals/approval.rb +33 -2
- data/lib/rspec/approvals/dsl.rb +2 -2
- data/lib/rspec/approvals/version.rb +1 -1
- data/lib/rspec/approvals.rb +1 -0
- data/rspec-approvals.gemspec +1 -0
- data/spec/approval_spec.rb +27 -0
- data/spec/approvals/rspec_approvals_formats_html_nicely.approved.txt +48 -0
- data/spec/approvals/rspec_approvals_formats_xml_nicely.approved.txt +10 -0
- data/spec/approvals_spec.rb +10 -0
- metadata +18 -3
data/README.md
CHANGED
@@ -53,6 +53,18 @@ empty.
|
|
53
53
|
|
54
54
|
The contents of the two files are compared, and the approval will fail at this point.
|
55
55
|
|
56
|
+
### Formatting
|
57
|
+
|
58
|
+
You can pass options to format output before it gets written to the file.
|
59
|
+
At the moment, only xml and html are supported.
|
60
|
+
|
61
|
+
Simply add a `:format => :xml` or `:format => :html` option to the example:
|
62
|
+
|
63
|
+
approve "some html", :format => :html do
|
64
|
+
"<html><head></head><body><h1>ZOMG</h1></body></html>"
|
65
|
+
end
|
66
|
+
|
67
|
+
|
56
68
|
### Approving a spec
|
57
69
|
|
58
70
|
If the contents of the received file is to your liking, you can approve
|
@@ -9,6 +9,7 @@ module RSpec
|
|
9
9
|
def inspect
|
10
10
|
""
|
11
11
|
end
|
12
|
+
def strip; end
|
12
13
|
end
|
13
14
|
|
14
15
|
class Approval
|
@@ -23,8 +24,9 @@ module RSpec
|
|
23
24
|
|
24
25
|
attr_reader :location
|
25
26
|
|
26
|
-
def initialize(example, received = '')
|
27
|
+
def initialize(example, received = '', options = {})
|
27
28
|
@path = Approval.base_path(example.full_description)
|
29
|
+
@options = options
|
28
30
|
|
29
31
|
example.options[:approval] = true
|
30
32
|
example.options[:approval_diff_paths] = {
|
@@ -46,7 +48,11 @@ module RSpec
|
|
46
48
|
|
47
49
|
def write(suffix, contents)
|
48
50
|
File.open("#{@path}.#{suffix}.txt", 'w') do |f|
|
49
|
-
if
|
51
|
+
if xml?
|
52
|
+
parser = XML::Parser.string contents.strip
|
53
|
+
doc = parser.parse
|
54
|
+
f.write doc.to_s
|
55
|
+
elsif contents.respond_to?(:each_pair)
|
50
56
|
contents.each_pair do |k,v|
|
51
57
|
f.write "#{k.inspect} => #{v.inspect}\n"
|
52
58
|
end
|
@@ -61,6 +67,11 @@ module RSpec
|
|
61
67
|
end
|
62
68
|
|
63
69
|
def failure_message
|
70
|
+
return failure_message_exposing_received if show_received?
|
71
|
+
return default_failure_message
|
72
|
+
end
|
73
|
+
|
74
|
+
def default_failure_message
|
64
75
|
<<-FAILURE_MESSAGE
|
65
76
|
|
66
77
|
Approval Failure:
|
@@ -80,6 +91,10 @@ module RSpec
|
|
80
91
|
FAILURE_MESSAGE
|
81
92
|
end
|
82
93
|
|
94
|
+
def failure_message_exposing_received
|
95
|
+
default_failure_message << " received:\n " << received << "\n\n\n"
|
96
|
+
end
|
97
|
+
|
83
98
|
def location=(backtrace)
|
84
99
|
@location = [backtrace.first.gsub(Dir.pwd, '.')]
|
85
100
|
end
|
@@ -91,6 +106,22 @@ module RSpec
|
|
91
106
|
raise RSpec::Approvals::ReceivedDiffersError, failure_message, location
|
92
107
|
end
|
93
108
|
end
|
109
|
+
|
110
|
+
def approved
|
111
|
+
File.read(approved_path)
|
112
|
+
end
|
113
|
+
|
114
|
+
def received
|
115
|
+
File.read(received_path)
|
116
|
+
end
|
117
|
+
|
118
|
+
def xml?
|
119
|
+
[:xml, :html].include? @options[:format]
|
120
|
+
end
|
121
|
+
|
122
|
+
def show_received?
|
123
|
+
@options[:show_received]
|
124
|
+
end
|
94
125
|
end
|
95
126
|
end
|
96
127
|
end
|
data/lib/rspec/approvals/dsl.rb
CHANGED
@@ -4,10 +4,10 @@ module RSpec
|
|
4
4
|
module Approvals
|
5
5
|
module DSL
|
6
6
|
|
7
|
-
def approve(description)
|
7
|
+
def approve(description, options = {})
|
8
8
|
|
9
9
|
specify(description) do
|
10
|
-
approval = Approval.new(example, yield)
|
10
|
+
approval = Approval.new(example, yield, options)
|
11
11
|
|
12
12
|
# We may be able to set file_path and
|
13
13
|
# line_number on example in the approval
|
data/lib/rspec/approvals.rb
CHANGED
data/rspec-approvals.gemspec
CHANGED
data/spec/approval_spec.rb
CHANGED
@@ -149,6 +149,33 @@ The::Class \t \r\n \fname
|
|
149
149
|
mv #{approval.received_path} #{approval.approved_path}
|
150
150
|
|
151
151
|
|
152
|
+
FAILURE_MESSAGE
|
153
|
+
|
154
|
+
approval.failure_message.should eq(message)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "fails with exposed 'received'" do
|
158
|
+
approval = Approvals::Approval.new(example, 'aoeu', :show_received => true)
|
159
|
+
message = <<-FAILURE_MESSAGE
|
160
|
+
|
161
|
+
Approval Failure:
|
162
|
+
|
163
|
+
The received contents did not match the approved contents.
|
164
|
+
|
165
|
+
Inspect the differences in the following files:
|
166
|
+
#{approval.received_path}
|
167
|
+
#{approval.approved_path}
|
168
|
+
|
169
|
+
If you like what you see in the *.received.txt file, you can approve it
|
170
|
+
by renaming it with the .approved.txt suffix.
|
171
|
+
|
172
|
+
mv #{approval.received_path} #{approval.approved_path}
|
173
|
+
|
174
|
+
|
175
|
+
received:
|
176
|
+
#{approval.received}
|
177
|
+
|
178
|
+
|
152
179
|
FAILURE_MESSAGE
|
153
180
|
|
154
181
|
approval.failure_message.should eq(message)
|
@@ -0,0 +1,48 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
|
4
|
+
<head>
|
5
|
+
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
|
6
|
+
<title>blog</title>
|
7
|
+
<script type="text/javascript" src="/blog/static/jquery-1.1.3.pack.js"></script>
|
8
|
+
<script type="text/javascript" src="/blog/static/blog.js"></script>
|
9
|
+
<link type="text/css" rel="stylesheet" href="/blog/styles.css" media="screen" />
|
10
|
+
</head>
|
11
|
+
<body>
|
12
|
+
<h1 class="header">
|
13
|
+
<a href="/blog/">blog</a>
|
14
|
+
</h1>
|
15
|
+
<div class="content">
|
16
|
+
<h1 class="post_head">
|
17
|
+
<a href="/blog/view/2">Hej</a>
|
18
|
+
<a class="edit_link" href="/blog/edit/2">edit</a>
|
19
|
+
</h1>
|
20
|
+
<p>ASDJlksdjfsld
|
21
|
+
</p>
|
22
|
+
<h2 class="comment_head">
|
23
|
+
<a href="javascript:getComments(2)">See comments (2)</a>
|
24
|
+
</h2>
|
25
|
+
<div id="comments">
|
26
|
+
<h3>tyysen</h3>
|
27
|
+
<p>miljoooner</p>
|
28
|
+
<h3>hej</h3>
|
29
|
+
<p>bulan</p>
|
30
|
+
</div>
|
31
|
+
<h2 class="add_comment_head">
|
32
|
+
<a href="#comment_form">Add comment</a>
|
33
|
+
</h2>
|
34
|
+
<form method="post" name="comment_form" id="comment_form" action="/blog/comment">
|
35
|
+
<label for="post_username">Name</label>
|
36
|
+
<br />
|
37
|
+
<input type="text" name="post_username" />
|
38
|
+
<br />
|
39
|
+
<label for="post_body">Comment</label>
|
40
|
+
<br />
|
41
|
+
<textarea name="post_body"></textarea>
|
42
|
+
<br />
|
43
|
+
<input type="hidden" name="post_id" value="2" />
|
44
|
+
<input type="submit" value="Add comment" />
|
45
|
+
</form>
|
46
|
+
</div>
|
47
|
+
</body>
|
48
|
+
</html>
|
data/spec/approvals_spec.rb
CHANGED
@@ -63,4 +63,14 @@ describe Approvals do
|
|
63
63
|
hello # => output matches hello.inspect
|
64
64
|
end
|
65
65
|
|
66
|
+
approve "formats html nicely", :format => :html, :show_received => true do
|
67
|
+
<<-XML
|
68
|
+
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/><title>blog</title><script type="text/javascript" src="/blog/static/jquery-1.1.3.pack.js"></script><script type="text/javascript" src="/blog/static/blog.js"></script><link type="text/css" rel="stylesheet" href="/blog/styles.css" media="screen"/></head><body><h1 class="header"><a href="/blog/">blog</a></h1><div class="content"><h1 class="post_head"><a href="/blog/view/2">Hej</a><a class="edit_link" href="/blog/edit/2">edit</a></h1><p>ASDJlksdjfsld
|
69
|
+
</p><h2 class="comment_head"><a href="javascript:getComments(2)">See comments (2)</a></h2><div id="comments"><h3>tyysen</h3><p>miljoooner</p><h3>hej</h3><p>bulan</p></div><h2 class="add_comment_head"><a href="#comment_form">Add comment</a></h2><form method="post" name="comment_form" id="comment_form" action="/blog/comment"><label for="post_username">Name</label><br/><input type="text" name="post_username"/><br/><label for="post_body">Comment</label><br/><textarea name="post_body"></textarea><br/><input type="hidden" name="post_id" value="2"/><input type="submit" value="Add comment"/></form></div></body></html>
|
70
|
+
XML
|
71
|
+
end
|
72
|
+
|
73
|
+
approve "formats xml nicely", :format => :xml, :show_received => true do
|
74
|
+
"<xml testsdf=\"dsfsdf\"><test/><node><content attr='fgsd' /></node><node id='2'><content /></node></xml>"
|
75
|
+
end
|
66
76
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Katrina Owen
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-07-
|
17
|
+
date: 2011-07-29 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -31,6 +31,19 @@ dependencies:
|
|
31
31
|
version: "2.6"
|
32
32
|
type: :runtime
|
33
33
|
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: libxml-ruby
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
type: :runtime
|
46
|
+
version_requirements: *id002
|
34
47
|
description: An RSpec extension that adds support for approvals. Based on the idea of the Golden Master.
|
35
48
|
email:
|
36
49
|
- katrina.owen@gmail.com
|
@@ -58,6 +71,8 @@ files:
|
|
58
71
|
- spec/approvals/rspec_approvals_a_hash.approved.txt
|
59
72
|
- spec/approvals/rspec_approvals_a_string.approved.txt
|
60
73
|
- spec/approvals/rspec_approvals_an_array.approved.txt
|
74
|
+
- spec/approvals/rspec_approvals_formats_html_nicely.approved.txt
|
75
|
+
- spec/approvals/rspec_approvals_formats_xml_nicely.approved.txt
|
61
76
|
- spec/approvals_spec.rb
|
62
77
|
- spec/spec_helper.rb
|
63
78
|
has_rdoc: true
|