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(code, type) ⇒ Example

Returns a new instance of Example.



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

def initialize(code, type)
  @code, @type = code, type
  @full_code = @code
end

Instance Attribute Details

#code ⇒ Object (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#full_code ⇒ Object (readonly)

Returns the value of attribute full_code.



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

def full_code
  @full_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



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

def actual
  @actual ||= actual!
end

#diff ⇒ Object



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

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

#empty? ⇒ Boolean

Returns:

  • (Boolean)


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

def empty?
  actual.empty?
end

#expected ⇒ Object



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

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

#first_line ⇒ Object



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

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

#flags ⇒ Object



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

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

#ignore_failure? ⇒ Boolean

Returns:

  • (Boolean)


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

def ignore_failure?
  flags.include? :ignore_failure
end

#label ⇒ Object



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

def label
  @label ||= label!
end

#prepend(codes) ⇒ Object



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

def prepend(codes)
  codes = [codes] unless codes.is_a? Array
  codes = codes.join "\n\n"
  @full_code = "#{codes}\n#{@full_code}"
end

#success? ⇒ Boolean

Returns:

  • (Boolean)


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

def success?
  actual == expected
end