rspec-be_valid_when_matcher 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,74 +0,0 @@
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 42 }.to raise_error ArgumentError
60
- expect { be_valid_when(:rational_field).is_rational 42**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.to_c }.to raise_error ArgumentError
63
- expect do
64
- be_valid_when(:rational_field).is_rational BigDecimal.new
65
- end.to raise_error ArgumentError
66
- expect { be_valid_when(:rational_field).is_rational 'value' }.to raise_error ArgumentError
67
- expect { be_valid_when(:rational_field).is_rational '42' }.to raise_error ArgumentError
68
- expect { be_valid_when(:rational_field).is_rational(/^value$/) }.to raise_error ArgumentError
69
- expect { be_valid_when(:rational_field).is_rational [1, 2] }.to raise_error ArgumentError
70
- expect { be_valid_when(:rational_field).is_rational({}) }.to raise_error ArgumentError
71
- expect { be_valid_when(:rational_field).is_rational :value }.to raise_error ArgumentError
72
- end
73
- end
74
- end
@@ -1,80 +0,0 @@
1
- require 'active_model'
2
-
3
- # @private
4
- class FakeModel
5
- include ActiveModel::Validations
6
-
7
- attr_accessor :regexp_field
8
- attr_accessor :not_regexp_field
9
-
10
- validate :regexp_field_should_be_regexp,
11
- :regexp_field_cannot_be_empty,
12
- :not_regexp_field_cannot_be_regexp
13
-
14
- private
15
-
16
- def regexp_field_should_be_regexp
17
- errors.add(:regexp_field, 'should be regexp') unless regexp_field.is_a? Regexp
18
- end
19
-
20
- def regexp_field_cannot_be_empty
21
- errors.add(:regexp_field, "can't be empty") if regexp_field.inspect.length < 3
22
- end
23
-
24
- def not_regexp_field_cannot_be_regexp
25
- errors.add(:not_regexp_field, "can't be regexp") if not_regexp_field.is_a? Regexp
26
- end
27
- end
28
-
29
- describe 'be_valid_when#is_regexp' do
30
- let(:model) { FakeModel.new }
31
-
32
- context 'with no arguments' do
33
- let(:description) { %r{^be valid when #regexp_field is a regexp \(/\^value\$/\)$} }
34
-
35
- let(:passing_matcher) { be_valid_when(:regexp_field).is_regexp }
36
- let(:failing_matcher) { be_valid_when(:not_regexp_field).is_regexp }
37
-
38
- it 'has the correct description' do
39
- expect(passing_matcher.description).to match description
40
- end
41
-
42
- it 'returns proper result' do
43
- expect(passing_matcher.matches? model).to eq true
44
- expect(passing_matcher.does_not_match? model).to eq false
45
- expect(failing_matcher.matches? model).to eq false
46
- expect(failing_matcher.does_not_match? model).to eq true
47
- end
48
- end
49
-
50
- context 'with one argument' do
51
- let(:description) { %r{^be valid when #regexp_field is a regexp \(/some regexp/\)$} }
52
-
53
- let(:passing_matcher) { be_valid_when(:regexp_field).is_regexp(/some regexp/) }
54
- let(:failing_matcher) { be_valid_when(:regexp_field).is_regexp(//) }
55
-
56
- it 'has the correct description' do
57
- expect(passing_matcher.description).to match description
58
- end
59
-
60
- it 'returns proper result' do
61
- expect(passing_matcher.matches? model).to eq true
62
- expect(passing_matcher.does_not_match? model).to eq false
63
- expect(failing_matcher.matches? model).to eq false
64
- expect(failing_matcher.does_not_match? model).to eq true
65
- end
66
-
67
- it 'should fail if passed non regexp' do
68
- expect { be_valid_when(:regexp_field).is_regexp 42 }.to raise_error ArgumentError
69
- expect { be_valid_when(:regexp_field).is_regexp 42**42 }.to raise_error ArgumentError
70
- expect { be_valid_when(:regexp_field).is_regexp 3.14 }.to raise_error ArgumentError
71
- expect { be_valid_when(:regexp_field).is_regexp 42.to_c }.to raise_error ArgumentError
72
- expect { be_valid_when(:regexp_field).is_regexp 42.to_r }.to raise_error ArgumentError
73
- expect { be_valid_when(:regexp_field).is_regexp 'value' }.to raise_error ArgumentError
74
- expect { be_valid_when(:regexp_field).is_regexp '42' }.to raise_error ArgumentError
75
- expect { be_valid_when(:regexp_field).is_regexp [1, 2] }.to raise_error ArgumentError
76
- expect { be_valid_when(:regexp_field).is_regexp({}) }.to raise_error ArgumentError
77
- expect { be_valid_when(:regexp_field).is_regexp :value }.to raise_error ArgumentError
78
- end
79
- end
80
- end
@@ -1,70 +0,0 @@
1
- require 'active_model'
2
-
3
- # @private
4
- class FakeModel
5
- include ActiveModel::Validations
6
-
7
- attr_accessor :string_field
8
- attr_accessor :not_string_field
9
-
10
- validates_length_of :string_field, minimum: 4
11
- validate :not_string_field_cannot_be_string
12
-
13
- private
14
-
15
- def not_string_field_cannot_be_string
16
- errors.add(:not_string_field, "can't be string") if not_string_field.is_a? String
17
- end
18
- end
19
-
20
- describe 'be_valid_when#is_string' do
21
- let(:model) { FakeModel.new }
22
-
23
- context 'with no arguments' do
24
- let(:description) { /^be valid when #string_field is a string \("value"\)$/ }
25
-
26
- let(:passing_matcher) { be_valid_when(:string_field).is_string }
27
- let(:failing_matcher) { be_valid_when(:not_string_field).is_string }
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 #string_field is a string \("some string"\)$/ }
43
-
44
- let(:passing_matcher) { be_valid_when(:string_field).is_string 'some string' }
45
- let(:failing_matcher) { be_valid_when(:string_field).is_string 'a' }
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 string' do
59
- expect { be_valid_when(:string_field).is_string 42 }.to raise_error ArgumentError
60
- expect { be_valid_when(:string_field).is_string 42**42 }.to raise_error ArgumentError
61
- expect { be_valid_when(:string_field).is_string 3.14 }.to raise_error ArgumentError
62
- expect { be_valid_when(:string_field).is_string 42.to_c }.to raise_error ArgumentError
63
- expect { be_valid_when(:string_field).is_string 42.to_r }.to raise_error ArgumentError
64
- expect { be_valid_when(:string_field).is_string(/^value$/) }.to raise_error ArgumentError
65
- expect { be_valid_when(:string_field).is_string [1, 2] }.to raise_error ArgumentError
66
- expect { be_valid_when(:string_field).is_string({}) }.to raise_error ArgumentError
67
- expect { be_valid_when(:string_field).is_string :value }.to raise_error ArgumentError
68
- end
69
- end
70
- end
@@ -1,80 +0,0 @@
1
- require 'active_model'
2
-
3
- # @private
4
- class FakeModel
5
- include ActiveModel::Validations
6
-
7
- attr_accessor :symbol_field
8
- attr_accessor :not_symbol_field
9
-
10
- validate :symbol_field_should_be_symbol,
11
- :symbol_field_cannot_be_short,
12
- :not_symbol_field_cannot_be_symbol
13
-
14
- private
15
-
16
- def symbol_field_should_be_symbol
17
- errors.add(:symbol_field, 'should be symbol') unless symbol_field.is_a? Symbol
18
- end
19
-
20
- def symbol_field_cannot_be_short
21
- errors.add(:symbol_field, "can't be short") if !symbol_field.nil? && symbol_field.length < 2
22
- end
23
-
24
- def not_symbol_field_cannot_be_symbol
25
- errors.add(:not_symbol_field, "can't be symbol") if not_symbol_field.is_a? Symbol
26
- end
27
- end
28
-
29
- describe 'be_valid_when#is_symbol' do
30
- let(:model) { FakeModel.new }
31
-
32
- context 'with no arguments' do
33
- let(:description) { /^be valid when #symbol_field is a symbol \(:value\)$/ }
34
-
35
- let(:passing_matcher) { be_valid_when(:symbol_field).is_symbol }
36
- let(:failing_matcher) { be_valid_when(:not_symbol_field).is_symbol }
37
-
38
- it 'has the correct description' do
39
- expect(passing_matcher.description).to match description
40
- end
41
-
42
- it 'returns proper result' do
43
- expect(passing_matcher.matches? model).to eq true
44
- expect(passing_matcher.does_not_match? model).to eq false
45
- expect(failing_matcher.matches? model).to eq false
46
- expect(failing_matcher.does_not_match? model).to eq true
47
- end
48
- end
49
-
50
- context 'with one argument' do
51
- let(:description) { /^be valid when #symbol_field is a symbol \(:some_symbol\)$/ }
52
-
53
- let(:passing_matcher) { be_valid_when(:symbol_field).is_symbol :some_symbol }
54
- let(:failing_matcher) { be_valid_when(:symbol_field).is_symbol :a }
55
-
56
- it 'has the correct description' do
57
- expect(passing_matcher.description).to match description
58
- end
59
-
60
- it 'returns proper result' do
61
- expect(passing_matcher.matches? model).to eq true
62
- expect(passing_matcher.does_not_match? model).to eq false
63
- expect(failing_matcher.matches? model).to eq false
64
- expect(failing_matcher.does_not_match? model).to eq true
65
- end
66
-
67
- it 'should fail if passed non symbol' do
68
- expect { be_valid_when(:symbol_field).is_symbol 42 }.to raise_error ArgumentError
69
- expect { be_valid_when(:symbol_field).is_symbol 42**42 }.to raise_error ArgumentError
70
- expect { be_valid_when(:symbol_field).is_symbol 3.14 }.to raise_error ArgumentError
71
- expect { be_valid_when(:symbol_field).is_symbol 42.to_c }.to raise_error ArgumentError
72
- expect { be_valid_when(:symbol_field).is_symbol 42.to_r }.to raise_error ArgumentError
73
- expect { be_valid_when(:symbol_field).is_symbol 'value' }.to raise_error ArgumentError
74
- expect { be_valid_when(:symbol_field).is_symbol '42' }.to raise_error ArgumentError
75
- expect { be_valid_when(:symbol_field).is_symbol(/^value$/) }.to raise_error ArgumentError
76
- expect { be_valid_when(:symbol_field).is_symbol [1, 2] }.to raise_error ArgumentError
77
- expect { be_valid_when(:symbol_field).is_symbol({}) }.to raise_error ArgumentError
78
- end
79
- end
80
- end