rspec-hal 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +6 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +6 -0
- data/lib/rspec-hal.rb +1 -0
- data/lib/rspec/hal.rb +7 -0
- data/lib/rspec/hal/matchers.rb +118 -0
- data/lib/rspec/hal/matchers_spec.rb +5 -0
- data/lib/rspec/hal/version.rb +5 -0
- data/rspec-hal.gemspec +26 -0
- data/spec/rspec/hal/matchers_spec.rb +91 -0
- data/spec/spec_helper.rb +1 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 05575f5807ab23396a769f90057ba719283a0d1c
|
4
|
+
data.tar.gz: a01215a9b71a86782bb009d377b6dbf4f32a366f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b387994d1f7113fbaaa5f03579a90937823f0289f22d14ea3c4ce86b84473a6cc9dee2daf71905055a05493adad6074da7ee5472395e2ca1a6b1282927af081a
|
7
|
+
data.tar.gz: 7b58f38974355b33e8238a95684d09a1de7f8777644418b949fba69eb19156c1a8116bfab257b7f79abb15db24d2b316e55cda33b74f852364484b1c56ec1c8c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Peter Williams
|
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,63 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/pezra/hal-interpretation.png?branch=master)](https://travis-ci.org/pezra/hal-interpretation)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/pezra/hal-interpretation.png)](https://codeclimate.com/github/pezra/hal-interpretation)
|
3
|
+
|
4
|
+
# Rspec Hal
|
5
|
+
|
6
|
+
Provides matchers and convenience methods for verifying HAL documents.
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
Include the matchers by adding this to your spec_helper
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
RSpec.configuration.include RSpec::Hal::Matchers::Documents
|
14
|
+
```
|
15
|
+
|
16
|
+
(Don't forget to `require rspec-hal` if you are not using bundler.)
|
17
|
+
|
18
|
+
If you are using rspec-rails and want only include the matchers for views do this
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
RSpec.configuration.include RSpec::Hal::Matchers::Documents, type: 'view'
|
22
|
+
```
|
23
|
+
|
24
|
+
Once you have the matchers included you can use it like this
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
expect(a_user_doc).to be_hal
|
28
|
+
|
29
|
+
expect(first_page_of_users).to be_hal_collection
|
30
|
+
|
31
|
+
expect(a_user_doc).to have_property "name"
|
32
|
+
expect(a_user_doc).to have_property('name').matching(/ice$/)
|
33
|
+
expect(a_user_doc).to have_property('name').matching(end_with('ice'))
|
34
|
+
expect(a_user_doc).to have_property('hobbies')
|
35
|
+
.including(a_hash_including('type' => 'sport'))
|
36
|
+
|
37
|
+
expect(a_user_doc).to have_relation('tag')
|
38
|
+
```
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
gem 'rspec-hal'
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install rspec-hal
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it ( http://github.com/pezra/rspec-hal/fork )
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Make your changes and `lib/rspec/hal/version.rb` following [semver][] rules
|
59
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
60
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
6. Create new Pull Request
|
62
|
+
|
63
|
+
[semver]: http://semver.org
|
data/Rakefile
ADDED
data/lib/rspec-hal.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rspec/hal.rb"
|
data/lib/rspec/hal.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'hal-client'
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
module Hal
|
6
|
+
module Matchers
|
7
|
+
module Document
|
8
|
+
extend RSpec::Matchers::DSL
|
9
|
+
|
10
|
+
# Provide a 3.0 compatible DSL methods for 2.x RSpec.
|
11
|
+
module ForwardCompat
|
12
|
+
def failure_message(&blk)
|
13
|
+
failure_message_for_should(&blk)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Install 3.0 compatibility layer if needed.
|
18
|
+
class << self
|
19
|
+
def matcher(name, &blk)
|
20
|
+
super name do |*args|
|
21
|
+
extend ForwardCompat unless respond_to? :failure_message
|
22
|
+
|
23
|
+
self.instance_exec *args, &blk
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# Check that the document provided is a valid HAL document.
|
30
|
+
#
|
31
|
+
# Signature
|
32
|
+
#
|
33
|
+
# expect(a_user_doc).to be_hal
|
34
|
+
matcher :be_hal do
|
35
|
+
match do |a_json_doc|
|
36
|
+
!!(JSON.load(a_json_doc) rescue false)
|
37
|
+
end
|
38
|
+
|
39
|
+
failure_message do |a_json_doc|
|
40
|
+
message = begin
|
41
|
+
JSON.load(a_json_doc)
|
42
|
+
rescue err
|
43
|
+
err.message
|
44
|
+
end
|
45
|
+
message + " while parsing:\n" + a_json_doc
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Check that the document is both a valid HAL document and is
|
50
|
+
# a page of an RFC 6573 collection
|
51
|
+
#
|
52
|
+
# Signature
|
53
|
+
#
|
54
|
+
# expect(first_users_page).to be_hal_collection
|
55
|
+
matcher :be_hal_collection do
|
56
|
+
match do |a_doc|
|
57
|
+
a_doc = JSON.load(a_doc) rescue a_doc
|
58
|
+
|
59
|
+
a_doc.fetch("_embedded").has_key?("item") rescue false
|
60
|
+
end
|
61
|
+
|
62
|
+
failure_message do |a_doc|
|
63
|
+
"Expected `$._embedded.item` to exist in:\n" + a_doc
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Check that the document has the specified property.
|
68
|
+
#
|
69
|
+
# Signature
|
70
|
+
#
|
71
|
+
# expect(a_user_doc).to have_property "name"
|
72
|
+
# expect(a_user_doc).to have_property("name").matching(/bob/i)
|
73
|
+
# expect(a_user_doc).to have_property("hobbies").including(matching("golf"))
|
74
|
+
matcher :have_property do |prop_name|
|
75
|
+
match do |a_doc|
|
76
|
+
a_doc = JSON.load(a_doc) rescue a_doc
|
77
|
+
|
78
|
+
|
79
|
+
next false unless a_doc.key? prop_name
|
80
|
+
|
81
|
+
__value_matcher === a_doc.fetch(prop_name)
|
82
|
+
end
|
83
|
+
|
84
|
+
chain :matching do |val_pat|
|
85
|
+
@value_matcher = val_pat
|
86
|
+
end
|
87
|
+
|
88
|
+
chain :including do |val_pat|
|
89
|
+
@value_matcher = RSpec::Matchers::BuiltIn::Include.new val_pat
|
90
|
+
end
|
91
|
+
|
92
|
+
define_method(:__value_matcher) do
|
93
|
+
@value_matcher ||= ->(*_){ true }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Check that the document has the specified relation (in
|
98
|
+
# either the _links or _embedded sections.
|
99
|
+
#
|
100
|
+
# Signature
|
101
|
+
#
|
102
|
+
# expect(a_user_doc).to have_relation "tag"
|
103
|
+
matcher :have_relation do |link_rel|
|
104
|
+
match do |a_doc|
|
105
|
+
a_doc = JSON.load(a_doc) rescue a_doc
|
106
|
+
repr = HalClient::Representation.new(parsed_json: a_doc)
|
107
|
+
|
108
|
+
begin
|
109
|
+
repr.related_hrefs(link_rel).any?
|
110
|
+
rescue KeyError
|
111
|
+
false
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
data/rspec-hal.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rspec/hal/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rspec-hal"
|
8
|
+
spec.version = RSpec::Hal::VERSION
|
9
|
+
spec.authors = ["Peter Williams"]
|
10
|
+
spec.email = ["pezra@barelyenough.org"]
|
11
|
+
spec.summary = %q{Matchers and helpers for specing HAL documents.}
|
12
|
+
spec.homepage = "http://github.com/pezra/rspec-hal"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.1"
|
22
|
+
|
23
|
+
spec.add_runtime_dependency "rspec", [">= 2.0", "< 4.0"]
|
24
|
+
spec.add_runtime_dependency "hal-client", "~> 2.2
|
25
|
+
"
|
26
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require_relative "../../spec_helper"
|
2
|
+
|
3
|
+
describe RSpec::Hal::Matchers::Document do
|
4
|
+
describe "be_hal" do
|
5
|
+
subject(:matcher) { be_hal }
|
6
|
+
|
7
|
+
specify { expect(matcher.matches?(hal_doc)).to be true }
|
8
|
+
specify { expect(matcher.matches?("What's HAL?")).to be false }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "be_hal_collection" do
|
12
|
+
subject(:matcher) { be_hal_collection }
|
13
|
+
|
14
|
+
specify { expect(matcher.matches?(hal_collection)).to be true }
|
15
|
+
specify { expect(matcher.matches?("What's HAL?")).to be false }
|
16
|
+
specify { expect(matcher.matches?(hal_doc)).to be false }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "have_property('name')" do
|
20
|
+
subject(:matcher) { have_property('name') }
|
21
|
+
|
22
|
+
specify { expect(matcher.matches?(hal_doc)).to be true}
|
23
|
+
specify { expect(matcher.matches?("{}")).to be false}
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "have_property('name').matching(/ice$/)" do
|
27
|
+
subject(:matcher) { have_property('name').matching(/ice$/) }
|
28
|
+
|
29
|
+
specify { expect(matcher.matches?(hal_doc)).to be true}
|
30
|
+
specify { expect(matcher.matches?("{}")).to be false}
|
31
|
+
specify { expect(matcher.matches?(bob)).to be false}
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "have_property('name').matching(end_with('ice'))" do
|
35
|
+
subject(:matcher) { have_property('name')
|
36
|
+
.matching(RSpec::Matchers::BuiltIn::EndWith.new("ice")) }
|
37
|
+
|
38
|
+
specify { expect(matcher.matches?(hal_doc)).to be true}
|
39
|
+
specify { expect(matcher.matches?("{}")).to be false}
|
40
|
+
specify { expect(matcher.matches?(bob)).to be false}
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "have_property('hobbies').including(a_hash_including('type' => 'sport'))" do
|
44
|
+
subject(:matcher) { have_property('hobbies')
|
45
|
+
.including(RSpec::Matchers::BuiltIn::Include.new('type' => 'sport')) }
|
46
|
+
before do extend described_class end
|
47
|
+
|
48
|
+
specify { expect(matcher.matches?(hal_doc)).to be true}
|
49
|
+
specify { expect(matcher.matches?("{}")).to be false}
|
50
|
+
specify { expect(matcher.matches?(bob)).to be false}
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "have_relation('tag')" do
|
54
|
+
subject(:matcher) { have_relation('tag') }
|
55
|
+
|
56
|
+
specify { expect(matcher.matches?(hal_doc)).to be true}
|
57
|
+
specify { expect(matcher.matches?("{}")).to be false}
|
58
|
+
specify { expect(matcher.matches?(bob)).to be false}
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
before do
|
63
|
+
extend RSpec::Hal::Matchers::Document
|
64
|
+
end
|
65
|
+
|
66
|
+
let(:hal_collection) { <<-HAL }
|
67
|
+
{ "_embedded": { "item": [
|
68
|
+
#{hal_doc}
|
69
|
+
]}}
|
70
|
+
HAL
|
71
|
+
let(:hal_doc) { <<-HAL }
|
72
|
+
{ "name": "Alice"
|
73
|
+
,"hobbies": [{"type": "sport", "name": "golf"}]
|
74
|
+
,"_links": {
|
75
|
+
"tag": [{ "href": "http://tags.example.com/smart" }]
|
76
|
+
}
|
77
|
+
,"_embedded": {
|
78
|
+
"http://rel.example.com/boss_of": #{bob}
|
79
|
+
}
|
80
|
+
}
|
81
|
+
HAL
|
82
|
+
let(:bob) { <<-HAL }
|
83
|
+
{ "name": "Bob"
|
84
|
+
,"_links": {
|
85
|
+
"self": { "href": "http://example.com/users/42" }
|
86
|
+
}
|
87
|
+
,"_embedded": {
|
88
|
+
}
|
89
|
+
}
|
90
|
+
HAL
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "rspec-hal"
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-hal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Williams
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
- - "<"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '4.0'
|
51
|
+
type: :runtime
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '2.0'
|
58
|
+
- - "<"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '4.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: hal-client
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '2.2'
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '2.2'
|
75
|
+
description:
|
76
|
+
email:
|
77
|
+
- pezra@barelyenough.org
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- ".gitignore"
|
83
|
+
- ".travis.yml"
|
84
|
+
- Gemfile
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.md
|
87
|
+
- Rakefile
|
88
|
+
- lib/rspec-hal.rb
|
89
|
+
- lib/rspec/hal.rb
|
90
|
+
- lib/rspec/hal/matchers.rb
|
91
|
+
- lib/rspec/hal/matchers_spec.rb
|
92
|
+
- lib/rspec/hal/version.rb
|
93
|
+
- rspec-hal.gemspec
|
94
|
+
- spec/rspec/hal/matchers_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: http://github.com/pezra/rspec-hal
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.2.2
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Matchers and helpers for specing HAL documents.
|
120
|
+
test_files:
|
121
|
+
- spec/rspec/hal/matchers_spec.rb
|
122
|
+
- spec/spec_helper.rb
|