Class: Docspec::Example

Inherits:
Object
  • Object
show all
Includes:
OutputCapturer
Defined in:
lib/docspec/example.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OutputCapturer

#capture_output

Constructor Details

#initialize(type:, code:, before: nil) ⇒ Example



8
9
10
# File 'lib/docspec/example.rb', line 8

def initialize(type:, code:, before: nil)
  @code, @type, @before = code, type, before
end

Instance Attribute Details

#beforeObject (readonly)

Returns the value of attribute before.



6
7
8
# File 'lib/docspec/example.rb', line 6

def before
  @before
end

#codeObject (readonly)

Returns the value of attribute code.



6
7
8
# File 'lib/docspec/example.rb', line 6

def code
  @code
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/docspec/example.rb', line 6

def type
  @type
end

Instance Method Details

#actualObject



12
13
14
# File 'lib/docspec/example.rb', line 12

def actual
  @actual ||= actual!
end

#consider_failed?Boolean



16
17
18
# File 'lib/docspec/example.rb', line 16

def consider_failed?
  failed? and !ignore_failure?
end

#diffObject



20
21
22
# File 'lib/docspec/example.rb', line 20

def diff
  @diff ||= Diffy::Diff.new("#{expected}\n", "#{actual}\n", context: 2).to_s :color
end

#empty?Boolean



24
25
26
# File 'lib/docspec/example.rb', line 24

def empty?
  expected.empty?
end

#expectedObject



28
29
30
# File 'lib/docspec/example.rb', line 28

def expected
  @expected ||= code.scan(/#=> *(.*)/).map { |match| match.first.strip }.join "\n"
end

#failed?Boolean



32
33
34
# File 'lib/docspec/example.rb', line 32

def failed?
  !success? && !empty?
end

#first_lineObject



36
37
38
# File 'lib/docspec/example.rb', line 36

def first_line
  @first_line ||= code.split("\n").first
end

#flagsObject



40
41
42
# File 'lib/docspec/example.rb', line 40

def flags
  @flags ||= first_line.scan(/\[:(.+?)\]/).map { |f| f.first.to_sym }
end

#full_codeObject



44
45
46
# File 'lib/docspec/example.rb', line 44

def full_code
  @full_code ||= full_code!
end

#ignore_failure?Boolean



48
49
50
# File 'lib/docspec/example.rb', line 48

def ignore_failure?
  flags.include? :ignore_failure
end

#labelObject



52
53
54
# File 'lib/docspec/example.rb', line 52

def label
  @label ||= label!
end

#skip?Boolean



56
57
58
# File 'lib/docspec/example.rb', line 56

def skip?
  flags.include? :skip
end

#success?Boolean



60
61
62
# File 'lib/docspec/example.rb', line 60

def success?
  actual == expected
end