Module: CucumberValueParser

Defined in:
lib/test_support/cucumber/support/values.rb

Instance Method Summary collapse

Instance Method Details

#coerce_value(value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/test_support/cucumber/support/values.rb', line 6

def coerce_value(value)
  # what is this, PHP?
  if value.blank?
    nil
  elsif value == 'true'
    true
  elsif value == 'false'
    false
  elsif value =~ /\d+.*,.*\d/ # ???
    value
  elsif value =~ /^-?\d+\.\d+$/ # float
    value.to_f
  elsif value =~ /^-?\d+(\.\d+)?\.\.-?\d+(\.\d+)?$/ # range
    (value.split('..')[0].to_i)..(value.split('..')[1].to_i)
  elsif value =~ /^0+$/ # all zeros => 0
    0
  elsif value =~ /^0/ # zero preceding anything but a float or range => the input as a string
    value
  elsif value =~ /^-?\d+$/ # integer
    value.to_i
  elsif value =~ /Address:/
    value.sub(/Address:\s*/,'')
  elsif value.is_a?(String) and date = Chronic.parse(value)
    date
  else
    value
  end
end

#compare_values(a, b) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/test_support/cucumber/support/values.rb', line 35

def compare_values(a, b)
  if b.blank?
    a.should be_blank
  elsif a.is_a?(Float)
    a.should be_within(0.00001).of(b)
  elsif a.is_a? Date
    a.strftime('%Y-%m-%d').should == b.strftime('%Y-%m-%d')
  elsif a.is_a? Time
    a.strftime('%Y-%m-%d %H:%M:%s').should == b.strftime('%Y-%m-%d %H:%M:%s')
  else
    a.should == b
  end
end

#equality?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/test_support/cucumber/support/values.rb', line 49

def equality?(a, b)
  if b.blank?
    a.blank?
  elsif a.is_a? Date or a.is_a? Time
    b = Date.parse b
    a == b
  elsif b =~ /\d+.*,.*\d/
    a == b
  elsif b =~ /\d+\.\d+/
    (a.to_f - b.to_f).abs <= 0.00001
  elsif b =~ /^0/
    a == b
  elsif b =~ /^\d+$/
    a.to_i == b.to_i
  else
    a == b
  end
end