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.
8 9 10 11 12 |
# File 'lib/docspec/example.rb', line 8 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.
6 7 8 |
# File 'lib/docspec/example.rb', line 6 def before @before end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
6 7 8 |
# File 'lib/docspec/example.rb', line 6 def code @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
14 15 16 |
# File 'lib/docspec/example.rb', line 14 def actual @actual ||= actual! end |
#consider_failed? ⇒ Boolean
18 19 20 |
# File 'lib/docspec/example.rb', line 18 def consider_failed? failed? and !ignore_failure? end |
#diff ⇒ Object
22 23 24 |
# File 'lib/docspec/example.rb', line 22 def diff @diff ||= Diffy::Diff.new("#{expected}\n", "#{actual}\n", context: 2).to_s :color end |
#empty? ⇒ Boolean
26 27 28 |
# File 'lib/docspec/example.rb', line 26 def empty? expected.empty? end |
#expected ⇒ Object
30 31 32 |
# File 'lib/docspec/example.rb', line 30 def expected @expected ||= code.scan(/#=> *(.*)/).map { |match| match.first.strip }.join "\n" end |
#failed? ⇒ Boolean
34 35 36 |
# File 'lib/docspec/example.rb', line 34 def failed? !success? && !empty? end |
#first_line ⇒ Object
38 39 40 |
# File 'lib/docspec/example.rb', line 38 def first_line @first_line ||= code.split("\n").first end |
#flags ⇒ Object
42 43 44 |
# File 'lib/docspec/example.rb', line 42 def flags @flags ||= first_line.scan(/\[:(.+?)\]/).map { |f| f.first.to_sym } end |
#full_code ⇒ Object
46 47 48 |
# File 'lib/docspec/example.rb', line 46 def full_code @full_code ||= full_code! end |
#ignore_failure? ⇒ Boolean
50 51 52 |
# File 'lib/docspec/example.rb', line 50 def ignore_failure? flags.include? :ignore_failure end |
#label ⇒ Object
54 55 56 |
# File 'lib/docspec/example.rb', line 54 def label @label ||= label! end |
#skip? ⇒ Boolean
58 59 60 |
# File 'lib/docspec/example.rb', line 58 def skip? flags.include? :skip end |
#success? ⇒ Boolean
62 63 64 |
# File 'lib/docspec/example.rb', line 62 def success? actual == expected end |