Class: LineByLine::Checker

Inherits:
Struct
  • Object
show all
Defined in:
lib/linebyline.rb

Overview

The class that handles the actual checks between two lines

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#failureObject (readonly)

Returns the value of attribute failure.



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

def failure
  @failure
end

#linenoObject

Returns the value of attribute lineno

Returns:

  • (Object)

    the current value of lineno



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

def lineno
  @lineno
end

#output_lineObject

Returns the value of attribute output_line

Returns:

  • (Object)

    the current value of output_line



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

def output_line
  @output_line
end

#ref_lineObject

Returns the value of attribute ref_line

Returns:

  • (Object)

    the current value of ref_line



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

def ref_line
  @ref_line
end

Instance Method Details

#eof?Boolean

Returns true if both lines are nil

Returns:

  • (Boolean)


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

def eof?
  ref_line.nil? && output_line.nil?
end

#fail!(with_message) ⇒ Object

Records the failure and the details on where it occurred in the buffer into the @failure ivar



39
40
41
42
43
44
45
46
# File 'lib/linebyline.rb', line 39

def fail! with_message
  full_message = [with_message + " at line #{lineno}"]
  full_message << " + #{ref_line.inspect}"
  full_message << " - #{output_line.inspect}"
  
  @failure = full_message.join("\n")
  throw :fail
end

#register_mismatches!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/linebyline.rb', line 10

def register_mismatches!
  catch :fail do
    if ref_line.nil? && output_line
      fail! "Reference buffer ended, but the output still has data"
    end
  
    if ref_line && output_line.nil?
      fail! "Reference contains chars, while the output buffer ended"
    end
  
    if ref_line != output_line
      min_len = [ref_line.length, output_line.length].sort.shift
      min_len.times do | offset |
        if ref_line[offset..offset] != output_line[offset..offset]
          fail! "Line mismatch at column #{offset + 1}"
        end
      end
      fail! "Lines have different lengths (%d expected, but was %d)" % [ref_line.length, output_line.length]
    end
  end
end