diff options
-rw-r--r-- | lib/puppet/network/format_handler.rb | 33 | ||||
-rwxr-xr-x | spec/unit/network/format_handler.rb | 73 |
2 files changed, 106 insertions, 0 deletions
diff --git a/lib/puppet/network/format_handler.rb b/lib/puppet/network/format_handler.rb new file mode 100644 index 000000000..a69c29232 --- /dev/null +++ b/lib/puppet/network/format_handler.rb @@ -0,0 +1,33 @@ +require 'puppet/network' + +module Puppet::Network::FormatHandler + def self.extended(klass) + klass.extend(ClassMethods) + + # LAK:NOTE This won't work in 1.9 ('send' won't be able to send + # private methods, but I don't know how else to do it. + klass.send(:include, InstanceMethods) + end + + module ClassMethods + def convert_from(format, data) + raise ArgumentError, "Format %s not supported" % format unless support_format?(format) + send("from_%s" % format, data) + end + + def support_format?(name) + respond_to?("from_%s" % name) and instance_methods.include?("to_%s" % name) + end + end + + module InstanceMethods + def render(format) + raise ArgumentError, "Format %s not supported" % format unless support_format?(format) + send("to_%s" % format) + end + + def support_format?(name) + self.class.support_format?(name) + end + end +end diff --git a/spec/unit/network/format_handler.rb b/spec/unit/network/format_handler.rb new file mode 100755 index 000000000..ab863fc8e --- /dev/null +++ b/spec/unit/network/format_handler.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/network/format_handler' + +class FormatTester + extend Puppet::Network::FormatHandler + + def self.from_good + end + + def to_good + end +end + +describe Puppet::Network::FormatHandler do + it "should be able to test whether a format is supported" do + FormatTester.should respond_to(:support_format?) + end + + it "should consider the format supported if it can convert from an instance to the format and from the format to an instance" do + FormatTester.should be_support_format(:good) + end + + it "should not consider the format supported unless it can convert from an instance to the format and from the format to an instance" do + FormatTester.should_not be_support_format(:nope) + end + + it "should be able to convert from a given format" do + FormatTester.should respond_to(:convert_from) + end + + it "should fail if asked to convert from an unsupported format" do + FormatTester.expects(:support_format?).with(:nope).returns false + lambda { FormatTester.convert_from(:nope, "mydata") }.should raise_error(ArgumentError) + end + + it "should call the format-specific converter when asked to convert from a given format" do + FormatTester.expects(:from_good).with("mydata") + FormatTester.convert_from(:good, "mydata") + end + + describe "when an instance" do + it "should be able to test whether a format is supported" do + FormatTester.new.should respond_to(:support_format?) + end + + it "should consider the format supported if it can convert from an instance to the format and from the format to an instance" do + FormatTester.new.should be_support_format(:good) + end + + it "should not consider the format supported unless it can convert from an instance to the format and from the format to an instance" do + FormatTester.new.should_not be_support_format(:nope) + end + + it "should be able to convert to a given format" do + FormatTester.new.should respond_to(:render) + end + + it "should fail if asked to convert to an unsupported format" do + tester = FormatTester.new + tester.expects(:support_format?).with(:nope).returns false + lambda { tester.render(:nope) }.should raise_error(ArgumentError) + end + + it "should call the format-specific converter when asked to convert to a given format" do + tester = FormatTester.new + tester.expects(:to_good) + tester.render(:good) + end + end +end |