roar 0.11.4 → 0.11.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/CHANGES.markdown +5 -1
  2. data/Gemfile +1 -1
  3. data/README.textile +22 -21
  4. data/lib/roar/representer.rb +1 -1
  5. data/lib/roar/representer/feature/client.rb +3 -3
  6. data/lib/roar/representer/feature/coercion.rb +1 -1
  7. data/lib/roar/representer/feature/http_verbs.rb +8 -9
  8. data/lib/roar/representer/feature/hypermedia.rb +83 -63
  9. data/lib/roar/representer/json.rb +12 -12
  10. data/lib/roar/representer/json/hal.rb +98 -25
  11. data/lib/roar/representer/transport/faraday.rb +5 -11
  12. data/lib/roar/representer/transport/net_http.rb +5 -5
  13. data/lib/roar/representer/xml.rb +11 -11
  14. data/lib/roar/version.rb +1 -1
  15. data/roar.gemspec +2 -2
  16. data/test/client_test.rb +2 -2
  17. data/test/coercion_feature_test.rb +2 -2
  18. data/test/dummy/app/controllers/albums_controller.rb +6 -6
  19. data/test/dummy/app/models/album.rb +1 -1
  20. data/test/dummy/app/representers/representer/xml/album.rb +3 -3
  21. data/test/dummy/app/representers/representer/xml/song.rb +1 -1
  22. data/test/dummy/config/boot.rb +1 -1
  23. data/test/fake_server.rb +14 -15
  24. data/test/faraday_http_transport_test.rb +25 -10
  25. data/test/hal_json_test.rb +73 -31
  26. data/test/http_verbs_feature_test.rb +12 -12
  27. data/test/hypermedia_feature_test.rb +38 -145
  28. data/test/hypermedia_test.rb +100 -0
  29. data/test/integration_test.rb +22 -22
  30. data/test/json_representer_test.rb +31 -31
  31. data/test/net_http_transport_test.rb +24 -10
  32. data/test/order_representers.rb +11 -11
  33. data/test/rails/controller_methods_test.rb +25 -25
  34. data/test/rails/rails_representer_methods_test.rb +3 -3
  35. data/test/representer_test.rb +6 -6
  36. data/test/test_helper.rb +33 -4
  37. data/test/xml_representer_test.rb +47 -47
  38. metadata +3 -3
  39. data/lib/roar/rails.rb +0 -21
@@ -2,15 +2,15 @@ module Representer
2
2
  module XML
3
3
  class Album < Roar::Representer::XML
4
4
  self.representation_name= :album
5
-
5
+
6
6
  property :id
7
7
  property :year
8
8
  collection :songs
9
-
9
+
10
10
  link :self do
11
11
  album_url(represented.id)
12
12
  end
13
-
13
+
14
14
  link "album-search" do
15
15
  album_search_url
16
16
  end
@@ -2,7 +2,7 @@ module Representer
2
2
  module XML
3
3
  class Song < Roar::Representer::XML
4
4
  self.representation_name= :song
5
-
5
+
6
6
  property :title
7
7
  end
8
8
  end
@@ -7,4 +7,4 @@ if File.exist?(gemfile)
7
7
  Bundler.setup
8
8
  end
9
9
 
10
- $:.unshift File.expand_path('../../../../lib', __FILE__)
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -7,46 +7,45 @@ class FakeServer < Sinatra::Base
7
7
 
8
8
  module BandRepresenter
9
9
  include Roar::Representer::JSON
10
-
10
+
11
11
  property :name
12
12
  property :label
13
13
  end
14
-
14
+
15
15
  class Band
16
16
  attr_reader :name, :label
17
-
17
+
18
18
  def name=(value)
19
19
  @name = value.upcase
20
20
  end
21
-
21
+
22
22
  def label=(value)
23
23
  @label = value.upcase
24
24
  end
25
25
  end
26
-
26
+
27
27
  def consume_band
28
28
  Band.new.extend(BandRepresenter).from_json(request.body.string)
29
29
  end
30
-
31
-
30
+
32
31
  get "/method" do
33
32
  "<method>get</method>"
34
33
  end
35
34
 
36
35
  post "/method" do
37
- "<method>post</method>"
36
+ "<method>post - #{request.body.read}</method>"
38
37
  end
39
-
38
+
40
39
  put "/method" do
41
- "<method>put</method>"
40
+ "<method>put - #{request.body.read}</method>"
42
41
  end
43
-
42
+
44
43
  delete "/method" do
45
44
  "<method>delete</method>"
46
45
  end
47
-
46
+
48
47
  patch "/method" do
49
- "<method>patch</method>"
48
+ "<method>patch - #{request.body.read}</method>"
50
49
  end
51
50
 
52
51
  get '/deliberate-error' do
@@ -56,10 +55,10 @@ class FakeServer < Sinatra::Base
56
55
  post "/bands" do
57
56
  #if request.content_type =~ /xml/
58
57
  body consume_band.to_json
59
-
58
+
60
59
  status 201
61
60
  end
62
-
61
+
63
62
  put "/bands/strungout" do
64
63
  # DISCUSS: as long as we don't agree on what to return in PUT/PATCH, let's return an updated document.
65
64
  body consume_band.to_json
@@ -3,28 +3,31 @@ require 'roar/representer/transport/faraday'
3
3
 
4
4
  class FaradayHttpTransportTest < MiniTest::Spec
5
5
  describe 'FaradayHttpTransport' do
6
+ let(:url) { "http://roar.example.com/method" }
7
+ let(:body) { "booty" }
8
+ let(:as) { "application/xml" }
6
9
  before do
7
10
  @transport = Roar::Representer::Transport::Faraday.new
8
11
  end
9
12
 
10
13
  it "#get_uri returns response" do
11
- assert_equal "<method>get</method>", @transport.get_uri("http://roar.example.com/method", "application/xml").body
14
+ @transport.get_uri(url, as).must_match_faraday_response :get, url, as
12
15
  end
13
16
 
14
17
  it "#post_uri returns response" do
15
- assert_equal "<method>post</method>", @transport.post_uri("http://roar.example.com/method", "booty", "application/xml").body
18
+ @transport.post_uri(url, body, as).must_match_faraday_response :post, url, as, body
16
19
  end
17
20
 
18
21
  it "#put_uri returns response" do
19
- assert_equal "<method>put</method>", @transport.put_uri("http://roar.example.com/method", "booty", "application/xml").body
22
+ @transport.put_uri(url, body, as).must_match_faraday_response :put, url, as, body
20
23
  end
21
24
 
22
25
  it "#delete_uri returns response" do
23
- assert_equal "<method>delete</method>", @transport.delete_uri("http://roar.example.com/method", "application/xml").body
26
+ @transport.delete_uri(url, as).must_match_faraday_response :delete, url, as
24
27
  end
25
28
 
26
29
  it "#patch_uri returns response" do
27
- assert_equal "<method>patch</method>", @transport.patch_uri("http://roar.example.com/method", "booty", "application/xml").body
30
+ @transport.patch_uri(url, body, as).must_match_faraday_response :patch, url, as, body
28
31
  end
29
32
 
30
33
  describe 'non-existent resource' do
@@ -34,25 +37,25 @@ class FaradayHttpTransportTest < MiniTest::Spec
34
37
 
35
38
  it '#get_uri raises a ResourceNotFound error' do
36
39
  assert_raises(Faraday::Error::ResourceNotFound) do
37
- @transport.get_uri(@not_found_url, "application/xml").body
40
+ @transport.get_uri(@not_found_url, as).body
38
41
  end
39
42
  end
40
43
 
41
44
  it '#post_uri raises a ResourceNotFound error' do
42
45
  assert_raises(Faraday::Error::ResourceNotFound) do
43
- @transport.post_uri(@not_found_url, 'crisis', "application/xml").body
46
+ @transport.post_uri(@not_found_url, body, as).body
44
47
  end
45
48
  end
46
49
 
47
50
  it '#post_uri raises a ResourceNotFound error' do
48
51
  assert_raises(Faraday::Error::ResourceNotFound) do
49
- @transport.post_uri(@not_found_url, 'crisis', "application/xml").body
52
+ @transport.post_uri(@not_found_url, body, as).body
50
53
  end
51
54
  end
52
55
 
53
56
  it '#delete_uri raises a ResourceNotFound error' do
54
57
  assert_raises(Faraday::Error::ResourceNotFound) do
55
- @transport.delete_uri(@not_found_url, "application/xml").body
58
+ @transport.delete_uri(@not_found_url, as).body
56
59
  end
57
60
  end
58
61
  end
@@ -60,10 +63,22 @@ class FaradayHttpTransportTest < MiniTest::Spec
60
63
  describe 'server errors (500 Internal Server Error)' do
61
64
  it '#get_uri raises a ClientError' do
62
65
  assert_raises(Faraday::Error::ClientError) do
63
- @transport.get_uri('http://roar.example.com/deliberate-error', "application/xml").body
66
+ @transport.get_uri('http://roar.example.com/deliberate-error', as).body
64
67
  end
65
68
  end
66
69
  end
67
70
 
68
71
  end
69
72
  end
73
+
74
+ module MiniTest::Assertions
75
+
76
+ def assert_faraday_response(type, response, url, as, body = nil)
77
+ headers = response.env[:request_headers]
78
+ assert_equal [as, as], [headers["Accept"], headers["Content-Type"]]
79
+ assert_equal "<method>#{type}#{(' - ' + body) if body}</method>", response.body
80
+ end
81
+
82
+ end
83
+
84
+ Faraday::Response.infect_an_assertion :assert_faraday_response, :must_match_faraday_response
@@ -2,37 +2,65 @@ require 'test_helper'
2
2
  require 'roar/representer/json/hal'
3
3
 
4
4
  class HalJsonTest < MiniTest::Spec
5
- module SongRepresenter
6
- include Roar::Representer::JSON
7
- include Roar::Representer::JSON::HAL::Links
8
-
9
- link :self do
10
- "http://self"
11
- end
12
-
13
- link :rel => :next, :title => "Hey, @myabc" do
14
- "http://hit"
5
+ let(:rpr) do
6
+ Module.new do
7
+ include Roar::Representer::JSON
8
+ include Roar::Representer::JSON::HAL
9
+
10
+ links :self do
11
+ [{:lang => "en", :href => "http://en.hit"},
12
+ {:lang => "de", :href => "http://de.hit"}]
13
+ end
14
+
15
+ link :next do
16
+ "http://next"
17
+ end
15
18
  end
16
19
  end
17
-
18
- describe "JSON::HAL::Links" do
19
- before do
20
- @song = Object.new.extend(SongRepresenter)
20
+
21
+ subject { Object.new.extend(rpr) }
22
+
23
+ describe "#links" do
24
+ it "parses link array" do # TODO: remove me.
25
+ obj = subject.from_json("{\"_links\":{\"self\":[{\"lang\":\"en\",\"href\":\"http://en.hit\"},{\"lang\":\"de\",\"href\":\"http://de.hit\"}]}}")
26
+ obj.links.must_equal "self" => [link(:rel => :self, :href => "http://en.hit", :lang => :en), link(:rel => :self, :href => "http://de.hit", :lang => :de)]
27
+ end
28
+
29
+ it "parses single links" do # TODO: remove me.
30
+ obj = subject.from_json("{\"_links\":{\"next\":{\"href\":\"http://next\"}}}")
31
+ obj.links.must_equal "next" => link(:rel => :next, :href => "http://next")
32
+ end
33
+
34
+ it "parses link and link array" do
35
+ obj = subject.from_json("{\"_links\":{\"next\":{\"href\":\"http://next\"}, \"self\":[{\"lang\":\"en\",\"href\":\"http://en.hit\"},{\"lang\":\"de\",\"href\":\"http://de.hit\"}]}}")
36
+ obj.links.must_equal "next" => link(:rel => :next, :href => "http://next"), "self" => [link(:rel => :self, :href => "http://en.hit", :lang => :en), link(:rel => :self, :href => "http://de.hit", :lang => :de)]
37
+ end
38
+
39
+ it "rejects single links declared as array when parsing" do
40
+ assert_raises TypeError do
41
+ subject.from_json("{\"_links\":{\"self\":{\"href\":\"http://next\"}}}")
42
+ end
43
+ end
44
+
45
+ it "renders link and link array" do
46
+ subject.to_json.must_equal "{\"_links\":{\"self\":[{\"lang\":\"en\",\"href\":\"http://en.hit\"},{\"lang\":\"de\",\"href\":\"http://de.hit\"}],\"next\":{\"href\":\"http://next\"}}}"
21
47
  end
22
-
23
- it "renders links according to the HAL spec" do
24
- assert_equal "{\"links\":{\"self\":{\"href\":\"http://self\"},\"next\":{\"title\":\"Hey, @myabc\",\"href\":\"http://hit\"}}}", @song.to_json
48
+ end
49
+
50
+ describe "#prepare_links!" do
51
+ it "should map link arrays correctly" do
52
+ subject.send :prepare_links!
53
+ subject.links.must_equal "self" => [link(:rel => :self, :href => "http://en.hit", :lang => "en"),link(:rel => :self, :href => "http://de.hit", :lang => "de")], "next" => link(:href => "http://next", :rel => :next)
25
54
  end
26
-
27
- it "parses incoming JSON links correctly" do
28
- @song.from_json "{\"links\":{\"self\":{\"href\":\"http://self\",\"title\":\"Hey, @myabc\"}}}"
29
- assert_equal "http://self", @song.links[:self].href
30
- assert_equal "Hey, @myabc", @song.links[:self].title
31
- assert_equal nil, @song.links[:next]
55
+ end
56
+
57
+ describe "#link_array_rels" do
58
+ it "returns list of rels for array links" do
59
+ subject.send(:link_array_rels).must_equal [:self]
32
60
  end
33
61
  end
34
-
35
-
62
+
63
+
36
64
  describe "HAL/JSON" do
37
65
  before do
38
66
  Bla = Module.new do
@@ -42,7 +70,7 @@ class HalJsonTest < MiniTest::Spec
42
70
  "http://items/#{value}"
43
71
  end
44
72
  end
45
-
73
+
46
74
  @order_rep = Module.new do
47
75
  include Roar::Representer::JSON::HAL
48
76
  property :id
@@ -51,14 +79,14 @@ class HalJsonTest < MiniTest::Spec
51
79
  "http://orders/#{id}"
52
80
  end
53
81
  end
54
-
82
+
55
83
  @order = Order.new(:items => [Item.new(:value => "Beer")], :id => 1).extend(@order_rep)
56
84
  end
57
-
85
+
58
86
  it "render links and embedded resources according to HAL" do
59
87
  assert_equal "{\"id\":1,\"_embedded\":{\"items\":[{\"value\":\"Beer\",\"_links\":{\"self\":{\"href\":\"http://items/Beer\"}}}]},\"_links\":{\"self\":{\"href\":\"http://orders/1\"}}}", @order.to_json
60
88
  end
61
-
89
+
62
90
  it "parses links and resources following the mighty HAL" do
63
91
  @order.from_json("{\"id\":2,\"_embedded\":{\"items\":[{\"value\":\"Coffee\",\"_links\":{\"self\":{\"href\":\"http://items/Coffee\"}}}]},\"_links\":{\"self\":{\"href\":\"http://orders/2\"}}}")
64
92
  assert_equal 2, @order.id
@@ -66,12 +94,26 @@ class HalJsonTest < MiniTest::Spec
66
94
  assert_equal "http://items/Coffee", @order.items.first.links[:self].href
67
95
  assert_equal "http://orders/2", @order.links[:self].href
68
96
  end
69
-
97
+
70
98
  it "doesn't require _links and _embedded to be present" do
71
99
  @order.from_json("{\"id\":2}")
72
100
  assert_equal 2, @order.id
73
101
  assert_equal [], @order.items
74
- assert_equal [], @order.links
102
+ @order.links.must_equal({})
75
103
  end
76
104
  end
77
105
  end
106
+
107
+ class LinkCollectionTest < MiniTest::Spec
108
+ subject { Roar::Representer::JSON::HAL::LinkCollection.new([:self, "next"]) }
109
+ describe "#is_array?" do
110
+ it "returns true for array link" do
111
+ subject.is_array?(:self).must_equal true
112
+ subject.is_array?("self").must_equal true
113
+ end
114
+
115
+ it "returns false otherwise" do
116
+ subject.is_array?("prev").must_equal false
117
+ end
118
+ end
119
+ end
@@ -4,38 +4,38 @@ require 'roar/representer/json'
4
4
 
5
5
  class HttpVerbsTest < MiniTest::Spec
6
6
  BandRepresenter = FakeServer::BandRepresenter
7
-
7
+
8
8
  # keep this class clear of Roar modules.
9
9
  class Band
10
10
  attr_accessor :name, :label
11
11
  end
12
-
13
-
12
+
13
+
14
14
  describe "HttpVerbs" do
15
15
  before do
16
16
  @band = Band.new
17
17
  @band.extend(BandRepresenter)
18
18
  @band.extend(Roar::Representer::Feature::HttpVerbs)
19
19
  end
20
-
20
+
21
21
  describe "transport_engine" do
22
22
  before do
23
23
  @http_verbs = Roar::Representer::Feature::HttpVerbs
24
24
  @net_http = Roar::Representer::Transport::NetHTTP
25
25
  end
26
-
26
+
27
27
  it "has a default set in the transport module level" do
28
28
  assert_equal @net_http, @band.transport_engine
29
29
  end
30
-
30
+
31
31
  it "allows changing on instance level" do
32
32
  @band.transport_engine = :soap
33
33
  assert_equal @net_http, @http_verbs.transport_engine
34
34
  assert_equal :soap, @band.transport_engine
35
35
  end
36
36
  end
37
-
38
-
37
+
38
+
39
39
  describe "HttpVerbs.get" do
40
40
  it "returns instance from incoming representation" do
41
41
  band = @band.get("http://roar.example.com/bands/slayer", "application/json")
@@ -68,18 +68,18 @@ class HttpVerbsTest < MiniTest::Spec
68
68
  assert_equal "Canadian Maple", @band.label
69
69
  end
70
70
  end
71
-
71
+
72
72
  describe "#post" do
73
73
  it "updates instance with incoming representation" do
74
74
  @band.name = "Strung Out"
75
75
  assert_equal nil, @band.label
76
-
76
+
77
77
  @band.post("http://roar.example.com/bands", "application/xml")
78
78
  assert_equal "STRUNG OUT", @band.name
79
79
  assert_equal nil, @band.label
80
80
  end
81
81
  end
82
-
82
+
83
83
  describe "#put" do
84
84
  it "updates instance with incoming representation" do
85
85
  @band.name = "Strung Out"
@@ -89,7 +89,7 @@ class HttpVerbsTest < MiniTest::Spec
89
89
  assert_equal "FAT WRECK", @band.label
90
90
  end
91
91
  end
92
-
92
+
93
93
  describe "#patch" do
94
94
  it 'does something' do
95
95
  @band.label = 'Fat Mike'
@@ -4,59 +4,11 @@ require 'roar/representer/json'
4
4
 
5
5
  class HypermediaTest
6
6
  describe "Hypermedia Feature" do
7
- describe "Hypermedia.link" do
8
- before do
9
- @mod = Module.new do
10
- include Roar::Representer::JSON
11
- include Roar::Representer::Feature::Hypermedia
12
- end
13
- end
14
-
15
- it "accepts rel symbol, only" do
16
- @mod.class_eval do
17
- link :self do
18
- "http://self"
19
- end
20
- end
21
-
22
- assert_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"http://self\"}]}", Object.new.extend(@mod).to_json
23
- end
24
-
25
- it "accepts any options" do
26
- @mod.class_eval do
27
- link :rel => :self, :title => "Hey, @myabc" do
28
- "http://self"
29
- end
30
- end
31
-
32
- assert_equal "{\"links\":[{\"rel\":\"self\",\"title\":\"Hey, @myabc\",\"href\":\"http://self\"}]}", Object.new.extend(@mod).to_json
33
- end
34
-
35
- it "receives options from to_*" do
36
- @mod.class_eval do
37
- link :self do |opts|
38
- "http://self/#{opts[:id]}"
39
- end
40
- end
41
-
42
- assert_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"http://self/1\"}]}", Object.new.extend(@mod).to_json(:id => 1)
43
- end
44
-
45
- it "supports returning hash" do
46
- @mod.class_eval do
47
- link :self do
48
- {:href => "http://self", :type => "image/jpg"}
49
- end
50
- end
51
-
52
- assert_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"http://self\",\"type\":\"image/jpg\"}]}", Object.new.extend(@mod).to_json
53
- end
54
- end
55
7
 
56
8
 
57
9
  before do
58
10
  @bookmarks = Class.new do
59
- include AttributesContructor
11
+ include AttributesConstructor
60
12
  include Roar::Representer::XML
61
13
  include Roar::Representer::Feature::Hypermedia
62
14
 
@@ -111,7 +63,7 @@ class HypermediaTest
111
63
 
112
64
  it "sets up links even when nested" do
113
65
  class Page
114
- include AttributesContructor
66
+ include AttributesConstructor
115
67
  include Roar::Representer::JSON
116
68
  property :note, :class => Note
117
69
  attr_accessor :note
@@ -133,109 +85,52 @@ class HypermediaTest
133
85
 
134
86
  assert_kind_of Roar::Representer::Feature::Hypermedia::LinkCollection, doc.links
135
87
  assert_equal 1, doc.links.size
136
- assert_equal(["self", "http://bookmarks"], [doc.links.first.rel, doc.links.first.href])
88
+ assert_equal(["self", "http://bookmarks"], [doc.links_array.first.rel, doc.links_array.first.href])
137
89
  end
138
90
 
139
91
  it "sets up an empty link list if no links found in the document" do
140
- assert_equal [], @bookmarks_with_links.from_xml(%{<bookmarks/>}).links
92
+ assert_equal [], @bookmarks_with_links.from_xml(%{<bookmarks/>}).links_array
141
93
  end
142
94
  end
143
95
 
144
96
 
145
97
  describe "#links" do
146
- before do
147
- @set = @bookmarks.new
148
- hyper = Roar::Representer::Feature::Hypermedia::Hyperlink
149
-
150
- @set.links = [
151
- {:rel => "self", :href => "http://self"},
152
- {:rel => "next", :href => "http://next"}].collect do |config|
153
- link = hyper.new
154
- link.rel = config[:rel]
155
- link.href = config[:href]
156
- link
157
- end
158
- end
98
+ subject { Object.new.extend(rpr).tap do |obj|
99
+ obj.send :prepare_links!
100
+ end }
159
101
 
160
- describe "#links=" do
161
- it "wraps links in a LinkCollection" do
162
- assert_kind_of Roar::Representer::Feature::Hypermedia::LinkCollection, @set.links
163
- assert_equal 2, @set.links.size
164
- end
102
+ representer_for do
103
+ link(:self) { "//self" }
104
+ link(:next) { "//next" }
165
105
  end
166
106
 
167
- describe "#link[]" do
168
107
  it "returns link object" do
169
- assert_equal "http://self", @set.links["self"].href
170
- assert_equal "http://self", @set.links[:self].href
171
- assert_equal "http://next", @set.links[:next].href
172
- assert_equal nil, @set.links[:prev]
173
- end
174
- end
175
-
176
- it "returns an empty list when no links present" do
177
- assert_equal Roar::Representer::Feature::Hypermedia::LinkCollection.new, @bookmarks.new.links
178
- end
179
- end
180
-
181
-
182
- describe "#find_links_definition" do
183
- it "returns Definition if links are present" do
184
- @bookmarks.class_eval do
185
- property :id
186
- link :self
108
+ subject.links["self"].href.must_equal "//self"
109
+ subject.links[:self].href.must_equal "//self"
110
+ subject.links[:next].href.must_equal "//next"
111
+ subject.links["unknown"].must_equal nil
187
112
  end
188
113
 
189
- assert_equal "links", @bookmarks.find_links_definition.name
190
- end
191
-
192
- it "returns nil if no links defined" do
193
- assert_equal nil, @bookmarks.find_links_definition
194
- end
114
+ # it "returns an empty list when no links present" do
115
+ # assert_equal Roar::Representer::Feature::Hypermedia::LinkCollection.new, @bookmarks.new.links
116
+ # end
195
117
  end
196
118
  end
197
119
  end
198
120
 
199
- class LinksDefinitionTest < MiniTest::Spec
200
- describe "LinksDefinition" do
201
- before do
202
- @d = Roar::Representer::Feature::Hypermedia::LinksDefinition.new(:links)
203
- end
204
-
205
- it "accepts options in constructor" do
206
- assert_equal [], @d.rel2block
207
- end
208
-
209
- it "accepts configuration" do
210
- @d.rel2block << {:rel => :self}
211
- assert_equal [{:rel=>:self}], @d.rel2block
212
- end
213
-
214
- it "responds to #clone" do
215
- @d.rel2block << {:rel => :self}
216
- assert @d.clone.rel2block.object_id != @d.rel2block.object_id
217
- end
218
-
219
-
220
- it "responds to #each to iterate rel2block" do
221
- end
222
-
223
- end
224
- end
225
121
 
226
122
  class LinkCollectionTest < MiniTest::Spec
227
123
  describe "LinkCollection" do
228
- it "provides #update_link" do
229
- collection = Roar::Representer::Feature::Hypermedia::LinkCollection.new
230
- link = Roar::Representer::Feature::Hypermedia::Hyperlink.new
231
- link.rel = "self"
232
- link.href = "http://self"
233
-
234
- collection.update_link(link)
235
- assert_equal 1, collection.size
236
-
237
- collection.update_link(link)
238
- assert_equal 1, collection.size
124
+ subject { Roar::Representer::Feature::Hypermedia::LinkCollection.new }
125
+
126
+ describe "#add" do
127
+ it "keys by using rel string" do
128
+ subject.size.must_equal 0
129
+ subject.add(link = link(:rel => :self))
130
+ subject.values.must_equal [link]
131
+ subject.add(link = link(:rel => "self"))
132
+ subject.values.must_equal [link]
133
+ end
239
134
  end
240
135
  end
241
136
  end
@@ -243,30 +138,28 @@ end
243
138
  class HyperlinkTest < MiniTest::Spec
244
139
  Hyperlink = Roar::Representer::Feature::Hypermedia::Hyperlink
245
140
  describe "Hyperlink" do
246
- before do
247
- @link = Hyperlink.new(:rel => "self", "href" => "http://self", "data-whatever" => "Hey, @myabc")
248
- end
141
+ subject { Hyperlink.new(:rel => "self", "href" => "http://self", "data-whatever" => "Hey, @myabc") }
249
142
 
250
143
  it "accepts string keys in constructor" do
251
- assert_equal "Hey, @myabc", @link.send("data-whatever")
144
+ assert_equal "Hey, @myabc", subject.send("data-whatever")
252
145
  end
253
146
 
254
147
  it "responds to #rel" do
255
- assert_equal "self", @link.rel
148
+ assert_equal "self", subject.rel
256
149
  end
257
150
 
258
151
  it "responds to #href" do
259
- assert_equal "http://self", @link.href
152
+ assert_equal "http://self", subject.href
260
153
  end
261
-
154
+
262
155
  it "responds to #replace with string keys" do
263
- @link.replace("rel" => "next")
264
- assert_equal nil, @link.href
265
- assert_equal "next", @link.rel
156
+ subject.replace("rel" => "next")
157
+ assert_equal nil, subject.href
158
+ assert_equal "next", subject.rel
266
159
  end
267
-
160
+
268
161
  it "responds to #each and implements Enumerable" do
269
- assert_equal ["rel:self", "href:http://self", "data-whatever:Hey, @myabc"], @link.collect { |k,v| "#{k}:#{v}" }
162
+ assert_equal ["rel:self", "href:http://self", "data-whatever:Hey, @myabc"], subject.collect { |k,v| "#{k}:#{v}" }
270
163
  end
271
164
  end
272
165
  end
@@ -300,7 +193,7 @@ class HyperlinkInheritanceTest < MiniTest::Spec
300
193
 
301
194
  it "should inherit parent links" do
302
195
  foo = Object.new.extend(Foo)
303
-
196
+
304
197
  assert_equal "{\"links\":[{\"rel\":\"base\",\"href\":\"http://base\"},{\"rel\":\"foo\",\"href\":\"http://foo\"}]}", foo.to_json
305
198
  end
306
199