grape-entity-matchers 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,38 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+ .com.apple.timemachine.supported
4
+
5
+ ## TEXTMATE
6
+ *.tmproj
7
+ tmtags
8
+
9
+ ## EMACS
10
+ *~
11
+ \#*
12
+ .\#*
13
+
14
+ ## REDCAR
15
+ .redcar
16
+
17
+ ## VIM
18
+ *.swp
19
+ *.swo
20
+
21
+ ## RUBYMINE
22
+ .idea
23
+
24
+ ## PROJECT::GENERAL
25
+ coverage
26
+ doc
27
+ pkg
28
+ .rvmrc
29
+ .bundle
30
+ .yardoc/*
31
+ dist
32
+ Gemfile.lock
33
+ tmp
34
+
35
+ ## Rubinius
36
+ .rbx
37
+
38
+ ## PROJECT::SPECIFIC
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=progress
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby
6
+ - rbx
7
+ - ree
8
+
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ --markup-provider=redcarpet
2
+ --markup=markdown
@@ -0,0 +1,8 @@
1
+ Next Release
2
+ ============
3
+ * Your contribution here.
4
+
5
+ 0.1.0 (01/11/2013)
6
+ ==================
7
+
8
+ * Initial Release
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'pry'
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ gem 'guard-bundler'
10
+ gem 'rb-fsevent'
11
+ gem 'growl'
12
+ gem 'json'
13
+ gem 'rspec'
14
+ gem 'rack-test', "~> 0.6.2", :require => "rack/test"
15
+ gem 'github-markup'
16
+ end
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch(%r{^spec/support/shared_versioning_examples.rb$}) { |m| "spec/" }
8
+ watch('spec/spec_helper.rb') { "spec/" }
9
+ end
10
+
11
+
12
+ guard 'bundler' do
13
+ watch('Gemfile')
14
+ watch(/^.+\.gemspec/)
15
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Mark Madsen, and AGiLE ANiMAL INC.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,56 @@
1
+ # Grape::Entity::Matchers
2
+
3
+ [![Build Status](https://travis-ci.org/agileanimal/grape-entity-matchers.png?branch=master)](https://travis-ci.org/agileanimal/grape-entity-matchers)
4
+
5
+ ## Introduction
6
+
7
+ This gem provides shoulda-style matchers for Rspec to [GrapeEntity](https://github.com/agileanimal/grape-entity).
8
+
9
+ ## Using the Matchers
10
+
11
+ Here are some examples of the matchers in use:
12
+
13
+ ``` ruby
14
+ it { should represent(:date_of_birth).as(:brithday) }
15
+ it { should_not represent(:name).as(:brithday) }
16
+ it { should_not represent(:date_of_birth) }
17
+
18
+ it { should represent(:secret).when( :authorized? => true ) }
19
+ it { should_not represent(:secret).when( :authorized? => false ) }
20
+
21
+ it { should represent(:super_dooper_secret).as(:top_secret).when( :authorized? => true ) }
22
+ it { should_not represent(:super_dooper_secret).as(:top_secret).when( :authorized? => false ) }
23
+
24
+ it { should represent(:dog).using(PetEntity) }
25
+ it { should represent(:cat).as(:kitty).using(PetEntity) }
26
+ ```
27
+
28
+ ## Installation
29
+
30
+ Add this line to your application's Gemfile:
31
+
32
+ gem 'grape-entity-matchers', :group => :test
33
+
34
+ And then execute:
35
+
36
+ $ bundle
37
+
38
+ Or install it yourself as:
39
+
40
+ $ gem install grape-entity-matchers
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
49
+
50
+ ## License
51
+
52
+ MIT License. See LICENSE for details.
53
+
54
+ ## Copyright
55
+
56
+ Copyright (c) 2013 Mark Madsen, and AGiLE ANiMAL INC.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup :default, :test, :development
4
+
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = 'spec/**/*_spec.rb'
10
+ end
11
+
12
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
13
+ spec.pattern = 'spec/**/*_spec.rb'
14
+ spec.rcov = true
15
+ end
16
+
17
+ task :spec
18
+ task :default => :spec
19
+
20
+ #
21
+ # TODO: setup a place for documentation and then get this going again.
22
+ #
23
+ # begin
24
+ # require 'yard'
25
+ # DOC_FILES = ['lib/**/*.rb', 'README.markdown']
26
+ #
27
+ # YARD::Rake::YardocTask.new(:doc) do |t|
28
+ # t.files = DOC_FILES
29
+ # end
30
+ #
31
+ # namespace :doc do
32
+ # YARD::Rake::YardocTask.new(:pages) do |t|
33
+ # t.files = DOC_FILES
34
+ # t.options = ['-o', '../grape.doc']
35
+ # end
36
+ #
37
+ # namespace :pages do
38
+ # desc 'Generate and publish YARD docs to GitHub pages.'
39
+ # task :publish => ['doc:pages'] do
40
+ # Dir.chdir(File.dirname(__FILE__) + '/../grape.doc') do
41
+ # system("git add .")
42
+ # system("git add -u")
43
+ # system("git commit -m 'Generating docs for version #{version}.'")
44
+ # system("git push origin gh-pages")
45
+ # end
46
+ # end
47
+ # end
48
+ # end
49
+ # rescue LoadError
50
+ # puts "You need to install YARD."
51
+ # end
@@ -0,0 +1,30 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "grape_entity_matchers/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "grape-entity-matchers"
6
+ s.version = GrapeEntityMatchers::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Mark Madsen"]
9
+ s.email = ["mark@agileanimal.com"]
10
+ s.homepage = "https://github.com/agileanimal/grape-entity-matchers"
11
+ s.summary = %q{Shoulda-like matchers for Grape Entity.}
12
+ s.description = %q{Shoulda-like matchers for Grape Entity.}
13
+ s.license = "MIT"
14
+
15
+ s.rubyforge_project = "grape-entity-matchers"
16
+
17
+ s.add_runtime_dependency 'grape-entity', '>= 0.2.0'
18
+ s.add_runtime_dependency 'rspec', '~> 2.9'
19
+
20
+
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'maruku'
23
+ s.add_development_dependency 'yard'
24
+ s.add_development_dependency 'bundler'
25
+
26
+ s.files = `git ls-files`.split("\n")
27
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
28
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
29
+ s.require_paths = ["lib"]
30
+ end
@@ -0,0 +1 @@
1
+ require 'grape_entity_matchers'
@@ -0,0 +1,3 @@
1
+ require 'grape_entity_matchers/version'
2
+ require 'grape_entity_matchers/represent_matcher'
3
+ require 'grape_entity_matchers/rspec_integration'
@@ -0,0 +1,102 @@
1
+ require 'rspec/mocks'
2
+
3
+ module GrapeEntityMatchers
4
+ module RepresentMatcher
5
+ def represent(representable)
6
+ RepresentMatcher.new(representable)
7
+ end
8
+
9
+ class RepresentMatcher
10
+ def initialize(representable)
11
+ @expected_representable = representable
12
+ RSpec::Mocks::setup(self)
13
+ end
14
+
15
+ def matches?(subject)
16
+ @subject = subject
17
+
18
+ check_methods && verify_exposure && check_value
19
+ end
20
+
21
+ def as(representation)
22
+ @actual_representation = representation
23
+ self
24
+ end
25
+
26
+ def when(conditions)
27
+ @conditions = conditions
28
+ self
29
+ end
30
+
31
+ def using(other_entity)
32
+ @other_entity = other_entity
33
+ self
34
+ end
35
+
36
+ def failure_message
37
+ # why did it fail???
38
+ # 3 options, exposure, object not called, value not returned.
39
+ # TODO: fix this
40
+ #"Expected #{@expected_representable} to be called on mock, but it wasn't: #{@subject.exposures[@expected_representable]}"
41
+ message = ""
42
+ message << "#{@subject} didn't expose #{@expected_representable} as expected: #{@subject.exposures[@expected_representable]}" unless verify_exposure
43
+ message << "#{@subject} didn't call the method #{@actual_representation || @expected_representable} to get #{@expected_representable} from the test class.\n" unless check_methods
44
+ message << "#{@subject} return the correct value for #{@expected_representable}." unless check_value
45
+ end
46
+
47
+ def negative_failure_message
48
+ message = ""
49
+ message << "Didn't expect #{@subject} to expose #{@expected_representable} correctly: #{@subject.exposures[@expected_representable]}" if verify_exposure
50
+ message << "Didn't expect #{@subject} to call #{@actual_representation || @expected_representable} to get #{@expected_representable} from the test class.\n" if check_methods
51
+ message << "Didn't expect #{@subject} to return the correct value for #{@expected_representable}." if check_value
52
+ end
53
+
54
+ def description
55
+ "Ensures that #{@subject} properly obtains the value of #{@expected_representable} from a mock class."
56
+ end
57
+
58
+ private
59
+
60
+ def check_methods
61
+ representee = mock("RepresetedObject")
62
+ representee.should_receive(@expected_representable).and_return(:value)
63
+ representee.should_receive(@conditions.keys.first).and_return(@conditions.values.first) unless @conditions.nil?
64
+
65
+ @serialized_hash = @subject.represent(representee).serializable_hash
66
+
67
+ begin
68
+ RSpec::Mocks::verify # run mock verifications
69
+ methods_called = true
70
+ rescue RSpec::Mocks::MockExpectationError => e
71
+ # here one can use #{e} to construct an error message
72
+ methods_called = false
73
+ end
74
+
75
+ methods_called
76
+ end
77
+
78
+ def verify_exposure
79
+ hash = {}
80
+ hash[:using] = @other_entity unless @other_entity.nil?
81
+ hash[:as] = @actual_representation unless @actual_representation.nil?
82
+
83
+ if @conditions.nil?
84
+ @subject.exposures[@expected_representable] == hash
85
+ else
86
+ exposures = @subject.exposures[@expected_representable].dup
87
+ exposures.delete(:if) != nil && exposures == hash
88
+ end
89
+ end
90
+
91
+ def check_value
92
+ if @other_entity
93
+ # we aren't setting a value here, so it's going to be empty
94
+ @serialized_hash[@actual_representation || @expected_representable] == {}
95
+ else
96
+ @serialized_hash[@actual_representation || @expected_representable] == :value
97
+ end
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+
3
+ RSpec.configure do |config|
4
+ require 'grape_entity_matchers/represent_matcher'
5
+ config.include GrapeEntityMatchers::RepresentMatcher
6
+ end
@@ -0,0 +1,3 @@
1
+ module GrapeEntityMatchers
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ require 'grape_entity'
3
+
4
+ describe GrapeEntityMatchers do
5
+ before(:all) do
6
+ class PetEntity < Grape::Entity
7
+ expose :name, :age
8
+ end
9
+ class Person
10
+ include Grape::Entity::DSL
11
+ entity :name, :age do
12
+ expose :date_of_birth, :as => :brithday
13
+ expose :cat, :as => :cat, :using => PetEntity
14
+ expose :dog, :using => PetEntity
15
+ expose :secret, :if => lambda{ |person, options| person.authorized? }
16
+ expose :super_dooper_secret, :as => :top_secret, :if => lambda{ |person, options| person.authorized? }
17
+ end
18
+ end
19
+ end
20
+
21
+ context 'matchers' do
22
+ subject(:entity){ Person::Entity }
23
+
24
+ # TODO: move the tests to this format to shadow the thoughtbot tests.
25
+ # it { should represent(:name) }
26
+ it "should ensure the representation includes the specified property" do
27
+ entity.should represent :name
28
+ end
29
+
30
+
31
+
32
+ it { should represent(:date_of_birth).as(:brithday) }
33
+
34
+ it { should_not represent(:t_shirt_size) }
35
+ it { should_not represent(:t_shirt_size).as(:brithday) }
36
+ it { should_not represent(:name).as(:brithday) }
37
+ it { should_not represent(:date_of_birth) }
38
+
39
+ it { should represent(:secret).when( :authorized? => true ) }
40
+ it { should_not represent(:secret).when( :authorized? => false ) }
41
+
42
+ it { should represent(:super_dooper_secret).as(:top_secret).when( :authorized? => true ) }
43
+ it { should_not represent(:super_dooper_secret).as(:top_secret).when( :authorized? => false ) }
44
+ it { should_not represent(:super_dooper_secret).when( :authorized? => true ) }
45
+
46
+ it { should represent(:dog).using(PetEntity) }
47
+ it { should represent(:cat).as(:cat).using(PetEntity) }
48
+
49
+ end
50
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'support'))
4
+
5
+ require 'rubygems'
6
+ require 'bundler'
7
+
8
+ Bundler.require :default, :test
9
+
10
+ require 'pry'
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-entity-matchers
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Mark Madsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ type: :runtime
16
+ prerelease: false
17
+ name: grape-entity
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ! '>='
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.0
24
+ requirement: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ type: :runtime
32
+ prerelease: false
33
+ name: rspec
34
+ version_requirements: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '2.9'
40
+ requirement: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.9'
46
+ - !ruby/object:Gem::Dependency
47
+ type: :development
48
+ prerelease: false
49
+ name: rake
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirement: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ type: :development
64
+ prerelease: false
65
+ name: maruku
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirement: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ type: :development
80
+ prerelease: false
81
+ name: yard
82
+ version_requirements: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirement: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ type: :development
96
+ prerelease: false
97
+ name: bundler
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirement: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Shoulda-like matchers for Grape Entity.
111
+ email:
112
+ - mark@agileanimal.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - .travis.yml
120
+ - .yardopts
121
+ - CHANGELOG.markdown
122
+ - Gemfile
123
+ - Guardfile
124
+ - LICENSE
125
+ - README.markdown
126
+ - Rakefile
127
+ - grape-entity-matchers.gemspec
128
+ - lib/grape-entity-matchers.rb
129
+ - lib/grape_entity_matchers.rb
130
+ - lib/grape_entity_matchers/represent_matcher.rb
131
+ - lib/grape_entity_matchers/rspec_integration.rb
132
+ - lib/grape_entity_matchers/version.rb
133
+ - spec/grape_entity_matchers/represent_matcher_spec.rb
134
+ - spec/spec_helper.rb
135
+ homepage: https://github.com/agileanimal/grape-entity-matchers
136
+ licenses:
137
+ - MIT
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ segments:
148
+ - 0
149
+ hash: 3869361771623991090
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ! '>='
155
+ - !ruby/object:Gem::Version
156
+ segments:
157
+ - 0
158
+ hash: 3869361771623991090
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project: grape-entity-matchers
162
+ rubygems_version: 1.8.24
163
+ signing_key:
164
+ specification_version: 3
165
+ summary: Shoulda-like matchers for Grape Entity.
166
+ test_files:
167
+ - spec/grape_entity_matchers/represent_matcher_spec.rb
168
+ - spec/spec_helper.rb
169
+ has_rdoc: