aboutsummaryrefslogtreecommitdiffstats
path: root/spec/support
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/data_modifier.rb51
-rw-r--r--spec/support/data_modifier/bool.rb18
-rw-r--r--spec/support/data_modifier/box.rb22
-rw-r--r--spec/support/data_modifier/enum.rb19
-rw-r--r--spec/support/random.rb26
5 files changed, 136 insertions, 0 deletions
diff --git a/spec/support/data_modifier.rb b/spec/support/data_modifier.rb
new file mode 100644
index 000000000..7fb0db6f6
--- /dev/null
+++ b/spec/support/data_modifier.rb
@@ -0,0 +1,51 @@
+require_relative 'data_modifier/enum'
+module Support
+ module DataModifier
+ module InstanceMethods
+ CannotModify = Class.new RuntimeError
+
+ def advance_values(atts, *keys)
+ keys.inject(atts){ |h, k| h.merge( k => atts[k].next) }
+ end
+
+ # return array of atts wich each value modified, unboxing
+ # values if needed
+ def modify_atts(base_atts)
+ base_atts.keys.map do | key |
+ modify_att base_atts, key
+ end.compact
+ end
+
+ private
+ def modify_att atts, key
+ atts.merge(key => modify_value(atts[key]))
+ rescue CannotModify
+ nil
+ end
+ def modify_value value
+ case value
+ when String
+ "#{value}."
+ when Fixnum
+ value + 1
+ when TrueClass
+ false
+ when FalseClass
+ true
+ when Float
+ value * 1.1
+ when Date
+ value + 1.day
+ when Box
+ value.next.value
+ else
+ raise CannotModify
+ end
+ end
+ end
+ end
+end
+
+RSpec.configure do | c |
+ c.include Support::DataModifier::InstanceMethods, type: :checksum
+end
diff --git a/spec/support/data_modifier/bool.rb b/spec/support/data_modifier/bool.rb
new file mode 100644
index 000000000..f4be9bd89
--- /dev/null
+++ b/spec/support/data_modifier/bool.rb
@@ -0,0 +1,18 @@
+require_relative 'box'
+
+module Support
+ module DataModifier
+
+ class BoolBox
+ include Box
+ attr_reader :value
+
+ def initialize value
+ @value = value
+ end
+ def next
+ self.class.new(!value)
+ end
+ end
+ end
+end
diff --git a/spec/support/data_modifier/box.rb b/spec/support/data_modifier/box.rb
new file mode 100644
index 000000000..0fb95f743
--- /dev/null
+++ b/spec/support/data_modifier/box.rb
@@ -0,0 +1,22 @@
+module Support
+ module DataModifier
+ module Box
+ def next
+ raise "Need to implement #{__method__} in #{self.class}"
+ end
+
+ class << self
+ def unbox atts
+ atts.inject Hash.new do | h, (k,v) |
+ h.merge(k => value_of(v))
+ end
+ end
+ def value_of v
+ self === v ? v.value : v
+ end
+ end
+ end
+
+ end
+end
+
diff --git a/spec/support/data_modifier/enum.rb b/spec/support/data_modifier/enum.rb
new file mode 100644
index 000000000..c8a6fe573
--- /dev/null
+++ b/spec/support/data_modifier/enum.rb
@@ -0,0 +1,19 @@
+require_relative 'box'
+
+module Support
+ module DataModifier
+
+ class EnumBox
+ include Box
+ attr_reader :value, :values
+
+ def initialize *enum_values
+ @values = enum_values
+ @value = @values.first
+ end
+ def next
+ self.class.new(*(@values[1..-1] << @values.first))
+ end
+ end
+ end
+end
diff --git a/spec/support/random.rb b/spec/support/random.rb
new file mode 100644
index 000000000..f0168b9ef
--- /dev/null
+++ b/spec/support/random.rb
@@ -0,0 +1,26 @@
+module Support
+ module Random
+
+ PRETTY_LARGE_INT = 1 << 30
+
+ def random_hex
+ SecureRandom.hex
+ end
+
+ def random_int
+ (random_number * PRETTY_LARGE_INT).to_i
+ end
+
+ def random_number
+ SecureRandom.random_number
+ end
+
+ def random_string
+ SecureRandom.urlsafe_base64
+ end
+ end
+end
+
+RSpec.configure do | c |
+ c.include Support::Random
+end