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

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

#beforeObject (readonly)

Returns the value of attribute before.



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

def before
  @before
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#actualObject



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

def actual
  @actual ||= actual!
end

#consider_failed?Boolean

Returns:

  • (Boolean)


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

def consider_failed?
  failed? and !ignore_failure?
end

#diffObject



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

Returns:

  • (Boolean)


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

def empty?
  expected.empty?
end

#expectedObject



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

Returns:

  • (Boolean)


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

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

#first_lineObject



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

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

#flagsObject



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

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

#full_codeObject



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

def full_code
  @full_code ||= full_code!
end

#ignore_failure?Boolean

Returns:

  • (Boolean)


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

def ignore_failure?
  flags.include? :ignore_failure
end

#labelObject



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

def label
  @label ||= label!
end

#skip?Boolean

Returns:

  • (Boolean)


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

def skip?
  flags.include? :skip
end

#success?Boolean

Returns:

  • (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