rspec-be_valid_when_matcher 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/rspec/be_valid_when_matcher.rb +21 -20
- data/spec/examples.cache +68 -53
- data/spec/rspec/be_valid_when/is_bignum_spec.rb +7 -1
- data/spec/rspec/be_valid_when/is_complex_spec.rb +67 -0
- data/spec/rspec/be_valid_when/is_fixnum_spec.rb +7 -1
- data/spec/rspec/be_valid_when/is_float_spec.rb +66 -0
- data/spec/rspec/be_valid_when/is_number_spec.rb +2 -2
- data/spec/rspec/be_valid_when/is_rational_spec.rb +67 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab47d9add56592d481664f6e96727661aa0a4527
|
4
|
+
data.tar.gz: b6bd06a6c889d7c0fb65446dbad909ed97acede9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4201ba70381cf1f992d01e518c0155f5cc009f54bca1ff6ab1a8c1475fec1daaa55c534a1163e782763df39d24519649a65dfba5c6d0493cb11343cfbe9e14a3
|
7
|
+
data.tar.gz: 6a761ae1d4e7facb22d59b339efaa48d48614edbdf6f76ea9bf6961ab26f86e51da2091517d0fd714f22f2dd287a01b214b530854298ceabf42389473e8b043a
|
data/README.md
CHANGED
@@ -90,6 +90,9 @@ Test field validity with specific type values:
|
|
90
90
|
be_valid_when(:field).is_number 2 # Defaults to 42
|
91
91
|
be_valid_when(:field).is_fixnum 2 # Defaults to 42
|
92
92
|
be_valid_when(:field).is_bignum 1024**32 # Defaults to 42**13
|
93
|
+
be_valid_when(:field).is_float 0.1 # Defaults to 3.14
|
94
|
+
be_valid_when(:field).is_complex 2.to_c # Defaults to 42+0i
|
95
|
+
be_valid_when(:field).is_rational 2.to_r # Defaults to 42/1
|
93
96
|
```
|
94
97
|
|
95
98
|
## MIT Licensed
|
@@ -124,25 +124,18 @@ module RSpec
|
|
124
124
|
is(nil, 'not present')
|
125
125
|
end
|
126
126
|
|
127
|
-
#
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
# Used to setup matcher for checking for bignum values.
|
142
|
-
def is_bignum(number = 42**13)
|
143
|
-
fail ArgumentError, 'should be a bignum' unless number.is_a? Bignum
|
144
|
-
|
145
|
-
is(number, 'a bignum')
|
127
|
+
# Generate #is_* numeric methods.
|
128
|
+
{ number: { value: 42, type: Numeric },
|
129
|
+
fixnum: { value: 42, type: Fixnum },
|
130
|
+
bignum: { value: 42**13, type: Bignum },
|
131
|
+
float: { value: Math::PI, type: Float },
|
132
|
+
complex: { value: 42.to_c, type: Complex },
|
133
|
+
rational: { value: 42.to_r, type: Rational } }.each do |name, properties|
|
134
|
+
define_method "is_#{name}" do |number = properties[:value]|
|
135
|
+
fail ArgumentError, "should be #{name}" unless number.is_a? properties[:type]
|
136
|
+
|
137
|
+
is(number, "a #{name}")
|
138
|
+
end
|
146
139
|
end
|
147
140
|
|
148
141
|
private
|
@@ -171,10 +164,18 @@ module RSpec
|
|
171
164
|
end
|
172
165
|
|
173
166
|
def format_message
|
174
|
-
value =
|
167
|
+
value = value_to_string
|
175
168
|
message = @message.nil? ? "is #{value}" : "is #{@message} (#{value})"
|
176
169
|
"##{@field} #{message}"
|
177
170
|
end
|
171
|
+
|
172
|
+
def value_to_string
|
173
|
+
if [Complex, Rational].any? { |type| @value.is_a? type }
|
174
|
+
@value.to_s
|
175
|
+
else
|
176
|
+
@value.inspect
|
177
|
+
end
|
178
|
+
end
|
178
179
|
end
|
179
180
|
|
180
181
|
# Model validity assertion.
|
data/spec/examples.cache
CHANGED
@@ -1,59 +1,74 @@
|
|
1
1
|
example_id | status | run_time |
|
2
2
|
------------------------------------------------------ | ------ | --------------- |
|
3
|
-
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:1:1] | passed | 0.
|
4
|
-
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:1:2] | passed | 0.
|
5
|
-
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:2:1] | passed | 0.
|
6
|
-
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:2:2] | passed | 0.
|
7
|
-
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:2:3] | passed | 0.
|
8
|
-
./spec/rspec/be_valid_when/
|
9
|
-
./spec/rspec/be_valid_when/
|
10
|
-
./spec/rspec/be_valid_when/
|
11
|
-
./spec/rspec/be_valid_when/
|
12
|
-
./spec/rspec/be_valid_when/
|
13
|
-
./spec/rspec/be_valid_when/
|
14
|
-
./spec/rspec/be_valid_when/
|
15
|
-
./spec/rspec/be_valid_when/
|
16
|
-
./spec/rspec/be_valid_when/
|
3
|
+
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:1:1] | passed | 0.0002 seconds |
|
4
|
+
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:1:2] | passed | 0.01275 seconds |
|
5
|
+
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:2:1] | passed | 0.00014 seconds |
|
6
|
+
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:2:2] | passed | 0.01394 seconds |
|
7
|
+
./spec/rspec/be_valid_when/is_bignum_spec.rb[1:2:3] | passed | 0.00021 seconds |
|
8
|
+
./spec/rspec/be_valid_when/is_complex_spec.rb[1:1:1] | passed | 0.00047 seconds |
|
9
|
+
./spec/rspec/be_valid_when/is_complex_spec.rb[1:1:2] | passed | 0.05202 seconds |
|
10
|
+
./spec/rspec/be_valid_when/is_complex_spec.rb[1:2:1] | passed | 0.00014 seconds |
|
11
|
+
./spec/rspec/be_valid_when/is_complex_spec.rb[1:2:2] | passed | 0.01636 seconds |
|
12
|
+
./spec/rspec/be_valid_when/is_complex_spec.rb[1:2:3] | passed | 0.00144 seconds |
|
13
|
+
./spec/rspec/be_valid_when/is_fixnum_spec.rb[1:1:1] | passed | 0.00013 seconds |
|
14
|
+
./spec/rspec/be_valid_when/is_fixnum_spec.rb[1:1:2] | passed | 0.01288 seconds |
|
15
|
+
./spec/rspec/be_valid_when/is_fixnum_spec.rb[1:2:1] | passed | 0.00012 seconds |
|
16
|
+
./spec/rspec/be_valid_when/is_fixnum_spec.rb[1:2:2] | passed | 0.01389 seconds |
|
17
|
+
./spec/rspec/be_valid_when/is_fixnum_spec.rb[1:2:3] | passed | 0.00017 seconds |
|
18
|
+
./spec/rspec/be_valid_when/is_float_spec.rb[1:1:1] | passed | 0.00013 seconds |
|
19
|
+
./spec/rspec/be_valid_when/is_float_spec.rb[1:1:2] | passed | 0.01217 seconds |
|
20
|
+
./spec/rspec/be_valid_when/is_float_spec.rb[1:2:1] | passed | 0.00012 seconds |
|
21
|
+
./spec/rspec/be_valid_when/is_float_spec.rb[1:2:2] | passed | 0.01362 seconds |
|
22
|
+
./spec/rspec/be_valid_when/is_float_spec.rb[1:2:3] | passed | 0.00024 seconds |
|
23
|
+
./spec/rspec/be_valid_when/is_not_present_spec.rb[1:1] | passed | 0.00015 seconds |
|
24
|
+
./spec/rspec/be_valid_when/is_not_present_spec.rb[1:2] | passed | 0.01367 seconds |
|
25
|
+
./spec/rspec/be_valid_when/is_number_spec.rb[1:1:1] | passed | 0.0001 seconds |
|
26
|
+
./spec/rspec/be_valid_when/is_number_spec.rb[1:1:2] | passed | 0.01251 seconds |
|
17
27
|
./spec/rspec/be_valid_when/is_number_spec.rb[1:2:1] | passed | 0.00012 seconds |
|
18
|
-
./spec/rspec/be_valid_when/is_number_spec.rb[1:2:2] | passed | 0.
|
19
|
-
./spec/rspec/be_valid_when/is_number_spec.rb[1:2:3] | passed | 0.
|
20
|
-
./spec/rspec/be_valid_when/
|
21
|
-
./spec/rspec/be_valid_when/
|
22
|
-
./spec/rspec/be_valid_when/
|
23
|
-
./spec/rspec/be_valid_when/
|
24
|
-
./spec/rspec/be_valid_when/
|
25
|
-
./spec/rspec/
|
26
|
-
./spec/rspec/
|
27
|
-
./spec/rspec/
|
28
|
-
./spec/rspec/
|
29
|
-
./spec/rspec/
|
28
|
+
./spec/rspec/be_valid_when/is_number_spec.rb[1:2:2] | passed | 0.01285 seconds |
|
29
|
+
./spec/rspec/be_valid_when/is_number_spec.rb[1:2:3] | passed | 0.00017 seconds |
|
30
|
+
./spec/rspec/be_valid_when/is_rational_spec.rb[1:1:1] | passed | 0.00015 seconds |
|
31
|
+
./spec/rspec/be_valid_when/is_rational_spec.rb[1:1:2] | passed | 0.01233 seconds |
|
32
|
+
./spec/rspec/be_valid_when/is_rational_spec.rb[1:2:1] | passed | 0.00014 seconds |
|
33
|
+
./spec/rspec/be_valid_when/is_rational_spec.rb[1:2:2] | passed | 0.01475 seconds |
|
34
|
+
./spec/rspec/be_valid_when/is_rational_spec.rb[1:2:3] | passed | 0.00018 seconds |
|
35
|
+
./spec/rspec/be_valid_when/is_spec.rb[1:1] | passed | 0.00011 seconds |
|
36
|
+
./spec/rspec/be_valid_when/is_spec.rb[1:2:1] | passed | 0.01223 seconds |
|
37
|
+
./spec/rspec/be_valid_when/is_spec.rb[1:3:1] | passed | 0.01304 seconds |
|
38
|
+
./spec/rspec/be_valid_when/is_spec.rb[1:3:2] | passed | 0.00452 seconds |
|
39
|
+
./spec/rspec/be_valid_when/is_spec.rb[1:4] | passed | 0.00014 seconds |
|
40
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:1] | passed | 0.00044 seconds |
|
41
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:2] | passed | 0.00011 seconds |
|
42
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:3] | passed | 0.00016 seconds |
|
43
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:4] | passed | 0.00009 seconds |
|
44
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:5] | passed | 0.00014 seconds |
|
30
45
|
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:6] | passed | 0.00067 seconds |
|
31
46
|
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:7] | passed | 0.00009 seconds |
|
32
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:8] | passed | 0.
|
33
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:9] | passed | 0.
|
34
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:10] | passed | 0.
|
35
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:1] | passed | 0.
|
36
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:2] | passed | 0.
|
37
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:1] | passed | 0.
|
38
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:2] | passed | 0.
|
39
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:3] | passed | 0.
|
40
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:4] | passed | 0.
|
41
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:5] | passed | 0.
|
47
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:8] | passed | 0.00008 seconds |
|
48
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:9] | passed | 0.00013 seconds |
|
49
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:1:10] | passed | 0.00007 seconds |
|
50
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:1] | passed | 0.00009 seconds |
|
51
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:2] | passed | 0.00011 seconds |
|
52
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:1] | passed | 0.00016 seconds |
|
53
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:2] | passed | 0.00014 seconds |
|
54
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:3] | passed | 0.00015 seconds |
|
55
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:4] | passed | 0.0002 seconds |
|
56
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:2:3:5] | passed | 0.00014 seconds |
|
42
57
|
./spec/rspec/be_valid_when_matcher_spec.rb[1:3:1] | passed | 0.00009 seconds |
|
43
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:3:2:1] | passed | 0.
|
44
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:3:2:2] | passed | 0.
|
45
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:4:1] | passed | 0.
|
46
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:4:2:1] | passed | 0.
|
47
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:4:2:2] | passed | 0.
|
48
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:5] | passed | 0.
|
49
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:6:1] | passed | 0.
|
50
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:6:2] | passed | 0.
|
51
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:6:3] | passed | 0.
|
52
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:6:4] | passed | 0.
|
53
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:7:1:1] | passed | 0.
|
54
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:7:1:2] | passed | 0.
|
55
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:7:2:1] | passed | 0.
|
56
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:7:2:2] | passed | 0.
|
57
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:8:1] | passed | 0.
|
58
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:8:2:1] | passed | 0.
|
59
|
-
./spec/rspec/be_valid_when_matcher_spec.rb[1:9:1] | passed | 0.
|
58
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:3:2:1] | passed | 0.00293 seconds |
|
59
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:3:2:2] | passed | 0.00292 seconds |
|
60
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:4:1] | passed | 0.00013 seconds |
|
61
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:4:2:1] | passed | 0.00292 seconds |
|
62
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:4:2:2] | passed | 0.003 seconds |
|
63
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:5] | passed | 0.00012 seconds |
|
64
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:6:1] | passed | 0.00023 seconds |
|
65
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:6:2] | passed | 0.00021 seconds |
|
66
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:6:3] | passed | 0.00011 seconds |
|
67
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:6:4] | passed | 0.00012 seconds |
|
68
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:7:1:1] | passed | 0.00286 seconds |
|
69
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:7:1:2] | passed | 0.00627 seconds |
|
70
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:7:2:1] | passed | 0.00295 seconds |
|
71
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:7:2:2] | passed | 0.01202 seconds |
|
72
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:8:1] | passed | 0.0047 seconds |
|
73
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:8:2:1] | passed | 0.00394 seconds |
|
74
|
+
./spec/rspec/be_valid_when_matcher_spec.rb[1:9:1] | passed | 0.00011 seconds |
|
@@ -8,7 +8,13 @@ class FakeModel
|
|
8
8
|
attr_accessor :not_bignum_field
|
9
9
|
|
10
10
|
validates_numericality_of :bignum_field, greater_than: 30129469486639681537
|
11
|
-
|
11
|
+
validate :not_bignum_field_cannot_be_bignum
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def not_bignum_field_cannot_be_bignum
|
16
|
+
errors.add(:not_bignum_field, "can't be bignum") if not_bignum_field.is_a? Bignum
|
17
|
+
end
|
12
18
|
end
|
13
19
|
|
14
20
|
describe 'be_valid_when#is_bignum' do
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
class FakeModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
|
7
|
+
attr_accessor :complex_field
|
8
|
+
attr_accessor :not_complex_field
|
9
|
+
|
10
|
+
validates_numericality_of :complex_field, greater_than: 41
|
11
|
+
validate :not_complex_field_cannot_be_complex
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def not_complex_field_cannot_be_complex
|
16
|
+
errors.add(:not_complex_field, "can't be complex") if not_complex_field.is_a? Complex
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'be_valid_when#is_complex' do
|
21
|
+
let(:model) { FakeModel.new }
|
22
|
+
|
23
|
+
context 'with no arguments' do
|
24
|
+
let(:description) { /^be valid when #complex_field is a complex \(42\+0i\)$/ }
|
25
|
+
|
26
|
+
let(:passing_matcher) { be_valid_when(:complex_field).is_complex }
|
27
|
+
let(:failing_matcher) { be_valid_when(:not_complex_field).is_complex }
|
28
|
+
|
29
|
+
it 'has the correct description' do
|
30
|
+
expect(passing_matcher.description).to match description
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'returns proper result' do
|
34
|
+
expect(passing_matcher.matches? model).to eq true
|
35
|
+
expect(passing_matcher.does_not_match? model).to eq false
|
36
|
+
expect(failing_matcher.matches? model).to eq false
|
37
|
+
expect(failing_matcher.does_not_match? model).to eq true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with one argument' do
|
42
|
+
let(:description) { /^be valid when #complex_field is a complex \(50\+0i\)$/ }
|
43
|
+
|
44
|
+
let(:passing_matcher) { be_valid_when(:complex_field).is_complex 50.to_c }
|
45
|
+
let(:failing_matcher) { be_valid_when(:complex_field).is_complex 40.to_c }
|
46
|
+
|
47
|
+
it 'has the correct description' do
|
48
|
+
expect(passing_matcher.description).to match description
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns proper result' do
|
52
|
+
expect(passing_matcher.matches? model).to eq true
|
53
|
+
expect(passing_matcher.does_not_match? model).to eq false
|
54
|
+
expect(failing_matcher.matches? model).to eq false
|
55
|
+
expect(failing_matcher.does_not_match? model).to eq true
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should fail if passed non complex' do
|
59
|
+
expect { be_valid_when(:complex_field).is_complex 'value' }.to raise_error ArgumentError
|
60
|
+
expect { be_valid_when(:complex_field).is_complex '42' }.to raise_error ArgumentError
|
61
|
+
expect { be_valid_when(:complex_field).is_complex 42 }.to raise_error ArgumentError
|
62
|
+
expect { be_valid_when(:complex_field).is_complex 42**42 }.to raise_error ArgumentError
|
63
|
+
expect { be_valid_when(:complex_field).is_complex 3.14 }.to raise_error ArgumentError
|
64
|
+
expect { be_valid_when(:complex_field).is_complex :value }.to raise_error ArgumentError
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -8,7 +8,13 @@ class FakeModel
|
|
8
8
|
attr_accessor :not_fixnum_field
|
9
9
|
|
10
10
|
validates_numericality_of :fixnum_field, greater_than: 40
|
11
|
-
|
11
|
+
validate :not_fixnum_field_cannot_be_fixnum
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def not_fixnum_field_cannot_be_fixnum
|
16
|
+
errors.add(:not_fixnum_field, "can't be fixnum") if not_fixnum_field.is_a? Fixnum
|
17
|
+
end
|
12
18
|
end
|
13
19
|
|
14
20
|
describe 'be_valid_when#is_fixnum' do
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
class FakeModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
|
7
|
+
attr_accessor :float_field
|
8
|
+
attr_accessor :not_float_field
|
9
|
+
|
10
|
+
validates_numericality_of :float_field, greater_than: 3.0
|
11
|
+
validate :not_float_field_cannot_be_float
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def not_float_field_cannot_be_float
|
16
|
+
errors.add(:not_float_field, "can't be float") if not_float_field.is_a? Float
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'be_valid_when#is_float' do
|
21
|
+
let(:model) { FakeModel.new }
|
22
|
+
|
23
|
+
context 'with no arguments' do
|
24
|
+
let(:description) { /^be valid when #float_field is a float \(3.141592653589793\)$/ }
|
25
|
+
|
26
|
+
let(:passing_matcher) { be_valid_when(:float_field).is_float }
|
27
|
+
let(:failing_matcher) { be_valid_when(:not_float_field).is_float }
|
28
|
+
|
29
|
+
it 'has the correct description' do
|
30
|
+
expect(passing_matcher.description).to match description
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'returns proper result' do
|
34
|
+
expect(passing_matcher.matches? model).to eq true
|
35
|
+
expect(passing_matcher.does_not_match? model).to eq false
|
36
|
+
expect(failing_matcher.matches? model).to eq false
|
37
|
+
expect(failing_matcher.does_not_match? model).to eq true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with one argument' do
|
42
|
+
let(:description) { /^be valid when #float_field is a float \(4.2\)$/ }
|
43
|
+
|
44
|
+
let(:passing_matcher) { be_valid_when(:float_field).is_float 4.2 }
|
45
|
+
let(:failing_matcher) { be_valid_when(:float_field).is_float 2.4 }
|
46
|
+
|
47
|
+
it 'has the correct description' do
|
48
|
+
expect(passing_matcher.description).to match description
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns proper result' do
|
52
|
+
expect(passing_matcher.matches? model).to eq true
|
53
|
+
expect(passing_matcher.does_not_match? model).to eq false
|
54
|
+
expect(failing_matcher.matches? model).to eq false
|
55
|
+
expect(failing_matcher.does_not_match? model).to eq true
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should fail if passed non float' do
|
59
|
+
expect { be_valid_when(:float_field).is_float 'value' }.to raise_error ArgumentError
|
60
|
+
expect { be_valid_when(:float_field).is_float '42' }.to raise_error ArgumentError
|
61
|
+
expect { be_valid_when(:float_field).is_float 42 }.to raise_error ArgumentError
|
62
|
+
expect { be_valid_when(:float_field).is_float 42**42 }.to raise_error ArgumentError
|
63
|
+
expect { be_valid_when(:float_field).is_float :value }.to raise_error ArgumentError
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -15,7 +15,7 @@ describe 'be_valid_when#is_number' do
|
|
15
15
|
let(:model) { FakeModel.new }
|
16
16
|
|
17
17
|
context 'with no arguments' do
|
18
|
-
let(:description) { /^be valid when #number_field is number \(42\)$/ }
|
18
|
+
let(:description) { /^be valid when #number_field is a number \(42\)$/ }
|
19
19
|
|
20
20
|
let(:passing_matcher) { be_valid_when(:number_field).is_number }
|
21
21
|
let(:failing_matcher) { be_valid_when(:not_number_field).is_number }
|
@@ -33,7 +33,7 @@ describe 'be_valid_when#is_number' do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
context 'with one argument' do
|
36
|
-
let(:description) { /^be valid when #number_field is number \(50\)$/ }
|
36
|
+
let(:description) { /^be valid when #number_field is a number \(50\)$/ }
|
37
37
|
|
38
38
|
let(:passing_matcher) { be_valid_when(:number_field).is_number 50 }
|
39
39
|
let(:failing_matcher) { be_valid_when(:number_field).is_number 30 }
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
# @private
|
4
|
+
class FakeModel
|
5
|
+
include ActiveModel::Validations
|
6
|
+
|
7
|
+
attr_accessor :rational_field
|
8
|
+
attr_accessor :not_rational_field
|
9
|
+
|
10
|
+
validates_numericality_of :rational_field, greater_than: 40
|
11
|
+
validate :not_rational_field_cannot_be_rational
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def not_rational_field_cannot_be_rational
|
16
|
+
errors.add(:not_rational_field, "can't be rational") if not_rational_field.is_a? Rational
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'be_valid_when#is_rational' do
|
21
|
+
let(:model) { FakeModel.new }
|
22
|
+
|
23
|
+
context 'with no arguments' do
|
24
|
+
let(:description) { %r{^be valid when #rational_field is a rational \(42/1\)$} }
|
25
|
+
|
26
|
+
let(:passing_matcher) { be_valid_when(:rational_field).is_rational }
|
27
|
+
let(:failing_matcher) { be_valid_when(:not_rational_field).is_rational }
|
28
|
+
|
29
|
+
it 'has the correct description' do
|
30
|
+
expect(passing_matcher.description).to match description
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'returns proper result' do
|
34
|
+
expect(passing_matcher.matches? model).to eq true
|
35
|
+
expect(passing_matcher.does_not_match? model).to eq false
|
36
|
+
expect(failing_matcher.matches? model).to eq false
|
37
|
+
expect(failing_matcher.does_not_match? model).to eq true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with one argument' do
|
42
|
+
let(:description) { %r{^be valid when #rational_field is a rational \(50/1\)$} }
|
43
|
+
|
44
|
+
let(:passing_matcher) { be_valid_when(:rational_field).is_rational 50.to_r }
|
45
|
+
let(:failing_matcher) { be_valid_when(:rational_field).is_rational 30.to_r }
|
46
|
+
|
47
|
+
it 'has the correct description' do
|
48
|
+
expect(passing_matcher.description).to match description
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns proper result' do
|
52
|
+
expect(passing_matcher.matches? model).to eq true
|
53
|
+
expect(passing_matcher.does_not_match? model).to eq false
|
54
|
+
expect(failing_matcher.matches? model).to eq false
|
55
|
+
expect(failing_matcher.does_not_match? model).to eq true
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should fail if passed non rational' do
|
59
|
+
expect { be_valid_when(:rational_field).is_rational 'value' }.to raise_error ArgumentError
|
60
|
+
expect { be_valid_when(:rational_field).is_rational '42' }.to raise_error ArgumentError
|
61
|
+
expect { be_valid_when(:rational_field).is_rational 3.14 }.to raise_error ArgumentError
|
62
|
+
expect { be_valid_when(:rational_field).is_rational 42**42 }.to raise_error ArgumentError
|
63
|
+
expect { be_valid_when(:rational_field).is_rational 42.to_c }.to raise_error ArgumentError
|
64
|
+
expect { be_valid_when(:rational_field).is_rational :value }.to raise_error ArgumentError
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-be_valid_when_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marek Tuchowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -51,9 +51,12 @@ files:
|
|
51
51
|
- lib/rspec/be_valid_when_matcher.rb
|
52
52
|
- spec/examples.cache
|
53
53
|
- spec/rspec/be_valid_when/is_bignum_spec.rb
|
54
|
+
- spec/rspec/be_valid_when/is_complex_spec.rb
|
54
55
|
- spec/rspec/be_valid_when/is_fixnum_spec.rb
|
56
|
+
- spec/rspec/be_valid_when/is_float_spec.rb
|
55
57
|
- spec/rspec/be_valid_when/is_not_present_spec.rb
|
56
58
|
- spec/rspec/be_valid_when/is_number_spec.rb
|
59
|
+
- spec/rspec/be_valid_when/is_rational_spec.rb
|
57
60
|
- spec/rspec/be_valid_when/is_spec.rb
|
58
61
|
- spec/rspec/be_valid_when_matcher_spec.rb
|
59
62
|
- spec/spec_helper.rb
|
@@ -87,9 +90,12 @@ test_files:
|
|
87
90
|
- spec/spec_helper.rb
|
88
91
|
- spec/rspec/be_valid_when_matcher_spec.rb
|
89
92
|
- spec/rspec/be_valid_when/is_number_spec.rb
|
93
|
+
- spec/rspec/be_valid_when/is_float_spec.rb
|
94
|
+
- spec/rspec/be_valid_when/is_rational_spec.rb
|
90
95
|
- spec/rspec/be_valid_when/is_bignum_spec.rb
|
91
96
|
- spec/rspec/be_valid_when/is_spec.rb
|
92
97
|
- spec/rspec/be_valid_when/is_not_present_spec.rb
|
93
98
|
- spec/rspec/be_valid_when/is_fixnum_spec.rb
|
99
|
+
- spec/rspec/be_valid_when/is_complex_spec.rb
|
94
100
|
- spec/examples.cache
|
95
101
|
has_rdoc:
|