summaryrefslogtreecommitdiffstats
path: root/spec/unit/string
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-03-28 10:47:09 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-03-28 10:47:09 -0700
commit2ad8c96935ec53c2d98201ad77fd070dc40dadb6 (patch)
treef8f4d581c3b0445df836d5e55945f62547239598 /spec/unit/string
parent88aeb04a50d8997b5e1e0ed7a5a2239508b174ee (diff)
parentb859baa04737644e40002f511c5941d002a956e3 (diff)
downloadpuppet-2ad8c96935ec53c2d98201ad77fd070dc40dadb6.tar.gz
puppet-2ad8c96935ec53c2d98201ad77fd070dc40dadb6.tar.xz
puppet-2ad8c96935ec53c2d98201ad77fd070dc40dadb6.zip
Merge branch 'maint/master/puppet-strings-is-the-official-api-name'
Diffstat (limited to 'spec/unit/string')
-rwxr-xr-xspec/unit/string/action_builder_spec.rb30
-rwxr-xr-xspec/unit/string/action_manager_spec.rb216
-rwxr-xr-xspec/unit/string/action_spec.rb75
-rwxr-xr-xspec/unit/string/catalog_spec.rb6
-rwxr-xr-xspec/unit/string/certificate_request_spec.rb6
-rwxr-xr-xspec/unit/string/certificate_revocation_list_spec.rb6
-rwxr-xr-xspec/unit/string/certificate_spec.rb6
-rwxr-xr-xspec/unit/string/config_spec.rb24
-rwxr-xr-xspec/unit/string/configurer_spec.rb24
-rwxr-xr-xspec/unit/string/facts_spec.rb21
-rwxr-xr-xspec/unit/string/file_spec.rb6
-rwxr-xr-xspec/unit/string/indirector_spec.rb55
-rwxr-xr-xspec/unit/string/key_spec.rb6
-rwxr-xr-xspec/unit/string/node_spec.rb9
-rwxr-xr-xspec/unit/string/report_spec.rb6
-rwxr-xr-xspec/unit/string/resource_spec.rb6
-rwxr-xr-xspec/unit/string/resource_type_spec.rb6
-rwxr-xr-xspec/unit/string/string_collection_spec.rb249
18 files changed, 757 insertions, 0 deletions
diff --git a/spec/unit/string/action_builder_spec.rb b/spec/unit/string/action_builder_spec.rb
new file mode 100755
index 000000000..c3395cf6a
--- /dev/null
+++ b/spec/unit/string/action_builder_spec.rb
@@ -0,0 +1,30 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+require 'puppet/string/action_builder'
+
+describe Puppet::String::ActionBuilder do
+ describe "::build" do
+ it "should build an action" do
+ action = Puppet::String::ActionBuilder.build(nil,:foo) do
+ end
+ action.should be_a(Puppet::String::Action)
+ action.name.should == "foo"
+ end
+
+ it "should define a method on the string which invokes the action" do
+ string = Puppet::String.new(:action_builder_test_string, '0.0.1')
+ action = Puppet::String::ActionBuilder.build(string, :foo) do
+ invoke do
+ "invoked the method"
+ end
+ end
+
+ string.foo.should == "invoked the method"
+ end
+
+ it "should require a block" do
+ lambda { Puppet::String::ActionBuilder.build(nil,:foo) }.should raise_error("Action 'foo' must specify a block")
+ end
+ end
+end
diff --git a/spec/unit/string/action_manager_spec.rb b/spec/unit/string/action_manager_spec.rb
new file mode 100755
index 000000000..3921f02c0
--- /dev/null
+++ b/spec/unit/string/action_manager_spec.rb
@@ -0,0 +1,216 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+# This is entirely an internal class for String, so we have to load it instead of our class.
+require 'puppet/string'
+
+class ActionManagerTester
+ include Puppet::String::ActionManager
+end
+
+describe Puppet::String::ActionManager do
+ subject { ActionManagerTester.new }
+
+ describe "when included in a class" do
+ it "should be able to define an action" do
+ subject.action(:foo) do
+ invoke { "something "}
+ end
+ end
+
+ it "should be able to define a 'script' style action" do
+ subject.script :bar do
+ "a bar is where beer is found"
+ end
+ end
+
+ it "should be able to list defined actions" do
+ subject.action(:foo) do
+ invoke { "something" }
+ end
+ subject.action(:bar) do
+ invoke { "something" }
+ end
+
+ subject.actions.should =~ [:foo, :bar]
+ end
+
+ it "should list 'script' actions" do
+ subject.script :foo do "foo" end
+ subject.actions.should =~ [:foo]
+ end
+
+ it "should list both script and normal actions" do
+ subject.action :foo do
+ invoke do "foo" end
+ end
+ subject.script :bar do "a bar is where beer is found" end
+
+ subject.actions.should =~ [:foo, :bar]
+ end
+
+ it "should be able to indicate when an action is defined" do
+ subject.action(:foo) do
+ invoke { "something" }
+ end
+
+ subject.should be_action(:foo)
+ end
+
+ it "should indicate an action is defined for script actions" do
+ subject.script :foo do "foo" end
+ subject.should be_action :foo
+ end
+
+ it "should correctly treat action names specified as strings" do
+ subject.action(:foo) do
+ invoke { "something" }
+ end
+
+ subject.should be_action("foo")
+ end
+ end
+
+ describe "when used to extend a class" do
+ subject { Class.new.extend(Puppet::String::ActionManager) }
+
+ it "should be able to define an action" do
+ subject.action(:foo) do
+ invoke { "something "}
+ end
+ end
+
+ it "should be able to list defined actions" do
+ subject.action(:foo) do
+ invoke { "something" }
+ end
+ subject.action(:bar) do
+ invoke { "something" }
+ end
+
+ subject.actions.should include(:bar)
+ subject.actions.should include(:foo)
+ end
+
+ it "should be able to indicate when an action is defined" do
+ subject.action(:foo) { "something" }
+ subject.should be_action(:foo)
+ end
+ end
+
+ describe "when used both at the class and instance level" do
+ before do
+ @klass = Class.new do
+ include Puppet::String::ActionManager
+ extend Puppet::String::ActionManager
+ end
+ @instance = @klass.new
+ end
+
+ it "should be able to define an action at the class level" do
+ @klass.action(:foo) do
+ invoke { "something "}
+ end
+ end
+
+ it "should create an instance method when an action is defined at the class level" do
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.foo.should == "something"
+ end
+
+ it "should be able to define an action at the instance level" do
+ @instance.action(:foo) do
+ invoke { "something "}
+ end
+ end
+
+ it "should create an instance method when an action is defined at the instance level" do
+ @instance.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.foo.should == "something"
+ end
+
+ it "should be able to list actions defined at the class level" do
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
+ @klass.action(:bar) do
+ invoke { "something" }
+ end
+
+ @klass.actions.should include(:bar)
+ @klass.actions.should include(:foo)
+ end
+
+ it "should be able to list actions defined at the instance level" do
+ @instance.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.action(:bar) do
+ invoke { "something" }
+ end
+
+ @instance.actions.should include(:bar)
+ @instance.actions.should include(:foo)
+ end
+
+ it "should be able to list actions defined at both instance and class level" do
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.action(:bar) do
+ invoke { "something" }
+ end
+
+ @instance.actions.should include(:bar)
+ @instance.actions.should include(:foo)
+ end
+
+ it "should be able to indicate when an action is defined at the class level" do
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.should be_action(:foo)
+ end
+
+ it "should be able to indicate when an action is defined at the instance level" do
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.should be_action(:foo)
+ end
+
+ it "should list actions defined in superclasses" do
+ @subclass = Class.new(@klass)
+ @instance = @subclass.new
+
+ @klass.action(:parent) do
+ invoke { "a" }
+ end
+ @subclass.action(:sub) do
+ invoke { "a" }
+ end
+ @instance.action(:instance) do
+ invoke { "a" }
+ end
+
+ @instance.should be_action(:parent)
+ @instance.should be_action(:sub)
+ @instance.should be_action(:instance)
+ end
+
+ it "should create an instance method when an action is defined in a superclass" do
+ @subclass = Class.new(@klass)
+ @instance = @subclass.new
+
+ @klass.action(:foo) do
+ invoke { "something" }
+ end
+ @instance.foo.should == "something"
+ end
+ end
+end
diff --git a/spec/unit/string/action_spec.rb b/spec/unit/string/action_spec.rb
new file mode 100755
index 000000000..4026c9a58
--- /dev/null
+++ b/spec/unit/string/action_spec.rb
@@ -0,0 +1,75 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+require 'puppet/string/action'
+
+describe Puppet::String::Action do
+ describe "when validating the action name" do
+ it "should require a name" do
+ lambda { Puppet::String::Action.new(nil,nil) }.should raise_error("'' is an invalid action name")
+ end
+
+ it "should not allow empty names" do
+ lambda { Puppet::String::Action.new(nil,'') }.should raise_error("'' is an invalid action name")
+ end
+
+ it "should not allow names with whitespace" do
+ lambda { Puppet::String::Action.new(nil,'foo bar') }.should raise_error("'foo bar' is an invalid action name")
+ end
+
+ it "should not allow names beginning with dashes" do
+ lambda { Puppet::String::Action.new(nil,'-foobar') }.should raise_error("'-foobar' is an invalid action name")
+ end
+ end
+
+ describe "when invoking" do
+ it "should be able to call other actions on the same object" do
+ string = Puppet::String.new(:my_string, '0.0.1') do
+ action(:foo) do
+ invoke { 25 }
+ end
+
+ action(:bar) do
+ invoke { "the value of foo is '#{foo}'" }
+ end
+ end
+ string.foo.should == 25
+ string.bar.should == "the value of foo is '25'"
+ end
+
+ # bar is a class action calling a class action
+ # quux is a class action calling an instance action
+ # baz is an instance action calling a class action
+ # qux is an instance action calling an instance action
+ it "should be able to call other actions on the same object when defined on a class" do
+ class Puppet::String::MyStringBaseClass < Puppet::String
+ action(:foo) do
+ invoke { 25 }
+ end
+
+ action(:bar) do
+ invoke { "the value of foo is '#{foo}'" }
+ end
+
+ action(:quux) do
+ invoke { "qux told me #{qux}" }
+ end
+ end
+
+ string = Puppet::String::MyStringBaseClass.new(:my_inherited_string, '0.0.1') do
+ action(:baz) do
+ invoke { "the value of foo in baz is '#{foo}'" }
+ end
+
+ action(:qux) do
+ invoke { baz }
+ end
+ end
+ string.foo.should == 25
+ string.bar.should == "the value of foo is '25'"
+ string.quux.should == "qux told me the value of foo in baz is '25'"
+ string.baz.should == "the value of foo in baz is '25'"
+ string.qux.should == "the value of foo in baz is '25'"
+ end
+ end
+end
diff --git a/spec/unit/string/catalog_spec.rb b/spec/unit/string/catalog_spec.rb
new file mode 100755
index 000000000..a11d29a04
--- /dev/null
+++ b/spec/unit/string/catalog_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:catalog, '0.0.1') do
+end
diff --git a/spec/unit/string/certificate_request_spec.rb b/spec/unit/string/certificate_request_spec.rb
new file mode 100755
index 000000000..96e1d8837
--- /dev/null
+++ b/spec/unit/string/certificate_request_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:certificate_request, '0.0.1') do
+end
diff --git a/spec/unit/string/certificate_revocation_list_spec.rb b/spec/unit/string/certificate_revocation_list_spec.rb
new file mode 100755
index 000000000..cf50471c8
--- /dev/null
+++ b/spec/unit/string/certificate_revocation_list_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:certificate_revocation_list, '0.0.1') do
+end
diff --git a/spec/unit/string/certificate_spec.rb b/spec/unit/string/certificate_spec.rb
new file mode 100755
index 000000000..719ee6b06
--- /dev/null
+++ b/spec/unit/string/certificate_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:certificate, '0.0.1') do
+end
diff --git a/spec/unit/string/config_spec.rb b/spec/unit/string/config_spec.rb
new file mode 100755
index 000000000..562265287
--- /dev/null
+++ b/spec/unit/string/config_spec.rb
@@ -0,0 +1,24 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:config, '0.0.1') do
+ it "should use Settings#print_config_options when asked to print" do
+ Puppet.settings.stubs(:puts)
+ Puppet.settings.expects(:print_config_options)
+ subject.print
+ end
+
+ it "should set 'configprint' to all desired values and call print_config_options when a specific value is provided" do
+ Puppet.settings.stubs(:puts)
+ Puppet.settings.expects(:print_config_options)
+ subject.print("libdir", "ssldir")
+ Puppet.settings[:configprint].should == "libdir,ssldir"
+ end
+
+ it "should always return nil" do
+ Puppet.settings.stubs(:puts)
+ Puppet.settings.expects(:print_config_options)
+ subject.print("libdir").should be_nil
+ end
+end
diff --git a/spec/unit/string/configurer_spec.rb b/spec/unit/string/configurer_spec.rb
new file mode 100755
index 000000000..400bfb593
--- /dev/null
+++ b/spec/unit/string/configurer_spec.rb
@@ -0,0 +1,24 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+require 'puppet/indirector/catalog/rest'
+require 'tempfile'
+
+describe Puppet::String.define(:configurer, '0.0.1') do
+ describe "#synchronize" do
+ it "should retrieve and apply a catalog and return a report" do
+ dirname = Dir.mktmpdir("puppetdir")
+ Puppet[:vardir] = dirname
+ Puppet[:confdir] = dirname
+ @catalog = Puppet::Resource::Catalog.new
+ @file = Puppet::Resource.new(:file, File.join(dirname, "tmp_dir_resource"), :parameters => {:ensure => :present})
+ @catalog.add_resource(@file)
+ Puppet::Resource::Catalog::Rest.any_instance.stubs(:find).returns(@catalog)
+
+ report = subject.synchronize("foo")
+
+ report.kind.should == "apply"
+ report.status.should == "changed"
+ end
+ end
+end
diff --git a/spec/unit/string/facts_spec.rb b/spec/unit/string/facts_spec.rb
new file mode 100755
index 000000000..a537b7420
--- /dev/null
+++ b/spec/unit/string/facts_spec.rb
@@ -0,0 +1,21 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:facts, '0.0.1') do
+ it "should define an 'upload' fact" do
+ subject.should be_action(:upload)
+ end
+
+ it "should set its default format to :yaml" do
+ subject.default_format.should == :yaml
+ end
+
+ describe "when uploading" do
+ it "should set the terminus_class to :facter"
+
+ it "should set the cach_eclass to :rest"
+
+ it "should find the current certname"
+ end
+end
diff --git a/spec/unit/string/file_spec.rb b/spec/unit/string/file_spec.rb
new file mode 100755
index 000000000..bbc8c7e09
--- /dev/null
+++ b/spec/unit/string/file_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:file, '0.0.1') do
+end
diff --git a/spec/unit/string/indirector_spec.rb b/spec/unit/string/indirector_spec.rb
new file mode 100755
index 000000000..89306c416
--- /dev/null
+++ b/spec/unit/string/indirector_spec.rb
@@ -0,0 +1,55 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+require 'puppet/string/indirector'
+
+describe Puppet::String::Indirector do
+ before do
+ @instance = Puppet::String::Indirector.new(:test, '0.0.1')
+
+ @indirection = stub 'indirection', :name => :stub_indirection
+
+ @instance.stubs(:indirection).returns @indirection
+ end
+
+ it "should be able to return a list of indirections" do
+ Puppet::String::Indirector.indirections.should be_include("catalog")
+ end
+
+ it "should be able to return a list of terminuses for a given indirection" do
+ Puppet::String::Indirector.terminus_classes(:catalog).should be_include("compiler")
+ end
+
+ describe "as an instance" do
+ it "should be able to determine its indirection" do
+ # Loading actions here an get, um, complicated
+ Puppet::String.stubs(:load_actions)
+ Puppet::String::Indirector.new(:catalog, '0.0.1').indirection.should equal(Puppet::Resource::Catalog.indirection)
+ end
+ end
+
+ [:find, :search, :save, :destroy].each do |method|
+ it "should define a '#{method}' action" do
+ Puppet::String::Indirector.should be_action(method)
+ end
+
+ it "should just call the indirection method when the '#{method}' action is invoked" do
+ @instance.indirection.expects(method).with(:test, "myargs")
+ @instance.send(method, :test, "myargs")
+ end
+ end
+
+ it "should be able to override its indirection name" do
+ @instance.set_indirection_name :foo
+ @instance.indirection_name.should == :foo
+ end
+
+ it "should be able to set its terminus class" do
+ @instance.indirection.expects(:terminus_class=).with(:myterm)
+ @instance.set_terminus(:myterm)
+ end
+
+ it "should define a class-level 'info' action" do
+ Puppet::String::Indirector.should be_action(:info)
+ end
+end
diff --git a/spec/unit/string/key_spec.rb b/spec/unit/string/key_spec.rb
new file mode 100755
index 000000000..d77f02ec4
--- /dev/null
+++ b/spec/unit/string/key_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:key, '0.0.1') do
+end
diff --git a/spec/unit/string/node_spec.rb b/spec/unit/string/node_spec.rb
new file mode 100755
index 000000000..7198efe76
--- /dev/null
+++ b/spec/unit/string/node_spec.rb
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:node, '0.0.1') do
+ it "should set its default format to :yaml" do
+ subject.default_format.should == :yaml
+ end
+end
diff --git a/spec/unit/string/report_spec.rb b/spec/unit/string/report_spec.rb
new file mode 100755
index 000000000..51342c2fa
--- /dev/null
+++ b/spec/unit/string/report_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:report, '0.0.1') do
+end
diff --git a/spec/unit/string/resource_spec.rb b/spec/unit/string/resource_spec.rb
new file mode 100755
index 000000000..de7e747ed
--- /dev/null
+++ b/spec/unit/string/resource_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:resource, '0.0.1') do
+end
diff --git a/spec/unit/string/resource_type_spec.rb b/spec/unit/string/resource_type_spec.rb
new file mode 100755
index 000000000..8b0b4aaa7
--- /dev/null
+++ b/spec/unit/string/resource_type_spec.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+
+describe Puppet::String.define(:resource_type, '0.0.1') do
+end
diff --git a/spec/unit/string/string_collection_spec.rb b/spec/unit/string/string_collection_spec.rb
new file mode 100755
index 000000000..46c431f75
--- /dev/null
+++ b/spec/unit/string/string_collection_spec.rb
@@ -0,0 +1,249 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
+require 'tmpdir'
+
+describe Puppet::String::StringCollection do
+ before :all do
+ @strings = subject.instance_variable_get("@strings").dup
+ end
+
+ before :each do
+ subject.instance_variable_get("@strings").clear
+ end
+
+ after :all do
+ subject.instance_variable_set("@strings", @strings)
+ end
+
+ describe "::strings" do
+ end
+
+ describe "::versions" do
+ before :each do
+ @dir = Dir.mktmpdir
+ @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'string')
+ $LOAD_PATH.push(@dir)
+ end
+
+ after :each do
+ FileUtils.remove_entry_secure @dir
+ $LOAD_PATH.pop
+ end
+
+ it "should return an empty array when no versions are loadable" do
+ subject.versions(:fozzie).should == []
+ end
+
+ it "should return versions loadable as puppet/string/v{version}/{name}" do
+ FileUtils.mkdir_p(File.join @lib, 'v1.0.0')
+ FileUtils.touch(File.join @lib, 'v1.0.0', 'fozzie.rb')
+ subject.versions(:fozzie).should == ['1.0.0']
+ end
+
+ it "should an ordered list of all versions loadable as puppet/string/v{version}/{name}" do
+ %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version|
+ FileUtils.mkdir_p(File.join @lib, "v#{version}")
+ FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb')
+ end
+ subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ]
+ end
+
+ it "should not return a version for an empty puppet/string/v{version}/{name}" do
+ FileUtils.mkdir_p(File.join @lib, 'v1.0.0', 'fozzie')
+ subject.versions(:fozzie).should == []
+ end
+
+ it "should an ordered list of all versions loadable as puppet/string/v{version}/{name}/*.rb" do
+ %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version|
+ FileUtils.mkdir_p(File.join @lib, "v#{version}", "fozzie")
+ FileUtils.touch(File.join @lib, "v#{version}", 'fozzie', 'action.rb')
+ end
+ subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ]
+ end
+ end
+
+ describe "::validate_version" do
+ it 'should permit three number versions' do
+ subject.validate_version('10.10.10').should == true
+ end
+
+ it 'should permit versions with appended descriptions' do
+ subject.validate_version('10.10.10beta').should == true
+ end
+
+ it 'should not permit versions with more than three numbers' do
+ subject.validate_version('1.2.3.4').should == false
+ end
+
+ it 'should not permit versions with only two numbers' do
+ subject.validate_version('10.10').should == false
+ end
+
+ it 'should not permit versions with only one number' do
+ subject.validate_version('123').should == false
+ end
+
+ it 'should not permit versions with text in any position but at the end' do
+ subject.validate_version('v1.1.1').should == false
+ end
+ end
+
+ describe "::compare_versions" do
+ # (a <=> b) should be:
+ # -1 if a < b
+ # 0 if a == b
+ # 1 if a > b
+ it 'should sort major version numbers numerically' do
+ subject.compare_versions('1.0.0', '2.0.0').should == -1
+ subject.compare_versions('2.0.0', '1.1.1').should == 1
+ subject.compare_versions('2.0.0', '10.0.0').should == -1
+ end
+
+ it 'should sort minor version numbers numerically' do
+ subject.compare_versions('0.1.0', '0.2.0').should == -1
+ subject.compare_versions('0.2.0', '0.1.1').should == 1
+ subject.compare_versions('0.2.0', '0.10.0').should == -1
+ end
+
+ it 'should sort tiny version numbers numerically' do
+ subject.compare_versions('0.0.1', '0.0.2').should == -1
+ subject.compare_versions('0.0.2', '0.0.1').should == 1
+ subject.compare_versions('0.0.2', '0.0.10').should == -1
+ end
+
+ it 'should sort major version before minor version' do
+ subject.compare_versions('1.1.0', '1.2.0').should == -1
+ subject.compare_versions('1.2.0', '1.1.1').should == 1
+ subject.compare_versions('1.2.0', '1.10.0').should == -1
+
+ subject.compare_versions('1.1.0', '2.2.0').should == -1
+ subject.compare_versions('2.2.0', '1.1.1').should == 1
+ subject.compare_versions('2.2.0', '1.10.0').should == 1
+ end
+
+ it 'should sort minor version before tiny version' do
+ subject.compare_versions('0.1.1', '0.1.2').should == -1
+ subject.compare_versions('0.1.2', '0.1.1').should == 1
+ subject.compare_versions('0.1.2', '0.1.10').should == -1
+
+ subject.compare_versions('0.1.1', '0.2.2').should == -1
+ subject.compare_versions('0.2.2', '0.1.1').should == 1
+ subject.compare_versions('0.2.2', '0.1.10').should == 1
+ end
+
+ it 'should sort appended strings asciibetically' do
+ subject.compare_versions('0.0.0a', '0.0.0b').should == -1
+ subject.compare_versions('0.0.0beta1', '0.0.0beta2').should == -1
+ subject.compare_versions('0.0.0beta1', '0.0.0rc1').should == -1
+ subject.compare_versions('0.0.0beta1', '0.0.0alpha1').should == 1
+ subject.compare_versions('0.0.0beta1', '0.0.0beta1').should == 0
+ end
+
+ it "should sort appended strings before 'whole' versions" do
+ subject.compare_versions('0.0.1a', '0.0.1').should == -1
+ subject.compare_versions('0.0.1', '0.0.1beta').should == 1
+ end
+ end
+
+ describe "::[]" do
+ before :each do
+ subject.instance_variable_get("@strings")[:foo]['0.0.1'] = 10
+ end
+
+ before :each do
+ @dir = Dir.mktmpdir
+ @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'string')
+ $LOAD_PATH.push(@dir)
+ end
+
+ after :each do
+ FileUtils.remove_entry_secure @dir
+ $LOAD_PATH.pop
+ end
+
+ it "should return the string with the given name" do
+ subject["foo", '0.0.1'].should == 10
+ end
+
+ it "should attempt to load the string if it isn't found" do
+ subject.expects(:require).with('puppet/string/v0.0.1/bar')
+ subject["bar", '0.0.1']
+ end
+
+ it "should attempt to load the string with the greatest version for specified version :latest" do
+ %w[ 1.2.1 1.2.2 ].each do |version|
+ FileUtils.mkdir_p(File.join @lib, "v#{version}")
+ FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb')
+ end
+ subject.expects(:require).with('puppet/string/v1.2.2/fozzie')
+ subject['fozzie', :latest]
+ end
+ end
+
+ describe "::string?" do
+ before :each do
+ subject.instance_variable_get("@strings")[:foo]['0.0.1'] = 10
+ end
+
+ it "should return true if the string specified is registered" do
+ subject.string?("foo", '0.0.1').should == true
+ end
+
+ it "should attempt to require the string if it is not registered" do
+ subject.expects(:require).with('puppet/string/v0.0.1/bar')
+ subject.string?("bar", '0.0.1')
+ end
+
+ it "should return true if requiring the string registered it" do
+ subject.stubs(:require).with do
+ subject.instance_variable_get("@strings")[:bar]['0.0.1'] = 20
+ end
+ subject.string?("bar", '0.0.1').should == true
+ end
+
+ it "should return false if the string is not registered" do
+ subject.stubs(:require).returns(true)
+ subject.string?("bar", '0.0.1').should == false
+ end
+
+ it "should return false if there is a LoadError requiring the string" do
+ subject.stubs(:require).raises(LoadError)
+ subject.string?("bar", '0.0.1').should == false
+ end
+ end
+
+ describe "::register" do
+ it "should store the string by name" do
+ string = Puppet::String.new(:my_string, '0.0.1')
+ subject.register(string)
+ subject.instance_variable_get("@strings").should == {:my_string => {'0.0.1' => string}}
+ end
+ end
+
+ describe "::underscorize" do
+ faulty = [1, "#foo", "$bar", "sturm und drang", :"sturm und drang"]
+ valid = {
+ "Foo" => :foo,
+ :Foo => :foo,
+ "foo_bar" => :foo_bar,
+ :foo_bar => :foo_bar,
+ "foo-bar" => :foo_bar,
+ :"foo-bar" => :foo_bar,
+ }
+
+ valid.each do |input, expect|
+ it "should map #{input.inspect} to #{expect.inspect}" do
+ result = subject.underscorize(input)
+ result.should == expect
+ end
+ end
+
+ faulty.each do |input|
+ it "should fail when presented with #{input.inspect} (#{input.class})" do
+ expect { subject.underscorize(input) }.
+ should raise_error ArgumentError, /not a valid string name/
+ end
+ end
+ end
+end