Permalink
Browse files

Merge branch 'master' of git@github.com:quirkey/halpers

Conflicts:
	rails_template.rb
  • Loading branch information...
2 parents dc63e12 + 2ecaa46 commit 1e7ebb9a3cd11704874e941e5347aac164b57318 @quirkey committed Feb 19, 2009
View
@@ -1,4 +1,4 @@
-%w{quirkey_helper quirkey_test_helper quirkey_extensions}.each do |lib|
+%w{quirkey_helper quirkey_test_helper quirkey_extensions deep_hash super_hash}.each do |lib|
require File.join(File.dirname(__FILE__), 'lib', lib)
end
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,39 @@
+module DeepHash
+
+ def deep_value(key_array)
+ value = self
+ Array(key_array).each do |key|
+ value = value.fetch(key, nil)
+ end
+ value
+ rescue NoMethodError
+ nil
+ end
+
+ def deep_merge(other_hash)
+ self.merge(other_hash) do |key, old_val, new_val|
+ self[key] = (old_val.is_a?(Hash) && new_val.is_a?(Hash)) ? old_val.deep_merge(new_val) : new_val
+ end
+ end
+
+ def deep_find_and_replace(with_value, &block)
+ self.each do |key,val|
+ if yield(val)
+ self[key] = with_value
+ else
+ self[key].deep_find_and_replace(with_value, &block) if self[key].is_a?(Hash)
+ end
+ end
+ self
+ end
+
+ def deep_each(&block)
+ self.each do |key, val|
+ if self[key].is_a?(Hash)
+ self[key].deep_each(&block)
+ end
+ yield(self, key, val)
+ end
+ end
+
+end
@@ -124,7 +124,7 @@ def simple_menu(*controllers, &block)
if controller_pair.is_a? Array
name, controller = controller_pair[0], controller_pair[1]
else
- name, controller = controller_pair.humanize.downcase, controller_pair
+ name, controller = controller_pair, controller_pair
end
html << yield(name,controller)
end
@@ -1,5 +1,13 @@
-module QuirkeyTestHelper
+$:.unshift(File.dirname(__FILE__))
+require 'test_helpers/upload'
+require 'test_helpers/collections'
+require 'test_helpers/login'
+module QuirkeyTestHelper
+ include UploadTestHelper
+ include CollectionsTestHelper
+ include LoginTestHelper
+
def assert_toggled(object, method = nil, toggled = true)
initial_value = object.send(method)
yield
@@ -12,18 +20,6 @@ def assert_same_as_before(object, method, &block)
assert_toggled object, method, false, &block
end
- def uploaded_file(path, content_type="application/octet-stream", filename=nil)
- filename ||= File.basename(path)
- t = Tempfile.new(filename)
- FileUtils.copy_file(path, t.path)
- (class << t; self; end;).class_eval do
- alias local_path path
- define_method(:original_filename) { filename }
- define_method(:content_type) { content_type }
- end
- return t
- end
-
def assert_flash(key, content)
assert flash.include?(key),
"#{key.inspect} missing from flash, has #{flash.keys.inspect}"
@@ -50,10 +46,6 @@ def assert_shows_errors(eid = 'errorExplanation')
assert_select "div##{eid}", 1, "Should display div with id=#{eid}"
end
- def assert_redirected_to_login
- assert_redirected_to "/login"
- end
-
def assert_dom_id(record, tag = 'div', count = 1)
dom_id = dom_id(record)
assert_select "#{tag}##{dom_id}", count, "Could not find #{tag} with id=#{dom_id}"
@@ -74,64 +66,10 @@ def assert_relative_times(expected, test)
assert((expected - test).to_i < 10 && (expected - test).to_i > - 10, "Times #{expected} : #{test} are not relative")
end
- def logout
- @request.session[:user] = nil
- end
-
- def assert_validates_presence_of(klass, meths)
- k = klass.create
- assert !k.id
- assert_errors_on k, meths
- end
-
- def assert_validates_uniqueness_of(test_instance,meths)
- k = test_instance.class.create(test_instance.attributes.reject {|i,v| i == 'id' })
- assert !k.id
- assert_errors_on k, meths
- end
-
- def assert_errors_on(ob, meths)
- meths.each do |meth|
+ def assert_errors_on(ob, *meths)
+ [meths].flatten.each do |meth|
assert ob.errors.on(meth), "#{ob.class}.#{meth} should have errors"
end
end
-
- def assert_all(collection)
- collection.each do |one|
- assert yield(one), "#{one.inspect} is not true"
- end
- end
-
- def assert_assigns(expected, assigned, message = nil)
- message ||= "should assign #{expected} to @#{assigned}"
- assert_equal(expected, assigns(assigned), message)
- end
-
- def assert_any(collection, &block)
- has = collection.any? do |one|
- yield(one)
- end
- assert has
- end
-
- def assert_ordered(array_of_ordered_items, message = nil, &block)
- raise "Parameter must be an Array" unless array_of_ordered_items.is_a?(Array)
- message ||= "Items were not in the correct order"
- i = 0
- # puts array_of_ordered_items.length
- while i < (array_of_ordered_items.length - 1)
- # puts "j"
- a, b = array_of_ordered_items[i], array_of_ordered_items[i+1]
- comparison = yield(a,b)
- # raise "#{comparison}"
- assert(comparison, message + " - #{a}, #{b}")
- i += 1
- end
- end
-
- def assert_set_of(klass, set)
- assert set.respond_to?(:each), "#{set.inspect} is not a set (does not include Enumerable)"
- assert_all(set) {|a| a.is_a?(klass) }
- end
end
View
@@ -0,0 +1,23 @@
+class SuperHash < HashWithIndifferentAccess
+ include DeepHash
+
+ def deep_merge(other_hash)
+ self.dup.to_hash.merge(other_hash) do |key, old_val, new_val|
+ self[key] = (old_val.is_a?(Hash) && new_val.is_a?(Hash)) ? old_val.deep_merge(new_val) : new_val
+ end
+ end
+
+ private
+ def method_missing(m,*a)
+ if m.to_s =~ /\?$/ && self.has_key?(m.to_s[0...-1])
+ return !!self[m.to_s[0...-1]]
+ end
+ if m.to_s =~ /=$/
+ self[$`] = a[0]
+ elsif a.empty?
+ self[m.to_s]
+ else
+ super
+ end
+ end
+end
@@ -0,0 +1,46 @@
+module CollectionsTestHelper
+
+ def assert_all(collection)
+ collection.each do |one|
+ assert yield(one), "#{one.inspect} is not true"
+ end
+ end
+
+ def assert_assigns(expected, assigned, message = nil)
+ message ||= "should assign #{expected} to @#{assigned}"
+ assert_equal(expected, assigns(assigned), message)
+ end
+
+ def assert_any(collection, &block)
+ has = collection.any? do |one|
+ yield(one)
+ end
+ assert has
+ end
+
+ def assert_ordered(array_of_ordered_items, message = nil, &block)
+ raise "Parameter must be an Array" unless array_of_ordered_items.is_a?(Array)
+ message ||= "Items were not in the correct order"
+ i = 0
+ # puts array_of_ordered_items.length
+ while i < (array_of_ordered_items.length - 1)
+ # puts "j"
+ a, b = array_of_ordered_items[i], array_of_ordered_items[i+1]
+ comparison = yield(a,b)
+ # raise "#{comparison}"
+ assert(comparison, message + " - #{a}, #{b}")
+ i += 1
+ end
+ end
+
+ def assert_set_of(klass, set)
+ assert set.respond_to?(:each), "#{set.inspect} is not a set (does not include Enumerable)"
+ assert_all(set) {|a| a.is_a?(klass) }
+ end
+
+ def assert_paginated(collection)
+ collection = assigns(collection) if collection.is_a?(Symbol)
+ assert collection, "Should not be nil"
+ assert collection.respond_to?(:current_page), "Should be a paginated collection"
+ end
+end
@@ -0,0 +1,11 @@
+module LoginTestHelper
+
+ def assert_redirected_to_login
+ assert_redirected_to "/login"
+ end
+
+ def logout
+ @request.session[:user] = nil
+ end
+
+end
@@ -0,0 +1,46 @@
+module UploadTestHelper
+
+ def uploaded_file(path, content_type="application/octet-stream", filename=nil)
+ filename ||= File.basename(path)
+ t = Tempfile.new(filename)
+ FileUtils.copy_file(path, t.path)
+ (class << t; self; end;).class_eval do
+ alias local_path path
+ define_method(:original_filename) { filename }
+ define_method(:content_type) { content_type }
+ end
+ return t
+ end
+
+ def public_file_on_fs(file_path)
+ File.join(Rails.root,'public',file_path)
+ end
+
+ def assert_public_file_is_readable(file_path)
+ assert File.readable?(public_file_on_fs(file_path)), "#{file_path} is not readable or does not exist"
+ end
+
+ def assert_public_file_is_not_readable(file_path)
+ assert !File.readable?(public_file_on_fs(file_path)), "#{file_path} is readable but it shouldnt be"
+ end
+
+ def uploaded_pdf
+ uploaded_file(uploaded_pdf_path,'application/pdf')
+ end
+
+ def uploaded_jpg(filename = 'test.jpg')
+ uploaded_file(uploaded_jpg_path(filename),'image/jpeg')
+ end
+
+ def uploaded_asset_path(filename)
+ File.expand_path(File.join(__FILE__,'..','..','assets',filename))
+ end
+
+ def uploaded_pdf_path
+ uploaded_asset_path('test.pdf')
+ end
+
+ def uploaded_jpg_path(filename = 'test.jpg')
+ uploaded_asset_path(filename)
+ end
+end
Oops, something went wrong. Retry.

0 comments on commit 1e7ebb9

Please sign in to comment.