Class: RSpec::Approvals::Approval
- Inherits:
-
Object
- Object
- RSpec::Approvals::Approval
- Defined in:
- lib/rspec/approvals/approval.rb
Instance Attribute Summary collapse
-
#location ⇒ Object
Returns the value of attribute location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#received ⇒ Object
readonly
Returns the value of attribute received.
Class Method Summary collapse
Instance Method Summary collapse
- #approved_path ⇒ Object
- #approved_text ⇒ Object
- #diff_path ⇒ Object
- #failure_message ⇒ Object
-
#initialize(example, received = '', options = {}) ⇒ Approval
constructor
A new instance of Approval.
- #received_path ⇒ Object
- #received_text ⇒ Object
- #verify ⇒ Object
- #write(path, contents) ⇒ Object
Constructor Details
#initialize(example, received = '', options = {}) ⇒ Approval
Returns a new instance of Approval.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rspec/approvals/approval.rb', line 19 def initialize(example, received = '', = {}) @name = Approval.normalize(example.full_description) @path = Approvals.path + name @options = @received = received @formatter = Formatter.new(self) example.[:approval] = true example.[:approval_diff_paths] = { :received => received_path, :approved => approved_path, } end |
Instance Attribute Details
#location ⇒ Object
Returns the value of attribute location.
17 18 19 |
# File 'lib/rspec/approvals/approval.rb', line 17 def location @location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/rspec/approvals/approval.rb', line 17 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/rspec/approvals/approval.rb', line 17 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/rspec/approvals/approval.rb', line 17 def path @path end |
#received ⇒ Object (readonly)
Returns the value of attribute received.
17 18 19 |
# File 'lib/rspec/approvals/approval.rb', line 17 def received @received end |
Class Method Details
.normalize(s) ⇒ Object
12 13 14 |
# File 'lib/rspec/approvals/approval.rb', line 12 def normalize(s) s.gsub(/[\W]/, ' ').strip.squeeze(" ").gsub(' ', '_').downcase end |
Instance Method Details
#approved_path ⇒ Object
37 38 39 |
# File 'lib/rspec/approvals/approval.rb', line 37 def approved_path "#{@path}.approved.txt" end |
#approved_text ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rspec/approvals/approval.rb', line 49 def approved_text if File.exists?(approved_path) File.read(approved_path) else EmptyApproval.new.inspect end end |
#diff_path ⇒ Object
45 46 47 |
# File 'lib/rspec/approvals/approval.rb', line 45 def diff_path "#{received_path} #{approved_path}" end |
#failure_message ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rspec/approvals/approval.rb', line 79 def <<-FAILURE_MESSAGE Approval Failure: The received contents did not match the approved contents. Inspect the differences in the following files: #{received_path} #{approved_path} FAILURE_MESSAGE end |
#received_path ⇒ Object
41 42 43 |
# File 'lib/rspec/approvals/approval.rb', line 41 def received_path "#{@path}.received.txt" end |
#received_text ⇒ Object
57 58 59 |
# File 'lib/rspec/approvals/approval.rb', line 57 def received_text @received_text ||= Formatter.new(self).as_s(received) end |
#verify ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rspec/approvals/approval.rb', line 61 def verify unless received_text == approved_text write(received_path, received_text) Dotfile.append(diff_path) if received.respond_to?(:on_failure) received.on_failure.call(approved_text) received.on_failure.call(received_text) end raise RSpec::Approvals::ReceivedDiffersError, , location end end |
#write(path, contents) ⇒ Object
73 74 75 76 77 |
# File 'lib/rspec/approvals/approval.rb', line 73 def write(path, contents) File.open(path, 'w') do |f| f.write contents end end |