rspectacular 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rspectacular.rb +1 -1
- data/lib/rspectacular/{matchers.rb → active_record.rb} +0 -0
- data/lib/rspectacular/active_record/date_range_matcher.rb +45 -39
- data/lib/rspectacular/active_record/dateliness_matcher.rb +37 -31
- data/lib/rspectacular/active_record/persistence_matcher.rb +29 -23
- data/lib/rspectacular/active_record/positivity_matcher.rb +31 -25
- data/lib/rspectacular/active_record/truthfulness_matcher.rb +28 -22
- data/lib/rspectacular/rspec.rb +17 -0
- data/lib/rspectacular/version.rb +1 -1
- data/rspectacular.gemspec +2 -0
- metadata +18 -15
data/lib/rspectacular.rb
CHANGED
File without changes
|
@@ -1,41 +1,47 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
1
|
+
module RSpectacular
|
2
|
+
module ActiveRecord
|
3
|
+
module Matchers
|
4
|
+
class DateRangeMatcher
|
5
|
+
include Shoulda::ActiveRecord::Helpers
|
6
|
+
|
7
|
+
def initialize(dates)
|
8
|
+
@begin_field = dates[:from].to_s
|
9
|
+
@end_field = dates[:to].to_s
|
10
|
+
|
11
|
+
@begin_method = "#{dates[:from].to_s}=".to_sym
|
12
|
+
@end_method = "#{dates[:to].to_s}=".to_sym
|
13
|
+
end
|
14
|
+
|
15
|
+
def matches?(rangeable)
|
16
|
+
@rangeable = rangeable
|
17
|
+
beginning_date = Date.today
|
18
|
+
ending_date = Date.today - 1
|
19
|
+
|
20
|
+
rangeable.send(@begin_method, beginning_date)
|
21
|
+
rangeable.send(@end_method, ending_date)
|
22
|
+
|
23
|
+
rangeable.valid?
|
24
|
+
|
25
|
+
rangeable.errors[@begin_field].include?("must be on or before #{ending_date.to_s}") &&
|
26
|
+
rangeable.errors[@end_field].include?("must be on or after #{beginning_date.to_s}")
|
27
|
+
end
|
28
|
+
|
29
|
+
def failure_message
|
30
|
+
"#{@rangeable_class} should only accept a valid date range from #{@begin_field} to #{@end_field}, got: #{pretty_error_messages(@rangeable)}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def negative_failure_message
|
34
|
+
"#{@rangeable_class} shouldn't accept a valid date range from #{@begin_field} to #{@end_field}, got: #{pretty_error_messages(@rangeable)}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def description
|
38
|
+
"should be contain a valid range for #{@rangeable_class}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def validate_date_range(dates)
|
43
|
+
DateRangeMatcher.new(dates)
|
44
|
+
end
|
45
|
+
end
|
24
46
|
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
47
|
end
|
@@ -1,33 +1,39 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
1
|
+
module RSpectacular
|
2
|
+
module ActiveRecord
|
3
|
+
module Matchers
|
4
|
+
class DatelinessMatcher
|
5
|
+
include Shoulda::ActiveRecord::Helpers
|
6
|
+
|
7
|
+
def initialize(date_field)
|
8
|
+
@date_field = date_field.to_s
|
9
|
+
@date_method = "#{date_field.to_s}=".to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
def matches?(dateable)
|
13
|
+
@dateable = dateable
|
14
|
+
dateable.send(@date_method, Date.today)
|
15
|
+
|
16
|
+
dateable.valid?
|
17
|
+
|
18
|
+
!dateable.errors[@date_field].include?("is not a valid date")
|
19
|
+
end
|
20
|
+
|
21
|
+
def failure_message
|
22
|
+
"Expected #{@date_field} to contain a valid date, got: #{pretty_error_messages(@dateable)}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def negative_failure_message
|
26
|
+
"Expected #{@date_field} to contain an invalid date, got: #{pretty_error_messages(@dateable)}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def description
|
30
|
+
"should be contain a valid date"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate_dateliness_of(date)
|
35
|
+
DatelinessMatcher.new(date)
|
36
|
+
end
|
37
|
+
end
|
20
38
|
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
39
|
end
|
@@ -1,29 +1,35 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module RSpectacular
|
2
|
+
module ActiveRecord
|
3
|
+
module Matchers
|
4
|
+
class PersistenceMatcher
|
5
|
+
def initialize(persistable_object)
|
6
|
+
@persistable_class = persistable_object.class
|
7
|
+
@desired_attributes = persistable_object.is_a?(Hash) ? persistable_object : persistable_object.attributes
|
8
|
+
@desired_attributes = @desired_attributes.reject {|k,v| ['id', 'created_at', 'updated_at'].include? k}
|
9
|
+
end
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
+
def matches?(subject)
|
12
|
+
@persistable_class.where(@desired_attributes).first
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
+
def failure_message
|
16
|
+
"Expected to find an object saved with #{@desired_attributes}, didn't find one."
|
17
|
+
end
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
def negative_failure_message
|
20
|
+
"Didn't expect to find an object saved with #{@desired_attributes}, but got one anyway"
|
21
|
+
end
|
19
22
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
23
|
+
def description
|
24
|
+
"should be an instance of #{@expected_class_name}"
|
25
|
+
end
|
26
|
+
end
|
24
27
|
|
25
|
-
def persist(desired_object)
|
26
|
-
|
27
|
-
end
|
28
|
+
def persist(desired_object)
|
29
|
+
PersistenceMatcher.new(desired_object)
|
30
|
+
end
|
28
31
|
|
29
|
-
alias :persist_the :persist
|
32
|
+
alias :persist_the :persist
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,33 +1,39 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module RSpectacular
|
2
|
+
module ActiveRecord
|
3
|
+
module Matchers
|
4
|
+
class PositivityMatcher < Shoulda::ActiveRecord::Matchers::ValidateNumericalityOfMatcher
|
5
|
+
def matches?(positivitable)
|
6
|
+
super(positivitable)
|
4
7
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
+
disallows_zero_values &&
|
9
|
+
disallows_negative_values
|
10
|
+
end
|
8
11
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
+
def failure_message
|
13
|
+
"Expected #{@attribute.to_s} to be a positive number, got: #{pretty_error_messages(@attribute)}"
|
14
|
+
end
|
12
15
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
+
def negative_failure_message
|
17
|
+
"Expected #{@attribute.to_s} to be a negative number, got: #{pretty_error_messages(@attribute)}"
|
18
|
+
end
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
def disallows_negative_values
|
21
|
+
disallows_value_of(-1) &&
|
22
|
+
disallows_value_of(-100)
|
23
|
+
end
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
def disallows_zero_values
|
26
|
+
disallows_value_of(0)
|
27
|
+
end
|
25
28
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
29
|
+
def description
|
30
|
+
"should be contain a positive number"
|
31
|
+
end
|
32
|
+
end
|
30
33
|
|
31
|
-
def validate_positivity_of(date)
|
32
|
-
|
34
|
+
def validate_positivity_of(date)
|
35
|
+
PositivityMatcher.new(date)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
33
39
|
end
|
@@ -1,29 +1,35 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module RSpectacular
|
2
|
+
module ActiveRecord
|
3
|
+
module Matchers
|
4
|
+
class TruthfulnessMatcher < Shoulda::ActiveRecord::Matchers::ValidationMatcher
|
5
|
+
def matches?(truthable)
|
6
|
+
super(truthable)
|
4
7
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
allows_true &&
|
9
|
+
allows_false &&
|
10
|
+
disallows_nil
|
11
|
+
end
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
+
def allows_true
|
14
|
+
allows_value_of(true)
|
15
|
+
end
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
def allows_false
|
18
|
+
allows_value_of(false)
|
19
|
+
end
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
+
def disallows_nil
|
22
|
+
disallows_value_of(nil)
|
23
|
+
end
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
25
|
+
def description
|
26
|
+
"should accept only boolean true or false values"
|
27
|
+
end
|
28
|
+
end
|
26
29
|
|
27
|
-
def validate_truthfulness_of(truthteller)
|
28
|
-
|
30
|
+
def validate_truthfulness_of(truthteller)
|
31
|
+
TruthfulnessMatcher.new(truthteller)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
29
35
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rspectacular/active_record/matchers'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Matchers
|
5
|
+
include RSpectacular::ActiveRecord::Matchers
|
6
|
+
end
|
7
|
+
|
8
|
+
# module Rails
|
9
|
+
# module ControllerExampleGroup
|
10
|
+
# include Shoulda::ActionController::Matchers
|
11
|
+
# end
|
12
|
+
|
13
|
+
# module MailerExampleGroup
|
14
|
+
# include Shoulda::ActionMailer::Matchers
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
end
|
data/lib/rspectacular/version.rb
CHANGED
data/rspectacular.gemspec
CHANGED
@@ -18,4 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency('shoulda', '~> 2.11.0')
|
21
23
|
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspectacular
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: 0.0.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- jfelchner
|
@@ -15,10 +11,20 @@ autorequire:
|
|
15
11
|
bindir: bin
|
16
12
|
cert_chain: []
|
17
13
|
|
18
|
-
date: 2011-
|
14
|
+
date: 2011-05-12 00:00:00 -05:00
|
19
15
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: shoulda
|
19
|
+
prerelease: false
|
20
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
none: false
|
22
|
+
requirements:
|
23
|
+
- - ~>
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 2.11.0
|
26
|
+
type: :runtime
|
27
|
+
version_requirements: *id001
|
22
28
|
description: We rock some custom RSpec matchers like it ain't nobody's bidnezz.
|
23
29
|
email:
|
24
30
|
- support@thekompanee.com
|
@@ -33,13 +39,14 @@ files:
|
|
33
39
|
- Gemfile
|
34
40
|
- Rakefile
|
35
41
|
- lib/rspectacular.rb
|
42
|
+
- lib/rspectacular/active_record.rb
|
36
43
|
- lib/rspectacular/active_record/date_range_matcher.rb
|
37
44
|
- lib/rspectacular/active_record/dateliness_matcher.rb
|
38
45
|
- lib/rspectacular/active_record/matchers.rb
|
39
46
|
- lib/rspectacular/active_record/persistence_matcher.rb
|
40
47
|
- lib/rspectacular/active_record/positivity_matcher.rb
|
41
48
|
- lib/rspectacular/active_record/truthfulness_matcher.rb
|
42
|
-
- lib/rspectacular/
|
49
|
+
- lib/rspectacular/rspec.rb
|
43
50
|
- lib/rspectacular/version.rb
|
44
51
|
- rails/init.rb
|
45
52
|
- rspectacular.gemspec
|
@@ -57,21 +64,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
64
|
requirements:
|
58
65
|
- - ">="
|
59
66
|
- !ruby/object:Gem::Version
|
60
|
-
segments:
|
61
|
-
- 0
|
62
67
|
version: "0"
|
63
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
69
|
none: false
|
65
70
|
requirements:
|
66
71
|
- - ">="
|
67
72
|
- !ruby/object:Gem::Version
|
68
|
-
segments:
|
69
|
-
- 0
|
70
73
|
version: "0"
|
71
74
|
requirements: []
|
72
75
|
|
73
76
|
rubyforge_project: rspectacular
|
74
|
-
rubygems_version: 1.
|
77
|
+
rubygems_version: 1.6.2
|
75
78
|
signing_key:
|
76
79
|
specification_version: 3
|
77
80
|
summary: Custom RSpec matchers
|