virtus-matchers 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52e8338d991fb465d12e9ab65a69e8f61015b8d2
4
- data.tar.gz: b17b4acc8f0f20047d3cada608cfe58ab8ad56b2
3
+ metadata.gz: 97b686cd951cd3ea9ffda93559f825c5fa077443
4
+ data.tar.gz: 39020162c5f9df630eef2a2f83b347452c920eec
5
5
  SHA512:
6
- metadata.gz: 007a99b23c490bcdf51462a1f0fb17a362a5d4780d5739186f54fda254fdf5e6e1393a6fdf7e1b4483b7113a35674358e4c14afbf04229437331ee5e1e0321fb
7
- data.tar.gz: d4ce5da2e13df2c46eabc2a17936a7358ec6fdf64b4584752e81805df5761b35daf801b328ce3cb6d29b76c2b99f449e111c28c92fa450567550366461fa9ad5
6
+ metadata.gz: 17f3cb5243479df8a5be34ec1bd735aba7e61fed638795349e5a786f8a72a03f448e7b6316a928f714f525b4fa694e8c19479eb468210fc7eace0a91a7bb624f
7
+ data.tar.gz: 7b75a66e9eb008db3bec0eed281df21eb25c4a3a9973baabc9859c82549633bf0e58d89492e17fc81eb061e705199c4ac70224aa850e9f38ae95cccd2d56f21c
@@ -3,4 +3,3 @@
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'
@@ -23,8 +23,10 @@ module Virtus
23
23
  self
24
24
  end
25
25
 
26
- def matches?(klass)
27
- @klass = klass
26
+ def matches?(klass_or_instance)
27
+ @instance = instance_from(klass_or_instance)
28
+ @klass = @instance.class
29
+
28
30
  @attribute = @klass.attribute_set[@name]
29
31
  return false unless @attribute
30
32
 
@@ -70,6 +72,22 @@ module Virtus
70
72
  def coercer_description
71
73
  @custom_coercer && " coerced with #{@attribute.coercer}"
72
74
  end
75
+
76
+ def instance_from(klass_or_instance)
77
+ unless klass_or_instance.respond_to?(:attribute_set)
78
+ return klass_or_instance
79
+ end
80
+
81
+ klass = klass_or_instance
82
+ strict_attributes = klass.attribute_set.select(&:strict?)
83
+ attributes = strict_attributes.each_with_object({}) do |attribute, hash|
84
+ hash[attribute.name] = "val"
85
+ end.merge(@name => "val")
86
+ klass.new(attributes)
87
+ rescue => e
88
+ warn "NOTE: Due to the complexity of putting the right value for the attributes, please pass in an instance that has already been properly initialized"
89
+ raise e
90
+ end
73
91
  end
74
92
 
75
93
  def have_attribute(name, type = nil)
@@ -0,0 +1,5 @@
1
+ require "virtus/matchers"
2
+
3
+ RSpec.configure do |config|
4
+ config.include Virtus::Matchers, type: :virtus
5
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Virtus
4
4
  module Matchers
5
- VERSION = '0.3.0'.freeze
5
+ VERSION = '0.4.0'.freeze
6
6
  end
7
7
  end
@@ -13,23 +13,23 @@ RSpec.describe Virtus::Matchers::BeAValueObjectMatcher do
13
13
 
14
14
  context 'when Virtus::ValueObject is included' do
15
15
  it 'should match' do
16
- matcher.matches?(ExampleValueObject).should be_true
16
+ expect(matcher.matches?(ExampleValueObject)).to be true
17
17
  end
18
18
 
19
19
  it 'should have a description' do
20
20
  matcher.matches?(ExampleValueObject)
21
- matcher.description.should == 'be a value object'
21
+ expect(matcher.description).to eq 'be a value object'
22
22
  end
23
23
  end
24
24
 
25
25
  context 'when Virtus::ValueObject is not included' do
26
26
  it 'should not match' do
27
- matcher.matches?(Example).should be_false
27
+ expect(matcher.matches?(Example)).to be false
28
28
  end
29
29
 
30
30
  it 'should have a failure message' do
31
31
  matcher.matches?(Example)
32
- matcher.failure_message.should == "expected #{Example} to be a value object"
32
+ expect(matcher.failure_message).to eq "expected #{Example} to be a value object"
33
33
  end
34
34
  end
35
35
  end
@@ -2,7 +2,11 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  RSpec.describe Virtus::Matchers::HaveAttributeMatcher do
5
- class FakeCoercer; end
5
+ class FakeCoercer
6
+ def self.call(val)
7
+
8
+ end
9
+ end
6
10
 
7
11
  class Example
8
12
  include Virtus.model
@@ -19,46 +23,46 @@ RSpec.describe Virtus::Matchers::HaveAttributeMatcher do
19
23
  attribute :lenient, String
20
24
  end
21
25
 
22
- context 'when attribute is defined', 'with no type' do
26
+ context 'when attribute is defined with no type' do
23
27
  let(:matcher) { described_class.new(:any) }
24
28
 
25
29
  it 'should match' do
26
- matcher.matches?(Example).should be_true
30
+ expect(matcher.matches?(Example)).to be true
27
31
  end
28
32
 
29
33
  it 'should have a description' do
30
34
  matcher.matches?(Example)
31
- matcher.description.should == 'have attribute any'
35
+ expect(matcher.description).to eq 'have attribute any'
32
36
  end
33
37
  end
34
38
 
35
- context 'when attribute is defined', 'with simple type' do
39
+ context 'when attribute is defined with simple type' do
36
40
  let(:matcher) { described_class.new(:foo, String) }
37
41
 
38
42
  it 'should match' do
39
- matcher.matches?(Example).should be_true
43
+ expect(matcher.matches?(Example)).to be true
40
44
  end
41
45
 
42
46
  it 'should have a description' do
43
47
  matcher.matches?(Example)
44
- matcher.description.should == 'have attribute foo of type String'
48
+ expect(matcher.description).to eq 'have attribute foo of type String'
45
49
  end
46
50
  end
47
51
 
48
- context 'when attribute is defined', 'with array type', 'and correct member type' do
52
+ context 'when attribute is defined with array type and correct member type' do
49
53
  let(:matcher) { described_class.new(:bar, Array[String]) }
50
54
 
51
55
  it 'should match' do
52
- matcher.matches?(Example).should be_true
56
+ expect(matcher.matches?(Example)).to be true
53
57
  end
54
58
 
55
59
  it 'should have a description' do
56
60
  matcher.matches?(Example)
57
- matcher.description.should == 'have attribute bar of type Array[String]'
61
+ expect(matcher.description).to eq 'have attribute bar of type Array[String]'
58
62
  end
59
63
  end
60
64
 
61
- context 'when attribute is defined', 'with array type', 'with default' do
65
+ context 'when attribute is defined with array type with default' do
62
66
  let(:matcher) do
63
67
  described_class.
64
68
  new(:array_attribute_with_default, Array[String]).
@@ -67,84 +71,84 @@ RSpec.describe Virtus::Matchers::HaveAttributeMatcher do
67
71
  let(:array_default) { ['hello', 'world'] }
68
72
 
69
73
  it 'should match' do
70
- matcher.matches?(Example).should be_true
74
+ expect(matcher.matches?(Example)).to be true
71
75
  end
72
76
 
73
77
  context 'different array default' do
74
78
  let(:array_default) { ['different', 'default'] }
75
79
  it 'should not match' do
76
- matcher.matches?(Example).should be_false
80
+ expect(matcher.matches?(Example)).to be false
77
81
  end
78
82
  end
79
83
 
80
84
  it 'should have a description' do
81
85
  matcher.matches?(Example)
82
- matcher.description.should == "have attribute array_attribute_with_default of type Array[String] with default \"[\"hello\", \"world\"]\""
86
+ expect(matcher.description).to eq "have attribute array_attribute_with_default of type Array[String] with default \"[\"hello\", \"world\"]\""
83
87
  end
84
88
  end
85
89
 
86
- context 'when attribute is defined', 'with a valid coercer' do
90
+ context 'when attribute is defined with a valid coercer' do
87
91
  let(:matcher) { described_class.new(:lol, DateTime).coerced_with(FakeCoercer) }
88
92
 
89
93
  it 'should match' do
90
- matcher.matches?(Example).should be_true
94
+ expect(matcher.matches?(Example)).to be true
91
95
  end
92
96
 
93
97
  it 'should have a description' do
94
98
  matcher.matches?(Example)
95
- matcher.description.should == 'have attribute lol of type DateTime coerced with FakeCoercer'
99
+ expect(matcher.description).to eq 'have attribute lol of type DateTime coerced with FakeCoercer'
96
100
  end
97
101
  end
98
102
 
99
- context 'when attribute is defined', 'with invalid coercer' do
103
+ context 'when attribute is defined with invalid coercer' do
100
104
  let(:matcher) { described_class.new(:lol, DateTime).coerced_with(String) }
101
105
 
102
106
  it 'should not match' do
103
- matcher.matches?(Example).should be_false
107
+ expect(matcher.matches?(Example)).to be false
104
108
  end
105
109
 
106
110
  it 'should have a failure message' do
107
111
  matcher.matches?(Example)
108
- matcher.failure_message.should == "expected #{Example} to have attribute lol of type DateTime coerced with FakeCoercer"
112
+ expect(matcher.failure_message).to eq "expected #{Example} to have attribute lol of type DateTime coerced with FakeCoercer"
109
113
  end
110
114
  end
111
115
 
112
- context 'when attribute is defined', 'with array type', 'and no member type' do
116
+ context 'when attribute is defined with array type and no member type' do
113
117
  let(:matcher) { described_class.new(:baz, Array) }
114
118
 
115
119
  it 'should match' do
116
- matcher.matches?(Example).should be_true
120
+ expect(matcher.matches?(Example)).to be true
117
121
  end
118
122
 
119
123
  it 'should have a description' do
120
124
  matcher.matches?(Example)
121
- matcher.description.should == 'have attribute baz of type Array'
125
+ expect(matcher.description).to eq 'have attribute baz of type Array'
122
126
  end
123
127
  end
124
128
 
125
- context 'when attribute is defined', 'with array type', 'but wrong member type' do
129
+ context 'when attribute is defined with array type but wrong member type' do
126
130
  let(:matcher) { described_class.new(:bar, Array[Integer]) }
127
131
 
128
132
  it 'should not match' do
129
- matcher.matches?(Example).should be_false
133
+ expect(matcher.matches?(Example)).to be false
130
134
  end
131
135
 
132
136
  it 'should have a failure message' do
133
137
  matcher.matches?(Example)
134
- matcher.failure_message.should == "expected #{Example} to have attribute bar of type Array[Integer]"
138
+ expect(matcher.failure_message).to eq "expected #{Example} to have attribute bar of type Array[Integer]"
135
139
  end
136
140
  end
137
141
 
138
- context 'when attribute is defined', 'with wrong type' do
142
+ context 'when attribute is defined with wrong type' do
139
143
  let(:matcher) { described_class.new(:foo, Hash) }
140
144
 
141
145
  it 'should not match' do
142
- matcher.matches?(Example).should be_false
146
+ expect(matcher.matches?(Example)).to be false
143
147
  end
144
148
 
145
149
  it 'should have a failure message' do
146
150
  matcher.matches?(Example)
147
- matcher.failure_message.should == "expected #{Example} to have attribute foo of type Hash"
151
+ expect(matcher.failure_message).to eq "expected #{Example} to have attribute foo of type Hash"
148
152
  end
149
153
  end
150
154
 
@@ -152,12 +156,12 @@ RSpec.describe Virtus::Matchers::HaveAttributeMatcher do
152
156
  let(:matcher) { described_class.new(:baz, String) }
153
157
 
154
158
  it 'should not match' do
155
- matcher.matches?(Example).should be_false
159
+ expect(matcher.matches?(Example)).to be false
156
160
  end
157
161
 
158
162
  it 'should have a failure message' do
159
163
  matcher.matches?(Example)
160
- matcher.failure_message.should == "expected #{Example} to have attribute baz of type String"
164
+ expect(matcher.failure_message).to eq "expected #{Example} to have attribute baz of type String"
161
165
  end
162
166
  end
163
167
 
@@ -228,4 +232,11 @@ RSpec.describe Virtus::Matchers::HaveAttributeMatcher do
228
232
  expect(matcher.description).to eq 'have attribute lenient of type String and is strict'
229
233
  end
230
234
  end
235
+
236
+ context "subject is an instance" do
237
+ it "matches" do
238
+ matcher = described_class.new(:strict, String)
239
+ expect(matcher.matches?(Example.new(strict: "strict"))).to be true
240
+ end
241
+ end
231
242
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtus-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Mattia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-12 00:00:00.000000000 Z
11
+ date: 2017-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '2.14'
61
+ version: '3.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '2.14'
68
+ version: '3.0'
69
69
  description: RSpec matchers for Virtus
70
70
  email: tomas.mattia@gmail.com
71
71
  executables: []
@@ -76,7 +76,7 @@ files:
76
76
  - lib/virtus/matchers.rb
77
77
  - lib/virtus/matchers/be_value_object_matcher.rb
78
78
  - lib/virtus/matchers/have_attribute_matcher.rb
79
- - lib/virtus/matchers/integrations.rb
79
+ - lib/virtus/matchers/rspec.rb
80
80
  - lib/virtus/matchers/version.rb
81
81
  - spec/spec_helper.rb
82
82
  - spec/virtus/matchers/be_value_object_matcher_spec.rb
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  requirements: []
102
102
  rubyforge_project:
103
- rubygems_version: 2.5.1
103
+ rubygems_version: 2.6.10
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: RSpec matchers for Virtus
@@ -1,5 +0,0 @@
1
- if defined?(RSpec)
2
- RSpec.configure do |config|
3
- config.include Virtus::Matchers, type: :virtus
4
- end
5
- end