summaryrefslogtreecommitdiffstats
path: root/spec/unit/network/format.rb
blob: 244bff306d178b9fbf074009f162e2e4da31e6ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/network/format'

# A class with all of the necessary
# hooks.
class FormatRenderer
    def self.to_multiple_my_format(list)
    end

    def self.from_multiple_my_format(text)
    end

    def self.from_my_format(text)
    end

    def to_my_format
    end
end

describe Puppet::Network::Format do
    describe "when initializing" do
        it "should require a name" do
            lambda { Puppet::Network::Format.new }.should raise_error(ArgumentError)
        end

        it "should be able to provide its name" do
            Puppet::Network::Format.new(:my_format).name.should == :my_format
        end

        it "should always convert its name to a downcased symbol" do
            Puppet::Network::Format.new(:My_Format).name.should == :my_format
        end

        it "should be able to set its downcased mime type at initialization" do
            format = Puppet::Network::Format.new(:my_format, :mime => "Foo/Bar")
            format.mime.should == "foo/bar"
        end

        it "should default to text plus the name of the format as the mime type" do
            Puppet::Network::Format.new(:my_format).mime.should == "text/my_format"
        end

        it "should fail if unsupported options are provided" do
            lambda { Puppet::Network::Format.new(:my_format, :foo => "bar") }.should raise_error(ArgumentError)
        end
    end

    describe "instances" do
        before do
            @format = Puppet::Network::Format.new(:my_format)
        end

        it "should support being confined" do
            @format.should respond_to(:confine)
        end

        it "should not be considered suitable if confinement conditions are not met" do
            @format.confine :true => false
            @format.should_not be_suitable
        end

        it "should be able to determine if a class is supported" do
            @format.should respond_to(:supported?)
        end

        it "should consider a class to be supported if it has the individual and multiple methods for rendering and interning" do
            @format.should be_supported(FormatRenderer)
        end

        it "should not consider a class to be supported unless it has the individual and multiple methods for rendering and interning" do
            Puppet::Network::Format.new(:yaml).should_not be_supported(String)
        end

        it "should not consider a class supported unless the format is suitable" do
            @format.expects(:suitable?).returns false
            @format.should_not be_supported(FormatRenderer)
        end

        it "should always downcase mimetypes" do
            @format.mime = "Foo/Bar"
            @format.mime.should == "foo/bar"
        end

        it "should support having a weight" do
            @format.should respond_to(:weight)
        end

        it "should default to a weight of of 5" do
            @format.weight.should == 5
        end

        it "should be able to override its weight at initialization" do
            Puppet::Network::Format.new(:foo, :weight => 1).weight.should == 1
        end
    end

    describe "when converting between instances and formatted text" do
        before do
            @format = Puppet::Network::Format.new(:my_format)
            @instance = FormatRenderer.new
        end

        it "should have a method for rendering a single instance" do
            @format.should respond_to(:render)
        end

        it "should have a method for rendering multiple instances" do
            @format.should respond_to(:render_multiple)
        end

        it "should have a method for interning text" do
            @format.should respond_to(:intern)
        end

        it "should have a method for interning text into multiple instances" do
            @format.should respond_to(:intern_multiple)
        end

        it "should return the results of calling the instance-specific render method if the method is present" do
            @instance.expects(:to_my_format).returns "foo"
            @format.render(@instance).should == "foo"
        end

        it "should return the results of calling the class-specific render_multiple method if the method is present" do
            @instance.class.expects(:to_multiple_my_format).returns ["foo"]
            @format.render_multiple([@instance]).should == ["foo"]
        end

        it "should return the results of calling the class-specific intern method if the method is present" do
            FormatRenderer.expects(:from_my_format).with("foo").returns @instance
            @format.intern(FormatRenderer, "foo").should equal(@instance)
        end

        it "should return the results of calling the class-specific intern_multiple method if the method is present" do
            FormatRenderer.expects(:from_multiple_my_format).with("foo").returns [@instance]
            @format.intern_multiple(FormatRenderer, "foo").should == [@instance]
        end

        it "should fail if asked to render and the instance does not respond to 'to_<format>'" do
            lambda { @format.render("foo") }.should raise_error(NotImplementedError)
        end

        it "should fail if asked to intern and the class does not respond to 'from_<format>'" do
            lambda { @format.intern(String, "foo") }.should raise_error(NotImplementedError)
        end

        it "should fail if asked to intern multiple and the class does not respond to 'from_multiple_<format>'" do
            lambda { @format.intern_multiple(String, "foo") }.should raise_error(NotImplementedError)
        end

        it "should fail if asked to render multiple and the instance does not respond to 'to_multiple_<format>'" do
            lambda { @format.render_multiple(["foo", "bar"]) }.should raise_error(NotImplementedError)
        end
    end
end