virtus-matchers 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 52e8338d991fb465d12e9ab65a69e8f61015b8d2
4
+ data.tar.gz: b17b4acc8f0f20047d3cada608cfe58ab8ad56b2
5
+ SHA512:
6
+ metadata.gz: 007a99b23c490bcdf51462a1f0fb17a362a5d4780d5739186f54fda254fdf5e6e1393a6fdf7e1b4483b7113a35674358e4c14afbf04229437331ee5e1e0321fb
7
+ data.tar.gz: d4ce5da2e13df2c46eabc2a17936a7358ec6fdf64b4584752e81805df5761b35daf801b328ce3cb6d29b76c2b99f449e111c28c92fa450567550366461fa9ad5
@@ -3,3 +3,4 @@
3
3
  require 'virtus/matchers/version'
4
4
  require 'virtus/matchers/be_value_object_matcher'
5
5
  require 'virtus/matchers/have_attribute_matcher'
6
+ require 'virtus/matchers/integrations'
@@ -13,23 +13,30 @@ module Virtus
13
13
  self
14
14
  end
15
15
 
16
+ def with_default(expected_default)
17
+ @expected_default = expected_default
18
+ self
19
+ end
20
+
21
+ def strict
22
+ @expected_strict = true
23
+ self
24
+ end
25
+
16
26
  def matches?(klass)
17
27
  @klass = klass
18
28
  @attribute = @klass.attribute_set[@name]
19
29
  return false unless @attribute
20
30
 
21
- if @type.class == Array
22
- return @attribute.primitive == Array &&
23
- @attribute.options[:member_type].primitive == @type.first
24
- end
25
-
26
- valid_type && valid_coercer
31
+ valid_type && valid_coercer && valid_default && matches_strict
27
32
  end
28
33
 
29
34
  def description
30
35
  type = @type.class == Array ? "Array#{@type}" : @type
31
36
  str = "have attribute #{@name}"
32
37
  str += " of type #{type}#{coercer_description}" unless @type.nil?
38
+ str += " with default \"#{@expected_default}\"" if @expected_default
39
+ str += " and is strict" if @expected_strict
33
40
  str
34
41
  end
35
42
 
@@ -38,7 +45,21 @@ module Virtus
38
45
  end
39
46
 
40
47
  private
48
+
49
+ def matches_strict
50
+ !@expected_strict || @attribute.strict?
51
+ end
52
+
53
+ def valid_default
54
+ @expected_default.nil? || @attribute.default_value.value == @expected_default
55
+ end
56
+
41
57
  def valid_type
58
+ if @type.class == Array
59
+ return @attribute.primitive == Array &&
60
+ @attribute.options[:member_type].primitive == @type.first
61
+ end
62
+
42
63
  @type.nil? || @attribute.primitive == @type
43
64
  end
44
65
 
@@ -0,0 +1,5 @@
1
+ if defined?(RSpec)
2
+ RSpec.configure do |config|
3
+ config.include Virtus::Matchers, type: :virtus
4
+ end
5
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Virtus
4
4
  module Matchers
5
- VERSION = '0.2.1'.freeze
5
+ VERSION = '0.3.0'.freeze
6
6
  end
7
7
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'virtus'
4
4
  require 'virtus-matchers'
5
+ require 'pry'
5
6
 
6
7
  RSpec.configure do |config|
7
8
  config.include Virtus::Matchers
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
+ require 'spec_helper'
2
3
 
3
- describe Virtus::Matchers::BeAValueObjectMatcher do
4
+ RSpec.describe Virtus::Matchers::BeAValueObjectMatcher do
4
5
  class Example
5
6
  end
6
7
 
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
+ require 'spec_helper'
2
3
 
3
- describe Virtus::Matchers::HaveAttributeMatcher do
4
+ RSpec.describe Virtus::Matchers::HaveAttributeMatcher do
4
5
  class FakeCoercer; end
5
6
 
6
7
  class Example
@@ -9,8 +10,13 @@ describe Virtus::Matchers::HaveAttributeMatcher do
9
10
  attribute :any
10
11
  attribute :foo, String
11
12
  attribute :bar, Array[String]
13
+ attribute :array_attribute_with_default, Array[String], default: ["hello", "world"]
12
14
  attribute :baz, Array
13
15
  attribute :lol, DateTime, coercer: FakeCoercer
16
+ attribute :hello, String, default: "Hello"
17
+ attribute :hi_no_default, String
18
+ attribute :strict, String, strict: true
19
+ attribute :lenient, String
14
20
  end
15
21
 
16
22
  context 'when attribute is defined', 'with no type' do
@@ -52,6 +58,31 @@ describe Virtus::Matchers::HaveAttributeMatcher do
52
58
  end
53
59
  end
54
60
 
61
+ context 'when attribute is defined', 'with array type', 'with default' do
62
+ let(:matcher) do
63
+ described_class.
64
+ new(:array_attribute_with_default, Array[String]).
65
+ with_default(array_default)
66
+ end
67
+ let(:array_default) { ['hello', 'world'] }
68
+
69
+ it 'should match' do
70
+ matcher.matches?(Example).should be_true
71
+ end
72
+
73
+ context 'different array default' do
74
+ let(:array_default) { ['different', 'default'] }
75
+ it 'should not match' do
76
+ matcher.matches?(Example).should be_false
77
+ end
78
+ end
79
+
80
+ it 'should have a description' do
81
+ matcher.matches?(Example)
82
+ matcher.description.should == "have attribute array_attribute_with_default of type Array[String] with default \"[\"hello\", \"world\"]\""
83
+ end
84
+ end
85
+
55
86
  context 'when attribute is defined', 'with a valid coercer' do
56
87
  let(:matcher) { described_class.new(:lol, DateTime).coerced_with(FakeCoercer) }
57
88
 
@@ -129,4 +160,72 @@ describe Virtus::Matchers::HaveAttributeMatcher do
129
160
  matcher.failure_message.should == "expected #{Example} to have attribute baz of type String"
130
161
  end
131
162
  end
163
+
164
+ context 'checking for default' do
165
+ context "when default matches expected default" do
166
+ let(:matcher) { described_class.new(:hello, String).with_default("Hello") }
167
+
168
+ it 'matches' do
169
+ expect(matcher.matches?(Example)).to be true
170
+ end
171
+ end
172
+
173
+ context "when default does not match the expected default" do
174
+ let(:matcher) { described_class.new(:hello, String).with_default("Hala") }
175
+
176
+ it 'does not match' do
177
+ expect(matcher.matches?(Example)).to be false
178
+ end
179
+ end
180
+
181
+ context "there is an expected default, but none is set" do
182
+ let(:matcher) do
183
+ described_class.new(:hi_no_default, String).with_default("Hello")
184
+ end
185
+
186
+ it 'does not match' do
187
+ expect(matcher.matches?(Example)).to be false
188
+ end
189
+ end
190
+
191
+ it 'has a description' do
192
+ matcher = described_class.new(:hello, String).with_default("Hello")
193
+ matcher.matches?(Example)
194
+ expect(matcher.description).to eq 'have attribute hello of type String with default "Hello"'
195
+ end
196
+ end
197
+
198
+ context 'checking for strict' do
199
+ context "it matches expected strictness" do
200
+ let(:matcher) { described_class.new(:strict, String).strict }
201
+
202
+ it 'matches' do
203
+ expect(matcher.matches?(Example)).to be true
204
+ end
205
+ end
206
+
207
+ context "it does not match the expected strictness" do
208
+ let(:matcher) { described_class.new(:lenient, String).strict }
209
+
210
+ it 'does not match' do
211
+ expect(matcher.matches?(Example)).to be false
212
+ end
213
+ end
214
+
215
+ context "spec does not specify strictness" do
216
+ it "matches" do
217
+ matcher = described_class.new(:strict, String)
218
+ expect(matcher.matches?(Example)).to be true
219
+
220
+ matcher = described_class.new(:lenient, String)
221
+ expect(matcher.matches?(Example)).to be true
222
+ end
223
+ end
224
+
225
+ it 'has a description' do
226
+ matcher = described_class.new(:lenient, String).strict
227
+ matcher.matches?(Example)
228
+ expect(matcher.description).to eq 'have attribute lenient of type String and is strict'
229
+ end
230
+ end
132
231
  end
metadata CHANGED
@@ -1,155 +1,108 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtus-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tomas Mattia
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-02-06 00:00:00.000000000 Z
11
+ date: 2016-08-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: virtus
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: 1.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 1.0.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: bundler
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - "~>"
68
60
  - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
- - !ruby/object:Gem::Dependency
79
- name: guard-rspec
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ! '>='
84
- - !ruby/object:Gem::Version
85
- version: '0'
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: '0'
94
- - !ruby/object:Gem::Dependency
95
- name: rb-fsevent
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ! '>='
100
- - !ruby/object:Gem::Version
101
- version: '0'
61
+ version: '2.14'
102
62
  type: :development
103
63
  prerelease: false
104
64
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
65
  requirements:
107
- - - ! '>='
66
+ - - "~>"
108
67
  - !ruby/object:Gem::Version
109
- version: '0'
68
+ version: '2.14'
110
69
  description: RSpec matchers for Virtus
111
70
  email: tomas.mattia@gmail.com
112
71
  executables: []
113
72
  extensions: []
114
73
  extra_rdoc_files: []
115
74
  files:
75
+ - lib/virtus-matchers.rb
76
+ - lib/virtus/matchers.rb
116
77
  - lib/virtus/matchers/be_value_object_matcher.rb
117
78
  - lib/virtus/matchers/have_attribute_matcher.rb
79
+ - lib/virtus/matchers/integrations.rb
118
80
  - lib/virtus/matchers/version.rb
119
- - lib/virtus/matchers.rb
120
- - lib/virtus-matchers.rb
121
81
  - spec/spec_helper.rb
122
82
  - spec/virtus/matchers/be_value_object_matcher_spec.rb
123
83
  - spec/virtus/matchers/have_attribute_matcher_spec.rb
124
84
  homepage: https://github.com/tmattia/virtus-matchers
125
85
  licenses: []
86
+ metadata: {}
126
87
  post_install_message:
127
88
  rdoc_options: []
128
89
  require_paths:
129
90
  - lib
130
91
  required_ruby_version: !ruby/object:Gem::Requirement
131
- none: false
132
92
  requirements:
133
- - - ! '>='
93
+ - - ">="
134
94
  - !ruby/object:Gem::Version
135
95
  version: '0'
136
- segments:
137
- - 0
138
- hash: -3226766731789943269
139
96
  required_rubygems_version: !ruby/object:Gem::Requirement
140
- none: false
141
97
  requirements:
142
- - - ! '>='
98
+ - - ">="
143
99
  - !ruby/object:Gem::Version
144
100
  version: '0'
145
- segments:
146
- - 0
147
- hash: -3226766731789943269
148
101
  requirements: []
149
102
  rubyforge_project:
150
- rubygems_version: 1.8.23
103
+ rubygems_version: 2.5.1
151
104
  signing_key:
152
- specification_version: 3
105
+ specification_version: 4
153
106
  summary: RSpec matchers for Virtus
154
107
  test_files:
155
108
  - spec/spec_helper.rb