flipstone-matchers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in flipstone-matchers.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "flipstone-matchers/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "flipstone-matchers"
7
+ s.version = Flipstone::Matchers::VERSION
8
+ s.authors = ["David Vollbracht, Scott Conley"]
9
+ s.email = ["development@flipstone.com"]
10
+ s.homepage = "http://github.com/flipstone/flipstone-matchers"
11
+ s.summary = %q{Some useful RSpec matchers we've used on multiple projects}
12
+ s.description = %q{Some useful RSpec matchers we've used on multiple projects}
13
+
14
+ s.add_dependency 'rspec', ">= 2.6.0"
15
+
16
+ s.rubyforge_project = "flipstone-matchers"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,6 @@
1
+ require "rspec"
2
+ require "flipstone-matchers/version"
3
+
4
+ Dir[File.join(File.dirname(__FILE__), 'flipstone-matchers/**/*.rb')].each do |file|
5
+ require file
6
+ end
@@ -0,0 +1,16 @@
1
+ RSpec::Matchers.define :all do |matcher|
2
+ match do |array|
3
+ array.all? do |item|
4
+ matcher.matches? item
5
+ end
6
+ end
7
+
8
+ failure_message_for_should do |array|
9
+ array.each_with_index.map do |item, idx|
10
+ unless matcher.matches? item
11
+ "item #{idx} (#{item}): #{matcher.failure_message_for_should}"
12
+ end
13
+ end.compact.join("\n")
14
+ end
15
+ end
16
+
@@ -0,0 +1,19 @@
1
+ RSpec::Matchers.define :all_be_within do |allowable_delta|
2
+ chain :of do |expected_array|
3
+ @expected_array = expected_array
4
+ end
5
+
6
+ def within?(left, right, delta)
7
+ (left - right).abs < delta
8
+ end
9
+
10
+ match do |actual_array|
11
+ @expected_array.count == actual_array.count &&
12
+ actual_array.zip(@expected_array).all? { |left, right| within?(left, right, allowable_delta)}
13
+ end
14
+
15
+ failure_message_for_should do |actual_array|
16
+ "Expected all of #{actual_array} to be within #{allowable_delta} of #{@expected_array}"
17
+ end
18
+ end
19
+
@@ -0,0 +1,24 @@
1
+ RSpec::Matchers.define :be_in_order_by do |expression, options = {}|
2
+ match do |actual|
3
+ @values_to_be_sorted_by = actual.map do |o|
4
+ o.instance_eval expression
5
+ end
6
+
7
+ @sorted = @values_to_be_sorted_by.sort
8
+
9
+ if options[:descending]
10
+ @sorted = @sorted.reverse
11
+ end
12
+
13
+ @sorted == @values_to_be_sorted_by
14
+ end
15
+
16
+ failure_message_for_should do |actual|
17
+ <<-end_message
18
+ Expected items to be in order by #{expression}
19
+ Expected: #{@sorted}
20
+ Actual: #{@values_to_be_sorted_by}
21
+ end_message
22
+ end
23
+ end
24
+
@@ -0,0 +1,41 @@
1
+ RSpec::Matchers.define :be_protected_attribute_of do |model_class, value|
2
+ match do |attribute|
3
+ new_instance = model_class.new attribute => value
4
+ @mass_assignable = (new_instance.send(attribute) == value)
5
+
6
+ saved_instance = model_class.new
7
+ saved_instance.update_protected_attributes(
8
+ Factory.attributes_for(model_class.name.underscore,
9
+ attribute => value)
10
+ )
11
+
12
+ @updateable = (saved_instance.send(attribute) == value)
13
+
14
+ clear_instance = model_class.new
15
+ clear_instance.send("#{attribute}=", value)
16
+ clear_instance.update_protected_attributes({})
17
+
18
+ @cleared_without_request = (clear_instance.send(attribute) != value)
19
+
20
+ !@mass_assignable && @updateable && !@cleared_without_request
21
+ end
22
+
23
+ failure_message_for_should do |attribute|
24
+ errors = ["Expected #{attribute} to be a protected attribute"]
25
+
26
+ if @mass_assignable
27
+ errors << " But it was mass assignable via hash"
28
+ end
29
+
30
+ if !@updateable
31
+ errors << " But it was not updateable via update_protected_attributes"
32
+ end
33
+
34
+ if @cleared_without_request
35
+ errors << " But it was cleared out by update_protected_attributes({})"
36
+ end
37
+
38
+ errors.join("\n")
39
+ end
40
+ end
41
+
@@ -0,0 +1,13 @@
1
+ RSpec::Matchers.define :be_same_set_as do |expected|
2
+ match do |actual|
3
+ Set.new(actual) == Set.new(expected)
4
+ end
5
+
6
+ failure_message_for_should do |actual|
7
+ <<-message
8
+ expected #{actual.inspect} have same set of elements as #{expected.inspect}.
9
+ actual values: #{actual.to_a.inspect}
10
+ expected values: #{expected.to_a.inspect}
11
+ message
12
+ end
13
+ end
@@ -0,0 +1,75 @@
1
+ RSpec::Matchers.define :match_an_email do
2
+ chain :subject do
3
+ @email_field = :subject
4
+ end
5
+
6
+ chain :body do
7
+ @email_field = :body
8
+ end
9
+
10
+ chain :just_one do
11
+ @count = 1
12
+ end
13
+
14
+ chain :to do |user|
15
+ @to_email = user.email
16
+ end
17
+
18
+ match do |actual|
19
+ actual = [actual] unless actual.is_a?(Array)
20
+ matchers = actual.map do |actual|
21
+ case actual
22
+ when String then /#{actual}/
23
+ else actual
24
+ end
25
+ end
26
+
27
+ @all_fields = ActionMailer::Base.deliveries.select do |email|
28
+ @to_email.nil? || email.to.include?(@to_email)
29
+ end.map(&@email_field)
30
+
31
+ @matches = @all_fields.select do |value|
32
+ matchers.all? { |matcher| matcher === value.to_s }
33
+ end
34
+
35
+ @count_matches = true
36
+
37
+ if @count
38
+ matched = @matches.size == @count
39
+ else
40
+ matched = @matches.any?
41
+ end
42
+
43
+ @email_field && matched
44
+ end
45
+
46
+ failure_message_for_should do |actual|
47
+ if @email_field.blank?
48
+ "No email field specified. Did you mean should match_an_email.subject ?"
49
+ elsif @count
50
+ message = []
51
+ message << "Expected #{@count} #{@to_email} emails with #{@email_field} matching #{actual}"
52
+ message << "Matching fields were: ["
53
+ message << " " + @matches.join(",\n ")
54
+ message << "]"
55
+ message.join("\n")
56
+ else
57
+ message = []
58
+ message << "Found no email with #{@email_field} matching #{actual}"
59
+ message << "Actual message fields were: ["
60
+ message << " " + @all_fields.join(",\n ")
61
+ message << "]"
62
+ message.join("\n")
63
+ end
64
+ end
65
+
66
+ failure_message_for_should_not do |actual|
67
+ message = []
68
+ message << "Found #{@to_email} email with #{@email_field} matching #{actual}"
69
+ message << "The matching items where: ["
70
+ message << " " + @matches.join(",\n ")
71
+ message << "]"
72
+ message.join("\n")
73
+ end
74
+ end
75
+
@@ -0,0 +1,5 @@
1
+ module Flipstone
2
+ module Matchers
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flipstone-matchers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Vollbracht, Scott Conley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2154337120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.6.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2154337120
25
+ description: Some useful RSpec matchers we've used on multiple projects
26
+ email:
27
+ - development@flipstone.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - flipstone-matchers.gemspec
36
+ - lib/flipstone-matchers.rb
37
+ - lib/flipstone-matchers/all.rb
38
+ - lib/flipstone-matchers/all_be_within.rb
39
+ - lib/flipstone-matchers/be_in_order_by.rb
40
+ - lib/flipstone-matchers/be_protected_attribute_of.rb
41
+ - lib/flipstone-matchers/be_same_set_as.rb
42
+ - lib/flipstone-matchers/match_an_email.rb
43
+ - lib/flipstone-matchers/version.rb
44
+ homepage: http://github.com/flipstone/flipstone-matchers
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project: flipstone-matchers
64
+ rubygems_version: 1.8.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Some useful RSpec matchers we've used on multiple projects
68
+ test_files: []