rspec-virtus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p194
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-virtus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Michael Smith
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Rspec::Virtus
2
+
3
+ Simple RSpec matchers for your Virtus objects
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rspec-virtus'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rspec-virtus
18
+
19
+ ## Usage
20
+
21
+ Here is a sample Virtus object
22
+
23
+ class Post
24
+ include Virtus
25
+ attribute :title, String
26
+ attribtue :body, String
27
+ end
28
+
29
+ And with `rspec-virtus` we can now make simple assertions about these models
30
+
31
+ require 'spec_helper'
32
+
33
+ describe Post
34
+ describe 'attributes' do
35
+ it "has an attribute" do
36
+ expect(described_class).to have_attribute(:title)
37
+ end
38
+
39
+ it "has an attribute with a type check" do
40
+ expect(described_class).to have_attribute(:body).of_type(String)
41
+ end
42
+ end
43
+ end
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,50 @@
1
+ module RSpec
2
+ module Virtus
3
+ class Matcher
4
+ def initialize(attribute_name)
5
+ @attribute_name = attribute_name
6
+ @options = {}
7
+ end
8
+
9
+ def of_type(type)
10
+ @options[:type] = type
11
+ self
12
+ end
13
+
14
+ def matches?(subject)
15
+ @subject = subject
16
+ attribute_exists? && type_correct?
17
+ end
18
+
19
+ def failure_message
20
+ "expected #{@attribute_name} to be defined"
21
+ end
22
+
23
+ def negative_failure_message
24
+ "expected #{@attribute_name} not to be defined"
25
+ end
26
+
27
+ private
28
+
29
+ def attribute
30
+ @subject.attribute_set[@attribute_name]
31
+ end
32
+
33
+ def attribute_type
34
+ attribute.options[:primitive]
35
+ end
36
+
37
+ def attribute_exists?
38
+ attribute != nil
39
+ end
40
+
41
+ def type_correct?
42
+ if @options[:type]
43
+ attribute_type == @options[:type]
44
+ else
45
+ true
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module RSpec
2
+ module Virtus
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require "rspec/core"
2
+ require "rspec-virtus/matcher"
3
+ require "rspec-virtus/version"
4
+
5
+ module RSpec
6
+ module Virtus
7
+ def have_attribute(attribute_name)
8
+ Matcher.new(attribute_name)
9
+ end
10
+ end
11
+ end
12
+
13
+ RSpec.configure do |config|
14
+ config.include RSpec::Virtus
15
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rspec-virtus/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rspec-virtus"
8
+ gem.version = RSpec::Virtus::VERSION
9
+ gem.authors = ["Michael Smith"]
10
+ gem.email = ["mike@spokefire.co.uk"]
11
+ gem.description = %q{Simple RSpec matchers for Virtus objects}
12
+ gem.summary = %q{Simple RSpec matchers for Virtus objects}
13
+ gem.homepage = "https://github.com/mikespokefire/rspec-virtus"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rspec", "~> 2.0"
21
+ gem.add_dependency "virtus", "~> 0.5"
22
+
23
+ gem.add_development_dependency "rake", "~> 10.0.0"
24
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+ require 'virtus'
3
+
4
+ describe RSpec::Virtus::Matcher do
5
+ let(:instance) { described_class.new(attribute_name) }
6
+ let(:attribute_name) { :the_attribute }
7
+ let(:type) { String }
8
+
9
+ class DummyVirtus
10
+ include Virtus
11
+
12
+ attribute :the_attribute, String
13
+ end
14
+
15
+ describe '#matches?' do
16
+ subject { instance.matches?(actual) }
17
+ let(:actual) { DummyVirtus }
18
+
19
+ context 'successful match on attribute name' do
20
+ it 'returns true' do
21
+ expect(subject).to eql(true)
22
+ end
23
+ end
24
+
25
+ context 'successful match on attribute name and type' do
26
+ it 'returns true' do
27
+ expect(subject).to eql(true)
28
+ end
29
+ end
30
+
31
+ context 'unsuccessful match on attribute name' do
32
+ let(:attribute_name) { :something_else }
33
+
34
+ it 'returns false' do
35
+ expect(subject).to eql(false)
36
+ end
37
+ end
38
+
39
+ context 'unsuccessful match on attribute name and type' do
40
+ let(:attribute_name) { :something_else }
41
+ let(:type) { Integer }
42
+
43
+ it 'returns false' do
44
+ expect(subject).to eql(false)
45
+ end
46
+ end
47
+ end
48
+
49
+ describe '#of_type' do
50
+ subject { instance.of_type(type) }
51
+
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)
55
+ end
56
+
57
+ it 'returns itsself so it can be chained' do
58
+ expect(subject).to eql(instance)
59
+ end
60
+ end
61
+
62
+ describe '#failure_message' do
63
+ subject { instance.negative_failure_message }
64
+
65
+ it 'tells you which attribute failed' do
66
+ expect(subject).to include(attribute_name.to_s)
67
+ end
68
+ end
69
+
70
+ describe '#negative_failure_message' do
71
+ subject { instance.negative_failure_message }
72
+
73
+ it 'tells you which attribute failed' do
74
+ expect(subject).to include(attribute_name.to_s)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpec::Virtus do
4
+ let(:instance) { Class.new { include RSpec::Virtus }.new }
5
+
6
+ describe '#have_attribute' do
7
+ subject { instance.have_attribute(:attribute_name) }
8
+
9
+ it 'returns a new matcher instance' do
10
+ expect(subject).to be_an_instance_of(RSpec::Virtus::Matcher)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ require 'rspec-virtus'
2
+
3
+ RSpec.configure do |config|
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
+ config.order = 'random'
8
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-virtus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: virtus
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.5'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.5'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 10.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 10.0.0
62
+ description: Simple RSpec matchers for Virtus objects
63
+ email:
64
+ - mike@spokefire.co.uk
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - .ruby-version
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - lib/rspec-virtus.rb
77
+ - lib/rspec-virtus/matcher.rb
78
+ - lib/rspec-virtus/version.rb
79
+ - rspec-virtus.gemspec
80
+ - spec/lib/rspec-virtus/matcher_spec.rb
81
+ - spec/lib/rspec-virtus_spec.rb
82
+ - spec/spec_helper.rb
83
+ homepage: https://github.com/mikespokefire/rspec-virtus
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: 2276233809426382691
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: 2276233809426382691
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 1.8.24
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Simple RSpec matchers for Virtus objects
113
+ test_files:
114
+ - spec/lib/rspec-virtus/matcher_spec.rb
115
+ - spec/lib/rspec-virtus_spec.rb
116
+ - spec/spec_helper.rb