Class: Docspec::Example
- Inherits:
-
Object
- Object
- Docspec::Example
- Includes:
- OutputCapturer
- Defined in:
- lib/docspec/example.rb
Instance Attribute Summary collapse
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #actual ⇒ Object
- #consider_failed? ⇒ Boolean
- #diff ⇒ Object
- #empty? ⇒ Boolean
- #expected ⇒ Object
- #failed? ⇒ Boolean
- #first_line ⇒ Object
- #flags ⇒ Object
- #full_code ⇒ Object
- #ignore_failure? ⇒ Boolean
-
#initialize(type:, code:, before: nil) ⇒ Example
constructor
A new instance of Example.
- #label ⇒ Object
- #skip? ⇒ Boolean
- #success? ⇒ Boolean
Methods included from OutputCapturer
Constructor Details
#initialize(type:, code:, before: nil) ⇒ Example
Returns a new instance of Example.
10 11 12 13 14 |
# File 'lib/docspec/example.rb', line 10 def initialize(type:, code:, before: nil) @code = code @type = type @before = before end |
Instance Attribute Details
#before ⇒ Object (readonly)
Returns the value of attribute before.
8 9 10 |
# File 'lib/docspec/example.rb', line 8 def before @before end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
8 9 10 |
# File 'lib/docspec/example.rb', line 8 def code @code end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/docspec/example.rb', line 8 def type @type end |
Instance Method Details
#actual ⇒ Object
16 17 18 |
# File 'lib/docspec/example.rb', line 16 def actual @actual ||= actual! end |
#consider_failed? ⇒ Boolean
20 21 22 |
# File 'lib/docspec/example.rb', line 20 def consider_failed? failed? and !ignore_failure? end |
#diff ⇒ Object
24 25 26 |
# File 'lib/docspec/example.rb', line 24 def diff @diff ||= Diffy::Diff.new("#{expected}\n", "#{actual}\n", context: 2).to_s :color end |
#empty? ⇒ Boolean
28 29 30 |
# File 'lib/docspec/example.rb', line 28 def empty? expected.empty? end |
#expected ⇒ Object
32 33 34 |
# File 'lib/docspec/example.rb', line 32 def expected @expected ||= code.scan(/#=> *(.*)/).map { |match| match.first.strip }.join "\n" end |
#failed? ⇒ Boolean
36 37 38 |
# File 'lib/docspec/example.rb', line 36 def failed? !success? && !empty? end |
#first_line ⇒ Object
40 41 42 |
# File 'lib/docspec/example.rb', line 40 def first_line @first_line ||= code.split("\n").first end |
#flags ⇒ Object
44 45 46 |
# File 'lib/docspec/example.rb', line 44 def flags @flags ||= first_line.scan(/\[:(.+?)\]/).map { |f| f.first.to_sym } end |
#full_code ⇒ Object
48 49 50 |
# File 'lib/docspec/example.rb', line 48 def full_code @full_code ||= full_code! end |
#ignore_failure? ⇒ Boolean
52 53 54 |
# File 'lib/docspec/example.rb', line 52 def ignore_failure? flags.include? :ignore_failure end |
#label ⇒ Object
56 57 58 |
# File 'lib/docspec/example.rb', line 56 def label @label ||= label! end |
#skip? ⇒ Boolean
60 61 62 |
# File 'lib/docspec/example.rb', line 60 def skip? flags.include? :skip end |
#success? ⇒ Boolean
64 65 66 67 68 69 70 |
# File 'lib/docspec/example.rb', line 64 def success? if expected.include? '...' expected.ellipses_match? actual else actual == expected end end |