3
require File.dirname(__FILE__) + '/../../spec_helper'
5
require 'puppet/network/format_handler'
8
extend Puppet::Network::FormatHandler
11
describe Puppet::Network::FormatHandler do
13
formats = Puppet::Network::FormatHandler.instance_variable_get("@formats")
14
formats.each do |name, format|
15
formats.delete(name) unless format.is_a?(Puppet::Network::Format)
19
it "should be able to list supported formats" do
20
FormatTester.should respond_to(:supported_formats)
23
it "should include all supported formats" do
24
one = stub 'supported', :supported? => true, :name => :one, :weight => 1
25
two = stub 'supported', :supported? => false, :name => :two, :weight => 1
26
three = stub 'supported', :supported? => true, :name => :three, :weight => 1
27
four = stub 'supported', :supported? => false, :name => :four, :weight => 1
28
Puppet::Network::FormatHandler.stubs(:formats).returns [:one, :two, :three, :four]
29
Puppet::Network::FormatHandler.stubs(:format).with(:one).returns one
30
Puppet::Network::FormatHandler.stubs(:format).with(:two).returns two
31
Puppet::Network::FormatHandler.stubs(:format).with(:three).returns three
32
Puppet::Network::FormatHandler.stubs(:format).with(:four).returns four
33
result = FormatTester.supported_formats
34
result.length.should == 2
35
result.should be_include(:one)
36
result.should be_include(:three)
39
it "should return the supported formats in decreasing order of weight" do
40
one = stub 'supported', :supported? => true, :name => :one, :weight => 1
41
two = stub 'supported', :supported? => true, :name => :two, :weight => 6
42
three = stub 'supported', :supported? => true, :name => :three, :weight => 2
43
four = stub 'supported', :supported? => true, :name => :four, :weight => 8
44
Puppet::Network::FormatHandler.stubs(:formats).returns [:one, :two, :three, :four]
45
Puppet::Network::FormatHandler.stubs(:format).with(:one).returns one
46
Puppet::Network::FormatHandler.stubs(:format).with(:two).returns two
47
Puppet::Network::FormatHandler.stubs(:format).with(:three).returns three
48
Puppet::Network::FormatHandler.stubs(:format).with(:four).returns four
49
FormatTester.supported_formats.should == [:four, :two, :three, :one]
52
it "should always put the preferred serialization format first if it is supported" do
53
one = stub 'supported', :supported? => true, :name => :one, :weight => 1
54
two = stub 'supported', :supported? => true, :name => :two, :weight => 6
56
Puppet.settings.expects(:value).with(:preferred_serialization_format).returns :one
57
Puppet::Network::FormatHandler.stubs(:formats).returns [:one, :two]
58
Puppet::Network::FormatHandler.stubs(:format).with(:one).returns one
59
Puppet::Network::FormatHandler.stubs(:format).with(:two).returns two
60
FormatTester.supported_formats.should == [:one, :two]
63
it "should return the first format as the default format" do
64
FormatTester.expects(:supported_formats).returns [:one, :two]
65
FormatTester.default_format.should == :one
68
it "should be able to use a protected format for better logging on errors" do
69
Puppet::Network::FormatHandler.should respond_to(:protected_format)
72
it "should delegate all methods from the informative format to the specified format" do
73
format = mock 'format'
74
format.stubs(:name).returns(:myformat)
75
Puppet::Network::FormatHandler.expects(:format).twice.with(:myformat).returns format
77
format.expects(:render).with("foo").returns "yay"
78
Puppet::Network::FormatHandler.protected_format(:myformat).render("foo").should == "yay"
81
it "should provide better logging if a failure is encountered when delegating from the informative format to the real format" do
82
format = mock 'format'
83
format.stubs(:name).returns(:myformat)
84
Puppet::Network::FormatHandler.expects(:format).twice.with(:myformat).returns format
86
format.expects(:render).with("foo").raises "foo"
87
lambda { Puppet::Network::FormatHandler.protected_format(:myformat).render("foo") }.should raise_error(Puppet::Network::FormatHandler::FormatError)
90
it "should raise an error if we couldn't find a format by name or mime-type" do
91
Puppet::Network::FormatHandler.stubs(:format).with(:myformat).returns nil
92
lambda { Puppet::Network::FormatHandler.protected_format(:myformat) }.should raise_error
95
describe "when using formats" do
97
@format = mock 'format'
98
@format.stubs(:supported?).returns true
99
@format.stubs(:name).returns :my_format
100
Puppet::Network::FormatHandler.stubs(:format).with(:my_format).returns @format
101
Puppet::Network::FormatHandler.stubs(:mime).with("text/myformat").returns @format
102
Puppet::Network::Format.stubs(:===).returns false
103
Puppet::Network::Format.stubs(:===).with(@format).returns true
106
it "should be able to test whether a format is supported" do
107
FormatTester.should respond_to(:support_format?)
110
it "should use the Format to determine whether a given format is supported" do
111
@format.expects(:supported?).with(FormatTester)
112
FormatTester.support_format?(:my_format)
115
it "should be able to convert from a given format" do
116
FormatTester.should respond_to(:convert_from)
119
it "should call the format-specific converter when asked to convert from a given format" do
120
@format.expects(:intern).with(FormatTester, "mydata")
121
FormatTester.convert_from(:my_format, "mydata")
124
it "should call the format-specific converter when asked to convert from a given format by mime-type" do
125
@format.expects(:intern).with(FormatTester, "mydata")
126
FormatTester.convert_from("text/myformat", "mydata")
129
it "should call the format-specific converter when asked to convert from a given format by format instance" do
130
@format.expects(:intern).with(FormatTester, "mydata")
131
FormatTester.convert_from(@format, "mydata")
134
it "should raise a FormatError when an exception is encountered when converting from a format" do
135
@format.expects(:intern).with(FormatTester, "mydata").raises "foo"
136
lambda { FormatTester.convert_from(:my_format, "mydata") }.should raise_error(Puppet::Network::FormatHandler::FormatError)
139
it "should be able to use a specific hook for converting into multiple instances" do
140
@format.expects(:intern_multiple).with(FormatTester, "mydata")
142
FormatTester.convert_from_multiple(:my_format, "mydata")
145
it "should raise a FormatError when an exception is encountered when converting multiple items from a format" do
146
@format.expects(:intern_multiple).with(FormatTester, "mydata").raises "foo"
147
lambda { FormatTester.convert_from_multiple(:my_format, "mydata") }.should raise_error(Puppet::Network::FormatHandler::FormatError)
150
it "should be able to use a specific hook for rendering multiple instances" do
151
@format.expects(:render_multiple).with("mydata")
153
FormatTester.render_multiple(:my_format, "mydata")
156
it "should raise a FormatError when an exception is encountered when rendering multiple items into a format" do
157
@format.expects(:render_multiple).with("mydata").raises "foo"
158
lambda { FormatTester.render_multiple(:my_format, "mydata") }.should raise_error(Puppet::Network::FormatHandler::FormatError)
162
describe "when managing formats" do
163
it "should have a method for defining a new format" do
164
Puppet::Network::FormatHandler.should respond_to(:create)
167
it "should create a format instance when asked" do
168
format = stub 'format', :name => :foo
169
Puppet::Network::Format.expects(:new).with(:foo).returns format
170
Puppet::Network::FormatHandler.create(:foo)
173
it "should instance_eval any block provided when creating a format" do
174
format = stub 'format', :name => :instance_eval
175
format.expects(:yayness)
176
Puppet::Network::Format.expects(:new).returns format
177
Puppet::Network::FormatHandler.create(:instance_eval) do
182
it "should be able to retrieve a format by name" do
183
format = Puppet::Network::FormatHandler.create(:by_name)
184
Puppet::Network::FormatHandler.format(:by_name).should equal(format)
187
it "should be able to retrieve formats by name irrespective of case and class" do
188
format = Puppet::Network::FormatHandler.create(:by_name)
189
Puppet::Network::FormatHandler.format(:By_Name).should equal(format)
192
it "should be able to retrieve a format by mime type" do
193
format = Puppet::Network::FormatHandler.create(:by_name, :mime => "foo/bar")
194
Puppet::Network::FormatHandler.mime("foo/bar").should equal(format)
197
it "should be able to retrieve a format by mime type irrespective of case" do
198
format = Puppet::Network::FormatHandler.create(:by_name, :mime => "foo/bar")
199
Puppet::Network::FormatHandler.mime("Foo/Bar").should equal(format)
202
it "should be able to return all formats" do
203
one = stub 'one', :name => :one
204
two = stub 'two', :name => :two
205
Puppet::Network::Format.expects(:new).with(:one).returns(one)
206
Puppet::Network::Format.expects(:new).with(:two).returns(two)
208
Puppet::Network::FormatHandler.create(:one)
209
Puppet::Network::FormatHandler.create(:two)
211
list = Puppet::Network::FormatHandler.formats
212
list.should be_include(:one)
213
list.should be_include(:two)
217
describe "when an instance" do
218
it "should be able to test whether a format is supported" do
219
FormatTester.new.should respond_to(:support_format?)
222
it "should be able to convert to a given format" do
223
FormatTester.new.should respond_to(:render)
226
it "should be able to get a format mime-type" do
227
FormatTester.new.should respond_to(:mime)
230
it "should raise a FormatError when a rendering error is encountered" do
231
format = stub 'rendering format', :supported? => true, :name => :foo
232
Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
234
tester = FormatTester.new
235
format.expects(:render).with(tester).raises "eh"
237
lambda { tester.render(:foo) }.should raise_error(Puppet::Network::FormatHandler::FormatError)
240
it "should call the format-specific converter when asked to convert to a given format" do
241
format = stub 'rendering format', :supported? => true, :name => :foo
243
Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
245
tester = FormatTester.new
246
format.expects(:render).with(tester).returns "foo"
248
tester.render(:foo).should == "foo"
251
it "should call the format-specific converter when asked to convert to a given format by mime-type" do
252
format = stub 'rendering format', :supported? => true, :name => :foo
253
Puppet::Network::FormatHandler.stubs(:mime).with("text/foo").returns format
254
Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
256
tester = FormatTester.new
257
format.expects(:render).with(tester).returns "foo"
259
tester.render("text/foo").should == "foo"
262
it "should call the format converter when asked to convert to a given format instance" do
263
format = stub 'rendering format', :supported? => true, :name => :foo
264
Puppet::Network::Format.stubs(:===).with(format).returns(true)
265
Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
267
tester = FormatTester.new
268
format.expects(:render).with(tester).returns "foo"
270
tester.render(format).should == "foo"
273
it "should render to the default format if no format is provided when rendering" do
274
format = stub 'rendering format', :supported? => true, :name => :foo
275
Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
277
FormatTester.expects(:default_format).returns :foo
278
tester = FormatTester.new
280
format.expects(:render).with(tester)
284
it "should call the format-specific converter when asked for the mime-type of a given format" do
285
format = stub 'rendering format', :supported? => true, :name => :foo
287
Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
289
tester = FormatTester.new
290
format.expects(:mime).returns "text/foo"
292
tester.mime(:foo).should == "text/foo"
295
it "should return the default format mime-type if no format is provided" do
296
format = stub 'rendering format', :supported? => true, :name => :foo
297
Puppet::Network::FormatHandler.stubs(:format).with(:foo).returns format
299
FormatTester.expects(:default_format).returns :foo
300
tester = FormatTester.new
302
format.expects(:mime).returns "text/foo"
303
tester.mime.should == "text/foo"