active_model_serializer-matchers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +52 -0
- data/Rakefile +13 -0
- data/active_model_serializers-matchers.gemspec +22 -0
- data/lib/active_model_serializers-matchers.rb +24 -0
- data/lib/active_model_serializers/matchers.rb +185 -0
- data/lib/active_model_serializers/matchers/version.rb +7 -0
- data/spec/active_model_serializers_matcher_spec.rb +82 -0
- data/spec/serializers/serializer_spec.rb +7 -0
- data/spec/spec_helper.rb +6 -0
- metadata +99 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 adman65
|
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,52 @@
|
|
1
|
+
# ActiveModelSerializer::Matchers
|
2
|
+
|
3
|
+
RSpec Matchers for testing ActiveModel::Serializers
|
4
|
+
|
5
|
+
This gem currently works with the **0.1.0** release. It will be updated
|
6
|
+
for the next public release when that happens.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'active_model_serializer-matchers'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install active_model_serializer-matchers
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Matchers are automatically included in all specs in `spec/serializers`.
|
25
|
+
|
26
|
+
Here are some examples
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
describe BlogPostSerializer do
|
30
|
+
it { should have_attribute(:title) }
|
31
|
+
it { should have_attribute(:text) }
|
32
|
+
|
33
|
+
it { should have_one(:author) }
|
34
|
+
|
35
|
+
it { should have_many(:comments) }
|
36
|
+
it { should have_many(:comments).as(:responses) }
|
37
|
+
|
38
|
+
it { should embed(:objects) }
|
39
|
+
|
40
|
+
it { should include_root }
|
41
|
+
|
42
|
+
it { should include_root(:blog_post) } # if you want to test the root is specifically set to a different value
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require 'rspec/core'
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
9
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
10
|
+
spec.rspec_opts = ['-c']
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :spec
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/active_model_serializers/matchers/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["adman65"]
|
6
|
+
gem.email = ["me@broadcastingadam.com"]
|
7
|
+
gem.description = %q{RSpec matchers for ActiveModel::Serializers}
|
8
|
+
gem.summary = %q{}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "active_model_serializer-matchers"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ActiveModel::Serializers::Matchers::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "active_model_serializers", "~> 0.1.0"
|
19
|
+
gem.add_dependency "rspec", "~> 2.0"
|
20
|
+
|
21
|
+
gem.add_development_dependency "simplecov"
|
22
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'active_model_serializers'
|
2
|
+
|
3
|
+
module ActiveModel::Serializers::Matchers
|
4
|
+
end
|
5
|
+
|
6
|
+
require "active_model_serializers/matchers/version"
|
7
|
+
require "active_model_serializers/matchers"
|
8
|
+
|
9
|
+
module SerializerExampleGroup
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
include ActiveModel::Serializers::Matchers
|
12
|
+
|
13
|
+
included do
|
14
|
+
subject { described_class }
|
15
|
+
|
16
|
+
metadata[:type] = :serializer
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.include SerializerExampleGroup, :example_group => {
|
22
|
+
:file_path => /spec\/serializers/
|
23
|
+
}
|
24
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Serializers
|
3
|
+
module Matchers
|
4
|
+
class Root
|
5
|
+
attr_accessor :name, :actual
|
6
|
+
|
7
|
+
def initialize(name = true)
|
8
|
+
@name = name
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(actual)
|
12
|
+
@actual = actual
|
13
|
+
|
14
|
+
serializer._root == name
|
15
|
+
end
|
16
|
+
|
17
|
+
def description
|
18
|
+
"have attribute #{name}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def failure_message_for_should
|
22
|
+
%Q{expected "#{actual._root}" to be "#{name}", but it wasn't}
|
23
|
+
end
|
24
|
+
|
25
|
+
def failure_message_for_should_not
|
26
|
+
%Q{expected "#{actual._root}" to be "#{name}", but it was}
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def serializer
|
31
|
+
if actual.is_a?(Class)
|
32
|
+
actual
|
33
|
+
else
|
34
|
+
actual.class
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def include_root(key = nil)
|
40
|
+
Root.new key
|
41
|
+
end
|
42
|
+
|
43
|
+
class Embed
|
44
|
+
attr_accessor :expected, :actual
|
45
|
+
|
46
|
+
def initialize(expected)
|
47
|
+
@expected = expected
|
48
|
+
end
|
49
|
+
|
50
|
+
def matches?(actual)
|
51
|
+
@actual = actual
|
52
|
+
|
53
|
+
serializer._embed == expected
|
54
|
+
end
|
55
|
+
|
56
|
+
def description
|
57
|
+
"embed #{name}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def failure_message_for_should
|
61
|
+
%Q{expected "#{actual._embed}" to be "#{name}", but it wasn't}
|
62
|
+
end
|
63
|
+
|
64
|
+
def failure_message_for_should_not
|
65
|
+
%Q{expected "#{actual._embed}" to be "#{name}", but it was}
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def serializer
|
70
|
+
if actual.is_a?(Class)
|
71
|
+
actual
|
72
|
+
else
|
73
|
+
actual.class
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def embed(value)
|
79
|
+
Embed.new value
|
80
|
+
end
|
81
|
+
|
82
|
+
class HaveAttribute
|
83
|
+
attr_accessor :name, :actual
|
84
|
+
|
85
|
+
def initialize(name)
|
86
|
+
@name = name
|
87
|
+
end
|
88
|
+
|
89
|
+
def matches?(actual)
|
90
|
+
@actual = actual
|
91
|
+
|
92
|
+
attributes.has_key?(name)
|
93
|
+
end
|
94
|
+
|
95
|
+
def description
|
96
|
+
"have attribute #{name}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def failure_message_for_should
|
100
|
+
%Q{expected #{actual.inspect} to include "#{name}", but it did not}
|
101
|
+
end
|
102
|
+
|
103
|
+
def failure_message_for_should_not
|
104
|
+
%Q{expected #{actual.inspect} to not include: "#{name}", but it did}
|
105
|
+
end
|
106
|
+
|
107
|
+
private
|
108
|
+
def serializer
|
109
|
+
if actual.is_a?(Class)
|
110
|
+
actual
|
111
|
+
else
|
112
|
+
actual.class
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def attributes
|
117
|
+
serializer._attributes
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def have_attribute(name)
|
122
|
+
HaveAttribute.new name
|
123
|
+
end
|
124
|
+
|
125
|
+
class AssociationMatcher
|
126
|
+
attr_accessor :name, :actual, :key
|
127
|
+
|
128
|
+
def initialize(name)
|
129
|
+
@name = name
|
130
|
+
end
|
131
|
+
|
132
|
+
def matches?(actual)
|
133
|
+
@actual = actual
|
134
|
+
|
135
|
+
matched_association = associations.select do |assc|
|
136
|
+
assc.name == name
|
137
|
+
end.first
|
138
|
+
|
139
|
+
return false unless matched_association
|
140
|
+
|
141
|
+
if key
|
142
|
+
return false if matched_association.key != key
|
143
|
+
end
|
144
|
+
|
145
|
+
true
|
146
|
+
end
|
147
|
+
|
148
|
+
def as(value)
|
149
|
+
self.key = value
|
150
|
+
self
|
151
|
+
end
|
152
|
+
|
153
|
+
def description
|
154
|
+
"have attribute #{name}"
|
155
|
+
end
|
156
|
+
|
157
|
+
def failure_message_for_should
|
158
|
+
%Q{expected #{actual.inspect} to include a "#{name}" association, but it did not}
|
159
|
+
end
|
160
|
+
|
161
|
+
def failure_message_for_should_not
|
162
|
+
%Q{expected #{actual.inspect} to not include a "#{name}" association, but it did}
|
163
|
+
end
|
164
|
+
|
165
|
+
private
|
166
|
+
def serializer
|
167
|
+
if actual.is_a?(Class)
|
168
|
+
actual
|
169
|
+
else
|
170
|
+
actual.class
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def associations
|
175
|
+
serializer._associations
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def have_many(name)
|
180
|
+
AssociationMatcher.new(name)
|
181
|
+
end
|
182
|
+
alias :have_one :have_many
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveModel::Serializers::Matchers do
|
4
|
+
include ActiveModel::Serializers::Matchers
|
5
|
+
|
6
|
+
it "should be false if the serializer does not have an attribute" do
|
7
|
+
serializer = Class.new ActiveModel::Serializer do
|
8
|
+
attributes :foo
|
9
|
+
end
|
10
|
+
|
11
|
+
serializer.should_not have_attribute :bar
|
12
|
+
serializer.should have_attribute :foo
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should match the embed setting" do
|
16
|
+
serializer = Class.new ActiveModel::Serializer do
|
17
|
+
embed :ids
|
18
|
+
end
|
19
|
+
|
20
|
+
serializer.should embed(:ids)
|
21
|
+
serializer.should_not embed(:objects)
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "The root key" do
|
25
|
+
it "should match the root setting" do
|
26
|
+
serializer = Class.new ActiveModel::Serializer do
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
serializer.should include_root
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should be able to match a specific key" do
|
34
|
+
serializer = Class.new ActiveModel::Serializer do
|
35
|
+
root :foo
|
36
|
+
end
|
37
|
+
|
38
|
+
serializer.should include_root(:foo)
|
39
|
+
serializer.should_not include_root(:bar)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "Associations" do
|
44
|
+
it "should work with has_many" do
|
45
|
+
serializer = Class.new ActiveModel::Serializer do
|
46
|
+
has_many :foos
|
47
|
+
end
|
48
|
+
|
49
|
+
serializer.should have_many(:foos)
|
50
|
+
serializer.should_not have_many(:bars)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should work with has_one" do
|
54
|
+
serializer = Class.new ActiveModel::Serializer do
|
55
|
+
has_one :foo
|
56
|
+
end
|
57
|
+
|
58
|
+
serializer.should have_one(:foo)
|
59
|
+
serializer.should_not have_one(:bar)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should work with has_one key options" do
|
63
|
+
serializer = Class.new ActiveModel::Serializer do
|
64
|
+
has_one :foo, :key => :bar
|
65
|
+
end
|
66
|
+
|
67
|
+
serializer.should have_one(:foo).as(:bar)
|
68
|
+
serializer.should have_one(:foo)
|
69
|
+
serializer.should_not have_one(:foo).as(:qux)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should work with has_many key options" do
|
73
|
+
serializer = Class.new ActiveModel::Serializer do
|
74
|
+
has_one :foos, :key => :bars
|
75
|
+
end
|
76
|
+
|
77
|
+
serializer.should have_one(:foos).as(:bars)
|
78
|
+
serializer.should have_one(:foos)
|
79
|
+
serializer.should_not have_one(:foos).as(:qux)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model_serializer-matchers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- adman65
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: active_model_serializers
|
16
|
+
requirement: &70209336318400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70209336318400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70209336317780 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70209336317780
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: simplecov
|
38
|
+
requirement: &70209336316600 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70209336316600
|
47
|
+
description: RSpec matchers for ActiveModel::Serializers
|
48
|
+
email:
|
49
|
+
- me@broadcastingadam.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- active_model_serializers-matchers.gemspec
|
60
|
+
- lib/active_model_serializers-matchers.rb
|
61
|
+
- lib/active_model_serializers/matchers.rb
|
62
|
+
- lib/active_model_serializers/matchers/version.rb
|
63
|
+
- spec/active_model_serializers_matcher_spec.rb
|
64
|
+
- spec/serializers/serializer_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: 849873857067692096
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
hash: 849873857067692096
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.11
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: ''
|
96
|
+
test_files:
|
97
|
+
- spec/active_model_serializers_matcher_spec.rb
|
98
|
+
- spec/serializers/serializer_spec.rb
|
99
|
+
- spec/spec_helper.rb
|