rspec-hal 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85d20df14665e358ca885f51e704037f76921549
4
- data.tar.gz: 34be55d63db57e860c59f58414a7cc1327a188e2
3
+ metadata.gz: 1c835ecfb6c798d5dfb5c41101dd5c61cde75c1e
4
+ data.tar.gz: ab13e80124286588ddc088a3ff1cb773544a84d8
5
5
  SHA512:
6
- metadata.gz: 3160252d521538cbcb3cce0119edf2265104a33ca03eb1fb0d55ab8d4fb4f3961ed64102400e5c657a9224b36176272b88d73267d163ffa490c18bfefcb30b77
7
- data.tar.gz: 3d28dade0333bf9732bd6ff535ae25b542a2b922eb7f2dc9642d9fa283de1db342ac92c5503a6baefa0c23d7d2c08866014a6ce23e520c71fc0f787f68cbb0b7
6
+ metadata.gz: 2e3fd356bb643b6dd6ac9edcc16dfea03bee88fe2cca2039b6a75709ebd340be389b035e6ebd623fe56ea12c6498bd3de52a53831ee7deb97544ff5a4ea5ab4f
7
+ data.tar.gz: a08fd297f02eb699afaa48b3298aedb14603d3c7fe1e45441de4c633e45d24b70bb6f78550eb1f9ce15608a5923f17ef5ea81b7058600c318a0844206104ac35
data/README.md CHANGED
@@ -7,37 +7,55 @@ Provides matchers and convenience methods for verifying HAL documents.
7
7
 
8
8
  ## Usage
9
9
 
10
- Include the matchers by adding this to your spec_helper
10
+ Given the following string stored in `a_user_doc`.
11
+
12
+ ```json
13
+ {
14
+ "name": "Alice",
15
+ "hobbies": [{"name": "Basketball", "type": "sport"},
16
+ {"name": "Basket weaving", "type": "craft"}],
17
+ "_links": {
18
+ "self": { "href": "http://example.com/alice" },
19
+ "knows": [{ "href": "http://example.com/bob" },
20
+ { "href": "http://example.com/jane" }],
21
+ "checkBusy": { "href": "http://example.com/is_alice_busy{?at}",
22
+ "templated": true }
23
+ }
24
+ }
25
+ ```
26
+
27
+ Rspec Hal allows very expressive validation of documents.
11
28
 
12
29
  ```ruby
13
- RSpec.configuration.include RSpec::Hal::Matchers
14
- ```
30
+ expect(a_user_doc).to be_hal
15
31
 
16
- (Don't forget to `require "rspec-hal"` if you are not using bundler.)
32
+ expect(a_user_doc).not_to be_hal_collection
33
+
34
+ expect(a_user_doc).to have_property "name"
35
+ expect(a_user_doc).to have_property 'name', eq("Alice")
36
+ expect(a_user_doc).to have_property :name, matching(/ice$/)
37
+ expect(a_user_doc).to have_property 'hobbies', including(a_hash_including('type' => 'sport'))
17
38
 
18
- If you are using rspec-rails and want only include the matchers for views do this
39
+ expect(a_user_doc).to have_relation "knows"
40
+ expect(a_user_doc).to have_relation "knows", eq("http://example.com/jane")
19
41
 
20
- ```ruby
21
- RSpec.configuration.include RSpec::Hal::Matchers, type: 'view'
42
+ expect(a_user_doc).to have_templated_relation "checkBusy"
43
+ expect(a_user_doc).to have_templated_relation "checkBusy", with_variable("at")
44
+ expect(a_user_doc).to have_templated_relation("checkBusy").with_variables("at")
45
+
46
+ expect(parse_hal(users_collection_doc).first).to have_property "name"
22
47
  ```
23
48
 
24
- Once you have the matchers included you can use it like this
49
+ Any matcher (actually anything that responds to `#===`) can
50
+ be passed as the second argument and will be used to verify the
51
+ property value or the link href.
25
52
 
26
- ```ruby
27
- expect(a_user_doc).to be_hal
53
+ `be_hal_collection` checks that the document is both a valid HAL
54
+ document and is a page of an RFC 6573 collection
28
55
 
29
- expect(first_page_of_users).to be_hal_collection
56
+ `be_hal` checks that the document is both a valid HAL
57
+ document.
30
58
 
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
- expect(a_user_doc).to have_templated_relation("search")
39
- expect(a_user_doc).to have_templated_relation("search").with_variables("q", "limit")
40
- ```
41
59
 
42
60
  ## Installation
43
61
 
@@ -53,6 +71,24 @@ Or install it yourself as:
53
71
 
54
72
  $ gem install rspec-hal
55
73
 
74
+ Then include the matchers by adding this to your spec_helper
75
+
76
+ ```ruby
77
+ RSpec.configuration.include RSpec::Hal::Matchers
78
+ RSpec.configuration.include RSpec::Hal::Helpers
79
+ ```
80
+
81
+ (Don't forget to `require "rspec-hal"` if you are not using bundler.)
82
+
83
+ If you want to only include the matchers for certain type of specs
84
+ (say, view specs for example)
85
+
86
+ ```ruby
87
+ RSpec.configuration.include RSpec::Hal::Matchers, type: 'view'
88
+ RSpec.configuration.include RSpec::Hal::Helpers, type: 'view'
89
+ ```
90
+
91
+
56
92
  ## Contributing
57
93
 
58
94
  1. Fork it ( http://github.com/pezra/rspec-hal/fork )
data/lib/rspec/hal.rb CHANGED
@@ -3,5 +3,6 @@ require "rspec/hal/version"
3
3
  module RSpec
4
4
  module Hal
5
5
  autoload :Matchers, "rspec/hal/matchers"
6
+ autoload :Helpers, "rspec/hal/helpers"
6
7
  end
7
8
  end
@@ -0,0 +1,32 @@
1
+ module RSpec
2
+ module Hal
3
+ module Helpers
4
+ # Returns HalClient::Representation for `jsonish` (or
5
+ # HalClient::Collection if `jsonish` is a RFC 6573 collection)
6
+ #
7
+ # jsonish - A HAL document (as a string or pre-parsed hash) or
8
+ # an object that can be converted into one via its `#to_hal`
9
+ # or `#to_json` methods.
10
+ def parse_hal(jsonish)
11
+ json = if jsonish.kind_of? String
12
+ jsonish
13
+
14
+ elsif jsonish.respond_to? :to_hal
15
+ jsonish.to_hal
16
+
17
+ else jsonish.respond_to? :to_json
18
+ jsonish.to_json
19
+ end
20
+
21
+ repr = HalClient::Representation.new(parsed_json: MultiJson.load(json))
22
+
23
+ if repr.has_related? "item"
24
+ HalClient::Collection.new(repr)
25
+ else
26
+ repr
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -51,18 +51,15 @@ module RSpec
51
51
  # Signature
52
52
  #
53
53
  # expect(a_uri_template_str).to have_variables "q", "limit"
54
- def have_variables(*args)
55
- UriTemplateHasVariablesMatcher.new(*args)
56
- end
57
-
58
- # Signature
59
- #
60
54
  # expect(a_uri_template_str).to has_variable "q"
61
- def has_variable(*args)
55
+ def have_variables(*args)
62
56
  UriTemplateHasVariablesMatcher.new(*args)
63
57
  end
58
+ alias_method :has_variable, :have_variables
59
+ alias_method :with_variables, :have_variables
60
+ alias_method :with_variable, :have_variables
64
61
 
65
- module Document
62
+ module DocumentMatchers
66
63
  extend RSpec::Matchers::DSL
67
64
 
68
65
  # Provide a 3.0 compatible DSL methods for 2.x RSpec.
@@ -97,10 +94,12 @@ module RSpec
97
94
  failure_message do |a_json_doc|
98
95
  message = begin
99
96
  JSON.load(a_json_doc)
100
- rescue err
97
+ rescue => err
101
98
  err.message
102
99
  end
103
- message + " while parsing:\n" + a_json_doc
100
+
101
+ "Expected a HAL document but it wasn't because #{message} in:\n" +
102
+ a_json_doc
104
103
  end
105
104
  end
106
105
 
@@ -122,7 +121,8 @@ module RSpec
122
121
  end
123
122
  end
124
123
  end
125
- include Document
124
+
125
+ include DocumentMatchers
126
126
  end
127
127
  end
128
128
  end
@@ -2,6 +2,9 @@ module RSpec
2
2
  module Hal
3
3
  module Matchers
4
4
  module HalMatcherHelpers
5
+
6
+ # A matcher that always matches. Useful for avoiding special
7
+ # cases in value testing logic.
5
8
  NullMatcher = Class.new do
6
9
  def matches?(*args)
7
10
  true
@@ -58,16 +61,20 @@ module RSpec
58
61
  HalClient::Representation.new(parsed_json: MultiJson.load(json))
59
62
  end
60
63
 
64
+ # Returns `expected` coerced into an RSpec matcher
61
65
  def matcherize(expected)
62
66
  if matcher? expected
63
67
  expected
64
- elsif expected.kind_of? Regexp
68
+
69
+ elsif expected.respond_to? :===
65
70
  RSpec::Matchers::BuiltIn::Match.new(expected)
71
+
66
72
  else
67
73
  RSpec::Matchers::BuiltIn::Eq.new(expected)
68
74
  end
69
75
  end
70
76
 
77
+ # Returns true if object is an RSpec matcher
71
78
  def matcher?(obj)
72
79
  obj.respond_to?(:matches?) and (obj.respond_to?(:failure_message) or
73
80
  obj.respond_to?(:failure_message_for_should))
@@ -11,7 +11,7 @@ module RSpec
11
11
  include HalMatcherHelpers
12
12
 
13
13
  def initialize(property_name, expected=NullMatcher)
14
- @prop_name = property_name
14
+ @prop_name = property_name.to_s
15
15
  @expected = matcherize expected
16
16
  end
17
17
 
@@ -17,7 +17,7 @@ module RSpec
17
17
  # expect(doc).to have_relation link_rel, template_variables
18
18
  # expect(doc).to have_relation link_rel, template_variables, href_matcher
19
19
  def initialize(link_rel, *args)
20
- @link_rel = link_rel
20
+ @link_rel = link_rel.to_s
21
21
 
22
22
  @tmpl_vars, @expected = *if args.empty?
23
23
  [{},NullMatcher]
@@ -14,7 +14,7 @@ module RSpec
14
14
  include HalMatcherHelpers
15
15
 
16
16
  def initialize(link_rel, expected=NullMatcher)
17
- @link_rel = link_rel
17
+ @link_rel = link_rel.to_s
18
18
  @expected = expected
19
19
  end
20
20
 
@@ -6,7 +6,7 @@ module RSpec
6
6
  module Matchers
7
7
  class UriTemplateHasVariablesMatcher
8
8
  def initialize(vars)
9
- @expected_vars = Set.new(vars)
9
+ @expected_vars = Set.new(vars.map(&:to_s))
10
10
  end
11
11
 
12
12
  def matches?(actual)
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Hal
3
- VERSION = "1.3.2"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
@@ -0,0 +1,25 @@
1
+ require_relative "../../spec_helper"
2
+ require "rspec/version"
3
+
4
+ describe RSpec::Hal::Helpers do
5
+ subject { Class.new do
6
+ include RSpec::Hal::Helpers
7
+ end.new }
8
+
9
+ specify { expect(subject.parse_hal(hal_doc)).to be_kind_of HalClient::Representation }
10
+ specify { expect(subject.parse_hal(hal_collection)).to be_kind_of HalClient::Collection }
11
+
12
+ # Background/Support
13
+ # ---
14
+
15
+ let(:hal_collection) { <<-HAL }
16
+ { "_embedded": { "item": [
17
+ #{hal_doc}
18
+ ]}}
19
+ HAL
20
+
21
+ let(:hal_doc) { <<-HAL }
22
+ { "name": "Alice"
23
+ }
24
+ HAL
25
+ end
@@ -88,6 +88,13 @@ describe RSpec::Hal::Matchers::HavePropertyMatcher do
88
88
  specify { expect(matcher.failure_message).to match(/eq "hector"/) }
89
89
  end
90
90
 
91
+ context "name as symbol" do
92
+ subject(:matcher) { described_class.new(a_prop_name.to_sym) }
93
+
94
+ specify { expect(matcher.matches?(json_str_w_property)).to be_truthy}
95
+ specify { expect(matcher.matches?(json_str_wo_property)).to be_falsey}
96
+ end
97
+
91
98
 
92
99
  # Background
93
100
  # ---
@@ -13,6 +13,13 @@ describe RSpec::Hal::Matchers::RelationMatcher do
13
13
  specify{ expect(matcher.matches?(json_str_w_link)).to be_truthy }
14
14
  specify{ expect(matcher.matches?(json_str_wo_link)).to be_falsey }
15
15
 
16
+ context "name as symbol" do
17
+ subject(:matcher) { described_class.new(a_link_rel.to_sym) }
18
+
19
+ specify{ expect(matcher.matches?(json_str_w_link)).to be_truthy }
20
+ specify{ expect(matcher.matches?(json_str_wo_link)).to be_falsey }
21
+ end
22
+
16
23
  context "curries" do
17
24
  let(:a_link_rel) { "http://example.com/rels/foo" }
18
25
  specify{ expect(matcher.matches?(json_str_w_curied_link)).to be_truthy }
@@ -78,7 +85,6 @@ describe RSpec::Hal::Matchers::RelationMatcher do
78
85
  specify{ expect(matcher.matches? json_str_w_link).to be_falsy }
79
86
  end
80
87
 
81
-
82
88
  context "failed due to missing relation matcher" do
83
89
  before do
84
90
  matcher.matches? json_str_wo_link
@@ -26,6 +26,13 @@ describe RSpec::Hal::Matchers::TemplatedRelationMatcher do
26
26
  specify { expect(matcher.with_variables("since", "before")).to be_a_matcher }
27
27
  specify { expect(matcher.with_variable("since")).to be_a_matcher }
28
28
 
29
+ context "symbol rel name" do
30
+ subject(:matcher) { described_class.new(a_link_rel.to_sym) }
31
+
32
+ specify{ expect(matcher.matches?(json_str_w_link)).to be }
33
+ specify{ expect(matcher.matches?(json_str_wo_link)).not_to be }
34
+ end
35
+
29
36
  context "failed due to missing relation matcher" do
30
37
  before do
31
38
  matcher.matches? json_str_wo_link
@@ -13,6 +13,13 @@ describe RSpec::Hal::Matchers::UriTemplateHasVariablesMatcher do
13
13
  specify { expect(matcher.matches?("http://example.com{?foo}")).to be_falsy }
14
14
  specify { expect(matcher.description).to match /foo, bar/ }
15
15
 
16
+ context "names as symbol" do
17
+ subject(:matcher) { described_class.new([:foo, :bar]) }
18
+
19
+ specify { expect(matcher.matches?("http://example.com{?foo,bar}")).to be_truthy }
20
+ specify { expect(matcher.matches?("http://example.com{?foo}")).to be_falsy }
21
+ end
22
+
16
23
  describe "failed matcher" do
17
24
  before do matcher.matches?("http://example.com{?foo}") end
18
25
 
@@ -22,12 +22,18 @@ describe RSpec::Hal::Matchers do
22
22
  end
23
23
  end
24
24
 
25
- describe RSpec::Hal::Matchers::Document do
25
+ describe RSpec::Hal::Matchers::DocumentMatchers do
26
26
  describe "be_hal" do
27
27
  subject(:matcher) { be_hal }
28
28
 
29
29
  specify { expect(matcher.matches?(hal_doc)).to be_truthy }
30
30
  specify { expect(matcher.matches?("What's HAL?")).to be_falsey }
31
+
32
+ context "failed matcher" do
33
+ before do matcher.matches?("What's HAL?") end
34
+
35
+ specify { expect(matcher.failure_message).to match "unexpected token" }
36
+ end
31
37
  end
32
38
 
33
39
  describe "be_hal_collection" do
@@ -39,7 +45,7 @@ describe RSpec::Hal::Matchers::Document do
39
45
  end
40
46
 
41
47
  before do
42
- extend RSpec::Hal::Matchers::Document
48
+ extend described_class
43
49
  end
44
50
 
45
51
  let(:hal_collection) { <<-HAL }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-hal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-14 00:00:00.000000000 Z
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -109,6 +109,7 @@ files:
109
109
  - gemfiles/rspec3_0.gemfile
110
110
  - lib/rspec-hal.rb
111
111
  - lib/rspec/hal.rb
112
+ - lib/rspec/hal/helpers.rb
112
113
  - lib/rspec/hal/matchers.rb
113
114
  - lib/rspec/hal/matchers/hal_matcher_helpers.rb
114
115
  - lib/rspec/hal/matchers/have_property_matcher.rb
@@ -117,6 +118,7 @@ files:
117
118
  - lib/rspec/hal/matchers/uri_template_has_variables_matcher.rb
118
119
  - lib/rspec/hal/version.rb
119
120
  - rspec-hal.gemspec
121
+ - spec/rspec/hal/helpers_spec.rb
120
122
  - spec/rspec/hal/matchers/have_property_matcher_spec.rb
121
123
  - spec/rspec/hal/matchers/relation_matcher_spec.rb
122
124
  - spec/rspec/hal/matchers/templated_relation_matcher_spec.rb
@@ -148,9 +150,11 @@ signing_key:
148
150
  specification_version: 4
149
151
  summary: Matchers and helpers for specing HAL documents.
150
152
  test_files:
153
+ - spec/rspec/hal/helpers_spec.rb
151
154
  - spec/rspec/hal/matchers/have_property_matcher_spec.rb
152
155
  - spec/rspec/hal/matchers/relation_matcher_spec.rb
153
156
  - spec/rspec/hal/matchers/templated_relation_matcher_spec.rb
154
157
  - spec/rspec/hal/matchers/uri_template_has_variables_matcher_spec.rb
155
158
  - spec/rspec/hal/matchers_spec.rb
156
159
  - spec/spec_helper.rb
160
+ has_rdoc: