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.



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

#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



14
15
16
# File 'lib/docspec/example.rb', line 14

def actual
  @actual ||= actual!
end

#consider_failed?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/docspec/example.rb', line 18

def consider_failed?
  failed? and !ignore_failure?
end

#diffObject



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

Returns:

  • (Boolean)


26
27
28
# File 'lib/docspec/example.rb', line 26

def empty?
  expected.empty?
end

#expectedObject



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

Returns:

  • (Boolean)


34
35
36
# File 'lib/docspec/example.rb', line 34

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

#first_lineObject



38
39
40
# File 'lib/docspec/example.rb', line 38

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

#flagsObject



42
43
44
# File 'lib/docspec/example.rb', line 42

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

#full_codeObject



46
47
48
# File 'lib/docspec/example.rb', line 46

def full_code
  @full_code ||= full_code!
end

#ignore_failure?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/docspec/example.rb', line 50

def ignore_failure?
  flags.include? :ignore_failure
end

#labelObject



54
55
56
# File 'lib/docspec/example.rb', line 54

def label
  @label ||= label!
end

#skip?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/docspec/example.rb', line 58

def skip?
  flags.include? :skip
end

#success?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/docspec/example.rb', line 62

def success?
  actual == expected
end