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.
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.3.0
4
+ version: 1.0.0
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-19 00:00:00.000000000 Z
11
+ date: 2015-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -50,22 +50,14 @@ files:
50
50
  - README.md
51
51
  - lib/rspec/be_valid_when_matcher.rb
52
52
  - spec/examples.cache
53
- - spec/rspec/be_valid_when/is_array_spec.rb
54
- - spec/rspec/be_valid_when/is_bigdecimal_spec.rb
55
- - spec/rspec/be_valid_when/is_bignum_spec.rb
56
- - spec/rspec/be_valid_when/is_complex_spec.rb
57
- - spec/rspec/be_valid_when/is_fixnum_spec.rb
58
- - spec/rspec/be_valid_when/is_float_spec.rb
59
- - spec/rspec/be_valid_when/is_hash_spec.rb
60
53
  - spec/rspec/be_valid_when/is_not_present_spec.rb
61
- - spec/rspec/be_valid_when/is_number_spec.rb
62
- - spec/rspec/be_valid_when/is_rational_spec.rb
63
- - spec/rspec/be_valid_when/is_regex_spec.rb
64
54
  - spec/rspec/be_valid_when/is_spec.rb
65
- - spec/rspec/be_valid_when/is_string_spec.rb
66
- - spec/rspec/be_valid_when/is_symbol_spec.rb
55
+ - spec/rspec/be_valid_when/is_type_spec.rb
67
56
  - spec/rspec/be_valid_when_matcher_spec.rb
68
57
  - spec/spec_helper.rb
58
+ - spec/support/has_correct_description.rb
59
+ - spec/support/returns_proper_results.rb
60
+ - spec/support/takes_no_arguments.rb
69
61
  homepage: https://github.com/mtuchowski/rspec-be_valid_when_matcher
70
62
  licenses:
71
63
  - MIT
@@ -80,7 +72,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
72
  requirements:
81
73
  - - ">="
82
74
  - !ruby/object:Gem::Version
83
- version: '0'
75
+ version: 2.0.0
84
76
  required_rubygems_version: !ruby/object:Gem::Requirement
85
77
  requirements:
86
78
  - - ">="
@@ -95,19 +87,11 @@ summary: RSpec be_valid_when matcher for ActiveRecord models.
95
87
  test_files:
96
88
  - spec/spec_helper.rb
97
89
  - spec/rspec/be_valid_when_matcher_spec.rb
98
- - spec/rspec/be_valid_when/is_symbol_spec.rb
99
- - spec/rspec/be_valid_when/is_number_spec.rb
100
- - spec/rspec/be_valid_when/is_float_spec.rb
101
- - spec/rspec/be_valid_when/is_rational_spec.rb
102
- - spec/rspec/be_valid_when/is_bignum_spec.rb
103
90
  - spec/rspec/be_valid_when/is_spec.rb
104
91
  - spec/rspec/be_valid_when/is_not_present_spec.rb
105
- - spec/rspec/be_valid_when/is_fixnum_spec.rb
106
- - spec/rspec/be_valid_when/is_array_spec.rb
107
- - spec/rspec/be_valid_when/is_bigdecimal_spec.rb
108
- - spec/rspec/be_valid_when/is_complex_spec.rb
109
- - spec/rspec/be_valid_when/is_string_spec.rb
110
- - spec/rspec/be_valid_when/is_regex_spec.rb
111
- - spec/rspec/be_valid_when/is_hash_spec.rb
92
+ - spec/rspec/be_valid_when/is_type_spec.rb
93
+ - spec/support/returns_proper_results.rb
94
+ - spec/support/has_correct_description.rb
95
+ - spec/support/takes_no_arguments.rb
112
96
  - spec/examples.cache
113
97
  has_rdoc:
@@ -1,80 +0,0 @@
1
- require 'active_model'
2
-
3
- # @private
4
- class FakeModel
5
- include ActiveModel::Validations
6
-
7
- attr_accessor :array_field
8
- attr_accessor :not_array_field
9
-
10
- validate :array_field_should_be_array,
11
- :array_field_cannot_be_empty,
12
- :not_array_field_cannot_be_array
13
-
14
- private
15
-
16
- def array_field_should_be_array
17
- errors.add(:array_field, 'should be array') unless array_field.is_a? Array
18
- end
19
-
20
- def array_field_cannot_be_empty
21
- errors.add(:array_field, "can't be empty") if !array_field.nil? && array_field.length == 0
22
- end
23
-
24
- def not_array_field_cannot_be_array
25
- errors.add(:not_array_field, "can't be array") if not_array_field.is_a? Array
26
- end
27
- end
28
-
29
- describe 'be_valid_when#is_array' do
30
- let(:model) { FakeModel.new }
31
-
32
- context 'with no arguments' do
33
- let(:description) { /^be valid when #array_field is a array \(\[42\]\)$/ }
34
-
35
- let(:passing_matcher) { be_valid_when(:array_field).is_array }
36
- let(:failing_matcher) { be_valid_when(:not_array_field).is_array }
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 #array_field is a array \(\[1, 2\]\)$/ }
52
-
53
- let(:passing_matcher) { be_valid_when(:array_field).is_array [1, 2] }
54
- let(:failing_matcher) { be_valid_when(:array_field).is_array [] }
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 array' do
68
- expect { be_valid_when(:array_field).is_array 42 }.to raise_error ArgumentError
69
- expect { be_valid_when(:array_field).is_array 42**42 }.to raise_error ArgumentError
70
- expect { be_valid_when(:array_field).is_array 3.14 }.to raise_error ArgumentError
71
- expect { be_valid_when(:array_field).is_array 42.to_c }.to raise_error ArgumentError
72
- expect { be_valid_when(:array_field).is_array 42.to_r }.to raise_error ArgumentError
73
- expect { be_valid_when(:array_field).is_array 'value' }.to raise_error ArgumentError
74
- expect { be_valid_when(:array_field).is_array '42' }.to raise_error ArgumentError
75
- expect { be_valid_when(:array_field).is_array(/^value$/) }.to raise_error ArgumentError
76
- expect { be_valid_when(:array_field).is_array({}) }.to raise_error ArgumentError
77
- expect { be_valid_when(:array_field).is_array :value }.to raise_error ArgumentError
78
- end
79
- end
80
- end
@@ -1,81 +0,0 @@
1
- require 'active_model'
2
- require 'bigdecimal'
3
-
4
- # @private
5
- class FakeModel
6
- include ActiveModel::Validations
7
-
8
- attr_accessor :bigdecimal_field
9
- attr_accessor :not_bigdec_field
10
-
11
- validates_numericality_of :bigdecimal_field, greater_than: 40
12
- validate :not_bigdec_field_cannot_be_bigdecimal
13
-
14
- private
15
-
16
- def not_bigdec_field_cannot_be_bigdecimal
17
- errors.add(:not_bigdec_field, "can't be bigdecimal") if not_bigdec_field.is_a? BigDecimal
18
- end
19
- end
20
-
21
- describe 'be_valid_when#is_bigdecimal' do
22
- let(:model) { FakeModel.new }
23
-
24
- context 'with no arguments' do
25
- let(:description) { /^be valid when #bigdecimal_field is a bigdecimal \(0.42E2\)$/ }
26
-
27
- let(:passing_matcher) { be_valid_when(:bigdecimal_field).is_bigdecimal }
28
- let(:failing_matcher) { be_valid_when(:not_bigdec_field).is_bigdecimal }
29
-
30
- it 'has the correct description' do
31
- expect(passing_matcher.description).to match description
32
- end
33
-
34
- it 'returns proper result' do
35
- expect(passing_matcher.matches? model).to eq true
36
- expect(passing_matcher.does_not_match? model).to eq false
37
- expect(failing_matcher.matches? model).to eq false
38
- expect(failing_matcher.does_not_match? model).to eq true
39
- end
40
- end
41
-
42
- context 'with one argument' do
43
- let(:description) { /^be valid when #bigdecimal_field is a bigdecimal \(0.5E2\)$/ }
44
-
45
- let(:passing_matcher) { be_valid_when(:bigdecimal_field).is_bigdecimal BigDecimal.new('50') }
46
- let(:failing_matcher) { be_valid_when(:bigdecimal_field).is_bigdecimal BigDecimal.new('30') }
47
-
48
- it 'has the correct description' do
49
- expect(passing_matcher.description).to match description
50
- end
51
-
52
- it 'returns proper result' do
53
- expect(passing_matcher.matches? model).to eq true
54
- expect(passing_matcher.does_not_match? model).to eq false
55
- expect(failing_matcher.matches? model).to eq false
56
- expect(failing_matcher.does_not_match? model).to eq true
57
- end
58
-
59
- it 'should fail if passed non bigdecimal' do
60
- expect { be_valid_when(:bigdecimal_field).is_bigdecimal 42 }.to raise_error ArgumentError
61
- expect { be_valid_when(:bigdecimal_field).is_bigdecimal 42**42 }.to raise_error ArgumentError
62
- expect { be_valid_when(:bigdecimal_field).is_bigdecimal 3.14 }.to raise_error ArgumentError
63
- expect do
64
- be_valid_when(:bigdecimal_field).is_bigdecimal 42.to_c
65
- end.to raise_error ArgumentError
66
- expect do
67
- be_valid_when(:bigdecimal_field).is_bigdecimal 42.to_r
68
- end.to raise_error ArgumentError
69
- expect do
70
- be_valid_when(:bigdecimal_field).is_bigdecimal 'value'
71
- end.to raise_error ArgumentError
72
- expect { be_valid_when(:bigdecimal_field).is_bigdecimal '42' }.to raise_error ArgumentError
73
- expect do
74
- be_valid_when(:bigdecimal_field).is_bigdecimal(/^value$/)
75
- end.to raise_error ArgumentError
76
- expect { be_valid_when(:bigdecimal_field).is_bigdecimal [1, 2] }.to raise_error ArgumentError
77
- expect { be_valid_when(:bigdecimal_field).is_bigdecimal({}) }.to raise_error ArgumentError
78
- expect { be_valid_when(:bigdecimal_field).is_bigdecimal :value }.to raise_error ArgumentError
79
- end
80
- end
81
- end
@@ -1,72 +0,0 @@
1
- require 'active_model'
2
-
3
- # @private
4
- class FakeModel
5
- include ActiveModel::Validations
6
-
7
- attr_accessor :bignum_field
8
- attr_accessor :not_bignum_field
9
-
10
- validates_numericality_of :bignum_field, greater_than: 30129469486639681537
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
18
- end
19
-
20
- describe 'be_valid_when#is_bignum' do
21
- let(:model) { FakeModel.new }
22
-
23
- context 'with no arguments' do
24
- let(:description) { /^be valid when #bignum_field is a bignum \(1265437718438866624512\)$/ }
25
-
26
- let(:passing_matcher) { be_valid_when(:bignum_field).is_bignum }
27
- let(:failing_matcher) { be_valid_when(:not_bignum_field).is_bignum }
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 #bignum_field is a bignum \(2232232135326160725639168\)$/ }
43
-
44
- let(:passing_matcher) { be_valid_when(:bignum_field).is_bignum 2232232135326160725639168 }
45
- let(:failing_matcher) { be_valid_when(:bignum_field).is_bignum 30129469486639681536 }
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 bignum' do
59
- expect { be_valid_when(:bignum_field).is_bignum 42 }.to raise_error ArgumentError
60
- expect { be_valid_when(:bignum_field).is_bignum 3.14 }.to raise_error ArgumentError
61
- expect { be_valid_when(:bignum_field).is_bignum 42.to_c }.to raise_error ArgumentError
62
- expect { be_valid_when(:bignum_field).is_bignum 42.to_r }.to raise_error ArgumentError
63
- expect { be_valid_when(:bignum_field).is_bignum BigDecimal.new }.to raise_error ArgumentError
64
- expect { be_valid_when(:bignum_field).is_bignum 'value' }.to raise_error ArgumentError
65
- expect { be_valid_when(:bignum_field).is_bignum '42' }.to raise_error ArgumentError
66
- expect { be_valid_when(:bignum_field).is_bignum(/^value$/) }.to raise_error ArgumentError
67
- expect { be_valid_when(:bignum_field).is_bignum [1, 2] }.to raise_error ArgumentError
68
- expect { be_valid_when(:bignum_field).is_bignum({}) }.to raise_error ArgumentError
69
- expect { be_valid_when(:bignum_field).is_bignum :value }.to raise_error ArgumentError
70
- end
71
- end
72
- end
@@ -1,74 +0,0 @@
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 42 }.to raise_error ArgumentError
60
- expect { be_valid_when(:complex_field).is_complex 42**42 }.to raise_error ArgumentError
61
- expect { be_valid_when(:complex_field).is_complex 3.14 }.to raise_error ArgumentError
62
- expect { be_valid_when(:complex_field).is_complex 42.to_r }.to raise_error ArgumentError
63
- expect do
64
- be_valid_when(:complex_field).is_complex BigDecimal.new
65
- end.to raise_error ArgumentError
66
- expect { be_valid_when(:complex_field).is_complex 'value' }.to raise_error ArgumentError
67
- expect { be_valid_when(:complex_field).is_complex '42' }.to raise_error ArgumentError
68
- expect { be_valid_when(:complex_field).is_complex(/^value$/) }.to raise_error ArgumentError
69
- expect { be_valid_when(:complex_field).is_complex [1, 2] }.to raise_error ArgumentError
70
- expect { be_valid_when(:complex_field).is_complex({}) }.to raise_error ArgumentError
71
- expect { be_valid_when(:complex_field).is_complex :value }.to raise_error ArgumentError
72
- end
73
- end
74
- end
@@ -1,72 +0,0 @@
1
- require 'active_model'
2
-
3
- # @private
4
- class FakeModel
5
- include ActiveModel::Validations
6
-
7
- attr_accessor :fixnum_field
8
- attr_accessor :not_fixnum_field
9
-
10
- validates_numericality_of :fixnum_field, greater_than: 40
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
18
- end
19
-
20
- describe 'be_valid_when#is_fixnum' do
21
- let(:model) { FakeModel.new }
22
-
23
- context 'with no arguments' do
24
- let(:description) { /^be valid when #fixnum_field is a fixnum \(42\)$/ }
25
-
26
- let(:passing_matcher) { be_valid_when(:fixnum_field).is_fixnum }
27
- let(:failing_matcher) { be_valid_when(:not_fixnum_field).is_fixnum }
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 #fixnum_field is a fixnum \(50\)$/ }
43
-
44
- let(:passing_matcher) { be_valid_when(:fixnum_field).is_fixnum 50 }
45
- let(:failing_matcher) { be_valid_when(:fixnum_field).is_fixnum 30 }
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 fixnum' do
59
- expect { be_valid_when(:fixnum_field).is_fixnum 42**42 }.to raise_error ArgumentError
60
- expect { be_valid_when(:fixnum_field).is_fixnum 3.14 }.to raise_error ArgumentError
61
- expect { be_valid_when(:fixnum_field).is_fixnum 42.to_c }.to raise_error ArgumentError
62
- expect { be_valid_when(:fixnum_field).is_fixnum 42.to_r }.to raise_error ArgumentError
63
- expect { be_valid_when(:fixnum_field).is_fixnum BigDecimal.new }.to raise_error ArgumentError
64
- expect { be_valid_when(:fixnum_field).is_fixnum 'value' }.to raise_error ArgumentError
65
- expect { be_valid_when(:fixnum_field).is_fixnum '42' }.to raise_error ArgumentError
66
- expect { be_valid_when(:fixnum_field).is_fixnum(/^value$/) }.to raise_error ArgumentError
67
- expect { be_valid_when(:fixnum_field).is_fixnum [1, 2] }.to raise_error ArgumentError
68
- expect { be_valid_when(:fixnum_field).is_fixnum({}) }.to raise_error ArgumentError
69
- expect { be_valid_when(:fixnum_field).is_fixnum :value }.to raise_error ArgumentError
70
- end
71
- end
72
- end
@@ -1,72 +0,0 @@
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 42 }.to raise_error ArgumentError
60
- expect { be_valid_when(:float_field).is_float 42**42 }.to raise_error ArgumentError
61
- expect { be_valid_when(:float_field).is_float 42.to_c }.to raise_error ArgumentError
62
- expect { be_valid_when(:float_field).is_float 42.to_r }.to raise_error ArgumentError
63
- expect { be_valid_when(:float_field).is_float BigDecimal.new }.to raise_error ArgumentError
64
- expect { be_valid_when(:float_field).is_float 'value' }.to raise_error ArgumentError
65
- expect { be_valid_when(:float_field).is_float '42' }.to raise_error ArgumentError
66
- expect { be_valid_when(:float_field).is_float(/^value$/) }.to raise_error ArgumentError
67
- expect { be_valid_when(:float_field).is_float [1, 2] }.to raise_error ArgumentError
68
- expect { be_valid_when(:float_field).is_float({}) }.to raise_error ArgumentError
69
- expect { be_valid_when(:float_field).is_float :value }.to raise_error ArgumentError
70
- end
71
- end
72
- end
@@ -1,80 +0,0 @@
1
- require 'active_model'
2
-
3
- # @private
4
- class FakeModel
5
- include ActiveModel::Validations
6
-
7
- attr_accessor :hash_field
8
- attr_accessor :not_hash_field
9
-
10
- validate :hash_field_should_be_hash,
11
- :hash_field_cannot_be_empty,
12
- :not_hash_field_cannot_be_hash
13
-
14
- private
15
-
16
- def hash_field_should_be_hash
17
- errors.add(:hash_field, 'should be hash') unless hash_field.is_a? Hash
18
- end
19
-
20
- def hash_field_cannot_be_empty
21
- errors.add(:hash_field, "can't be empty") if !hash_field.nil? && hash_field.keys.length == 0
22
- end
23
-
24
- def not_hash_field_cannot_be_hash
25
- errors.add(:not_hash_field, "can't be hash") if not_hash_field.is_a? Hash
26
- end
27
- end
28
-
29
- describe 'be_valid_when#is_hash' do
30
- let(:model) { FakeModel.new }
31
-
32
- context 'with no arguments' do
33
- let(:description) { /^be valid when #hash_field is a hash \(\{:value=>42\}\)$/ }
34
-
35
- let(:passing_matcher) { be_valid_when(:hash_field).is_hash }
36
- let(:failing_matcher) { be_valid_when(:not_hash_field).is_hash }
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 #hash_field is a hash \({:a=>1\}\)$/ }
52
-
53
- let(:passing_matcher) { be_valid_when(:hash_field).is_hash(a: 1) }
54
- let(:failing_matcher) { be_valid_when(:hash_field).is_hash({}) }
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 hash' do
68
- expect { be_valid_when(:hash_field).is_hash 42 }.to raise_error ArgumentError
69
- expect { be_valid_when(:hash_field).is_hash 42**42 }.to raise_error ArgumentError
70
- expect { be_valid_when(:hash_field).is_hash 3.14 }.to raise_error ArgumentError
71
- expect { be_valid_when(:hash_field).is_hash 42.to_c }.to raise_error ArgumentError
72
- expect { be_valid_when(:hash_field).is_hash 42.to_r }.to raise_error ArgumentError
73
- expect { be_valid_when(:hash_field).is_hash 'value' }.to raise_error ArgumentError
74
- expect { be_valid_when(:hash_field).is_hash '42' }.to raise_error ArgumentError
75
- expect { be_valid_when(:hash_field).is_hash(/^value$/) }.to raise_error ArgumentError
76
- expect { be_valid_when(:hash_field).is_hash [1, 2] }.to raise_error ArgumentError
77
- expect { be_valid_when(:hash_field).is_hash :value }.to raise_error ArgumentError
78
- end
79
- end
80
- end
@@ -1,61 +0,0 @@
1
- require 'active_model'
2
-
3
- # @private
4
- class FakeModel
5
- include ActiveModel::Validations
6
-
7
- attr_accessor :number_field
8
- attr_accessor :not_number_field
9
-
10
- validates_numericality_of :number_field, greater_than: 40
11
- validates_length_of :not_number_field, minimum: 31
12
- end
13
-
14
- describe 'be_valid_when#is_number' do
15
- let(:model) { FakeModel.new }
16
-
17
- context 'with no arguments' do
18
- let(:description) { /^be valid when #number_field is a number \(42\)$/ }
19
-
20
- let(:passing_matcher) { be_valid_when(:number_field).is_number }
21
- let(:failing_matcher) { be_valid_when(:not_number_field).is_number }
22
-
23
- it 'has the correct description' do
24
- expect(passing_matcher.description).to match description
25
- end
26
-
27
- it 'returns proper result' do
28
- expect(passing_matcher.matches? model).to eq true
29
- expect(passing_matcher.does_not_match? model).to eq false
30
- expect(failing_matcher.matches? model).to eq false
31
- expect(failing_matcher.does_not_match? model).to eq true
32
- end
33
- end
34
-
35
- context 'with one argument' do
36
- let(:description) { /^be valid when #number_field is a number \(50\)$/ }
37
-
38
- let(:passing_matcher) { be_valid_when(:number_field).is_number 50 }
39
- let(:failing_matcher) { be_valid_when(:number_field).is_number 30 }
40
-
41
- it 'has the correct description' do
42
- expect(passing_matcher.description).to match description
43
- end
44
-
45
- it 'returns proper result' do
46
- expect(passing_matcher.matches? model).to eq true
47
- expect(passing_matcher.does_not_match? model).to eq false
48
- expect(failing_matcher.matches? model).to eq false
49
- expect(failing_matcher.does_not_match? model).to eq true
50
- end
51
-
52
- it 'should fail if passed non number' do
53
- expect { be_valid_when(:number_field).is_number 'value' }.to raise_error ArgumentError
54
- expect { be_valid_when(:number_field).is_number '42' }.to raise_error ArgumentError
55
- expect { be_valid_when(:number_field).is_number(/^value$/) }.to raise_error ArgumentError
56
- expect { be_valid_when(:number_field).is_number [1, 2] }.to raise_error ArgumentError
57
- expect { be_valid_when(:number_field).is_number({}) }.to raise_error ArgumentError
58
- expect { be_valid_when(:number_field).is_number :value }.to raise_error ArgumentError
59
- end
60
- end
61
- end