carlosbrando-remarkable 0.0.99
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Manifest.txt +124 -0
- data/PostInstall.txt +7 -0
- data/README.rdoc +48 -0
- data/Rakefile +29 -0
- data/init.rb +1 -0
- data/lib/remarkable.rb +13 -0
- data/lib/remarkable/active_record/active_record.rb +21 -0
- data/lib/remarkable/active_record/helpers.rb +16 -0
- data/lib/remarkable/active_record/macros/associations/belong_to.rb +81 -0
- data/lib/remarkable/active_record/macros/associations/have_and_belong_to_many.rb +77 -0
- data/lib/remarkable/active_record/macros/associations/have_many.rb +160 -0
- data/lib/remarkable/active_record/macros/associations/have_one.rb +133 -0
- data/lib/remarkable/active_record/macros/database/have_db_column.rb +81 -0
- data/lib/remarkable/active_record/macros/database/have_db_columns.rb +73 -0
- data/lib/remarkable/active_record/macros/database/have_indices.rb +75 -0
- data/lib/remarkable/active_record/macros/validations/allow_values_for.rb +103 -0
- data/lib/remarkable/active_record/macros/validations/ensure_length_at_least.rb +97 -0
- data/lib/remarkable/active_record/macros/validations/ensure_length_in_range.rb +134 -0
- data/lib/remarkable/active_record/macros/validations/ensure_length_is.rb +106 -0
- data/lib/remarkable/active_record/macros/validations/ensure_value_in_range.rb +117 -0
- data/lib/remarkable/active_record/macros/validations/have_class_methods.rb +74 -0
- data/lib/remarkable/active_record/macros/validations/have_instance_methods.rb +74 -0
- data/lib/remarkable/active_record/macros/validations/have_named_scope.rb +148 -0
- data/lib/remarkable/active_record/macros/validations/have_readonly_attributes.rb +81 -0
- data/lib/remarkable/active_record/macros/validations/only_allow_numeric_values_for.rb +89 -0
- data/lib/remarkable/active_record/macros/validations/protect_attributes.rb +89 -0
- data/lib/remarkable/active_record/macros/validations/require_acceptance_of.rb +94 -0
- data/lib/remarkable/active_record/macros/validations/require_attributes.rb +94 -0
- data/lib/remarkable/active_record/macros/validations/require_unique_attributes.rb +146 -0
- data/lib/remarkable/controller/controller.rb +15 -0
- data/lib/remarkable/controller/helpers.rb +48 -0
- data/lib/remarkable/controller/macros/assign_to.rb +110 -0
- data/lib/remarkable/controller/macros/filter_params.rb +52 -0
- data/lib/remarkable/controller/macros/redirect_to.rb +24 -0
- data/lib/remarkable/controller/macros/render_a_form.rb +23 -0
- data/lib/remarkable/controller/macros/render_template.rb +18 -0
- data/lib/remarkable/controller/macros/render_with_layout.rb +61 -0
- data/lib/remarkable/controller/macros/respond_with.rb +86 -0
- data/lib/remarkable/controller/macros/respond_with_content_type.rb +45 -0
- data/lib/remarkable/controller/macros/return_from_session.rb +45 -0
- data/lib/remarkable/controller/macros/route.rb +91 -0
- data/lib/remarkable/controller/macros/set_the_flash_to.rb +58 -0
- data/lib/remarkable/example/example_methods.rb +32 -0
- data/lib/remarkable/private_helpers.rb +123 -0
- data/rails/init.rb +1 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/controllers/posts_controller_spec.rb +166 -0
- data/spec/controllers/users_controller_spec.rb +14 -0
- data/spec/fixtures/addresses.yml +3 -0
- data/spec/fixtures/friendships.yml +0 -0
- data/spec/fixtures/posts.yml +5 -0
- data/spec/fixtures/products.yml +0 -0
- data/spec/fixtures/taggings.yml +0 -0
- data/spec/fixtures/tags.yml +9 -0
- data/spec/fixtures/users.yml +6 -0
- data/spec/models/address_spec.rb +21 -0
- data/spec/models/dog_spec.rb +17 -0
- data/spec/models/flea_spec.rb +9 -0
- data/spec/models/friendship_spec.rb +13 -0
- data/spec/models/post_spec.rb +35 -0
- data/spec/models/product_spec.rb +57 -0
- data/spec/models/tag_spec.rb +21 -0
- data/spec/models/tagging_spec.rb +13 -0
- data/spec/models/user_spec.rb +107 -0
- data/spec/rails_root/app/controllers/application.rb +25 -0
- data/spec/rails_root/app/controllers/posts_controller.rb +85 -0
- data/spec/rails_root/app/controllers/users_controller.rb +84 -0
- data/spec/rails_root/app/helpers/application_helper.rb +3 -0
- data/spec/rails_root/app/helpers/posts_helper.rb +2 -0
- data/spec/rails_root/app/helpers/users_helper.rb +2 -0
- data/spec/rails_root/app/models/address.rb +7 -0
- data/spec/rails_root/app/models/dog.rb +5 -0
- data/spec/rails_root/app/models/flea.rb +3 -0
- data/spec/rails_root/app/models/friendship.rb +4 -0
- data/spec/rails_root/app/models/post.rb +12 -0
- data/spec/rails_root/app/models/product.rb +12 -0
- data/spec/rails_root/app/models/tag.rb +8 -0
- data/spec/rails_root/app/models/tagging.rb +4 -0
- data/spec/rails_root/app/models/user.rb +28 -0
- data/spec/rails_root/app/views/layouts/posts.rhtml +17 -0
- data/spec/rails_root/app/views/layouts/users.rhtml +17 -0
- data/spec/rails_root/app/views/layouts/wide.html.erb +1 -0
- data/spec/rails_root/app/views/posts/edit.rhtml +27 -0
- data/spec/rails_root/app/views/posts/index.rhtml +25 -0
- data/spec/rails_root/app/views/posts/new.rhtml +26 -0
- data/spec/rails_root/app/views/posts/show.rhtml +18 -0
- data/spec/rails_root/app/views/users/edit.rhtml +22 -0
- data/spec/rails_root/app/views/users/index.rhtml +22 -0
- data/spec/rails_root/app/views/users/new.rhtml +21 -0
- data/spec/rails_root/app/views/users/show.rhtml +13 -0
- data/spec/rails_root/config/boot.rb +109 -0
- data/spec/rails_root/config/database.yml +4 -0
- data/spec/rails_root/config/environment.rb +14 -0
- data/spec/rails_root/config/environments/sqlite3.rb +0 -0
- data/spec/rails_root/config/initializers/new_rails_defaults.rb +15 -0
- data/spec/rails_root/config/initializers/shoulda.rb +8 -0
- data/spec/rails_root/config/routes.rb +6 -0
- data/spec/rails_root/db/migrate/001_create_users.rb +18 -0
- data/spec/rails_root/db/migrate/002_create_posts.rb +13 -0
- data/spec/rails_root/db/migrate/003_create_taggings.rb +12 -0
- data/spec/rails_root/db/migrate/004_create_tags.rb +11 -0
- data/spec/rails_root/db/migrate/005_create_dogs.rb +12 -0
- data/spec/rails_root/db/migrate/006_create_addresses.rb +14 -0
- data/spec/rails_root/db/migrate/007_create_fleas.rb +11 -0
- data/spec/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
- data/spec/rails_root/db/migrate/009_create_products.rb +17 -0
- data/spec/rails_root/db/migrate/010_create_friendships.rb +14 -0
- data/spec/rails_root/db/schema.rb +0 -0
- data/spec/rails_root/log/.keep +0 -0
- data/spec/rails_root/public/.htaccess +40 -0
- data/spec/rails_root/public/404.html +30 -0
- data/spec/rails_root/public/422.html +30 -0
- data/spec/rails_root/public/500.html +30 -0
- data/spec/rails_root/script/console +3 -0
- data/spec/rails_root/script/generate +3 -0
- data/spec/rails_root/vendor/plugins/.keep +0 -0
- data/spec/rcov.opts +2 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +58 -0
- data/tasks/rspec.rake +21 -0
- metadata +216 -0
@@ -0,0 +1,97 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class EnsureLengthAtLeast
|
6
|
+
include Remarkable::Private
|
7
|
+
|
8
|
+
def initialize(attribute, min_length, opts)
|
9
|
+
@attribute = attribute
|
10
|
+
@min_length = min_length
|
11
|
+
@opts = opts
|
12
|
+
end
|
13
|
+
|
14
|
+
def matches?(klass)
|
15
|
+
@klass = klass
|
16
|
+
|
17
|
+
begin
|
18
|
+
valid_value = "x" * (@min_length)
|
19
|
+
fail("not allow #{@attribute} to be at least #{@min_length} chars long") unless assert_good_value(klass, @attribute, valid_value, /is too short/)
|
20
|
+
|
21
|
+
if @min_length > 0
|
22
|
+
min_value = "x" * (@min_length - 1)
|
23
|
+
fail("allow #{@attribute} to be less than #{@min_length} chars long") unless assert_bad_value(klass, @attribute, min_value, /is too short/)
|
24
|
+
end
|
25
|
+
|
26
|
+
true
|
27
|
+
rescue Exception => e
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def description
|
33
|
+
"allow #{@attribute} to be at least #{@min_length} chars long"
|
34
|
+
end
|
35
|
+
|
36
|
+
def failure_message
|
37
|
+
@failure_message || "expected allow #{@attribute} to be at least #{@min_length} chars long, but it didn't"
|
38
|
+
end
|
39
|
+
|
40
|
+
def negative_failure_message
|
41
|
+
"expected not allow #{@attribute} to be at least #{@min_length} chars long, but it did"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Ensures that the length of the attribute is at least a certain length
|
46
|
+
#
|
47
|
+
# If an instance variable has been created in the setup named after the
|
48
|
+
# model being tested, then this method will use that. Otherwise, it will
|
49
|
+
# create a new instance to test against.
|
50
|
+
#
|
51
|
+
# Options:
|
52
|
+
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
53
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % min_length</tt>
|
54
|
+
#
|
55
|
+
# Example:
|
56
|
+
# it { Tag.should ensure_length_at_least(:name, 3) }
|
57
|
+
#
|
58
|
+
def ensure_length_at_least(attribute, min_length, opts = {})
|
59
|
+
Remarkable::Syntax::RSpec::EnsureLengthAtLeast.new(attribute, min_length, opts)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
module Shoulda
|
64
|
+
# Ensures that the length of the attribute is at least a certain length
|
65
|
+
#
|
66
|
+
# If an instance variable has been created in the setup named after the
|
67
|
+
# model being tested, then this method will use that. Otherwise, it will
|
68
|
+
# create a new instance to test against.
|
69
|
+
#
|
70
|
+
# Options:
|
71
|
+
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
72
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % min_length</tt>
|
73
|
+
#
|
74
|
+
# Example:
|
75
|
+
# should_ensure_length_at_least :name, 3
|
76
|
+
#
|
77
|
+
def should_ensure_length_at_least(attribute, min_length, opts = {})
|
78
|
+
short_message = get_options!([opts], :short_message)
|
79
|
+
short_message ||= default_error_message(:too_short, :count => min_length)
|
80
|
+
|
81
|
+
klass = model_class
|
82
|
+
|
83
|
+
if min_length > 0
|
84
|
+
min_value = "x" * (min_length - 1)
|
85
|
+
it "should not allow #{attribute} to be less than #{min_length} chars long" do
|
86
|
+
assert_bad_value(klass, attribute, min_value, short_message).should be_true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
it "should allow #{attribute} to be at least #{min_length} chars long" do
|
90
|
+
valid_value = "x" * (min_length)
|
91
|
+
assert_good_value(klass, attribute, valid_value, short_message).should be_true
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class EnsureLengthInRange
|
6
|
+
include Remarkable::Private
|
7
|
+
|
8
|
+
def initialize(attribute, range, opts)
|
9
|
+
@min_length = range.first
|
10
|
+
@max_length = range.last
|
11
|
+
@same_length = (@min_length == @max_length)
|
12
|
+
|
13
|
+
@attribute = attribute
|
14
|
+
@range = range
|
15
|
+
@opts = opts
|
16
|
+
end
|
17
|
+
|
18
|
+
def matches?(klass)
|
19
|
+
@klass = klass
|
20
|
+
|
21
|
+
begin
|
22
|
+
if @min_length > 0
|
23
|
+
min_value = "x" * @min_length
|
24
|
+
fail("not allow #{@attribute} to be exactly #{@min_length} chars long") unless assert_good_value(klass, @attribute, min_value, /is too short/)
|
25
|
+
end
|
26
|
+
|
27
|
+
unless @same_length
|
28
|
+
max_value = "x" * @max_length
|
29
|
+
fail("not allow #{@attribute} to be exactly #{@max_length} chars long") unless assert_good_value(klass, @attribute, max_value, /is too long/)
|
30
|
+
end
|
31
|
+
|
32
|
+
if @min_length > 0
|
33
|
+
min_value = "x" * (@min_length - 1)
|
34
|
+
fail("allow #{@attribute} to be less than #{@min_length} chars long") unless assert_bad_value(klass, @attribute, min_value, /is too short/)
|
35
|
+
end
|
36
|
+
|
37
|
+
max_value = "x" * (@max_length + 1)
|
38
|
+
fail("allow #{@attribute} to be more than #{@max_length} chars long") unless assert_bad_value(klass, @attribute, max_value, /is too long/)
|
39
|
+
|
40
|
+
true
|
41
|
+
rescue Exception => e
|
42
|
+
false
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def description
|
47
|
+
"ensure that the length of the #{@attribute} is in #{@range}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def failure_message
|
51
|
+
@failure_message || "expected that the length of the #{@attribute} is in #{@range}, but it didn't"
|
52
|
+
end
|
53
|
+
|
54
|
+
def negative_failure_message
|
55
|
+
"expected that the length of the #{@attribute} isn't in #{@range}, but it did"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Ensures that the length of the attribute is in the given range
|
60
|
+
#
|
61
|
+
# If an instance variable has been created in the setup named after the
|
62
|
+
# model being tested, then this method will use that. Otherwise, it will
|
63
|
+
# create a new instance to test against.
|
64
|
+
#
|
65
|
+
# Options:
|
66
|
+
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
67
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % range.first</tt>
|
68
|
+
# * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
69
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_long') % range.last</tt>
|
70
|
+
#
|
71
|
+
# Example:
|
72
|
+
# it { User.should ensure_length_in_range(:password, 6..20) }
|
73
|
+
#
|
74
|
+
def ensure_length_in_range(attribute, range, opts = {})
|
75
|
+
Remarkable::Syntax::RSpec::EnsureLengthInRange.new(attribute, range, opts)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
module Shoulda
|
80
|
+
# Ensures that the length of the attribute is in the given range
|
81
|
+
#
|
82
|
+
# If an instance variable has been created in the setup named after the
|
83
|
+
# model being tested, then this method will use that. Otherwise, it will
|
84
|
+
# create a new instance to test against.
|
85
|
+
#
|
86
|
+
# Options:
|
87
|
+
# * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
88
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % range.first</tt>
|
89
|
+
# * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
90
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.too_long') % range.last</tt>
|
91
|
+
#
|
92
|
+
# Example:
|
93
|
+
# should_ensure_length_in_range :password, (6..20)
|
94
|
+
#
|
95
|
+
def should_ensure_length_in_range(attribute, range, opts = {})
|
96
|
+
short_message, long_message = get_options!([opts], :short_message, :long_message)
|
97
|
+
short_message ||= default_error_message(:too_short, :count => range.first)
|
98
|
+
long_message ||= default_error_message(:too_long, :count => range.last)
|
99
|
+
|
100
|
+
klass = model_class
|
101
|
+
min_length = range.first
|
102
|
+
max_length = range.last
|
103
|
+
same_length = (min_length == max_length)
|
104
|
+
|
105
|
+
if min_length > 0
|
106
|
+
it "should not allow #{attribute} to be less than #{min_length} chars long" do
|
107
|
+
min_value = "x" * (min_length - 1)
|
108
|
+
assert_bad_value(klass, attribute, min_value, short_message).should be_true
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
if min_length > 0
|
113
|
+
it "should allow #{attribute} to be exactly #{min_length} chars long" do
|
114
|
+
min_value = "x" * min_length
|
115
|
+
assert_good_value(klass, attribute, min_value, short_message).should be_true
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should not allow #{attribute} to be more than #{max_length} chars long" do
|
120
|
+
max_value = "x" * (max_length + 1)
|
121
|
+
assert_bad_value(klass, attribute, max_value, long_message).should be_true
|
122
|
+
end
|
123
|
+
|
124
|
+
unless same_length
|
125
|
+
it "should allow #{attribute} to be exactly #{max_length} chars long" do
|
126
|
+
max_value = "x" * max_length
|
127
|
+
assert_good_value(klass, attribute, max_value, long_message).should be_true
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class EnsureLengthIs
|
6
|
+
include Remarkable::Private
|
7
|
+
|
8
|
+
def initialize(attribute, length, opts)
|
9
|
+
@message = get_options!([opts], :message)
|
10
|
+
@message ||= default_error_message(:wrong_length, :count => length)
|
11
|
+
|
12
|
+
@attribute = attribute
|
13
|
+
@length = length
|
14
|
+
@opts = opts
|
15
|
+
end
|
16
|
+
|
17
|
+
def matches?(klass)
|
18
|
+
@klass = klass
|
19
|
+
|
20
|
+
begin
|
21
|
+
min_value = "x" * (@length - 1)
|
22
|
+
fail("allow #{@attribute} to be less than #{@length} chars long") unless assert_bad_value(klass, @attribute, min_value, @message)
|
23
|
+
|
24
|
+
max_value = "x" * (@length + 1)
|
25
|
+
fail("allow #{@attribute} to be greater than #{@length} chars long") unless assert_bad_value(klass, @attribute, max_value, @message)
|
26
|
+
|
27
|
+
valid_value = "x" * (@length)
|
28
|
+
fail("not allow #{@attribute} to be #{@length} chars long") unless assert_good_value(klass, @attribute, valid_value, @message)
|
29
|
+
|
30
|
+
true
|
31
|
+
rescue Exception => e
|
32
|
+
false
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def description
|
38
|
+
"ensure that the length of the #{@attribute} is exactly #{@length} chars long"
|
39
|
+
end
|
40
|
+
|
41
|
+
def failure_message
|
42
|
+
@failure_message || "expected that the length of the #{@attribute} is exactly #{@length} chars long, but it didn't"
|
43
|
+
end
|
44
|
+
|
45
|
+
def negative_failure_message
|
46
|
+
"expected that the length of the #{@attribute} isn't exactly #{@length} chars long, but it did"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Ensures that the length of the attribute is exactly a certain length
|
51
|
+
#
|
52
|
+
# If an instance variable has been created in the setup named after the
|
53
|
+
# model being tested, then this method will use that. Otherwise, it will
|
54
|
+
# create a new instance to test against.
|
55
|
+
#
|
56
|
+
# Options:
|
57
|
+
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
58
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % length</tt>
|
59
|
+
#
|
60
|
+
# Example:
|
61
|
+
# it { User.should ensure_length_is(:ssn, 9) }
|
62
|
+
#
|
63
|
+
def ensure_length_is(attribute, length, opts = {})
|
64
|
+
Remarkable::Syntax::RSpec::EnsureLengthIs.new(attribute, length, opts)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
module Shoulda
|
69
|
+
# Ensures that the length of the attribute is exactly a certain length
|
70
|
+
#
|
71
|
+
# If an instance variable has been created in the setup named after the
|
72
|
+
# model being tested, then this method will use that. Otherwise, it will
|
73
|
+
# create a new instance to test against.
|
74
|
+
#
|
75
|
+
# Options:
|
76
|
+
# * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
77
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % length</tt>
|
78
|
+
#
|
79
|
+
# Example:
|
80
|
+
# should_ensure_length_is :ssn, 9
|
81
|
+
#
|
82
|
+
def should_ensure_length_is(attribute, length, opts = {})
|
83
|
+
message = get_options!([opts], :message)
|
84
|
+
message ||= default_error_message(:wrong_length, :count => length)
|
85
|
+
|
86
|
+
klass = model_class
|
87
|
+
|
88
|
+
it "should not allow #{attribute} to be less than #{length} chars long" do
|
89
|
+
min_value = "x" * (length - 1)
|
90
|
+
assert_bad_value(klass, attribute, min_value, message).should be_true
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should not allow #{attribute} to be greater than #{length} chars long" do
|
94
|
+
max_value = "x" * (length + 1)
|
95
|
+
assert_bad_value(klass, attribute, max_value, message).should be_true
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should allow #{attribute} to be #{length} chars long" do
|
99
|
+
valid_value = "x" * (length)
|
100
|
+
assert_good_value(klass, attribute, valid_value, message).should be_true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module Remarkable
|
2
|
+
module Syntax
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
class EnsureValueInRange
|
6
|
+
include Remarkable::Private
|
7
|
+
|
8
|
+
def initialize(attribute, range, opts = {})
|
9
|
+
@attribute = attribute
|
10
|
+
@range = range
|
11
|
+
@opts = opts
|
12
|
+
|
13
|
+
@min = range.first
|
14
|
+
@max = range.last
|
15
|
+
|
16
|
+
@low_message, @high_message = get_options!([@opts], :low_message, :high_message)
|
17
|
+
@low_message ||= default_error_message(:inclusion)
|
18
|
+
@high_message ||= default_error_message(:inclusion)
|
19
|
+
end
|
20
|
+
|
21
|
+
def matches?(klass)
|
22
|
+
@klass = klass
|
23
|
+
|
24
|
+
begin
|
25
|
+
fail("allow #{@attribute} to be less than #{@min}") unless assert_bad_value(klass, @attribute, @min - 1, @low_message)
|
26
|
+
fail("not allow #{@attribute} to be #{@min}") unless assert_good_value(klass, @attribute, @min, @low_message)
|
27
|
+
fail("allow #{@attribute} to be more than #{@max}") unless assert_bad_value(klass, @attribute, @max + 1, @high_message)
|
28
|
+
fail("not allow #{@attribute} to be #{@max}") unless assert_good_value(klass, @attribute, @max, @high_message)
|
29
|
+
|
30
|
+
true
|
31
|
+
rescue Exception => e
|
32
|
+
false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def description
|
37
|
+
"ensure that the #{@attribute} is in #{@range}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def failure_message
|
41
|
+
@failure_message || "expected that the #{@attribute} is in #{@range}, but it didn't"
|
42
|
+
end
|
43
|
+
|
44
|
+
def negative_failure_message
|
45
|
+
"expected that the #{@attribute} isn't in #{@range}, but it did"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Ensure that the attribute is in the range specified
|
50
|
+
#
|
51
|
+
# If an instance variable has been created in the setup named after the
|
52
|
+
# model being tested, then this method will use that. Otherwise, it will
|
53
|
+
# create a new instance to test against.
|
54
|
+
#
|
55
|
+
# Options:
|
56
|
+
# * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
57
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
|
58
|
+
# * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
59
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
|
60
|
+
#
|
61
|
+
# Example:
|
62
|
+
# it { User.should ensure_value_in_range(:age, 1..100) }
|
63
|
+
#
|
64
|
+
def ensure_value_in_range(attribute, range, opts = {})
|
65
|
+
Remarkable::Syntax::RSpec::EnsureValueInRange.new(attribute, range, opts)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module Shoulda
|
70
|
+
# Ensure that the attribute is in the range specified
|
71
|
+
#
|
72
|
+
# If an instance variable has been created in the setup named after the
|
73
|
+
# model being tested, then this method will use that. Otherwise, it will
|
74
|
+
# create a new instance to test against.
|
75
|
+
#
|
76
|
+
# Options:
|
77
|
+
# * <tt>:low_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
78
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
|
79
|
+
# * <tt>:high_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
|
80
|
+
# Regexp or string. Default = <tt>I18n.translate('activerecord.errors.messages.inclusion')</tt>
|
81
|
+
#
|
82
|
+
# Example:
|
83
|
+
# should_ensure_value_in_range :age, (0..100)
|
84
|
+
#
|
85
|
+
def should_ensure_value_in_range(attribute, range, opts = {})
|
86
|
+
low_message, high_message = get_options!([opts], :low_message, :high_message)
|
87
|
+
low_message ||= default_error_message(:inclusion)
|
88
|
+
high_message ||= default_error_message(:inclusion)
|
89
|
+
|
90
|
+
klass = model_class
|
91
|
+
min = range.first
|
92
|
+
max = range.last
|
93
|
+
|
94
|
+
it "should not allow #{attribute} to be less than #{min}" do
|
95
|
+
v = min - 1
|
96
|
+
assert_bad_value(klass, attribute, v, low_message).should be_true
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should allow #{attribute} to be #{min}" do
|
100
|
+
v = min
|
101
|
+
assert_good_value(klass, attribute, v, low_message).should be_true
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should not allow #{attribute} to be more than #{max}" do
|
105
|
+
v = max + 1
|
106
|
+
assert_bad_value(klass, attribute, v, high_message).should be_true
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should allow #{attribute} to be #{max}" do
|
110
|
+
v = max
|
111
|
+
assert_good_value(klass, attribute, v, high_message).should be_true
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|