Class: Docspec::Example
- Inherits:
-
Object
- Object
- Docspec::Example
- Includes:
- OutputCapturer
- Defined in:
- lib/docspec/example.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#full_code ⇒ Object
readonly
Returns the value of attribute full_code.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #actual ⇒ Object
- #diff ⇒ Object
- #empty? ⇒ Boolean
- #expected ⇒ Object
- #first_line ⇒ Object
- #flags ⇒ Object
- #ignore_failure? ⇒ Boolean
-
#initialize(code, type) ⇒ Example
constructor
A new instance of Example.
- #label ⇒ Object
- #prepend(codes) ⇒ Object
- #success? ⇒ Boolean
Methods included from OutputCapturer
Constructor Details
#initialize(code, type) ⇒ Example
Returns a new instance of Example.
8 9 10 11 |
# File 'lib/docspec/example.rb', line 8 def initialize(code, type) @code, @type = code, type @full_code = @code end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
6 7 8 |
# File 'lib/docspec/example.rb', line 6 def code @code end |
#full_code ⇒ Object (readonly)
Returns the value of attribute full_code.
6 7 8 |
# File 'lib/docspec/example.rb', line 6 def full_code @full_code end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/docspec/example.rb', line 6 def type @type end |
Instance Method Details
#actual ⇒ Object
13 14 15 |
# File 'lib/docspec/example.rb', line 13 def actual @actual ||= actual! end |
#diff ⇒ Object
17 18 19 |
# File 'lib/docspec/example.rb', line 17 def diff @diff ||= Diffy::Diff.new("#{expected}\n", "#{actual}\n", context: 2).to_s :color end |
#empty? ⇒ Boolean
21 22 23 |
# File 'lib/docspec/example.rb', line 21 def empty? actual.empty? end |
#expected ⇒ Object
25 26 27 |
# File 'lib/docspec/example.rb', line 25 def expected @expected ||= code.scan(/#=>\s*(.*)/).map { |match| match.first.strip }.join "\n" end |
#first_line ⇒ Object
29 30 31 |
# File 'lib/docspec/example.rb', line 29 def first_line @first_line ||= code.split("\n").first end |
#flags ⇒ Object
33 34 35 |
# File 'lib/docspec/example.rb', line 33 def flags @flags ||= first_line.scan(/\[:(.+?)\]/).map { |f| f.first.to_sym } end |
#ignore_failure? ⇒ Boolean
37 38 39 |
# File 'lib/docspec/example.rb', line 37 def ignore_failure? flags.include? :ignore_failure end |
#label ⇒ Object
41 42 43 |
# File 'lib/docspec/example.rb', line 41 def label @label ||= label! end |
#prepend(codes) ⇒ Object
45 46 47 48 49 |
# File 'lib/docspec/example.rb', line 45 def prepend(codes) codes = [codes] unless codes.is_a? Array codes = codes.join "\n\n" @full_code = "#{codes}\n#{@full_code}" end |
#success? ⇒ Boolean
51 52 53 |
# File 'lib/docspec/example.rb', line 51 def success? actual == expected end |