diff options
| -rw-r--r-- | lib/puppet/network/formats.rb | 24 | ||||
| -rwxr-xr-x | spec/unit/network/formats.rb | 42 |
2 files changed, 64 insertions, 2 deletions
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb index 79e2860af..bd171fa7d 100644 --- a/lib/puppet/network/formats.rb +++ b/lib/puppet/network/formats.rb @@ -28,12 +28,12 @@ end Puppet::Network::FormatHandler.create(:marshal, :mime => "text/marshal") do - # Yaml doesn't need the class name; it's serialized. + # Marshal doesn't need the class name; it's serialized. def intern(klass, text) Marshal.load(text) end - # Yaml doesn't need the class name; it's serialized. + # Marshal doesn't need the class name; it's serialized. def intern_multiple(klass, text) Marshal.load(text) end @@ -52,3 +52,23 @@ Puppet::Network::FormatHandler.create(:marshal, :mime => "text/marshal") do true end end + +Puppet::Network::FormatHandler.create(:str, :mime => "text/plain") do + # For now, use the YAML separator. + SEPARATOR = "\n---\n" + + # Yaml doesn't need the class name; it's serialized. + def intern_multiple(klass, text) + text.split(SEPARATOR).collect { |inst| intern(klass, inst) } + end + + # Yaml monkey-patches Array, so this works. + def render_multiple(instances) + instances.collect { |inst| render(inst) }.join(SEPARATOR) + end + + # Everything's supported + def supported?(klass) + true + end +end diff --git a/spec/unit/network/formats.rb b/spec/unit/network/formats.rb index a7d74e95c..1baf72597 100755 --- a/spec/unit/network/formats.rb +++ b/spec/unit/network/formats.rb @@ -89,4 +89,46 @@ describe "Puppet Network Format" do @marshal.intern_multiple(String, text).should == "bar" end end + + describe "plaintext" do + before do + @text = Puppet::Network::FormatHandler.format(:str) + end + + it "should have its mimetype set to text/plain" do + @text.mime.should == "text/plain" + end + + it "should fail if the instance does not respond to 'to_str'" do + instance = mock 'nope' + instance.expects(:to_str).never + lambda { @text.render(instance) }.should raise_error(NotImplementedError) + end + + it "should render by calling 'to_str' on the instance" do + # Use an instance that responds to 'to_str' + instance = "foo" + instance.expects(:to_str).returns "string" + @text.render(instance).should == "string" + end + + it "should render multiple instances by calling 'to_str' on each instance and joining the results with '\\n---\\n'" do + instances = ["string1", 'string2'] + + @text.render_multiple(instances).should == "string1\n---\nstring2" + end + + it "should intern by calling 'from_str' on the class" do + text = "foo" + String.expects(:from_str).with(text).returns "bar" + @text.intern(String, text).should == "bar" + end + + it "should intern multiples by splitting on '\\n---\\n' and converting each string to an instance" do + text = "foo\n---\nbar" + String.expects(:from_str).with("foo").returns "FOO" + String.expects(:from_str).with("bar").returns "BAR" + @text.intern_multiple(String, text).should == ["FOO", "BAR"] + end + end end |
