class Barometer::Support::Matchers::HaveField
Public Class Methods
new(field)
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 11 def initialize(field) @field = field end
Public Instance Methods
description()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 26 def description "have data field \"#{@field}\"" end
failure_message()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 22 def failure_message "Expected #{expectation} (#{@problem})" end
matches?(subject)
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 15 def matches?(subject) @subject = subject has_field? && type_casts_as_type? && sets_value? end
of_type(type)
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 30 def of_type(type) @type = type self end
Protected Instance Methods
has_field?()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 37 def has_field? assert @subject.respond_to?(@field), "does not have field '#{@field}'" end
sets_value?()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 58 def sets_value? if type_is_time? set_value "10 15 30 2013 01 01 am", "%H %M %S %Y %m %d %p" assert value.to_i == ::Time.utc(2013,01,01,10,15,30).to_i, "expected value of '2013-01-01 10:15:30 am', got '#{print_value}'" elsif type_is_sun? rise = ::Time.utc(2013,1,1,10,15,30) set = ::Time.utc(2013,1,1,18,14,56) set_value @type.new(rise: rise, set: set) assert print_value == "rise: 10:15, set: 18:14", "expected value of 'rise: 10:15, set: 18:14'', got '#{print_value}'" elsif type_is_symbol? set_value 'foo' assert value == :foo, "expected value of ':foo', got '#{value}'" elsif type_is_hash? set_value(foo: 'bar') assert value == {foo: 'bar'}, "expected value of '{foo: 'bar'}', got '#{value}'" else set_value 10 assert value.to_i == 10, "expected value of '10', got '#{value.to_i}'" end end
type_casts_as_type?()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 41 def type_casts_as_type? if type_is_time? set_value "2013-01-01 10:15:30 am" elsif type_is_sun? rise = ::Time.utc(2013,1,1,10,15,30) set = ::Time.utc(2013,1,1,18,14,56) set_value @type.new(rise: rise, set: set) elsif type_is_symbol? set_value 'foo' elsif type_is_hash? set_value(foo: 'bar') else set_value 10 end assert value.is_a?(@type), "#{@field} does not typecast as #{@type}" end
Private Instance Methods
assert(test, failure_message)
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 137 def assert(test, failure_message) if test true else @problem = failure_message false end end
expectation()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 105 def expectation "\"#{@field}\" to be a #{@type}" end
imperial_units()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 101 def imperial_units @type.send(:new, false).units end
metric_units()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 97 def metric_units @type.send(:new, true).units end
print_value()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 93 def print_value value.to_s end
set_value(*value)
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 85 def set_value(*value) if type_is_time? @subject.send("#{@field}=", value) else @subject.send("#{@field}=", *value) end end
type_is_hash?()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 133 def type_is_hash? @type == ::Hash end
type_is_sun?()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 125 def type_is_sun? @type.to_s.end_with? 'Data::Sun' end
type_is_symbol?()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 129 def type_is_symbol? @type == ::Symbol end
type_is_time?()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 121 def type_is_time? @type == ::Time end
value()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 81 def value @subject.send(@field) end
value_responds_to_metric?()
click to toggle source
# File lib/barometer/support/matchers/have_field.rb, line 109 def value_responds_to_metric? if type_is_time? false elsif @type == Float # rubinius does not like Float.new being called on the next line # so avoid it false else @type.respond_to?(:new) && @type.new.respond_to?(:metric) end end