Sha256: ea98f63e2d61e401bdc93e21be744d4db684a024e556af73cb1290bc0f8a0644

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

require 'date'
require 'active_support'
require 'chronic'

module CucumberValueParser
  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

  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

  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
end

include CucumberValueParser

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sniff-1.1.1 lib/test_support/cucumber/support/values.rb
sniff-1.1.0 lib/test_support/cucumber/support/values.rb
sniff-1.0.1 lib/test_support/cucumber/support/values.rb
sniff-1.0.0 lib/test_support/cucumber/support/values.rb
sniff-0.11.12 lib/test_support/cucumber/support/values.rb
sniff-0.11.11 lib/test_support/cucumber/support/values.rb