rspec-virtus 0.0.1 → 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.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+ - rbx-19mode
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Rspec::Virtus
1
+ # Rspec::Virtus [![Build Status](https://travis-ci.org/mikespokefire/rspec-virtus.png?branch=master)](https://travis-ci.org/mikespokefire/rspec-virtus)
2
2
 
3
3
  Simple RSpec matchers for your Virtus objects
4
4
 
@@ -24,6 +24,7 @@ Here is a sample Virtus object
24
24
  include Virtus
25
25
  attribute :title, String
26
26
  attribtue :body, String
27
+ attribtue :comments, Array[String]
27
28
  end
28
29
 
29
30
  And with `rspec-virtus` we can now make simple assertions about these models
@@ -39,6 +40,10 @@ And with `rspec-virtus` we can now make simple assertions about these models
39
40
  it "has an attribute with a type check" do
40
41
  expect(described_class).to have_attribute(:body).of_type(String)
41
42
  end
43
+
44
+ it "has an array attribute with a type check" do
45
+ expect(described_class).to have_attribute(:comments).of_type(Array, member_type: String)
46
+ end
42
47
  end
43
48
  end
44
49
 
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -6,8 +6,9 @@ module RSpec
6
6
  @options = {}
7
7
  end
8
8
 
9
- def of_type(type)
9
+ def of_type(type, options={})
10
10
  @options[:type] = type
11
+ @options[:member_type] = options.delete(:member_type)
11
12
  self
12
13
  end
13
14
 
@@ -30,6 +31,10 @@ module RSpec
30
31
  @subject.attribute_set[@attribute_name]
31
32
  end
32
33
 
34
+ def member_type
35
+ attribute.options[:member_type]
36
+ end
37
+
33
38
  def attribute_type
34
39
  attribute.options[:primitive]
35
40
  end
@@ -39,7 +44,9 @@ module RSpec
39
44
  end
40
45
 
41
46
  def type_correct?
42
- if @options[:type]
47
+ if @options[:member_type]
48
+ member_type == @options[:member_type] && attribute_type == @options[:type]
49
+ elsif @options[:type]
43
50
  attribute_type == @options[:type]
44
51
  else
45
52
  true
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Virtus
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -4,12 +4,12 @@ require 'virtus'
4
4
  describe RSpec::Virtus::Matcher do
5
5
  let(:instance) { described_class.new(attribute_name) }
6
6
  let(:attribute_name) { :the_attribute }
7
- let(:type) { String }
8
7
 
9
8
  class DummyVirtus
10
9
  include Virtus
11
10
 
12
11
  attribute :the_attribute, String
12
+ attribute :the_array_attribute, Array[String]
13
13
  end
14
14
 
15
15
  describe '#matches?' do
@@ -23,6 +23,22 @@ describe RSpec::Virtus::Matcher do
23
23
  end
24
24
 
25
25
  context 'successful match on attribute name and type' do
26
+ before do
27
+ instance.of_type(String)
28
+ end
29
+
30
+ it 'returns true' do
31
+ expect(subject).to eql(true)
32
+ end
33
+ end
34
+
35
+ context 'successful match on attribute name, type and member_type' do
36
+ let(:attribute_name) { :the_array_attribute }
37
+
38
+ before do
39
+ instance.of_type(Array, member_type: String)
40
+ end
41
+
26
42
  it 'returns true' do
27
43
  expect(subject).to eql(true)
28
44
  end
@@ -38,25 +54,56 @@ describe RSpec::Virtus::Matcher do
38
54
 
39
55
  context 'unsuccessful match on attribute name and type' do
40
56
  let(:attribute_name) { :something_else }
41
- let(:type) { Integer }
57
+
58
+ before do
59
+ instance.of_type(Integer)
60
+ end
42
61
 
43
62
  it 'returns false' do
44
63
  expect(subject).to eql(false)
45
64
  end
46
65
  end
47
- end
48
66
 
49
- describe '#of_type' do
50
- subject { instance.of_type(type) }
67
+ context 'unsuccessful match on attribute name, type and member_type' do
68
+ let(:attribute_name) { :the_array_attribute }
69
+
70
+ before do
71
+ instance.of_type(Array, member_type: Integer)
72
+ end
51
73
 
52
- it 'adds an option to allow the type to be checked' do
53
- options_type = subject.instance_variable_get(:@options)[:type]
54
- expect(options_type).to eql(type)
74
+ it 'returns false' do
75
+ expect(subject).to eql(false)
76
+ end
55
77
  end
78
+ end
79
+
80
+ describe '#of_type' do
81
+ subject { instance.of_type(String) }
56
82
 
57
83
  it 'returns itsself so it can be chained' do
58
84
  expect(subject).to eql(instance)
59
85
  end
86
+
87
+ context "singular values" do
88
+ it 'adds an option to allow the type to be checked' do
89
+ options_type = subject.instance_variable_get(:@options)[:type]
90
+ expect(options_type).to eql(String)
91
+ end
92
+ end
93
+
94
+ context "arrays of values" do
95
+ subject { instance.of_type(Array, member_type: String) }
96
+
97
+ it 'adds an option to allow the type to be checked' do
98
+ options_type = subject.instance_variable_get(:@options)[:type]
99
+ expect(options_type).to eql(Array)
100
+ end
101
+
102
+ it 'adds an option to allow the member_type to be checked' do
103
+ member_options_type = subject.instance_variable_get(:@options)[:member_type]
104
+ expect(member_options_type).to eql(String)
105
+ end
106
+ end
60
107
  end
61
108
 
62
109
  describe '#failure_message' do
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,5 @@ require 'rspec-virtus'
2
2
 
3
3
  RSpec.configure do |config|
4
4
  config.treat_symbols_as_metadata_keys_with_true_values = true
5
- config.run_all_when_everything_filtered = true
6
- config.filter_run :focus
7
5
  config.order = 'random'
8
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-virtus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
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-04-08 00:00:00.000000000 Z
12
+ date: 2013-05-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -69,6 +69,7 @@ files:
69
69
  - .gitignore
70
70
  - .rspec
71
71
  - .ruby-version
72
+ - .travis.yml
72
73
  - Gemfile
73
74
  - LICENSE.txt
74
75
  - README.md
@@ -94,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
95
  version: '0'
95
96
  segments:
96
97
  - 0
97
- hash: 2276233809426382691
98
+ hash: -299707439805697582
98
99
  required_rubygems_version: !ruby/object:Gem::Requirement
99
100
  none: false
100
101
  requirements:
@@ -103,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
104
  version: '0'
104
105
  segments:
105
106
  - 0
106
- hash: 2276233809426382691
107
+ hash: -299707439805697582
107
108
  requirements: []
108
109
  rubyforge_project:
109
110
  rubygems_version: 1.8.24