virtus-matchers 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,26 +8,45 @@ module Virtus
|
|
8
8
|
@type = type
|
9
9
|
end
|
10
10
|
|
11
|
+
def coerced_with(custom_coercer)
|
12
|
+
@custom_coercer = custom_coercer
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
11
16
|
def matches?(klass)
|
12
17
|
@klass = klass
|
13
|
-
attribute = @klass.attribute_set[@name]
|
14
|
-
return false unless attribute
|
18
|
+
@attribute = @klass.attribute_set[@name]
|
19
|
+
return false unless @attribute
|
20
|
+
|
15
21
|
if @type.class == Array
|
16
|
-
attribute.primitive == Array &&
|
17
|
-
attribute.options[:member_type].primitive == @type.first
|
18
|
-
else
|
19
|
-
attribute.primitive == @type
|
22
|
+
return @attribute.primitive == Array &&
|
23
|
+
@attribute.options[:member_type].primitive == @type.first
|
20
24
|
end
|
25
|
+
|
26
|
+
valid_type && valid_coercer
|
21
27
|
end
|
22
28
|
|
23
29
|
def description
|
24
30
|
type = @type.class == Array ? "Array#{@type}" : @type
|
25
|
-
"have attribute #{@name} of type #{type}"
|
31
|
+
"have attribute #{@name} of type #{type}#{coercer_description}"
|
26
32
|
end
|
27
33
|
|
28
34
|
def failure_message
|
29
35
|
"expected #{@klass} to #{description}"
|
30
36
|
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def valid_type
|
40
|
+
@attribute.primitive == @type
|
41
|
+
end
|
42
|
+
|
43
|
+
def valid_coercer
|
44
|
+
@custom_coercer.nil? || @custom_coercer == @attribute.coercer
|
45
|
+
end
|
46
|
+
|
47
|
+
def coercer_description
|
48
|
+
@custom_coercer && " coerced with #{@attribute.coercer}"
|
49
|
+
end
|
31
50
|
end
|
32
51
|
|
33
52
|
def have_attribute(name, type)
|
@@ -1,12 +1,15 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
describe Virtus::Matchers::HaveAttributeMatcher do
|
4
|
+
class FakeCoercer; end
|
5
|
+
|
4
6
|
class Example
|
5
7
|
include Virtus.model
|
6
8
|
|
7
9
|
attribute :foo, String
|
8
10
|
attribute :bar, Array[String]
|
9
11
|
attribute :baz, Array
|
12
|
+
attribute :lol, DateTime, coercer: FakeCoercer
|
10
13
|
end
|
11
14
|
|
12
15
|
context 'when attribute is defined', 'with simple type' do
|
@@ -35,6 +38,32 @@ describe Virtus::Matchers::HaveAttributeMatcher do
|
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
41
|
+
context 'when attribute is defined', 'with a valid coercer' do
|
42
|
+
let(:matcher) { described_class.new(:lol, DateTime).coerced_with(FakeCoercer) }
|
43
|
+
|
44
|
+
it 'should match' do
|
45
|
+
matcher.matches?(Example).should be_true
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should have a description' do
|
49
|
+
matcher.matches?(Example)
|
50
|
+
matcher.description.should == 'have attribute lol of type DateTime coerced with FakeCoercer'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'when attribute is defined', 'with invalid coercer' do
|
55
|
+
let(:matcher) { described_class.new(:lol, DateTime).coerced_with(String) }
|
56
|
+
|
57
|
+
it 'should not match' do
|
58
|
+
matcher.matches?(Example).should be_false
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should have a failure message' do
|
62
|
+
matcher.matches?(Example)
|
63
|
+
matcher.failure_message.should == "expected #{Example} to have attribute lol of type DateTime coerced with FakeCoercer"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
38
67
|
context 'when attribute is defined', 'with array type', 'and no member type' do
|
39
68
|
let(:matcher) { described_class.new(:baz, Array) }
|
40
69
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: virtus-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-10-
|
12
|
+
date: 2013-10-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: virtus
|
@@ -135,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
segments:
|
137
137
|
- 0
|
138
|
-
hash: -
|
138
|
+
hash: -3202611215522321042
|
139
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
140
|
none: false
|
141
141
|
requirements:
|
@@ -144,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
144
|
version: '0'
|
145
145
|
segments:
|
146
146
|
- 0
|
147
|
-
hash: -
|
147
|
+
hash: -3202611215522321042
|
148
148
|
requirements: []
|
149
149
|
rubyforge_project:
|
150
150
|
rubygems_version: 1.8.23
|