Class: RSpec::Approvals::Approval

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/approvals/approval.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = '', options = {})
  @name = Approval.normalize(example.full_description)
  @path = Approvals.path + name
  @options = options
  @received = received
  @formatter = Formatter.new(self)

  example.options[:approval] = true
  example.options[:approval_diff_paths] = {
    :received => received_path,
    :approved => approved_path,
  }
end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



17
18
19
# File 'lib/rspec/approvals/approval.rb', line 17

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/rspec/approvals/approval.rb', line 17

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/rspec/approvals/approval.rb', line 17

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/rspec/approvals/approval.rb', line 17

def path
  @path
end

#receivedObject (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_pathObject



37
38
39
# File 'lib/rspec/approvals/approval.rb', line 37

def approved_path
  "#{@path}.approved.txt"
end

#approved_textObject



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_pathObject



45
46
47
# File 'lib/rspec/approvals/approval.rb', line 45

def diff_path
  "#{received_path} #{approved_path}"
end

#failure_messageObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rspec/approvals/approval.rb', line 79

def failure_message
  <<-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_pathObject



41
42
43
# File 'lib/rspec/approvals/approval.rb', line 41

def received_path
  "#{@path}.received.txt"
end

#received_textObject



57
58
59
# File 'lib/rspec/approvals/approval.rb', line 57

def received_text
  @received_text ||= Formatter.new(self).as_s(received)
end

#verifyObject



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, failure_message, 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