rspectacular 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,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rspectacular.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,3 @@
1
+ if defined? RSpec
2
+ require 'rspectacular/active_record'
3
+ end
@@ -0,0 +1,41 @@
1
+ class DateRangeMatcher
2
+ include Shoulda::ActiveRecord::Helpers
3
+
4
+ def initialize(dates)
5
+ @begin_field = dates[:from].to_s
6
+ @end_field = dates[:to].to_s
7
+
8
+ @begin_method = "#{dates[:from].to_s}=".to_sym
9
+ @end_method = "#{dates[:to].to_s}=".to_sym
10
+ end
11
+
12
+ def matches?(rangeable)
13
+ @rangeable = rangeable
14
+ beginning_date = Date.today
15
+ ending_date = Date.today - 1
16
+
17
+ rangeable.send(@begin_method, beginning_date)
18
+ rangeable.send(@end_method, ending_date)
19
+
20
+ rangeable.valid?
21
+
22
+ rangeable.errors[@begin_field].include?("must be on or before #{ending_date.to_s}") &&
23
+ rangeable.errors[@end_field].include?("must be on or after #{beginning_date.to_s}")
24
+ end
25
+
26
+ def failure_message
27
+ "#{@rangeable_class} should only accept a valid date range from #{@begin_field} to #{@end_field}, got: #{pretty_error_messages(@rangeable)}"
28
+ end
29
+
30
+ def negative_failure_message
31
+ "#{@rangeable_class} shouldn't accept a valid date range from #{@begin_field} to #{@end_field}, got: #{pretty_error_messages(@rangeable)}"
32
+ end
33
+
34
+ def description
35
+ "should be contain a valid range for #{@rangeable_class}"
36
+ end
37
+ end
38
+
39
+ def validate_date_range(dates)
40
+ DateRangeMatcher.new(dates)
41
+ end
@@ -0,0 +1,33 @@
1
+ class DatelinessMatcher
2
+ include Shoulda::ActiveRecord::Helpers
3
+
4
+ def initialize(date_field)
5
+ @date_field = date_field.to_s
6
+ @date_method = "#{date_field.to_s}=".to_sym
7
+ end
8
+
9
+ def matches?(dateable)
10
+ @dateable = dateable
11
+ dateable.send(@date_method, Date.today)
12
+
13
+ dateable.valid?
14
+
15
+ !dateable.errors[@date_field].include?("is not a valid date")
16
+ end
17
+
18
+ def failure_message
19
+ "Expected #{@date_field} to contain a valid date, got: #{pretty_error_messages(@dateable)}"
20
+ end
21
+
22
+ def negative_failure_message
23
+ "Expected #{@date_field} to contain an invalid date, got: #{pretty_error_messages(@dateable)}"
24
+ end
25
+
26
+ def description
27
+ "should be contain a valid date"
28
+ end
29
+ end
30
+
31
+ def validate_dateliness_of(date)
32
+ DatelinessMatcher.new(date)
33
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspectacular/active_record/date_range_matcher'
2
+ require 'rspectacular/active_record/dateliness_matcher'
3
+ require 'rspectacular/active_record/persistence_matcher'
4
+ require 'rspectacular/active_record/positivity_matcher'
5
+ require 'rspectacular/active_record/truthfulness_matcher'
@@ -0,0 +1,29 @@
1
+ class PersistenceMatcher
2
+ def initialize(persistable_object)
3
+ @persistable_class = persistable_object.class
4
+ @desired_attributes = persistable_object.is_a?(Hash) ? persistable_object : persistable_object.attributes
5
+ @desired_attributes = @desired_attributes.reject {|k,v| ['id', 'created_at', 'updated_at'].include? k}
6
+ end
7
+
8
+ def matches?(subject)
9
+ @persistable_class.where(@desired_attributes).first
10
+ end
11
+
12
+ def failure_message
13
+ "Expected to find an object saved with #{@desired_attributes}, didn't find one."
14
+ end
15
+
16
+ def negative_failure_message
17
+ "Didn't expect to find an object saved with #{@desired_attributes}, but got one anyway"
18
+ end
19
+
20
+ def description
21
+ "should be an instance of #{@expected_class_name}"
22
+ end
23
+ end
24
+
25
+ def persist(desired_object)
26
+ PersistenceMatcher.new(desired_object)
27
+ end
28
+
29
+ alias :persist_the :persist
@@ -0,0 +1,33 @@
1
+ class PositivityMatcher < Shoulda::ActiveRecord::Matchers::ValidateNumericalityOfMatcher
2
+ def matches?(positivitable)
3
+ super(positivitable)
4
+
5
+ disallows_zero_values &&
6
+ disallows_negative_values
7
+ end
8
+
9
+ def failure_message
10
+ "Expected #{@attribute.to_s} to be a positive number, got: #{pretty_error_messages(@attribute)}"
11
+ end
12
+
13
+ def negative_failure_message
14
+ "Expected #{@attribute.to_s} to be a negative number, got: #{pretty_error_messages(@attribute)}"
15
+ end
16
+
17
+ def disallows_negative_values
18
+ disallows_value_of(-1) &&
19
+ disallows_value_of(-100)
20
+ end
21
+
22
+ def disallows_zero_values
23
+ disallows_value_of(0)
24
+ end
25
+
26
+ def description
27
+ "should be contain a positive number"
28
+ end
29
+ end
30
+
31
+ def validate_positivity_of(date)
32
+ PositivityMatcher.new(date)
33
+ end
@@ -0,0 +1,29 @@
1
+ class TruthfulnessMatcher < Shoulda::ActiveRecord::Matchers::ValidationMatcher
2
+ def matches?(truthable)
3
+ super(truthable)
4
+
5
+ allows_true &&
6
+ allows_false &&
7
+ disallows_nil
8
+ end
9
+
10
+ def allows_true
11
+ allows_value_of(true)
12
+ end
13
+
14
+ def allows_false
15
+ allows_value_of(false)
16
+ end
17
+
18
+ def disallows_nil
19
+ disallows_value_of(nil)
20
+ end
21
+
22
+ def description
23
+ "should accept only boolean true or false values"
24
+ end
25
+ end
26
+
27
+ def validate_truthfulness_of(truthteller)
28
+ TruthfulnessMatcher.new(truthteller)
29
+ end
@@ -0,0 +1 @@
1
+ require 'rspectacular/active_record/matchers'
@@ -0,0 +1,3 @@
1
+ module Rspectacular
2
+ VERSION = "0.0.1"
3
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ if RAILS_ENV == 'test'
2
+ if defined? RSpec
3
+ require 'rspectacular/matchers'
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rspectacular/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rspectacular"
7
+ s.version = Rspectacular::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["jfelchner", "thekompanee"]
10
+ s.email = ["support@thekompanee.com"]
11
+ s.homepage = "https://github.com/jfelchner/rspectacular"
12
+ s.summary = %q{Custom RSpec matchers}
13
+ s.description = %q{We rock some custom RSpec matchers like it ain't nobody's bidnezz.}
14
+
15
+ s.rubyforge_project = "rspectacular"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspectacular
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - jfelchner
13
+ - thekompanee
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-30 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: We rock some custom RSpec matchers like it ain't nobody's bidnezz.
23
+ email:
24
+ - support@thekompanee.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - lib/rspectacular.rb
36
+ - lib/rspectacular/active_record/date_range_matcher.rb
37
+ - lib/rspectacular/active_record/dateliness_matcher.rb
38
+ - lib/rspectacular/active_record/matchers.rb
39
+ - lib/rspectacular/active_record/persistence_matcher.rb
40
+ - lib/rspectacular/active_record/positivity_matcher.rb
41
+ - lib/rspectacular/active_record/truthfulness_matcher.rb
42
+ - lib/rspectacular/matchers.rb
43
+ - lib/rspectacular/version.rb
44
+ - rails/init.rb
45
+ - rspectacular.gemspec
46
+ has_rdoc: true
47
+ homepage: https://github.com/jfelchner/rspectacular
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project: rspectacular
74
+ rubygems_version: 1.3.7
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Custom RSpec matchers
78
+ test_files: []
79
+