Class: Wrong::StringComparison

Inherits:
Object
  • Object
show all
Defined in:
lib/wrong/message/string_comparison.rb

Constant Summary collapse

@@window =
[64, Terminal.width - 20].min
@@prelude =
12

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, second) ⇒ StringComparison

Returns a new instance of StringComparison.



24
25
26
27
# File 'lib/wrong/message/string_comparison.rb', line 24

def initialize(first, second)
  @first = first
  @second = second
end

Class Method Details

.preludeObject



16
17
18
# File 'lib/wrong/message/string_comparison.rb', line 16

def self.prelude
  @@prelude
end

.prelude=(val) ⇒ Object



20
21
22
# File 'lib/wrong/message/string_comparison.rb', line 20

def self.prelude=(val)
  @@prelude = val
end

.windowObject



8
9
10
# File 'lib/wrong/message/string_comparison.rb', line 8

def self.window
  @@window
end

.window=(val) ⇒ Object



12
13
14
# File 'lib/wrong/message/string_comparison.rb', line 12

def self.window=(val)
  @@window = val
end

Instance Method Details

#chunk(s) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wrong/message/string_comparison.rb', line 54

def chunk(s)
  prefix, middle, suffix = "...", "", "..."

  start = different_at - @@prelude
  if start < 0
    prefix = ""
    start = 0
  end

  stop = start + @@window
  if stop >= s.size
    suffix = ""
    stop = s.size
  end

  [prefix, s[start...stop].inspect, suffix].join
end

#different_atObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wrong/message/string_comparison.rb', line 33

def different_at
  if (@first.nil? || @second.nil?)
    0
  else
    i = 0
    while (i < @first.size && i < @second.size)
      if @first[i] != @second[i]
        break
      end
      i += 1
    end
    return i
  end
end

#messageObject



48
49
50
51
52
# File 'lib/wrong/message/string_comparison.rb', line 48

def message
  "Strings differ at position #{different_at}:\n" +
          " first: #{chunk(@first)}\n" +
          "second: #{chunk(@second)}"
end

#same?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/wrong/message/string_comparison.rb', line 29

def same?
  @first == @second
end