]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Convert tests to use plain rspec-puppet
authorDavid Schmitt <david.schmitt@puppetlabs.com>
Mon, 1 Jun 2015 11:21:59 +0000 (12:21 +0100)
committerDavid Schmitt <david.schmitt@puppetlabs.com>
Mon, 1 Jun 2015 17:02:22 +0000 (18:02 +0100)
Tests in the new style produces the following documentation output:

abs
  should not eq nil
  should run abs() and raise an Puppet::ParseError
  should run abs(-34) and return 34
  should run abs("-34") and return 34
  should run abs(34) and return 34
  should run abs("34") and return 34

115 files changed:
Gemfile
Rakefile
lib/puppet/parser/functions/member.rb
spec/acceptance/anchor_spec.rb [new file with mode: 0755]
spec/classes/anchor_spec.rb [deleted file]
spec/functions/abs_spec.rb
spec/functions/any2array_spec.rb
spec/functions/assert_private_spec.rb
spec/functions/base64_spec.rb
spec/functions/basename_spec.rb
spec/functions/bool2num_spec.rb
spec/functions/bool2str_spec.rb
spec/functions/camelcase_spec.rb
spec/functions/capitalize_spec.rb
spec/functions/ceiling_spec.rb
spec/functions/chomp_spec.rb
spec/functions/chop_spec.rb
spec/functions/concat_spec.rb
spec/functions/count_spec.rb
spec/functions/deep_merge_spec.rb
spec/functions/defined_with_params_spec.rb
spec/functions/delete_at_spec.rb
spec/functions/delete_spec.rb
spec/functions/delete_undef_values_spec.rb
spec/functions/delete_values_spec.rb
spec/functions/difference_spec.rb
spec/functions/dirname_spec.rb
spec/functions/downcase_spec.rb
spec/functions/empty_spec.rb
spec/functions/ensure_packages_spec.rb
spec/functions/ensure_resource_spec.rb
spec/functions/flatten_spec.rb
spec/functions/floor_spec.rb
spec/functions/fqdn_rand_string_spec.rb
spec/functions/fqdn_rotate_spec.rb
spec/functions/get_module_path_spec.rb
spec/functions/getparam_spec.rb
spec/functions/getvar_spec.rb
spec/functions/grep_spec.rb
spec/functions/has_interface_with_spec.rb
spec/functions/has_ip_address_spec.rb
spec/functions/has_ip_network_spec.rb
spec/functions/has_key_spec.rb
spec/functions/hash_spec.rb
spec/functions/intersection_spec.rb
spec/functions/is_array_spec.rb
spec/functions/is_bool_spec.rb
spec/functions/is_domain_name_spec.rb
spec/functions/is_float_spec.rb
spec/functions/is_function_available.rb
spec/functions/is_hash_spec.rb
spec/functions/is_integer_spec.rb
spec/functions/is_ip_address_spec.rb
spec/functions/is_mac_address_spec.rb
spec/functions/is_numeric_spec.rb
spec/functions/is_string_spec.rb
spec/functions/join_keys_to_values_spec.rb
spec/functions/join_spec.rb
spec/functions/keys_spec.rb
spec/functions/loadyaml_spec.rb
spec/functions/lstrip_spec.rb
spec/functions/max_spec.rb
spec/functions/member_spec.rb
spec/functions/merge_spec.rb
spec/functions/min_spec.rb
spec/functions/num2bool_spec.rb
spec/functions/parsejson_spec.rb
spec/functions/parseyaml_spec.rb
spec/functions/pick_default_spec.rb
spec/functions/pick_spec.rb
spec/functions/prefix_spec.rb
spec/functions/private_spec.rb
spec/functions/pw_hash_spec.rb
spec/functions/range_spec.rb
spec/functions/reject_spec.rb
spec/functions/reverse_spec.rb
spec/functions/rstrip_spec.rb
spec/functions/shuffle_spec.rb
spec/functions/size_spec.rb
spec/functions/sort_spec.rb
spec/functions/squeeze_spec.rb
spec/functions/str2bool_spec.rb
spec/functions/str2saltedsha512_spec.rb
spec/functions/strftime_spec.rb
spec/functions/strip_spec.rb
spec/functions/suffix_spec.rb
spec/functions/swapcase_spec.rb
spec/functions/time_spec.rb
spec/functions/to_bytes_spec.rb
spec/functions/type3x_spec.rb
spec/functions/type_of_spec.rb
spec/functions/type_spec.rb
spec/functions/union_spec.rb
spec/functions/unique_spec.rb
spec/functions/upcase_spec.rb
spec/functions/uriescape_spec.rb
spec/functions/validate_absolute_path_spec.rb
spec/functions/validate_array_spec.rb
spec/functions/validate_augeas_spec.rb
spec/functions/validate_bool_spec.rb
spec/functions/validate_cmd_spec.rb
spec/functions/validate_hash_spec.rb
spec/functions/validate_integer_spec.rb
spec/functions/validate_ipv4_address_spec.rb
spec/functions/validate_ipv6_address_spec.rb
spec/functions/validate_numeric_spec.rb
spec/functions/validate_re_spec.rb
spec/functions/validate_slength_spec.rb
spec/functions/validate_string_spec.rb
spec/functions/values_at_spec.rb
spec/functions/values_spec.rb
spec/functions/zip_spec.rb
spec/puppetlabs_spec_helper_clone.rb [new file with mode: 0644]
spec/spec_helper.rb
spec/unit/facter/util/puppet_settings_spec.rb

diff --git a/Gemfile b/Gemfile
index 9d38f35e7d90d9a863e76bfee9a53c350493aff5..e35a72984fef948bfe8d18bb0c2e45894a74b0e6 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -15,6 +15,7 @@ group :development, :unit_tests do
   gem 'rspec', '~> 3.1.0',       :require => false
   gem 'rspec-puppet',            :require => false
   gem 'mocha',                   :require => false
+  # keep for its rake task for now
   gem 'puppetlabs_spec_helper',  :require => false
   gem 'puppet-lint',             :require => false
   gem 'metadata-json-lint',      :require => false
index 13ee3d9b79e1c4e66849b08645c101e5009750b2..e136b8e41f195a89f40646e92d4728016cca7dd1 100644 (file)
--- a/Rakefile
+++ b/Rakefile
@@ -1,4 +1,5 @@
 require 'rubygems'
+# keep for compatibility for now
 require 'puppetlabs_spec_helper/rake_tasks'
 require 'puppet-lint/tasks/puppet-lint'
 PuppetLint.configuration.send('disable_80chars')
index 88609ce5fd95ac61128a9cd7ba317080c497712d..1e5b3def943c4c16f3590f3a2ac716c3b64c00dd 100644 (file)
@@ -44,7 +44,7 @@ would return: false
     end
 
     if arguments[1].is_a? String or arguments[1].is_a? Fixnum
-      item = Array(arguments[1])
+      item = [arguments[1]]
     else
       item = arguments[1]
     end
diff --git a/spec/acceptance/anchor_spec.rb b/spec/acceptance/anchor_spec.rb
new file mode 100755 (executable)
index 0000000..5bc2bbb
--- /dev/null
@@ -0,0 +1,26 @@
+require 'spec_helper_acceptance'
+
+describe 'anchor type', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
+  describe 'success' do
+    it 'should effect proper chaining of resources' do
+      pp = <<-EOS
+      class anchored {
+        anchor { 'anchored::begin': }
+        ~> anchor { 'anchored::end': }
+      }
+
+      class anchorrefresh {
+        notify { 'first': }
+        ~> class { 'anchored': }
+        ~> anchor { 'final': }
+      }
+
+      include anchorrefresh
+      EOS
+
+      apply_manifest(pp, :catch_failures => true) do |r|
+        expect(r.stdout).to match(/Anchor\[final\]: Triggered 'refresh'/)
+      end
+    end
+  end
+end
diff --git a/spec/classes/anchor_spec.rb b/spec/classes/anchor_spec.rb
deleted file mode 100755 (executable)
index 2d4455e..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-#! /usr/bin/env ruby -S rspec
-require 'spec_helper'
-require 'puppet_spec/compiler'
-
-describe "anchorrefresh" do
-  include PuppetSpec::Compiler
-
-  let :transaction do
-    apply_compiled_manifest(<<-ANCHORCLASS)
-      class anchored {
-        anchor { 'anchored::begin': }
-        ~> anchor { 'anchored::end': }
-      }
-
-      class anchorrefresh {
-        notify { 'first': }
-        ~> class { 'anchored': }
-        ~> anchor { 'final': }
-      }
-
-      include anchorrefresh
-    ANCHORCLASS
-  end
-
-  it 'propagates events through the anchored class' do
-    resource = transaction.resource_status('Anchor[final]')
-
-    expect(resource.restarted).to eq(true)
-  end
-end
index 3c25ce28f8e03572ea6f6c0442bc68a71671f9f1..7d2257b02d7104d58c7c0d963cd00fdbede691be 100755 (executable)
@@ -1,25 +1,30 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the abs function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("abs")).to eq("function_abs")
-  end
+describe 'abs' do
+  it { is_expected.not_to eq(nil) }
 
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_abs([]) }.to( raise_error(Puppet::ParseError))
+  describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it {
+      pending("Current implementation ignores parameters after the first.")
+      is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+    }
   end
 
-  it "should convert a negative number into a positive" do
-    result = scope.function_abs(["-34"])
-    expect(result).to(eq(34))
+  describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params().and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params([]).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params({}).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
   end
 
-  it "should do nothing with a positive number" do
-    result = scope.function_abs(["5678"])
-    expect(result).to(eq(5678))
-  end
+  it { is_expected.to run.with_params(-34).and_return(34) }
+  it { is_expected.to run.with_params("-34").and_return(34) }
+  it { is_expected.to run.with_params(34).and_return(34) }
+  it { is_expected.to run.with_params("34").and_return(34) }
+  it { is_expected.to run.with_params(-34.5).and_return(34.5) }
+  it { is_expected.to run.with_params("-34.5").and_return(34.5) }
+  it { is_expected.to run.with_params(34.5).and_return(34.5) }
+  it { is_expected.to run.with_params("34.5").and_return(34.5) }
 end
index 87cd04b5ee29bd7d577a7879b3360985b896a812..70121f1e354f203a943e37d3d7c9dd0796681988 100755 (executable)
@@ -1,55 +1,15 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the any2array function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("any2array")).to eq("function_any2array")
-  end
-
-  it "should return an empty array if there is less than 1 argument" do
-    result = scope.function_any2array([])
-    expect(result).to(eq([]))
-  end
-
-  it "should convert boolean true to [ true ] " do
-    result = scope.function_any2array([true])
-    expect(result).to(eq([true]))
-  end
-
-  it "should convert one object to [object]" do
-    result = scope.function_any2array(['one'])
-    expect(result).to(eq(['one']))
-  end
-
-  it "should convert multiple objects to [objects]" do
-    result = scope.function_any2array(['one', 'two'])
-    expect(result).to(eq(['one', 'two']))
-  end
-
-  it "should return empty array it was called with" do
-    result = scope.function_any2array([[]])
-    expect(result).to(eq([]))
-  end
-
-  it "should return one-member array it was called with" do
-    result = scope.function_any2array([['string']])
-    expect(result).to(eq(['string']))
-  end
-
-  it "should return multi-member array it was called with" do
-    result = scope.function_any2array([['one', 'two']])
-    expect(result).to(eq(['one', 'two']))
-  end
-
-  it "should return members of a hash it was called with" do
-    result = scope.function_any2array([{ 'key' => 'value' }])
-    expect(result).to(eq(['key', 'value']))
-  end
-
-  it "should return an empty array if it was called with an empty hash" do
-    result = scope.function_any2array([{ }])
-    expect(result).to(eq([]))
-  end
+describe "any2array" do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_return([]) }
+  it { is_expected.to run.with_params(true).and_return([true]) }
+  it { is_expected.to run.with_params('one').and_return(['one']) }
+  it { is_expected.to run.with_params('one', 'two').and_return(['one', 'two']) }
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(['one']).and_return(['one']) }
+  it { is_expected.to run.with_params(['one', 'two']).and_return(['one', 'two']) }
+  it { is_expected.to run.with_params({}).and_return([]) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key', 'value']) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key', 'value']) }
 end
index a009d28dcb09732d99f6459c53d63cb6242372d9..98f2598ea128ac758fdb7546c4d1572e1933ecdb 100755 (executable)
@@ -1,15 +1,7 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:assert_private) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  subject do
-    function_name = Puppet::Parser::Functions.function(:assert_private)
-    scope.method(function_name)
-  end
-
-  context "when called from inside module" do
+describe 'assert_private' do
+  context 'when called from inside module' do
     it "should not fail" do
       scope.expects(:lookupvar).with('module_name').returns('foo')
       scope.expects(:lookupvar).with('caller_module_name').returns('foo')
index e93fafcd0436102054c9ec9b5afdf551ce07a6e6..42512b3e9779dc79ce99d6edcb1e14bb3b587272 100755 (executable)
@@ -1,34 +1,15 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the base64 function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("base64")).to eq("function_base64")
-  end
-
-  it "should raise a ParseError if there are other than 2 arguments" do
-    expect { scope.function_base64([]) }.to(raise_error(Puppet::ParseError))
-    expect { scope.function_base64(["asdf"]) }.to(raise_error(Puppet::ParseError))
-    expect { scope.function_base64(["asdf","moo","cow"]) }.to(raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if argument 1 isn't 'encode' or 'decode'" do
-    expect { scope.function_base64(["bees","astring"]) }.to(raise_error(Puppet::ParseError, /first argument must be one of/))
-  end
-
-  it "should raise a ParseError if argument 2 isn't a string" do
-    expect { scope.function_base64(["encode",["2"]]) }.to(raise_error(Puppet::ParseError, /second argument must be a string/))
-  end
-
-  it "should encode a encoded string" do
-    result = scope.function_base64(["encode",'thestring'])
-    expect(result).to match(/\AdGhlc3RyaW5n\n\Z/)
-  end
-  it "should decode a base64 encoded string" do
-    result = scope.function_base64(["decode",'dGhlc3RyaW5n'])
-    expect(result).to eq('thestring')
-  end
+describe 'base64' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params("one").and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /first argument must be one of/) }
+  it { is_expected.to run.with_params("encode", ["two"]).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
+  it { is_expected.to run.with_params("encode", 2).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
+
+  it { is_expected.to run.with_params("encode", "thestring").and_return("dGhlc3RyaW5n\n") }
+  it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n").and_return("thestring") }
+  it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n").and_return("thestring") }
 end
index 8a2d0dc3d8e2301f4cb090f2e81e4bde7f43097a..0ea30e7a7e4e40383fca0eb968e7fb0e0bb72294 100755 (executable)
@@ -1,46 +1,13 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the basename function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    Puppet::Parser::Functions.function("basename").should == "function_basename"
-  end
-
-  it "should raise a ParseError if there is less than 1 argument" do
-    lambda { scope.function_basename([]) }.should( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if there are more than 2 arguments" do
-    lambda { scope.function_basename(['a', 'b', 'c']) }.should( raise_error(Puppet::ParseError))
-  end
-
-  it "should return basename for an absolute path" do
-    result = scope.function_basename(['/path/to/a/file.ext'])
-    result.should(eq('file.ext'))
-  end
-
-  it "should return basename for a relative path" do
-    result = scope.function_basename(['path/to/a/file.ext'])
-    result.should(eq('file.ext'))
-  end
-
-  it "should strip extention when extension specified (absolute path)" do
-    result = scope.function_basename(['/path/to/a/file.ext', '.ext'])
-    result.should(eq('file'))
-  end
-
-  it "should strip extention when extension specified (relative path)" do
-    result = scope.function_basename(['path/to/a/file.ext', '.ext'])
-    result.should(eq('file'))
-  end
-
-  it "should complain about non-string first argument" do
-    lambda { scope.function_basename([[]]) }.should( raise_error(Puppet::ParseError))
-  end
-
-  it "should complain about non-string second argument" do
-    lambda { scope.function_basename(['/path/to/a/file.ext', []]) }.should( raise_error(Puppet::ParseError))
-  end
+describe 'basename' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('/path/to/a/file.ext', []).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('/path/to/a/file.ext').and_return('file.ext') }
+  it { is_expected.to run.with_params('relative_path/to/a/file.ext').and_return('file.ext') }
+  it { is_expected.to run.with_params('/path/to/a/file.ext', '.ext').and_return('file') }
+  it { is_expected.to run.with_params('relative_path/to/a/file.ext', '.ext').and_return('file') }
 end
index 3904d7e404214fe6e04a6906031060e05fbc0af0..e5068594b706dd71e7982e07de47903ad6d03ddb 100755 (executable)
@@ -1,38 +1,14 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the bool2num function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'bool2num' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("bool2num")).to eq("function_bool2num")
+  [ true, 'true', AlsoString.new('true') ].each do |truthy|
+    it { is_expected.to run.with_params(truthy).and_return(1) }
   end
 
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_bool2num([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should convert true to 1" do
-    result = scope.function_bool2num([true])
-    expect(result).to(eq(1))
-  end
-
-  it "should convert 'true' to 1" do
-    result = scope.function_bool2num(['true'])
-    result.should(eq(1))
-  end
-
-  it "should convert 'false' to 0" do
-    result = scope.function_bool2num(['false'])
-    expect(result).to(eq(0))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new('true')
-    result = scope.function_bool2num([value])
-    result.should(eq(1))
+  [ false, 'false', AlsoString.new('false') ].each do |falsey|
+    it { is_expected.to run.with_params(falsey).and_return(0) }
   end
 end
index b8788918052f4489fa43bf10376e26331e128e8b..8d35598e80bd742919d0cd22c006c5f7c131181c 100755 (executable)
@@ -1,46 +1,11 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the bool2str function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("bool2str")).to eq("function_bool2str")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_bool2str([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should convert true to 'true'" do
-    result = scope.function_bool2str([true])
-    expect(result).to(eq('true'))
-  end
-
-  it "should convert true to a string" do
-    result = scope.function_bool2str([true])
-    expect(result.class).to(eq(String))
-  end
-
-  it "should convert false to 'false'" do
-    result = scope.function_bool2str([false])
-    expect(result).to(eq('false'))
-  end
-
-  it "should convert false to a string" do
-    result = scope.function_bool2str([false])
-    expect(result.class).to(eq(String))
-  end
-
-  it "should not accept a string" do
-    expect { scope.function_bool2str(["false"]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should not accept a nil value" do
-    expect { scope.function_bool2str([nil]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should not accept an undef" do
-    expect { scope.function_bool2str([:undef]) }.to( raise_error(Puppet::ParseError))
+describe 'bool2str' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  [ 'true', 'false', nil, :undef, ''].each do |invalid|
+    it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError) }
   end
+  it { is_expected.to run.with_params(true).and_return("true") }
+  it { is_expected.to run.with_params(false).and_return("false") }
 end
index 70382adb1c949f588460c5c1cc75e6c35a57a493..c78aa62ff4d696989a4f5b6be6f20912f35ec494 100755 (executable)
@@ -1,24 +1,17 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the camelcase function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("camelcase")).to eq("function_camelcase")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_camelcase([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should capitalize the beginning of a normal string" do
-    result = scope.function_camelcase(["abc"])
-    expect(result).to(eq("Abc"))
-  end
-
-  it "should camelcase an underscore-delimited string" do
-    result = scope.function_camelcase(["aa_bb_cc"])
-    expect(result).to(eq("AaBbCc"))
-  end
+describe 'camelcase' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params("abc").and_return("Abc") }
+  it { is_expected.to run.with_params("aa_bb_cc").and_return("AaBbCc") }
+  it { is_expected.to run.with_params("_aa__bb__cc_").and_return("AaBbCc") }
+  it { is_expected.to run.with_params("100").and_return("100") }
+  it { is_expected.to run.with_params("1_00").and_return("100") }
+  it { is_expected.to run.with_params("_").and_return("") }
+  it { is_expected.to run.with_params("").and_return("") }
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(["abc", "aa_bb_cc"]).and_return(["Abc", "AaBbCc"]) }
+  it { is_expected.to run.with_params(["abc", 1, "aa_bb_cc"]).and_return(["Abc", 1, "AaBbCc"]) }
 end
index fd0e92ba26702d2fdc1e693196250c79d0ddd60d..7ce2e1630804005dbaab8535c8590b3baf762f12 100755 (executable)
@@ -1,28 +1,15 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the capitalize function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("capitalize")).to eq("function_capitalize")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_capitalize([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should capitalize the beginning of a string" do
-    result = scope.function_capitalize(["abc"])
-    expect(result).to(eq("Abc"))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new('abc')
-    result = scope.function_capitalize([value])
-    result.should(eq('Abc'))
-  end
+describe 'capitalize' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params("one").and_return("One") }
+  it { is_expected.to run.with_params("one two").and_return("One two") }
+  it { is_expected.to run.with_params("ONE TWO").and_return("One two") }
+
+  it { is_expected.to run.with_params(AlsoString.new("one")).and_return("One") }
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(["one", "two"]).and_return(["One", "Two"]) }
+  it { is_expected.to run.with_params(["one", 1, "two"]).and_return(["One", 1, "Two"]) }
 end
index 814aa7c01135389e1d40b05810f7673e7862a81c..567426fd39756b5e74972a04b0f6978999ff3b7e 100755 (executable)
@@ -1,39 +1,13 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the ceiling function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("ceiling")).to eq("function_ceiling")
-  end
-
-  it "should raise a ParseError if there is less than 1 argument" do
-    expect { scope.function_ceiling([]) }.to( raise_error(Puppet::ParseError, /Wrong number of arguments/))
-  end
-
-  it "should should raise a ParseError if input isn't numeric (eg. String)" do
-    expect { scope.function_ceiling(["foo"]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
-  end
-
-  it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do
-    expect { scope.function_ceiling([true]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
-  end
-
-  it "should return an integer when a numeric type is passed" do
-    result = scope.function_ceiling([12.4])
-    expect(result.is_a?(Integer)).to(eq(true))
-  end
-
-  it "should return the input when an integer is passed" do
-    result = scope.function_ceiling([7])
-    expect(result).to(eq(7))
-  end
-
-  it "should return the smallest integer greater than or equal to the input" do
-    result = scope.function_ceiling([3.8])
-    expect(result).to(eq(4))
-  end
+describe 'ceiling' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params("foo").and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(34).and_return(34) }
+  it { is_expected.to run.with_params(-34).and_return(-34) }
+  it { is_expected.to run.with_params(33.1).and_return(34) }
+  it { is_expected.to run.with_params(-33.1).and_return(-33) }
 end
 
index b1e1e60f35e21249705a3294744d64cf199d4c59..6878742922664cec78e923f576f95fc8543183b6 100755 (executable)
@@ -1,28 +1,20 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the chomp function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'chomp' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params("a", "b").and_raise_error(Puppet::ParseError)
+  }
+  it { is_expected.to run.with_params("one").and_return("one") }
+  it { is_expected.to run.with_params("one\n").and_return("one") }
+  it { is_expected.to run.with_params("one\n\n").and_return("one\n") }
+  it { is_expected.to run.with_params(["one\n", "two", "three\n"]).and_return(["one", "two", "three"]) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("chomp")).to eq("function_chomp")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_chomp([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should chomp the end of a string" do
-    result = scope.function_chomp(["abc\n"])
-    expect(result).to(eq("abc"))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new("abc\n")
-    result = scope.function_chomp([value])
-    result.should(eq("abc"))
-  end
+  it { is_expected.to run.with_params(AlsoString.new("one")).and_return("one") }
+  it { is_expected.to run.with_params(AlsoString.new("one\n")).and_return("one") }
+  it { is_expected.to run.with_params(AlsoString.new("one\n\n")).and_return("one\n") }
+  it { is_expected.to run.with_params([AlsoString.new("one\n"), AlsoString.new("two"), "three\n"]).and_return(["one", "two", "three"]) }
 end
index c8a19519a98d0c159f6ac6417b81d48abbb70b1a..db7d18b8c428a7f88f9b82298c628844a571a73f 100755 (executable)
@@ -1,28 +1,20 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the chop function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'chop' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params("a", "b").and_raise_error(Puppet::ParseError)
+  }
+  it { is_expected.to run.with_params("one").and_return("on") }
+  it { is_expected.to run.with_params("one\n").and_return("one") }
+  it { is_expected.to run.with_params("one\n\n").and_return("one\n") }
+  it { is_expected.to run.with_params(["one\n", "two", "three\n"]).and_return(["one", "tw", "three"]) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("chop")).to eq("function_chop")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_chop([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should chop the end of a string" do
-    result = scope.function_chop(["asdf\n"])
-    expect(result).to(eq("asdf"))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new("abc\n")
-    result = scope.function_chop([value])
-    result.should(eq('abc'))
-  end
+  it { is_expected.to run.with_params(AlsoString.new("one")).and_return("on") }
+  it { is_expected.to run.with_params(AlsoString.new("one\n")).and_return("one") }
+  it { is_expected.to run.with_params(AlsoString.new("one\n\n")).and_return("one\n") }
+  it { is_expected.to run.with_params([AlsoString.new("one\n"), AlsoString.new("two"), "three\n"]).and_return(["one", "tw", "three"]) }
 end
index 49fa6bb3640a025cc508b8f0cb79ffd6ccc5852d..1694d5ee55fcd53cb27240f0af7e11e404047cdf 100755 (executable)
@@ -1,50 +1,24 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the concat function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should raise a ParseError if the client does not provide at least two arguments" do
-    expect { scope.function_concat([]) }.to(raise_error(Puppet::ParseError))
-    expect { scope.function_concat([[1]]) }.to(raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if the first parameter is not an array" do
-    expect { scope.function_concat([1, []])}.to(raise_error(Puppet::ParseError))
-  end
-
-  it "should not raise a ParseError if the client provides more than two arguments" do
-    expect { scope.function_concat([[1],[2],[3]]) }.not_to raise_error
-  end
-
-  it "should be able to concat an array" do
-    result = scope.function_concat([['1','2','3'],['4','5','6']])
-    expect(result).to(eq(['1','2','3','4','5','6']))
-  end
-
-  it "should be able to concat a primitive to an array" do
-    result = scope.function_concat([['1','2','3'],'4'])
-    expect(result).to(eq(['1','2','3','4']))
-  end
-
-  it "should not accidentally flatten nested arrays" do
-    result = scope.function_concat([['1','2','3'],[['4','5'],'6']])
-    expect(result).to(eq(['1','2','3',['4','5'],'6']))
-  end
+describe 'concat' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([1]).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1, [2]).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([1], [2], [3]).and_return([1, 2, 3]) }
+  it { is_expected.to run.with_params(['1','2','3'],['4','5','6']).and_return(['1','2','3','4','5','6']) }
+  it { is_expected.to run.with_params(['1','2','3'],'4').and_return(['1','2','3','4']) }
+  it { is_expected.to run.with_params(['1','2','3'],[['4','5'],'6']).and_return(['1','2','3',['4','5'],'6']) }
+  it { is_expected.to run.with_params(['1','2'],['3','4'],['5','6']).and_return(['1','2','3','4','5','6']) }
+  it { is_expected.to run.with_params(['1','2'],'3','4',['5','6']).and_return(['1','2','3','4','5','6']) }
 
   it "should leave the original array intact" do
-    array_original = ['1','2','3']
-    result = scope.function_concat([array_original,['4','5','6']])
-    array_original.should(eq(['1','2','3']))
-  end
-
-  it "should be able to concat multiple arrays" do
-    result = scope.function_concat([['1','2','3'],['4','5','6'],['7','8','9']])
-    expect(result).to(eq(['1','2','3','4','5','6','7','8','9']))
-  end
-
-  it "should be able to concat mix of primitives and arrays to a final array" do
-    result = scope.function_concat([['1','2','3'],'4',['5','6','7']])
-    expect(result).to(eq(['1','2','3','4','5','6','7']))
+    argument1 = ['1','2','3']
+    original1 = argument1.dup
+    argument2 = ['4','5','6']
+    original2 = argument2.dup
+    result = subject.call([argument1,argument2])
+    expect(argument1).to eq(original1)
+    expect(argument2).to eq(original2)
   end
 end
index f8f1d484277672cfd64fab87c0f6be3fa2ff4fa7..c8d19601cecd7c169a3018aa7c50bd090076fc1a 100755 (executable)
@@ -1,31 +1,18 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the count function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("count")).to eq("function_count")
-  end
-
-  it "should raise a ArgumentError if there is more than 2 arguments" do
-    expect { scope.function_count(['foo', 'bar', 'baz']) }.to( raise_error(ArgumentError))
-  end
-
-  it "should be able to count arrays" do
-    expect(scope.function_count([["1","2","3"]])).to(eq(3))
-  end
-
-  it "should be able to count matching elements in arrays" do
-    expect(scope.function_count([["1", "2", "2"], "2"])).to(eq(2))
-  end
-
-  it "should not count nil or empty strings" do
-    expect(scope.function_count([["foo","bar",nil,""]])).to(eq(2))
-  end
-
-  it 'does not count an undefined hash key or an out of bound array index (which are both :undef)' do
-    expect(scope.function_count([["foo",:undef,:undef]])).to eq(1)
-  end
+describe 'count' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+  it { is_expected.to run.with_params("one").and_raise_error(ArgumentError) }
+  it { is_expected.to run.with_params("one", "two").and_return(1) }
+  it {
+    pending("should actually be like this, and not like above")
+    is_expected.to run.with_params("one", "two").and_raise_error(ArgumentError)
+  }
+  it { is_expected.to run.with_params("one", "two", "three").and_raise_error(ArgumentError) }
+  it { is_expected.to run.with_params(["one", "two", "three"]).and_return(3) }
+  it { is_expected.to run.with_params(["one", "two", "two"], "two").and_return(2) }
+  it { is_expected.to run.with_params(["one", nil, "two"]).and_return(2) }
+  it { is_expected.to run.with_params(["one", "", "two"]).and_return(2) }
+  it { is_expected.to run.with_params(["one", :undef, "two"]).and_return(2) }
 end
index 7087904a8d98e270f112bc071f0dc0d3af868121..397e048cebd76ba98a08ff8aee7599f2e80236e3 100755 (executable)
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:deep_merge) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe 'when calling deep_merge from puppet' do
-    it "should not compile when no arguments are passed" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = '$x = deep_merge()'
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
-
-    it "should not compile when 1 argument is passed" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = "$my_hash={'one' => 1}\n$x = deep_merge($my_hash)"
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
+describe 'deep_merge' do
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params({}, '2').and_raise_error(Puppet::ParseError, /unexpected argument type String/) }
+  it { is_expected.to run.with_params({}, 2).and_raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) }
+  it { is_expected.to run.with_params({}, '').and_return({}) }
+  it { is_expected.to run.with_params({}, {}).and_return({}) }
+  it { is_expected.to run.with_params({}, {}, {}).and_return({}) }
+  it { is_expected.to run.with_params({}, {}, {}, {}).and_return({}) }
+  it { is_expected.to run.with_params({'key' => 'value'}, '').and_return({'key' => 'value'}) }
+  it { is_expected.to run.with_params({'key1' => 'value1'}, {'key2' => 'value2' }).and_return({'key1' => 'value1', 'key2' => 'value2'}) }
+
+  describe 'when arguments have key collisions' do
+    it 'should prefer values from the last hash' do
+      is_expected.to run \
+        .with_params(
+          {'key1' => 'value1', 'key2' => 'value2' },
+          {'key2' => 'replacement_value', 'key3' => 'value3'}) \
+        .and_return(
+          {'key1' => 'value1', 'key2' => 'replacement_value', 'key3' => 'value3'})
+    end
+    it { is_expected.to run \
+      .with_params({'key1' => 'value1'}, {'key1' => 'value2'}, {'key1' => 'value3'}) \
+      .and_return({'key1' => 'value3' })
+    }
   end
 
-  describe 'when calling deep_merge on the scope instance' do
-    it 'should require all parameters are hashes' do
-      expect { new_hash = scope.function_deep_merge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/)
-      expect { new_hash = scope.function_deep_merge([{}, 2])}.to raise_error(Puppet::ParseError, /unexpected argument type Fixnum/)
-    end
-
-    it 'should accept empty strings as puppet undef' do
-      expect { new_hash = scope.function_deep_merge([{}, ''])}.not_to raise_error
-    end
-
-    it 'should be able to deep_merge two hashes' do
-      new_hash = scope.function_deep_merge([{'one' => '1', 'two' => '1'}, {'two' => '2', 'three' => '2'}])
-      expect(new_hash['one']).to   eq('1')
-      expect(new_hash['two']).to   eq('2')
-      expect(new_hash['three']).to eq('2')
-    end
-
-    it 'should deep_merge multiple hashes' do
-      hash = scope.function_deep_merge([{'one' => 1}, {'one' => '2'}, {'one' => '3'}])
-      expect(hash['one']).to eq('3')
-    end
-
-    it 'should accept empty hashes' do
-      expect(scope.function_deep_merge([{},{},{}])).to eq({})
-    end
-
-    it 'should deep_merge subhashes' do
-      hash = scope.function_deep_merge([{'one' => 1}, {'two' => 2, 'three' => { 'four' => 4 } }])
-      expect(hash['one']).to eq(1)
-      expect(hash['two']).to eq(2)
-      expect(hash['three']).to eq({ 'four' => 4 })
-    end
-
-    it 'should append to subhashes' do
-      hash = scope.function_deep_merge([{'one' => { 'two' => 2 } }, { 'one' => { 'three' => 3 } }])
-      expect(hash['one']).to eq({ 'two' => 2, 'three' => 3 })
-    end
-
-    it 'should append to subhashes 2' do
-      hash = scope.function_deep_merge([{'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }, {'two' => 'dos', 'three' => { 'five' => 5 } }])
-      expect(hash['one']).to eq(1)
-      expect(hash['two']).to eq('dos')
-      expect(hash['three']).to eq({ 'four' => 4, 'five' => 5 })
-    end
-
-    it 'should append to subhashes 3' do
-      hash = scope.function_deep_merge([{ 'key1' => { 'a' => 1, 'b' => 2 }, 'key2' => { 'c' => 3 } }, { 'key1' => { 'b' => 99 } }])
-      expect(hash['key1']).to eq({ 'a' => 1, 'b' => 99 })
-      expect(hash['key2']).to eq({ 'c' => 3 })
-    end
-
-    it 'should not change the original hashes' do
-      hash1 = {'one' => { 'two' => 2 } }
-      hash2 = { 'one' => { 'three' => 3 } }
-      hash = scope.function_deep_merge([hash1, hash2])
-      expect(hash1).to eq({'one' => { 'two' => 2 } })
-      expect(hash2).to eq({ 'one' => { 'three' => 3 } })
-      expect(hash['one']).to eq({ 'two' => 2, 'three' => 3 })
-    end
+  describe 'when arguments have subhashes' do
+    it { is_expected.to run \
+      .with_params({'key1' => 'value1'}, {'key2' => 'value2', 'key3' => {'subkey1' => 'value4'}}) \
+      .and_return( {'key1' => 'value1', 'key2' => 'value2', 'key3' => {'subkey1' => 'value4'}})
+    }
+    it { is_expected.to run \
+      .with_params({'key1' => {'subkey1' => 'value1'}}, {'key1' => {'subkey2' => 'value2'}}) \
+      .and_return( {'key1' => {'subkey1' => 'value1', 'subkey2' => 'value2'}})
+    }
+    it { is_expected.to run \
+      .with_params({'key1' => {'subkey1' => {'subsubkey1' => 'value1'}}}, {'key1' => {'subkey1' => {'subsubkey1' => 'value2'}}}) \
+      .and_return( {'key1' => {'subkey1' => {'subsubkey1' => 'value2'}}})
+    }
+  end
 
-    it 'should not change the original hashes 2' do
-      hash1 = {'one' => { 'two' => [1,2] } }
-      hash2 = { 'one' => { 'three' => 3 } }
-      hash = scope.function_deep_merge([hash1, hash2])
-      expect(hash1).to eq({'one' => { 'two' => [1,2] } })
-      expect(hash2).to eq({ 'one' => { 'three' => 3 } })
-      expect(hash['one']).to eq({ 'two' => [1,2], 'three' => 3 })
-    end
+  it 'should not change the original hashes' do
+    argument1 = { 'key1' => 'value1' }
+    original1 = argument1.dup
+    argument2 = { 'key2' => 'value2' }
+    original2 = argument2.dup
 
-    it 'should not change the original hashes 3' do
-      hash1 = {'one' => { 'two' => [1,2, {'two' => 2} ] } }
-      hash2 = { 'one' => { 'three' => 3 } }
-      hash = scope.function_deep_merge([hash1, hash2])
-      expect(hash1).to eq({'one' => { 'two' => [1,2, {'two' => 2}] } })
-      expect(hash2).to eq({ 'one' => { 'three' => 3 } })
-      expect(hash['one']).to eq({ 'two' => [1,2, {'two' => 2} ], 'three' => 3 })
-      expect(hash['one']['two']).to eq([1,2, {'two' => 2}])
-    end
+    subject.call([argument1, argument2])
+    expect(argument1).to eq(original1)
+    expect(argument2).to eq(original2)
   end
 end
index 359030474b55baed0d83e13499b2494b2769576e..516d986dbd2279af75d9aac45da8fd02bf641ffd 100755 (executable)
@@ -1,37 +1,26 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-require 'rspec-puppet'
 describe 'defined_with_params' do
-  describe 'when a resource is not specified' do
+  describe 'when no resource is specified' do
     it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
   end
   describe 'when compared against a resource with no attributes' do
     let :pre_condition do
       'user { "dan": }'
     end
-    it do
-      is_expected.to run.with_params('User[dan]', {}).and_return(true)
-      is_expected.to run.with_params('User[bob]', {}).and_return(false)
-      is_expected.to run.with_params('User[dan]', {'foo' => 'bar'}).and_return(false)
-    end
+    it { is_expected.to run.with_params('User[dan]', {}).and_return(true) }
+    it { is_expected.to run.with_params('User[bob]', {}).and_return(false) }
+    it { is_expected.to run.with_params('User[dan]', {'foo' => 'bar'}).and_return(false) }
   end
 
   describe 'when compared against a resource with attributes' do
     let :pre_condition do
       'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}'
     end
-    it do
-      is_expected.to run.with_params('User[dan]', {}).and_return(true)
-      is_expected.to run.with_params('User[dan]', '').and_return(true)
-      is_expected.to run.with_params('User[dan]', {'ensure' => 'present'}
-                            ).and_return(true)
-      is_expected.to run.with_params('User[dan]',
-                             {'ensure' => 'present', 'managehome' => false}
-                            ).and_return(true)
-      is_expected.to run.with_params('User[dan]',
-                             {'ensure' => 'absent', 'managehome' => false}
-                            ).and_return(false)
-    end
+    it { is_expected.to run.with_params('User[dan]', {}).and_return(true) }
+    it { is_expected.to run.with_params('User[dan]', '').and_return(true) }
+    it { is_expected.to run.with_params('User[dan]', {'ensure' => 'present'}).and_return(true) }
+    it { is_expected.to run.with_params('User[dan]', {'ensure' => 'present', 'managehome' => false}).and_return(true) }
+    it { is_expected.to run.with_params('User[dan]', {'ensure' => 'absent', 'managehome' => false}).and_return(false) }
   end
 end
index 7c20aec42c96026712e335b091122edad9cc33d4..0e19472efe2c74da1207b89607ea3e5e0c219a80 100755 (executable)
@@ -1,25 +1,28 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the delete_at function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'delete_at' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 1).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(['one'], 'two').and_raise_error(Puppet::ParseError) }
+  it {
+    pending("Current implementation ignores parameters after the first two.")
+    is_expected.to run.with_params(['one'], 0, 1).and_raise_error(Puppet::ParseError)
+  }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("delete_at")).to eq("function_delete_at")
+  describe 'argument validation' do
+    it { is_expected.to run.with_params([0, 1, 2], 3).and_raise_error(Puppet::ParseError) }
   end
 
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_delete_at([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should delete an item at specified location from an array" do
-    result = scope.function_delete_at([['a','b','c'],1])
-    expect(result).to(eq(['a','c']))
-  end
+  it { is_expected.to run.with_params([0, 1, 2], 1).and_return([0, 2]) }
+  it { is_expected.to run.with_params([0, 1, 2], -1).and_return([0, 1]) }
+  it { is_expected.to run.with_params([0, 1, 2], -4).and_return([0, 1, 2]) }
 
-  it "should not change origin array passed as argument" do
-    origin_array = ['a','b','c','d']
-    result = scope.function_delete_at([origin_array, 1])
-    expect(origin_array).to(eq(['a','b','c','d']))
+  it "should leave the original array intact" do
+    argument = [1, 2, 3]
+    original = argument.dup
+    result = subject.call([argument,2])
+    expect(argument).to eq(original)
   end
 end
index c8edd78e28af9061fd7e2e4a00e94081bc85e5cb..6c4747bbde1bd13c7e12973153f8b16f6d03315e 100755 (executable)
@@ -1,61 +1,67 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the delete function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'delete' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("delete")).to eq("function_delete")
+  describe 'deleting from an array' do
+    it { is_expected.to run.with_params([], '').and_return([]) }
+    it { is_expected.to run.with_params([], 'two').and_return([]) }
+    it { is_expected.to run.with_params(['two'], 'two').and_return([]) }
+    it { is_expected.to run.with_params(['two', 'two'], 'two').and_return([]) }
+    it { is_expected.to run.with_params(['one', 'two', 'three'], 'four').and_return(['one', 'two', 'three']) }
+    it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
+    it { is_expected.to run.with_params(['two', 'one', 'two', 'three', 'two'], 'two').and_return(['one', 'three']) }
+    it { is_expected.to run.with_params(['one', 'two', 'three', 'two'], ['one', 'two']).and_return(['three']) }
   end
 
-  it "should raise a ParseError if there are fewer than 2 arguments" do
-    expect { scope.function_delete([]) }.to(raise_error(Puppet::ParseError))
+  describe 'deleting from a string' do
+    it { is_expected.to run.with_params('', '').and_return('') }
+    it { is_expected.to run.with_params('bar', '').and_return('bar') }
+    it { is_expected.to run.with_params('', 'bar').and_return('') }
+    it { is_expected.to run.with_params('bar', 'bar').and_return('') }
+    it { is_expected.to run.with_params('barbar', 'bar').and_return('') }
+    it { is_expected.to run.with_params('barfoobar', 'bar').and_return('foo') }
+    it { is_expected.to run.with_params('foobarbabarz', 'bar').and_return('foobaz') }
+    it { is_expected.to run.with_params('foobarbabarz', ['foo', 'bar']).and_return('baz') }
+    # this is so sick
+    it { is_expected.to run.with_params('barfoobar', ['barbar', 'foo']).and_return('barbar') }
+    it { is_expected.to run.with_params('barfoobar', ['foo', 'barbar']).and_return('') }
   end
 
-  it "should raise a ParseError if there are greater than 2 arguments" do
-    expect { scope.function_delete([[], 'foo', 'bar']) }.to(raise_error(Puppet::ParseError))
+  describe 'deleting from an array' do
+    it { is_expected.to run.with_params({}, '').and_return({}) }
+    it { is_expected.to run.with_params({}, 'key').and_return({}) }
+    it { is_expected.to run.with_params({'key' => 'value'}, 'key').and_return({}) }
+    it { is_expected.to run \
+      .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, 'key2') \
+      .and_return( {'key1' => 'value1', 'key3' => 'value3'})
+    }
+    it { is_expected.to run \
+      .with_params({'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}, ['key1', 'key2']) \
+      .and_return( {'key3' => 'value3'})
+    }
   end
 
-  it "should raise a TypeError if a number is passed as the first argument" do
-    expect { scope.function_delete([1, 'bar']) }.to(raise_error(TypeError))
+  it "should leave the original array intact" do
+    argument1 = ['one','two','three']
+    original1 = argument1.dup
+    result = subject.call([argument1,'two'])
+    expect(argument1).to eq(original1)
   end
-
-  it "should delete all instances of an element from an array" do
-    result = scope.function_delete([['a', 'b', 'c', 'b'], 'b'])
-    expect(result).to(eq(['a', 'c']))
-  end
-
-  it "should delete all instances of a substring from a string" do
-    result = scope.function_delete(['foobarbabarz', 'bar'])
-    expect(result).to(eq('foobaz'))
+  it "should leave the original string intact" do
+    argument1 = 'onetwothree'
+    original1 = argument1.dup
+    result = subject.call([argument1,'two'])
+    expect(argument1).to eq(original1)
   end
-
-  it "should delete a key from a hash" do
-    result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, 'b'])
-    expect(result).to(eq({'a' => 1, 'c' => 3}))
+  it "should leave the original hash intact" do
+    argument1 = {'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'}
+    original1 = argument1.dup
+    result = subject.call([argument1,'key2'])
+    expect(argument1).to eq(original1)
   end
-
-  it 'should accept an array of items to delete' do
-    result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, ['b', 'c']])
-    expect(result).to(eq({'a' => 1}))
-  end
-
-  it "should not change origin array passed as argument" do
-    origin_array = ['a', 'b', 'c', 'd']
-    result = scope.function_delete([origin_array, 'b'])
-    expect(origin_array).to(eq(['a', 'b', 'c', 'd']))
-  end
-
-  it "should not change the origin string passed as argument" do
-    origin_string = 'foobarbabarz'
-    result = scope.function_delete([origin_string, 'bar'])
-    expect(origin_string).to(eq('foobarbabarz'))
-  end
-
-  it "should not change origin hash passed as argument" do
-    origin_hash = {'a' => 1, 'b' => 2, 'c' => 3}
-    result = scope.function_delete([origin_hash, 'b'])
-    expect(origin_hash).to(eq({'a' => 1, 'b' => 2, 'c' => 3}))
-  end
-
 end
index dc679535f4125cf3345e7c135191dcbdcfe9493a..ec9fb9c23179a26870a08265d64b7179a5bf7946 100755 (executable)
@@ -1,41 +1,56 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the delete_undef_values function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("delete_undef_values")).to eq("function_delete_undef_values")
-  end
-
-  it "should raise a ParseError if there is less than 1 argument" do
-    expect { scope.function_delete_undef_values([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if the argument is not Array nor Hash" do
-    expect { scope.function_delete_undef_values(['']) }.to( raise_error(Puppet::ParseError))
-    expect { scope.function_delete_undef_values([nil]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should delete all undef items from Array and only these" do
-    result = scope.function_delete_undef_values([['a',:undef,'c','undef']])
-    expect(result).to(eq(['a','c','undef']))
-  end
-
-  it "should delete all undef items from Hash and only these" do
-    result = scope.function_delete_undef_values([{'a'=>'A','b'=>:undef,'c'=>'C','d'=>'undef'}])
-    expect(result).to(eq({'a'=>'A','c'=>'C','d'=>'undef'}))
-  end
-
-  it "should not change origin array passed as argument" do
-    origin_array = ['a',:undef,'c','undef']
-    result = scope.function_delete_undef_values([origin_array])
-    expect(origin_array).to(eq(['a',:undef,'c','undef']))
+describe 'delete_undef_values' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) }
+
+  describe 'when deleting from an array' do
+    [ :undef, '', nil ].each do |undef_value|
+      describe "when undef is represented by #{undef_value.inspect}" do
+        before do
+          pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == ''
+          pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == nil
+        end
+        it { is_expected.to run.with_params([undef_value]).and_return([]) }
+        it { is_expected.to run.with_params(['one',undef_value,'two','three']).and_return(['one','two','three']) }
+      end
+
+      it "should leave the original argument intact" do
+        argument = ['one',undef_value,'two']
+        original = argument.dup
+        result = subject.call([argument,2])
+        expect(argument).to eq(original)
+      end
+    end
+
+    it { is_expected.to run.with_params(['undef']).and_return(['undef']) }
   end
 
-  it "should not change origin hash passed as argument" do
-    origin_hash = { 'a' => 1, 'b' => :undef, 'c' => 'undef' }
-    result = scope.function_delete_undef_values([origin_hash])
-    expect(origin_hash).to(eq({ 'a' => 1, 'b' => :undef, 'c' => 'undef' }))
+  describe 'when deleting from a hash' do
+    [ :undef, '', nil ].each do |undef_value|
+      describe "when undef is represented by #{undef_value.inspect}" do
+        before do
+          pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == ''
+          pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == nil
+        end
+        it { is_expected.to run.with_params({'key' => undef_value}).and_return({}) }
+        it { is_expected.to run \
+          .with_params({'key1' => 'value1', 'undef_key' => undef_value, 'key2' => 'value2'}) \
+          .and_return({'key1' => 'value1', 'key2' => 'value2'})
+        }
+      end
+
+      it "should leave the original argument intact" do
+        argument = { 'key1' => 'value1', 'key2' => undef_value }
+        original = argument.dup
+        result = subject.call([argument,2])
+        expect(argument).to eq(original)
+      end
+    end
+
+    it { is_expected.to run.with_params({'key' => 'undef'}).and_return({'key' => 'undef'}) }
   end
 end
index 4f4d411b8442d0825cc39f37822a84b6771bb945..12907d4b770b103d6e48ec724b514ea4a355bd9b 100755 (executable)
@@ -1,36 +1,37 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the delete_values function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("delete_values")).to eq("function_delete_values")
-  end
-
-  it "should raise a ParseError if there are fewer than 2 arguments" do
-    expect { scope.function_delete_values([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if there are greater than 2 arguments" do
-    expect { scope.function_delete_values([[], 'foo', 'bar']) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a TypeError if the argument is not a hash" do
-    expect { scope.function_delete_values([1,'bar']) }.to( raise_error(TypeError))
-    expect { scope.function_delete_values(['foo','bar']) }.to( raise_error(TypeError))
-    expect { scope.function_delete_values([[],'bar']) }.to( raise_error(TypeError))
+describe 'delete_values' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
+  describe 'when the first argument is not a hash' do
+    it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) }
+    it { is_expected.to run.with_params('one', 'two').and_raise_error(TypeError) }
+    it { is_expected.to run.with_params([], 'two').and_raise_error(TypeError) }
   end
 
-  it "should delete all instances of a value from a hash" do
-    result = scope.function_delete_values([{ 'a'=>'A', 'b'=>'B', 'B'=>'C', 'd'=>'B' },'B'])
-    expect(result).to(eq({ 'a'=>'A', 'B'=>'C' }))
+  describe 'when deleting from a hash' do
+    it { is_expected.to run.with_params({}, 'value').and_return({}) }
+    it { is_expected.to run \
+      .with_params({'key1' => 'value1'}, 'non-existing value') \
+      .and_return({'key1' => 'value1'})
+    }
+    it { is_expected.to run \
+      .with_params({'key1' => 'value1', 'key2' => 'value to delete'}, 'value to delete') \
+      .and_return({'key1' => 'value1'})
+    }
+    it { is_expected.to run \
+      .with_params({'key1' => 'value1', 'key2' => 'value to delete', 'key3' => 'value to delete'}, 'value to delete') \
+      .and_return({'key1' => 'value1'})
+    }
   end
 
-  it "should not change origin hash passed as argument" do
-    origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
-    result = scope.function_delete_values([origin_hash, 2])
-    expect(origin_hash).to(eq({ 'a' => 1, 'b' => 2, 'c' => 3 }))
+  it "should leave the original argument intact" do
+    argument = { 'key1' => 'value1', 'key2' => 'value2' }
+    original = argument.dup
+    result = subject.call([argument, 'value2'])
+    expect(argument).to eq(original)
   end
-
 end
index 24b2b1bc630a721c607ec3892c955f2a35ace84b..d5e983d8f83d6cd61fa4c7562e536b0293d8b060 100755 (executable)
@@ -1,19 +1,21 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the difference function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("difference")).to eq("function_difference")
-  end
-
-  it "should raise a ParseError if there are fewer than 2 arguments" do
-    expect { scope.function_difference([]) }.to( raise_error(Puppet::ParseError) )
-  end
-
-  it "should return the difference between two arrays" do
-    result = scope.function_difference([["a","b","c"],["b","c","d"]])
-    expect(result).to(eq(["a"]))
-  end
+describe 'difference' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([], []).and_return([]) }
+  it { is_expected.to run.with_params([], ['one']).and_return([]) }
+  it { is_expected.to run.with_params(['one'], ['one']).and_return([]) }
+  it { is_expected.to run.with_params(['one'], []).and_return(['one']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['one']) }
+  it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['one']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['one']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three', 'four']).and_return(['one']) }
+  it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return(['1', '2', '3']) end
 end
index 4261144ec7ee549159c3f19eb6d515e545fbc766..46c4c35c7b42a2f84e1b5b4858a9b6122b3a44a5 100755 (executable)
@@ -1,38 +1,13 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the dirname function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("dirname")).to eq("function_dirname")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_dirname([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if there is more than 1 argument" do
-    expect { scope.function_dirname(['a', 'b']) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return dirname for an absolute path" do
-    result = scope.function_dirname(['/path/to/a/file.ext'])
-    expect(result).to(eq('/path/to/a'))
-  end
-
-  it "should return dirname for a relative path" do
-    result = scope.function_dirname(['path/to/a/file.ext'])
-    expect(result).to(eq('path/to/a'))
-  end
-
-  it "should complain about hash argument" do
-    expect { scope.function_dirname([{}]) }.to( raise_error(Puppet::ParseError))
-  end
-  it "should complain about list argument" do
-    expect { scope.function_dirname([[]]) }.to( raise_error(Puppet::ParseError))
-  end
-  it "should complain about numeric argument" do
-    expect { scope.function_dirname([2112]) }.to( raise_error(Puppet::ParseError))
-  end
+describe 'dirname' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('/path/to/a/file.ext', []).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('/path/to/a/file.ext').and_return('/path/to/a') }
+  it { is_expected.to run.with_params('relative_path/to/a/file.ext').and_return('relative_path/to/a') }
 end
index edebc44f1475360b9b172a842542a9626af495cd..c594560a0d85797c23f275d1ba8655c2f64ad46b 100755 (executable)
@@ -1,33 +1,15 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the downcase function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("downcase")).to eq("function_downcase")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_downcase([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should downcase a string" do
-    result = scope.function_downcase(["ASFD"])
-    expect(result).to(eq("asfd"))
-  end
-
-  it "should do nothing to a string that is already downcase" do
-    result = scope.function_downcase(["asdf asdf"])
-    expect(result).to(eq("asdf asdf"))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new("ASFD")
-    result = scope.function_downcase([value])
-    result.should(eq('asfd'))
-  end
+describe 'downcase' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params("abc").and_return("abc") }
+  it { is_expected.to run.with_params("Abc").and_return("abc") }
+  it { is_expected.to run.with_params("ABC").and_return("abc") }
+
+  it { is_expected.to run.with_params(AlsoString.new("ABC")).and_return("abc") }
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(["ONE", "TWO"]).and_return(["one", "two"]) }
+  it { is_expected.to run.with_params(["One", 1, "Two"]).and_return(["one", 1, "two"]) }
 end
index 6a97c4cd3ba073fab1fded6d2e3a6749a8e1ba18..94b1c6856bd71a53f52ca258321ef7900e63eac7 100755 (executable)
@@ -1,32 +1,22 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the empty function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("empty")).to eq("function_empty")
-  end
+describe 'empty' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError)
+  }
+  it { is_expected.to run.with_params('').and_return(true) }
+  it { is_expected.to run.with_params('one').and_return(false) }
 
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_empty([]) }.to( raise_error(Puppet::ParseError))
-  end
+  it { is_expected.to run.with_params(AlsoString.new('')).and_return(true) }
+  it { is_expected.to run.with_params(AlsoString.new('one')).and_return(false) }
 
-  it "should return a true for an empty string" do
-    result = scope.function_empty([''])
-    expect(result).to(eq(true))
-  end
+  it { is_expected.to run.with_params([]).and_return(true) }
+  it { is_expected.to run.with_params(['one']).and_return(false) }
 
-  it "should return a false for a non-empty string" do
-    result = scope.function_empty(['asdf'])
-    expect(result).to(eq(false))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new()
-    result = scope.function_empty([value])
-    result.should(eq(true))
-  end
+  it { is_expected.to run.with_params({}).and_return(true) }
+  it { is_expected.to run.with_params({'key' => 'value'}).and_return(false) }
 end
index 436be10bc4e83a5dc807b36a04a035fbb3068873..c824732999898281f2e36f95d4638c562181df41 100755 (executable)
@@ -1,81 +1,36 @@
-#! /usr/bin/env ruby
-
 require 'spec_helper'
-require 'rspec-puppet'
-require 'puppet_spec/compiler'
 
 describe 'ensure_packages' do
-  include PuppetSpec::Compiler
-
-  before :each do
-    Puppet::Parser::Functions.autoloader.loadall
-    Puppet::Parser::Functions.function(:ensure_packages)
-    Puppet::Parser::Functions.function(:ensure_resource)
-    Puppet::Parser::Functions.function(:defined_with_params)
-    Puppet::Parser::Functions.function(:create_resources)
-  end
-
-  let :node     do Puppet::Node.new('localhost') end
-  let :compiler do Puppet::Parser::Compiler.new(node) end
-  let :scope    do
-    if Puppet.version.to_f >= 3.0
-      Puppet::Parser::Scope.new(compiler)
-    else
-      newscope = Puppet::Parser::Scope.new
-      newscope.compiler = compiler
-      newscope.source   = Puppet::Resource::Type.new(:node, :localhost)
-      newscope
-    end
-  end
-
-  describe 'argument handling' do
-    it 'fails with no arguments' do
-      expect {
-        scope.function_ensure_packages([])
-      }.to raise_error(Puppet::ParseError, /0 for 1 or 2/)
-    end
-
-    it 'accepts an array of values' do
-      scope.function_ensure_packages([['foo']])
-    end
-
-    it 'accepts a single package name as a string' do
-      scope.function_ensure_packages(['foo'])
-    end
-  end
-
-  context 'given a catalog with puppet package => absent' do
-    let :catalog do
-      compile_to_catalog(<<-EOS
-        ensure_packages(['facter'])
-        package { puppet: ensure => absent }
-      EOS
-      )
-    end
-
-    it 'has no effect on Package[puppet]' do
-      expect(catalog.resource(:package, 'puppet')['ensure']).to eq('absent')
-    end
-  end
-
-  context 'given a clean catalog' do
-    let :catalog do
-      compile_to_catalog('ensure_packages(["facter"])')
-    end
-
-    it 'declares package resources with ensure => present' do
-      expect(catalog.resource(:package, 'facter')['ensure']).to eq('present')
-    end
-  end
-
-  context 'given a clean catalog and specified defaults' do
-    let :catalog do
-      compile_to_catalog('ensure_packages(["facter"], {"provider" => "gem"})')
-    end
-
-    it 'declares package resources with ensure => present' do
-      expect(catalog.resource(:package, 'facter')['ensure']).to eq('present')
-      expect(catalog.resource(:package, 'facter')['provider']).to eq('gem')
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it {
+    pending("should not accept numbers as arguments")
+    is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError)
+  }
+  it {
+    pending("should not accept numbers as arguments")
+    is_expected.to run.with_params(["packagename", 1]).and_raise_error(Puppet::ParseError)
+  }
+  it { is_expected.to run.with_params("packagename") }
+  it { is_expected.to run.with_params(["packagename1", "packagename2"]) }
+
+  context 'given a catalog with "package { puppet: ensure => absent }"' do
+    let(:pre_condition) { 'package { puppet: ensure => absent }' }
+
+    describe 'after running ensure_package("facter")' do
+      before { subject.call(['facter']) }
+
+      # this lambda is required due to strangeness within rspec-puppet's expectation handling
+      it { expect(lambda { catalogue }).to contain_package('puppet').with_ensure('absent') }
+      it { expect(lambda { catalogue }).to contain_package('facter').with_ensure('present') }
+    end
+
+    describe 'after running ensure_package("facter", { "provider" => "gem" })' do
+      before { subject.call(['facter', { "provider" => "gem" }]) }
+
+      # this lambda is required due to strangeness within rspec-puppet's expectation handling
+      it { expect(lambda { catalogue }).to contain_package('puppet').with_ensure('absent').without_provider() }
+      it { expect(lambda { catalogue }).to contain_package('facter').with_ensure('present').with_provider("gem") }
     end
   end
 end
index 33bcac0d1f96ce1e2f7ef1138a7e51a25920baa6..c4f2cbd0ec5e2b0eeb08fff6dea5d4ac4abc6ec8 100755 (executable)
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
-require 'rspec-puppet'
-require 'puppet_spec/compiler'
 
 describe 'ensure_resource' do
-  include PuppetSpec::Compiler
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(ArgumentError, /Must specify a type/) }
+  it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, /Must specify a title/) }
+  it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(Puppet::ParseError) }
+  it {
+    pending("should not accept numbers as arguments")
+    is_expected.to run.with_params(1,2,3).and_raise_error(Puppet::ParseError)
+  }
 
-  before :all do
-    Puppet::Parser::Functions.autoloader.loadall
-    Puppet::Parser::Functions.function(:ensure_packages)
-  end
-
-  let :node     do Puppet::Node.new('localhost') end
-  let :compiler do Puppet::Parser::Compiler.new(node) end
-  let :scope    do Puppet::Parser::Scope.new(compiler) end
+  context 'given a catalog with "user { username1: ensure => present }"' do
+    let(:pre_condition) { 'user { username1: ensure => present }' }
 
-  describe 'when a type or title is not specified' do
-    it { expect { scope.function_ensure_resource([]) }.to raise_error }
-    it { expect { scope.function_ensure_resource(['type']) }.to raise_error }
-  end
-
-  describe 'when compared against a resource with no attributes' do
-    let :catalog do
-      compile_to_catalog(<<-EOS
-        user { "dan": }
-        ensure_resource('user', 'dan', {})
-      EOS
-      )
-    end
+    describe 'after running ensure_resource("user", "username1", {})' do
+      before { subject.call(['User', 'username1', {}]) }
 
-    it 'should contain the the ensured resources' do
-      expect(catalog.resource(:user, 'dan').to_s).to eq('User[dan]')
+      # this lambda is required due to strangeness within rspec-puppet's expectation handling
+      it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
     end
-  end
 
-  describe 'works when compared against a resource with non-conflicting attributes' do
-    [
-      "ensure_resource('User', 'dan', {})",
-      "ensure_resource('User', 'dan', '')",
-      "ensure_resource('User', 'dan', {'ensure' => 'present'})",
-      "ensure_resource('User', 'dan', {'ensure' => 'present', 'managehome' => false})"
-    ].each do |ensure_resource|
-      pp = <<-EOS
-        user { "dan": ensure => present, shell => "/bin/csh", managehome => false}
-        #{ensure_resource}
-      EOS
+    describe 'after running ensure_resource("user", "username2", {})' do
+      before { subject.call(['User', 'username2', {}]) }
 
-      it { expect { compile_to_catalog(pp) }.to_not raise_error }
+      # this lambda is required due to strangeness within rspec-puppet's expectation handling
+      it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
+      it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
     end
-  end
-
-  describe 'fails when compared against a resource with conflicting attributes' do
-    pp = <<-EOS
-      user { "dan": ensure => present, shell => "/bin/csh", managehome => false}
-      ensure_resource('User', 'dan', {'ensure' => 'absent', 'managehome' => false})
-    EOS
-
-    it { expect { compile_to_catalog(pp) }.to raise_error }
-  end
 
-  describe 'when an array of new resources are passed in' do
-    let :catalog do
-      compile_to_catalog("ensure_resource('User', ['dan', 'alex'], {})")
-    end
+    describe 'after running ensure_resource("user", ["username1", "username2"], {})' do
+      before { subject.call(['User', ['username1', 'username2'], {}]) }
 
-    it 'should contain the ensured resources' do
-      expect(catalog.resource('User[dan]').to_s).to eq('User[dan]')
-      expect(catalog.resource('User[alex]').to_s).to eq('User[alex]')
+      # this lambda is required due to strangeness within rspec-puppet's expectation handling
+      it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
+      it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
     end
-  end
 
-  describe 'when an array of existing resources is compared against existing resources' do
-    pp = <<-EOS
-      user { 'dan': ensure => present; 'alex': ensure => present }
-      ensure_resource('User', ['dan', 'alex'], {})
-    EOS
+    describe 'when providing already set params' do
+      let(:params) { { 'ensure' => 'present' } }
+      before { subject.call(['User', ['username2', 'username3'], params]) }
 
-    let :catalog do
-      compile_to_catalog(pp)
+      # this lambda is required due to strangeness within rspec-puppet's expectation handling
+      it { expect(lambda { catalogue }).to contain_user('username1').with(params) }
+      it { expect(lambda { catalogue }).to contain_user('username2').with(params) }
     end
 
-    it 'should return the existing resources' do
-      expect(catalog.resource('User[dan]').to_s).to eq('User[dan]')
-      expect(catalog.resource('User[alex]').to_s).to eq('User[alex]')
-    end
-  end
-
-  describe 'works when compared against existing resources with attributes' do
-    [
-      "ensure_resource('User', ['dan', 'alex'], {})",
-      "ensure_resource('User', ['dan', 'alex'], '')",
-      "ensure_resource('User', ['dan', 'alex'], {'ensure' => 'present'})",
-    ].each do |ensure_resource|
-      pp = <<-EOS
-        user { 'dan': ensure => present; 'alex': ensure => present }
-        #{ensure_resource}
-      EOS
-
-      it { expect { compile_to_catalog(pp) }.to_not raise_error }
+    context 'when trying to add params' do
+      it { is_expected.to run \
+        .with_params('User', 'username1', { 'ensure' => 'present', 'shell' => true }) \
+        .and_raise_error(Puppet::Resource::Catalog::DuplicateResourceError, /User\[username1\] is already declared/)
+      }
     end
   end
-
-  describe 'fails when compared against existing resources with conflicting attributes' do
-    pp = <<-EOS
-      user { 'dan': ensure => present; 'alex': ensure => present }
-      ensure_resource('User', ['dan', 'alex'], {'ensure' => 'absent'})
-    EOS
-
-    it { expect { compile_to_catalog(pp) }.to raise_error }
-  end
-
 end
index de8c66d662c4da1e098d598b2adc1af77d5add9b..a4338be4d4b909ef1f2803a54e227b0947262d38 100755 (executable)
@@ -1,27 +1,14 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the flatten function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("flatten")).to eq("function_flatten")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_flatten([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if there is more than 1 argument" do
-    expect { scope.function_flatten([[], []]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should flatten a complex data structure" do
-    result = scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]])
-    expect(result).to(eq(["a","b","c","d","e","f","g"]))
-  end
-
-  it "should do nothing to a structure that is already flat" do
-    result = scope.function_flatten([["a","b","c","d"]])
-    expect(result).to(eq(["a","b","c","d"]))
-  end
+describe 'flatten' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(['one']).and_return(['one']) }
+  it { is_expected.to run.with_params([['one']]).and_return(['one']) }
+  it { is_expected.to run.with_params(["a","b","c","d","e","f","g"]).and_return(["a","b","c","d","e","f","g"]) }
+  it { is_expected.to run.with_params([["a","b",["c",["d","e"],"f","g"]]]).and_return(["a","b","c","d","e","f","g"]) }
 end
index 12a69179805bebe2c21e026949cafd2e845bf59a..608c602faa9f42771a8c8b061d2a7dcbc977ca95 100755 (executable)
@@ -1,39 +1,12 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the floor function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("floor")).to eq("function_floor")
-  end
-
-  it "should raise a ParseError if there is less than 1 argument" do
-    expect { scope.function_floor([]) }.to( raise_error(Puppet::ParseError, /Wrong number of arguments/))
-  end
-
-  it "should should raise a ParseError if input isn't numeric (eg. String)" do
-    expect { scope.function_floor(["foo"]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
-  end
-
-  it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do
-    expect { scope.function_floor([true]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
-  end
-
-  it "should return an integer when a numeric type is passed" do
-    result = scope.function_floor([12.4])
-    expect(result.is_a?(Integer)).to(eq(true))
-  end
-
-  it "should return the input when an integer is passed" do
-    result = scope.function_floor([7])
-    expect(result).to(eq(7))
-  end
-
-  it "should return the largest integer less than or equal to the input" do
-    result = scope.function_floor([3.8])
-    expect(result).to(eq(3))
-  end
+describe 'floor' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params("foo").and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params(34).and_return(34) }
+  it { is_expected.to run.with_params(-34).and_return(-34) }
+  it { is_expected.to run.with_params(33.1).and_return(33) }
+  it { is_expected.to run.with_params(-33.1).and_return(-34) }
 end
-
index 949d930c7fedc2961cfa5edb78418804fc4f678a..e4070846b031819860db31dcd60b1bc2737478d7 100644 (file)
@@ -1,51 +1,25 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the fqdn_rand_string function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("fqdn_rand_string")).to eq("function_fqdn_rand_string")
-  end
-
-  it "should raise an ArgumentError if there is less than 1 argument" do
-    expect { fqdn_rand_string() }.to( raise_error(ArgumentError, /wrong number of arguments/))
-  end
-
-  it "should raise an ArgumentError if argument 1 isn't a positive integer" do
-    expect { fqdn_rand_string(0) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
-    expect { fqdn_rand_string(-1) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
-    expect { fqdn_rand_string(0.5) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
-  end
-
-  it "provides a valid alphanumeric string when no character set is provided" do
-    length = 100
-    string = %r{\A[a-zA-Z0-9]{#{length}}\z}
-    expect(fqdn_rand_string(length).match(string)).not_to eq(nil)
-  end
-
-  it "provides a valid alphanumeric string when an undef character set is provided" do
-    length = 100
-    string = %r{\A[a-zA-Z0-9]{#{length}}\z}
-    expect(fqdn_rand_string(length, :charset => nil).match(string)).not_to eq(nil)
-  end
-
-  it "provides a valid alphanumeric string when an empty character set is provided" do
-    length = 100
-    string = %r{\A[a-zA-Z0-9]{#{length}}\z}
-    expect(fqdn_rand_string(length, :charset => '').match(string)).not_to eq(nil)
-  end
-
-  it "uses a provided character set" do
-    length = 100
-    charset = '!@#$%^&*()-_=+'
-    string = %r{\A[#{charset}]{#{length}}\z}
-    expect(fqdn_rand_string(length, :charset => charset).match(string)).not_to eq(nil)
-  end
-
-  it "provides a random string exactly as long as the given length" do
-    expect(fqdn_rand_string(10).size).to eql(10)
-  end
+describe 'fqdn_rand_string' do
+  let(:default_charset) { %r{\A[a-zA-Z0-9]{100}\z} }
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(0).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+  it { is_expected.to run.with_params(1.5).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+  it { is_expected.to run.with_params(-10).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+  it { is_expected.to run.with_params("-10").and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+  it { is_expected.to run.with_params("string").and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+  it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+  it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+  it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, /second argument must be undef or a string/) }
+  it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, /second argument must be undef or a string/) }
+  it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, /second argument must be undef or a string/) }
+  it { is_expected.to run.with_params(100).and_return(default_charset) }
+  it { is_expected.to run.with_params("100").and_return(default_charset) }
+  it { is_expected.to run.with_params(100, nil).and_return(default_charset) }
+  it { is_expected.to run.with_params(100, '').and_return(default_charset) }
+  it { is_expected.to run.with_params(100, 'a').and_return(/\Aa{100}\z/) }
+  it { is_expected.to run.with_params(100, 'ab').and_return(/\A[ab]{100}\z/) }
 
   it "provides the same 'random' value on subsequent calls for the same host" do
     expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10))
@@ -77,9 +51,9 @@ describe "the fqdn_rand_string function" do
     charset = args[:charset]
     extra = args[:extra_identifier] || []
 
-    scope = PuppetlabsSpec::PuppetInternals.scope
-    scope.stubs(:[]).with("::fqdn").returns(host)
-    scope.stubs(:lookupvar).with("::fqdn").returns(host)
+    # workaround not being able to use let(:facts) because some tests need
+    # multiple different hostnames in one context
+    scope.stubs(:lookupvar).with("::fqdn", {}).returns(host)
 
     function_args = [max]
     if args.has_key?(:charset) or !extra.empty?
index 673a8a3214b60030df2add7f66c187fa787469fa..fe54490ef6539b18e2b9665938ff129461b74f5d 100755 (executable)
@@ -1,60 +1,65 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the fqdn_rotate function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'fqdn_rotate' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError)
+  }
+  it { is_expected.to run.with_params('').and_return('') }
+  it { is_expected.to run.with_params('a').and_return('a') }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("fqdn_rotate")).to eq("function_fqdn_rotate")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_fqdn_rotate([]) }.to( raise_error(Puppet::ParseError))
-  end
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(['a']).and_return(['a']) }
 
   it "should rotate a string and the result should be the same size" do
-    scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
-    result = scope.function_fqdn_rotate(["asdf"])
-    expect(result.size).to(eq(4))
+    expect(fqdn_rotate("asdf").size).to eq(4)
   end
 
   it "should rotate a string to give the same results for one host" do
-    scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice
-    expect(scope.function_fqdn_rotate(["abcdefg"])).to eql(scope.function_fqdn_rotate(["abcdefg"]))
+    val1 = fqdn_rotate("abcdefg", :host => 'one')
+    val2 = fqdn_rotate("abcdefg", :host => 'one')
+    expect(val1).to eq(val2)
   end
 
   it "should rotate a string to give different values on different hosts" do
-     scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
-     val1 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"])
-     scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.2")
-     val2 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"])
-     expect(val1).not_to eql(val2)
+    val1 = fqdn_rotate("abcdefg", :host => 'one')
+    val2 = fqdn_rotate("abcdefg", :host => 'two')
+    expect(val1).not_to eq(val2)
   end
 
   it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
-    value = AlsoString.new("asdf")
-    result = scope.function_fqdn_rotate([value])
-    result.size.should(eq(4))
+    result = fqdn_rotate(AlsoString.new('asdf'))
+    expect(result).to eq('dfas')
   end
 
-  it "should use the Puppet::Util.deterministic_rand function if available" do
-    scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
+  it "should use the Puppet::Util.deterministic_rand function" do
     if Puppet::Util.respond_to?(:deterministic_rand)
       Puppet::Util.expects(:deterministic_rand).with(113646079810780526294648115052177588845,4)
+      fqdn_rotate("asdf")
+    else
+      skip 'Puppet::Util#deterministic_rand not available'
     end
-    scope.function_fqdn_rotate(["asdf"])
   end
 
   it "should not leave the global seed in a deterministic state" do
-    scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice
-    scope.function_fqdn_rotate(["asdf"])
+    fqdn_rotate("asdf")
     rand1 = rand()
-    scope.function_fqdn_rotate(["asdf"])
+    fqdn_rotate("asdf")
     rand2 = rand()
     expect(rand1).not_to eql(rand2)
   end
+
+  def fqdn_rotate(value, args = {})
+    host = args[:host] || '127.0.0.1'
+
+    # workaround not being able to use let(:facts) because some tests need
+    # multiple different hostnames in one context
+    scope.stubs(:lookupvar).with("::fqdn").returns(host)
+
+    scope.function_fqdn_rotate([value])
+  end
 end
index 38ce64597e1177dcb588c83f336fa23ec4cc7086..b1f682fdb47bdd87109439f9251ec2c22e15353c 100755 (executable)
@@ -1,8 +1,16 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:get_module_path) do
-  Internals = PuppetlabsSpec::PuppetInternals
+describe 'get_module_path' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) }
+  it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) }
+  it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) }
+  if Puppet.version.to_f >= 4.0
+    it { is_expected.to run.with_params('one').and_raise_error(Puppet::Environments::EnvironmentNotFound, /Could not find a directory environment/) }
+  else
+    it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Could not find module/) }
+  end
+
   class StubModule
     attr_reader :path
     def initialize(path)
@@ -10,37 +18,35 @@ describe Puppet::Parser::Functions.function(:get_module_path) do
     end
   end
 
-  def scope(environment = "production")
-    Internals.scope(:compiler => Internals.compiler(:node => Internals.node(:environment => environment)))
-  end
-
-  it 'should only allow one argument' do
-    expect { scope.function_get_module_path([]) }.to raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
-    expect { scope.function_get_module_path(['1','2','3']) }.to raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
-  end
-  it 'should raise an exception when the module cannot be found' do
-    expect { scope.function_get_module_path(['foo']) }.to raise_error(Puppet::ParseError, /Could not find module/)
-  end
   describe 'when locating a module' do
     let(:modulepath) { "/tmp/does_not_exist" }
     let(:path_of_module_foo) { StubModule.new("/tmp/does_not_exist/foo") }
 
     before(:each) { Puppet[:modulepath] = modulepath }
 
-    it 'should be able to find module paths from the modulepath setting' do
-      Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo)
-      expect(scope.function_get_module_path(['foo'])).to eq(path_of_module_foo.path)
-    end
-    it 'should be able to find module paths when the modulepath is a list' do
-      Puppet[:modulepath] = modulepath + ":/tmp"
-      Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo)
-      expect(scope.function_get_module_path(['foo'])).to eq(path_of_module_foo.path)
+    context 'in the default environment' do
+      before(:each) { Puppet::Module.expects(:find).with('foo', 'rp_env').returns(path_of_module_foo) }
+
+      it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) }
+
+      context 'when the modulepath is a list' do
+        before(:each) { Puppet[:modulepath] = modulepath + 'tmp/something_else' }
+
+        it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) }
+      end
     end
-    it 'should respect the environment' do
-      skip("Disabled on Puppet 2.6.x") if Puppet.version =~ /^2\.6\b/
-      Puppet.settings[:environment] = 'danstestenv'
-      Puppet::Module.expects(:find).with('foo', 'danstestenv').returns(path_of_module_foo)
-      expect(scope('danstestenv').function_get_module_path(['foo'])).to eq(path_of_module_foo.path)
+
+    context 'in a non-default default environment' do
+      let(:environment) { 'test' }
+      before(:each) { Puppet::Module.expects(:find).with('foo', 'test').returns(path_of_module_foo) }
+
+      it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) }
+
+      context 'when the modulepath is a list' do
+        before(:each) { Puppet[:modulepath] = modulepath + 'tmp/something_else' }
+
+        it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) }
+      end
     end
   end
 end
index 833c4d4fb98ea31863b03704c4a1b76f2c56f6b9..9e3d9e470540ae44e02e394780d5a1d00f361df9 100755 (executable)
@@ -1,76 +1,30 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
-require 'rspec-puppet'
-require 'puppet_spec/compiler'
 
 describe 'getparam' do
-  include PuppetSpec::Compiler
-
-  before :each do
-    Puppet::Parser::Functions.autoloader.loadall
-    Puppet::Parser::Functions.function(:getparam)
-  end
-
-  let :node     do Puppet::Node.new('localhost') end
-  let :compiler do Puppet::Parser::Compiler.new(node) end
-  if Puppet.version.to_f >= 3.0
-    let :scope    do Puppet::Parser::Scope.new(compiler) end
-  else
-    let :scope    do
-      newscope = Puppet::Parser::Scope.new
-      newscope.compiler = compiler
-      newscope.source   = Puppet::Resource::Type.new(:node, :localhost)
-      newscope
-    end
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(ArgumentError, /Must specify a reference/) }
+  it { is_expected.to run.with_params('User[one]').and_raise_error(ArgumentError, /Must specify name of a parameter/) }
+  it { is_expected.to run.with_params('User[one]', 2).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
+  it { is_expected.to run.with_params('User[one]', []).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
+  it { is_expected.to run.with_params('User[one]', {}).and_raise_error(ArgumentError, /Must specify name of a parameter/) }
+
+  describe 'when compared against a user resource with no params' do
+    let(:pre_condition) { 'user { "one": }' }
+
+    it { is_expected.to run.with_params('User[one]', 'ensure').and_return('') }
+    it { is_expected.to run.with_params('User[two]', 'ensure').and_return('') }
+    it { is_expected.to run.with_params('User[one]', 'shell').and_return('') }
   end
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("getparam")).to eq("function_getparam")
-  end
-
-  describe 'when a resource is not specified' do
-    it { expect { scope.function_getparam([]) }.to raise_error }
-    it { expect { scope.function_getparam(['User[dan]']) }.to raise_error }
-    it { expect { scope.function_getparam(['User[dan]']) }.to raise_error }
-    it { expect { scope.function_getparam(['User[dan]', {}]) }.to raise_error }
-    # This seems to be OK because we just check for a string.
-    it { expect { scope.function_getparam(['User[dan]', '']) }.to_not raise_error }
-  end
-
-  describe 'when compared against a resource with no params' do
-    let :catalog do
-      compile_to_catalog(<<-EOS
-        user { "dan": }
-      EOS
-      )
-    end
-
-    it do
-      expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('')
-    end
-  end
-
-  describe 'when compared against a resource with params' do
-    let :catalog do
-      compile_to_catalog(<<-EOS
-        user { 'dan': ensure => present, shell => '/bin/sh', managehome => false}
-        $test = getparam(User[dan], 'shell')
-      EOS
-      )
-    end
-
-    it do
-      resource = Puppet::Parser::Resource.new(:user, 'dan', {:scope => scope})
-      resource.set_parameter('ensure', 'present')
-      resource.set_parameter('shell', '/bin/sh')
-      resource.set_parameter('managehome', false)
-      compiler.add_resource(scope, resource)
+  describe 'when compared against a user resource with params' do
+    let(:pre_condition) { 'user { "one": ensure => present, shell => "/bin/sh", managehome => false, }' }
 
-      expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('/bin/sh')
-      expect(scope.function_getparam(['User[dan]', ''])).to eq('')
-      expect(scope.function_getparam(['User[dan]', 'ensure'])).to eq('present')
-      # TODO: Expected this to be false, figure out why we're getting '' back.
-      expect(scope.function_getparam(['User[dan]', 'managehome'])).to eq('')
-    end
+    it { is_expected.to run.with_params('User[one]', 'ensure').and_return('present') }
+    it { is_expected.to run.with_params('User[two]', 'ensure').and_return('') }
+    it { is_expected.to run.with_params('User[one]', 'shell').and_return('/bin/sh') }
+    it {
+      pending("both rspec-puppet as well as the function do the wrong thing here.")
+      is_expected.to run.with_params('User[one]', 'managehome').and_return(false)
+    }
   end
 end
index 87ab9b5ac55d91c1eaa319f53b5dc6af71b1be4f..37125d1a4fb6d73e8c76d057adfc02ffe3e6e9d5 100755 (executable)
@@ -1,37 +1,28 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:getvar) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-  describe 'when calling getvar from puppet' do
+describe 'getvar' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
-    it "should not compile when no arguments are passed" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = '$foo = getvar()'
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
+  context 'given variables in namespaces' do
+    let(:pre_condition) {
+      <<-'ENDofPUPPETcode'
+      class site::data { $foo = 'baz' }
+      include site::data
+      ENDofPUPPETcode
+    }
 
-    it "should not compile when too many arguments are passed" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = '$foo = getvar("foo::bar", "baz")'
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+    it { is_expected.to run.with_params('site::data::foo').and_return('baz') }
+    it { is_expected.to run.with_params('::site::data::foo').and_return('baz') }
+
+    context 'with strict variable checking', :if => RSpec.configuration.strict_variables do
+      it { is_expected.to run.with_params('::site::data::bar').and_raise_error(ArgumentError, /undefined_variable/) }
     end
 
-    it "should lookup variables in other namespaces" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        class site::data { $foo = 'baz' }
-        include site::data
-        $foo = getvar("site::data::foo")
-        if $foo != 'baz' {
-          fail('getvar did not return what we expect')
-        }
-      ENDofPUPPETcode
-      scope.compiler.compile
+    context 'without strict variable checking', :unless => RSpec.configuration.strict_variables do
+      it { is_expected.to run.with_params('::site::data::bar').and_return(nil) }
     end
   end
 end
index 9c671dd8609fa73eda5ee8d8451adc3a9eb07ea9..6e0bd6eb94ac27aed98887af8702d280f75a67b1 100755 (executable)
@@ -1,19 +1,19 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the grep function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("grep")).to eq("function_grep")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_grep([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should grep contents from an array" do
-    result = scope.function_grep([["aaabbb","bbbccc","dddeee"], "bbb"])
-    expect(result).to(eq(["aaabbb","bbbccc"]))
-  end
+describe 'grep' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("grep does not actually check this, and raises NoMethodError instead")
+    is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /first argument not an array/)
+  }
+  it {
+    pending("grep does not actually check this, and raises NoMethodError instead")
+    is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, /first argument not an array/)
+  }
+  it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params([], 'two').and_return([]) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['two']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], 't(wo|hree)').and_return(['two', 'three']) }
 end
index 23e09a955291c5e21ccdffab9772286e3d41da05..7334d38b944f5f39f66cdc805950c8897a01aecc 100755 (executable)
@@ -1,64 +1,38 @@
-#!/usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:has_interface_with) do
-
-  let(:scope) do
-    PuppetlabsSpec::PuppetInternals.scope
-  end
-
-  # The subject of these examples is the method itself.
-  subject do
-    function_name = Puppet::Parser::Functions.function(:has_interface_with)
-    scope.method(function_name)
-  end
+describe 'has_interface_with' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
   # We need to mock out the Facts so we can specify how we expect this function
   # to behave on different platforms.
   context "On Mac OS X Systems" do
-    before :each do
-      scope.stubs(:lookupvar).with("interfaces").returns('lo0,gif0,stf0,en1,p2p0,fw0,en0,vmnet1,vmnet8,utun0')
-    end
-    it 'should have loopback (lo0)' do
-      expect(subject.call(['lo0'])).to be_truthy
-    end
-    it 'should not have loopback (lo)' do
-      expect(subject.call(['lo'])).to be_falsey
-    end
+    let(:facts) { { :interfaces => 'lo0,gif0,stf0,en1,p2p0,fw0,en0,vmnet1,vmnet8,utun0' } }
+    it { is_expected.to run.with_params('lo0').and_return(true) }
+    it { is_expected.to run.with_params('lo').and_return(false) }
   end
+
   context "On Linux Systems" do
-    before :each do
-      scope.stubs(:lookupvar).with("interfaces").returns('eth0,lo')
-      scope.stubs(:lookupvar).with("ipaddress").returns('10.0.0.1')
-      scope.stubs(:lookupvar).with("ipaddress_lo").returns('127.0.0.1')
-      scope.stubs(:lookupvar).with("ipaddress_eth0").returns('10.0.0.1')
-      scope.stubs(:lookupvar).with('muppet').returns('kermit')
-      scope.stubs(:lookupvar).with('muppet_lo').returns('mspiggy')
-      scope.stubs(:lookupvar).with('muppet_eth0').returns('kermit')
-    end
-    it 'should have loopback (lo)' do
-      expect(subject.call(['lo'])).to be_truthy
-    end
-    it 'should not have loopback (lo0)' do
-      expect(subject.call(['lo0'])).to be_falsey
-    end
-    it 'should have ipaddress with 127.0.0.1' do
-      expect(subject.call(['ipaddress', '127.0.0.1'])).to be_truthy
-    end
-    it 'should have ipaddress with 10.0.0.1' do
-      expect(subject.call(['ipaddress', '10.0.0.1'])).to be_truthy
-    end
-    it 'should not have ipaddress with 10.0.0.2' do
-      expect(subject.call(['ipaddress', '10.0.0.2'])).to be_falsey
-    end
-    it 'should have muppet named kermit' do
-      expect(subject.call(['muppet', 'kermit'])).to be_truthy
-    end
-    it 'should have muppet named mspiggy' do
-      expect(subject.call(['muppet', 'mspiggy'])).to be_truthy
-    end
-    it 'should not have muppet named bigbird' do
-      expect(subject.call(['muppet', 'bigbird'])).to be_falsey
+    let(:facts) do
+      {
+        :interfaces => 'eth0,lo',
+        :ipaddress => '10.0.0.1',
+        :ipaddress_lo => '127.0.0.1',
+        :ipaddress_eth0 => '10.0.0.1',
+        :muppet => 'kermit',
+        :muppet_lo => 'mspiggy',
+        :muppet_eth0 => 'kermit',
+      }
     end
+
+    it { is_expected.to run.with_params('lo').and_return(true) }
+    it { is_expected.to run.with_params('lo0').and_return(false) }
+    it { is_expected.to run.with_params('ipaddress', '127.0.0.1').and_return(true) }
+    it { is_expected.to run.with_params('ipaddress', '10.0.0.1').and_return(true) }
+    it { is_expected.to run.with_params('ipaddress', '8.8.8.8').and_return(false) }
+    it { is_expected.to run.with_params('muppet', 'kermit').and_return(true) }
+    it { is_expected.to run.with_params('muppet', 'mspiggy').and_return(true) }
+    it { is_expected.to run.with_params('muppet', 'bigbird').and_return(false) }
   end
 end
index 0df12a7be3444c1b15942c0f3537cfc4e9792fd1..42a5a79260ed49629d5514d008697d91c8751bf0 100755 (executable)
@@ -1,39 +1,22 @@
-#!/usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:has_ip_address) do
-
-  let(:scope) do
-    PuppetlabsSpec::PuppetInternals.scope
-  end
-
-  subject do
-    function_name = Puppet::Parser::Functions.function(:has_ip_address)
-    scope.method(function_name)
-  end
+describe 'has_ip_address' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
   context "On Linux Systems" do
-    before :each do
-      scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo')
-      scope.stubs(:lookupvar).with('ipaddress').returns('10.0.2.15')
-      scope.stubs(:lookupvar).with('ipaddress_eth0').returns('10.0.2.15')
-      scope.stubs(:lookupvar).with('ipaddress_lo').returns('127.0.0.1')
-    end
-
-    it 'should have primary address (10.0.2.15)' do
-      expect(subject.call(['10.0.2.15'])).to be_truthy
+    let(:facts) do
+      {
+        :interfaces => 'eth0,lo',
+        :ipaddress => '10.0.0.1',
+        :ipaddress_lo => '127.0.0.1',
+        :ipaddress_eth0 => '10.0.0.1',
+      }
     end
 
-    it 'should have lookupback address (127.0.0.1)' do
-      expect(subject.call(['127.0.0.1'])).to be_truthy
-    end
-
-    it 'should not have other address' do
-      expect(subject.call(['192.1681.1.1'])).to be_falsey
-    end
-
-    it 'should not have "mspiggy" on an interface' do
-      expect(subject.call(['mspiggy'])).to be_falsey
-    end
+    it { is_expected.to run.with_params('127.0.0.1').and_return(true) }
+    it { is_expected.to run.with_params('10.0.0.1').and_return(true) }
+    it { is_expected.to run.with_params('8.8.8.8').and_return(false) }
   end
 end
index 2a2578e234084d36d0ed06f536cd851cc4ebb932..7b5fe66ac8c480f0d5b630584dbf737b6843bf1c 100755 (executable)
@@ -1,36 +1,22 @@
-#!/usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:has_ip_network) do
-
-  let(:scope) do
-    PuppetlabsSpec::PuppetInternals.scope
-  end
-
-  subject do
-    function_name = Puppet::Parser::Functions.function(:has_ip_network)
-    scope.method(function_name)
-  end
+describe 'has_ip_network' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
   context "On Linux Systems" do
-    before :each do
-      scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo')
-      scope.stubs(:lookupvar).with('network').returns(:undefined)
-      scope.stubs(:lookupvar).with('network_eth0').returns('10.0.2.0')
-      scope.stubs(:lookupvar).with('network_lo').returns('127.0.0.1')
-    end
-
-    it 'should have primary network (10.0.2.0)' do
-      expect(subject.call(['10.0.2.0'])).to be_truthy
-    end
-
-    it 'should have loopback network (127.0.0.0)' do
-      expect(subject.call(['127.0.0.1'])).to be_truthy
+    let(:facts) do
+      {
+        :interfaces => 'eth0,lo',
+        :network => :undefined,
+        :network_lo => '127.0.0.0',
+        :network_eth0 => '10.0.0.0',
+      }
     end
 
-    it 'should not have other network' do
-      expect(subject.call(['192.168.1.0'])).to be_falsey
-    end
+    it { is_expected.to run.with_params('127.0.0.0').and_return(true) }
+    it { is_expected.to run.with_params('10.0.0.0').and_return(true) }
+    it { is_expected.to run.with_params('8.8.8.0').and_return(false) }
   end
 end
-
index 6b718005aaab92368aa17ff7956e7c281be242d2..965d5a657e58b6d9ac79ad458d68e99187a4d4de 100755 (executable)
@@ -1,42 +1,15 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:has_key) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe 'when calling has_key from puppet' do
-    it "should not compile when no arguments are passed" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = '$x = has_key()'
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
-
-    it "should not compile when 1 argument is passed" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = "$x = has_key('foo')"
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
-
-    it "should require the first value to be a Hash" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = "$x = has_key('foo', 'bar')"
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /expects the first argument to be a hash/)
-    end
-  end
-
-  describe 'when calling the function has_key from a scope instance' do
-    it 'should detect existing keys' do
-      expect(scope.function_has_key([{'one' => 1}, 'one'])).to be_truthy
-    end
-
-    it 'should detect existing keys' do
-      expect(scope.function_has_key([{'one' => 1}, 'two'])).to be_falsey
-    end
-  end
+describe 'has_key' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params("one").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /expects the first argument to be a hash/) }
+  it { is_expected.to run.with_params(1, "two").and_raise_error(Puppet::ParseError, /expects the first argument to be a hash/) }
+  it { is_expected.to run.with_params([], "two").and_raise_error(Puppet::ParseError, /expects the first argument to be a hash/) }
+
+  it { is_expected.to run.with_params({ 'key' => 'value' }, "key").and_return(true) }
+  it { is_expected.to run.with_params({}, "key").and_return(false) }
+  it { is_expected.to run.with_params({ 'key' => 'value'}, "not a key").and_return(false) }
 end
index ec2988b028b0461dc77694009e97daa979d5b486..4fe99ceeb3a11bb8bc84dc35d24ebd959c11dc63 100755 (executable)
@@ -1,19 +1,14 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the hash function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("hash")).to eq("function_hash")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_hash([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should convert an array to a hash" do
-    result = scope.function_hash([['a',1,'b',2,'c',3]])
-    expect(result).to(eq({'a'=>1,'b'=>2,'c'=>3}))
-  end
+describe 'hash' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params(['one']).and_raise_error(Puppet::ParseError, /Unable to compute/) }
+  it { is_expected.to run.with_params([]).and_return({}) }
+  it { is_expected.to run.with_params(['key1', 'value1']).and_return({ 'key1' => 'value1' }) }
+  it { is_expected.to run.with_params(['key1', 'value1', 'key2', 'value2']).and_return({ 'key1' => 'value1', 'key2' => 'value2' }) }
 end
index 6361304fecc34ec38ba61f276adeed330db2137f..c0f608690572c77e0dfde5c730bcdfa5c704a31a 100755 (executable)
@@ -1,19 +1,21 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the intersection function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("intersection")).to eq("function_intersection")
-  end
-
-  it "should raise a ParseError if there are fewer than 2 arguments" do
-    expect { scope.function_intersection([]) }.to( raise_error(Puppet::ParseError) )
-  end
-
-  it "should return the intersection of two arrays" do
-    result = scope.function_intersection([["a","b","c"],["b","c","d"]])
-    expect(result).to(eq(["b","c"]))
-  end
+describe 'intersection' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError) }
+  it { is_expected.to run.with_params([], []).and_return([]) }
+  it { is_expected.to run.with_params([], ['one']).and_return([]) }
+  it { is_expected.to run.with_params(['one'], []).and_return([]) }
+  it { is_expected.to run.with_params(['one'], ['one']).and_return(['one']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['two', 'three']) }
+  it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['two', 'three']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['two', 'three']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three', 'four']).and_return(['two', 'three']) }
+  it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return([]) end
 end
index 94920a4cbe516b7bc095d58186c7e8c74b0e32b7..7dd21c23b9e5a2f7fabd2b70988683f6a9bb2f17 100755 (executable)
@@ -1,29 +1,19 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_array function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_array")).to eq("function_is_array")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_array([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if passed an array" do
-    result = scope.function_is_array([[1,2,3]])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if passed a hash" do
-    result = scope.function_is_array([{'a'=>1}])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if passed a string" do
-    result = scope.function_is_array(["asdf"])
-    expect(result).to(eq(false))
-  end
+describe 'is_array' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params([]).and_return(true) }
+  it { is_expected.to run.with_params(['one']).and_return(true) }
+  it { is_expected.to run.with_params([1]).and_return(true) }
+  it { is_expected.to run.with_params([{}]).and_return(true) }
+  it { is_expected.to run.with_params([[]]).and_return(true) }
+  it { is_expected.to run.with_params('').and_return(false) }
+  it { is_expected.to run.with_params('one').and_return(false) }
+  it { is_expected.to run.with_params(1).and_return(false) }
+  it { is_expected.to run.with_params({}).and_return(false) }
 end
index 4a342ba4f31c6c4243b1e22213ab666826a7a445..76d619b00bb9210e1f932917b291f3eb7f42444e 100755 (executable)
@@ -1,44 +1,15 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_bool function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_bool")).to eq("function_is_bool")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_bool([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if passed a TrueClass" do
-    result = scope.function_is_bool([true])
-    expect(result).to(eq(true))
-  end
-
-  it "should return true if passed a FalseClass" do
-    result = scope.function_is_bool([false])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if passed the string 'true'" do
-    result = scope.function_is_bool(['true'])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if passed the string 'false'" do
-    result = scope.function_is_bool(['false'])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if passed an array" do
-    result = scope.function_is_bool([["a","b"]])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if passed a hash" do
-    result = scope.function_is_bool([{"a" => "b"}])
-    expect(result).to(eq(false))
-  end
+describe 'is_bool' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(true, false).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(true).and_return(true) }
+  it { is_expected.to run.with_params(false).and_return(true) }
+  it { is_expected.to run.with_params([1]).and_return(false) }
+  it { is_expected.to run.with_params([{}]).and_return(false) }
+  it { is_expected.to run.with_params([[]]).and_return(false) }
+  it { is_expected.to run.with_params([true]).and_return(false) }
+  it { is_expected.to run.with_params('true').and_return(false) }
+  it { is_expected.to run.with_params('false').and_return(false) }
 end
index ef880612fe5c899b0fd4cbdc1bae8fedc2347093..c1bf0e34b7f1d63cdb4a8780f9c0763bdbb4ff6d 100755 (executable)
@@ -1,81 +1,43 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_domain_name function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_domain_name")).to eq("function_is_domain_name")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_domain_name([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if a valid short domain name" do
-    result = scope.function_is_domain_name(["x.com"])
-    expect(result).to(be_truthy)
-  end
-
-  it "should return true if the domain is ." do
-    result = scope.function_is_domain_name(["."])
-    expect(result).to(be_truthy)
+describe 'is_domain_name' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(1).and_return(false) }
+  it { is_expected.to run.with_params([]).and_return(false) }
+  it { is_expected.to run.with_params({}).and_return(false) }
+  it { is_expected.to run.with_params('').and_return(false) }
+  it { is_expected.to run.with_params('.').and_return(true) }
+  it { is_expected.to run.with_params('com').and_return(true) }
+  it { is_expected.to run.with_params('com.').and_return(true) }
+  it { is_expected.to run.with_params('x.com').and_return(true) }
+  it { is_expected.to run.with_params('x.com.').and_return(true) }
+  it { is_expected.to run.with_params('foo.example.com').and_return(true) }
+  it { is_expected.to run.with_params('foo.example.com.').and_return(true) }
+  it { is_expected.to run.with_params('2foo.example.com').and_return(true) }
+  it { is_expected.to run.with_params('2foo.example.com.').and_return(true) }
+  it { is_expected.to run.with_params('www.2foo.example.com').and_return(true) }
+  it { is_expected.to run.with_params('www.2foo.example.com.').and_return(true) }
+  describe 'inputs with spaces' do
+    it { is_expected.to run.with_params('invalid domain').and_return(false) }
+  end
+  describe 'inputs with hyphens' do
+    it { is_expected.to run.with_params('foo-bar.example.com').and_return(true) }
+    it { is_expected.to run.with_params('foo-bar.example.com.').and_return(true) }
+    it { is_expected.to run.with_params('www.foo-bar.example.com').and_return(true) }
+    it { is_expected.to run.with_params('www.foo-bar.example.com.').and_return(true) }
+    it { is_expected.to run.with_params('-foo.example.com').and_return(false) }
+    it { is_expected.to run.with_params('-foo.example.com').and_return(false) }
   end
-
-  it "should return true if the domain is x.com." do
-    result = scope.function_is_domain_name(["x.com."])
-    expect(result).to(be_truthy)
-  end
-
-  it "should return true if a valid domain name" do
-    result = scope.function_is_domain_name(["foo.bar.com"])
-    expect(result).to(be_truthy)
-  end
-
-  it "should allow domain parts to start with numbers" do
-    result = scope.function_is_domain_name(["3foo.2bar.com"])
-    expect(result).to(be_truthy)
-  end
-
-  it "should allow domain to end with a dot" do
-    result = scope.function_is_domain_name(["3foo.2bar.com."])
-    expect(result).to(be_truthy)
-  end
-
-  it "should allow a single part domain" do
-    result = scope.function_is_domain_name(["orange"])
-    expect(result).to(be_truthy)
-  end
-
-  it "should return false if domain parts start with hyphens" do
-    result = scope.function_is_domain_name(["-3foo.2bar.com"])
-    expect(result).to(be_falsey)
-  end
-
-  it "should return true if domain contains hyphens" do
-    result = scope.function_is_domain_name(["3foo-bar.2bar-fuzz.com"])
-    expect(result).to(be_truthy)
-  end
-
-  it "should return false if domain name contains spaces" do
-    result = scope.function_is_domain_name(["not valid"])
-    expect(result).to(be_falsey)
-  end
-
   # Values obtained from Facter values will be frozen strings
   # in newer versions of Facter:
-  it "should not throw an exception if passed a frozen string" do
-    result = scope.function_is_domain_name(["my.domain.name".freeze])
-    expect(result).to(be_truthy)
+  it { is_expected.to run.with_params('www.example.com'.freeze).and_return(true) }
+  describe 'top level domain must be alphabetic if there are multiple labels' do
+    it { is_expected.to run.with_params('2com').and_return(true) }
+    it { is_expected.to run.with_params('www.example.2com').and_return(false) }
   end
-
-  it "should return false if top-level domain is not entirely alphabetic" do
-    result = scope.function_is_domain_name(["kiwi.2bar"])
-    expect(result).to(be_falsey)
-  end
-
-  it "should return false if domain name has the dotted-decimal form, e.g. an IPv4 address" do
-    result = scope.function_is_domain_name(["192.168.1.1"])
-    expect(result).to(be_falsey)
+  describe 'IP addresses are not domain names' do
+    it { is_expected.to run.with_params('192.168.1.1').and_return(false) }
   end
 end
index d926634e83353025ba18d5b8da4cae8d0ec946bc..ffff971a7dd213098dbad6a5a3f1ae3d08ace659 100755 (executable)
@@ -1,33 +1,22 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_float function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'is_float' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(0.1, 0.2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_float")).to eq("function_is_float")
+  describe 'passing a string' do
+    it { is_expected.to run.with_params('0.1').and_return(true) }
+    it { is_expected.to run.with_params('1.0').and_return(true) }
+    it { is_expected.to run.with_params('1').and_return(false) }
+    it { is_expected.to run.with_params('one').and_return(false) }
+    it { is_expected.to run.with_params('one 1.0').and_return(false) }
+    it { is_expected.to run.with_params('1.0 one').and_return(false) }
   end
 
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_float([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if a float" do
-    result = scope.function_is_float(["0.12"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if a string" do
-    result = scope.function_is_float(["asdf"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if an integer" do
-    result = scope.function_is_float(["3"])
-    expect(result).to(eq(false))
-  end
-  it "should return true if a float is created from an arithmetical operation" do
-    result = scope.function_is_float([3.2*2])
-    expect(result).to(eq(true))
+  describe 'passing numbers' do
+    it { is_expected.to run.with_params(0.1).and_return(true) }
+    it { is_expected.to run.with_params(1.0).and_return(true) }
+    it { is_expected.to run.with_params(1).and_return(false) }
   end
 end
index 3a9aa1b2302b91d4bb12fcc87f2d759c4032b7fe..44f08c0819e7ad2dbb78e0991b5b754e2ddf0293 100755 (executable)
@@ -1,31 +1,9 @@
-#!/usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_function_available function" do
-  before :all do
-    Puppet::Parser::Functions.autoloader.loadall
-  end
-
-  before :each do
-    @scope = Puppet::Parser::Scope.new
-  end
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_function_available")).to eq("function_is_function_available")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { @scope.function_is_function_available([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return false if a nonexistent function is passed" do
-    result = @scope.function_is_function_available(['jeff_mccunes_left_sock'])
-    expect(result).to(eq(false))
-  end
-
-  it "should return true if an available function is passed" do
-    result = @scope.function_is_function_available(['require'])
-    expect(result).to(eq(true))
-  end
-
+describe 'is_function_available' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('include').and_return(true) }
+  it { is_expected.to run.with_params('no_such_function').and_return(false) }
 end
index a8494111136df11056b8ea3c8511b42f0975ba66..c2599a02a710df1dd9101f09b1d2bb7e891467c3 100755 (executable)
@@ -1,29 +1,11 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_hash function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_hash")).to eq("function_is_hash")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_hash([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if passed a hash" do
-    result = scope.function_is_hash([{"a"=>1,"b"=>2}])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if passed an array" do
-    result = scope.function_is_hash([["a","b"]])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if passed a string" do
-    result = scope.function_is_hash(["asdf"])
-    expect(result).to(eq(false))
-  end
+describe 'is_hash' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('').and_return(false) }
+  it { is_expected.to run.with_params({}).and_return(true) }
+  it { is_expected.to run.with_params([]).and_return(false) }
+  it { is_expected.to run.with_params(1).and_return(false) }
 end
index f0cbca807fe5b943098e414994f5cce8ca37dbd4..67263c18fa4ae85694372ee57646da7376637f56 100755 (executable)
@@ -1,69 +1,25 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_integer function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_integer")).to eq("function_is_integer")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_integer([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if an integer" do
-    result = scope.function_is_integer(["3"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return true if a negative integer" do
-    result = scope.function_is_integer(["-7"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if a float" do
-    result = scope.function_is_integer(["3.2"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if a string" do
-    result = scope.function_is_integer(["asdf"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return true if an integer is created from an arithmetical operation" do
-    result = scope.function_is_integer([3*2])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if an array" do
-    result = scope.function_is_numeric([["asdf"]])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if a hash" do
-    result = scope.function_is_numeric([{"asdf" => false}])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if a boolean" do
-    result = scope.function_is_numeric([true])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if a whitespace is in the string" do
-    result = scope.function_is_numeric([" -1324"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if it is zero prefixed" do
-    result = scope.function_is_numeric(["0001234"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if it is wrapped inside an array" do
-    result = scope.function_is_numeric([[1234]])
-    expect(result).to(eq(false))
-  end
+describe 'is_integer' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+  it { is_expected.to run.with_params(3).and_return(true) }
+  it { is_expected.to run.with_params('3').and_return(true) }
+  it { is_expected.to run.with_params(-3).and_return(true) }
+  it { is_expected.to run.with_params('-3').and_return(true) }
+  
+  it { is_expected.to run.with_params(3.7).and_return(false) }
+  it { is_expected.to run.with_params('3.7').and_return(false) }
+  it { is_expected.to run.with_params(-3.7).and_return(false) }
+  it { is_expected.to run.with_params('3.7').and_return(false) }
+
+  it { is_expected.to run.with_params('one').and_return(false) }
+  it { is_expected.to run.with_params([]).and_return(false) }
+  it { is_expected.to run.with_params([1]).and_return(false) }
+  it { is_expected.to run.with_params({}).and_return(false) }
+  it { is_expected.to run.with_params(true).and_return(false) }
+  it { is_expected.to run.with_params(false).and_return(false) }
+  it { is_expected.to run.with_params('0001234').and_return(false) }
 end
index c16d12b16be4f7c4834e6ddb021c3f343ceaae39..a7a383a1e21cb5a41cdee7ebd335ebc08adc20e7 100755 (executable)
@@ -1,39 +1,23 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_ip_address function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_ip_address")).to eq("function_is_ip_address")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_ip_address([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if an IPv4 address" do
-    result = scope.function_is_ip_address(["1.2.3.4"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return true if a full IPv6 address" do
-    result = scope.function_is_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return true if a compressed IPv6 address" do
-    result = scope.function_is_ip_address(["fe00::1"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if not valid" do
-    result = scope.function_is_ip_address(["asdf"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if IP octets out of range" do
-    result = scope.function_is_ip_address(["1.1.1.300"])
-    expect(result).to(eq(false))
-  end
+describe 'is_ip_address' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('1.2.3.4').and_return(true) }
+  it { is_expected.to run.with_params('1.2.3.255').and_return(true) }
+  it { is_expected.to run.with_params('1.2.3.256').and_return(false) }
+  it { is_expected.to run.with_params('1.2.3').and_return(false) }
+  it { is_expected.to run.with_params('1.2.3.4.5').and_return(false) }
+  it { is_expected.to run.with_params('fe00::1').and_return(true) }
+  it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74').and_return(true) }
+  it { is_expected.to run.with_params('FE80:0000:CD12:D123:E2F8:47FF:FE09:DD74').and_return(true) }
+  it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:zzzz').and_return(false) }
+  it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09').and_return(false) }
+  it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74:dd74').and_return(false) }
+  it { is_expected.to run.with_params('').and_return(false) }
+  it { is_expected.to run.with_params('one').and_return(false) }
+  it { is_expected.to run.with_params(1).and_return(false) }
+  it { is_expected.to run.with_params({}).and_return(false) }
+  it { is_expected.to run.with_params([]).and_return(false) }
 end
index 66edd197e450e9130b41b5e57d671a96a4771dbd..5f76a91b4bdb85091bbbe6e9feaefa1f77682a32 100755 (executable)
@@ -1,29 +1,24 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_mac_address function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_mac_address")).to eq("function_is_mac_address")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_mac_address([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if a valid mac address" do
-    result = scope.function_is_mac_address(["00:a0:1f:12:7f:a0"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if octets are out of range" do
-    result = scope.function_is_mac_address(["00:a0:1f:12:7f:g0"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if not valid" do
-    result = scope.function_is_mac_address(["not valid"])
-    expect(result).to(eq(false))
-  end
+describe 'is_mac_address' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('00:a0:1f:12:7f:a0').and_return(true) }
+  it { is_expected.to run.with_params('00:A0:1F:12:7F:A0').and_return(true) }
+  it { is_expected.to run.with_params('00:00:00:00:00:0g').and_return(false) }
+  it { is_expected.to run.with_params('').and_return(false) }
+  it { is_expected.to run.with_params('one').and_return(false) }
+  it {
+    pending "should properly typecheck its arguments"
+    is_expected.to run.with_params(1).and_return(false)
+  }
+  it {
+    pending "should properly typecheck its arguments"
+    is_expected.to run.with_params({}).and_return(false)
+  }
+  it {
+    pending "should properly typecheck its arguments"
+    is_expected.to run.with_params([]).and_return(false)
+  }
 end
index 4176961d7af2f7c4f09e13ce25c2635072994db2..d0f5a6eb646ae3e121d4d88ca8e8b3044c32b230 100755 (executable)
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_numeric function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_numeric")).to eq("function_is_numeric")
-  end
-
-  it "should raise a ParseError if there is less than 1 argument" do
-    expect { scope.function_is_numeric([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if an integer" do
-    result = scope.function_is_numeric(["3"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return true if a float" do
-    result = scope.function_is_numeric(["3.2"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return true if an integer is created from an arithmetical operation" do
-    result = scope.function_is_numeric([3*2])
-    expect(result).to(eq(true))
-  end
-
-  it "should return true if a float is created from an arithmetical operation" do
-    result = scope.function_is_numeric([3.2*2])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if a string" do
-    result = scope.function_is_numeric(["asdf"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if an array" do
-    result = scope.function_is_numeric([["asdf"]])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if an array of integers" do
-    result = scope.function_is_numeric([[1,2,3,4]])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if a hash" do
-    result = scope.function_is_numeric([{"asdf" => false}])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if a hash with numbers in it" do
-    result = scope.function_is_numeric([{1 => 2}])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if a boolean" do
-    result = scope.function_is_numeric([true])
-    expect(result).to(eq(false))
-  end
-
-  it "should return true if a negative float with exponent" do
-    result = scope.function_is_numeric(["-342.2315e-12"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if a negative integer with whitespaces before/after the dash" do
-    result = scope.function_is_numeric([" -  751"])
-    expect(result).to(eq(false))
-  end
-
-#  it "should return true if a hexadecimal" do
-#    result = scope.function_is_numeric(["0x52F8c"])
-#    result.should(eq(true))
-#  end
-#
-#  it "should return true if a hexadecimal with uppercase 0X prefix" do
-#    result = scope.function_is_numeric(["0X52F8c"])
-#    result.should(eq(true))
-#  end
-#
-#  it "should return false if a hexadecimal without a prefix" do
-#    result = scope.function_is_numeric(["52F8c"])
-#    result.should(eq(false))
-#  end
-#
-#  it "should return true if a octal" do
-#    result = scope.function_is_numeric(["0751"])
-#    result.should(eq(true))
-#  end
-#
-#  it "should return true if a negative hexadecimal" do
-#    result = scope.function_is_numeric(["-0x52F8c"])
-#    result.should(eq(true))
-#  end
-#
-#  it "should return true if a negative octal" do
-#    result = scope.function_is_numeric(["-0751"])
-#    result.should(eq(true))
-#  end
-#
-#  it "should return false if a negative octal with whitespaces before/after the dash" do
-#    result = scope.function_is_numeric([" -  0751"])
-#    result.should(eq(false))
-#  end
-#
-#  it "should return false if a bad hexadecimal" do
-#    result = scope.function_is_numeric(["0x23d7g"])
-#    result.should(eq(false))
-#  end
-#
-#  it "should return false if a bad octal" do
-#    result = scope.function_is_numeric(["0287"])
-#    result.should(eq(false))
-#  end
+describe 'is_numeric' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+  it { is_expected.to run.with_params(3).and_return(true) }
+  it { is_expected.to run.with_params('3').and_return(true) }
+  it { is_expected.to run.with_params(-3).and_return(true) }
+  it { is_expected.to run.with_params('-3').and_return(true) }
+  
+  it { is_expected.to run.with_params(3.7).and_return(true) }
+  it { is_expected.to run.with_params('3.7').and_return(true) }
+  it { is_expected.to run.with_params(-3.7).and_return(true) }
+  it { is_expected.to run.with_params('3.7').and_return(true) }
+
+  it { is_expected.to run.with_params('-342.2315e-12').and_return(true) }
+
+  it { is_expected.to run.with_params('one').and_return(false) }
+  it { is_expected.to run.with_params([]).and_return(false) }
+  it { is_expected.to run.with_params([1]).and_return(false) }
+  it { is_expected.to run.with_params({}).and_return(false) }
+  it { is_expected.to run.with_params(true).and_return(false) }
+  it { is_expected.to run.with_params(false).and_return(false) }
+  it { is_expected.to run.with_params('0001234').and_return(false) }
+  it { is_expected.to run.with_params(' - 1234').and_return(false) }
 end
index 6a0801ae8414bca7db66dfca84e8eed646ad670c..8e459ccfe3ce03ae8b06eb509b997fc05500a536 100755 (executable)
@@ -1,34 +1,28 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the is_string function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'is_string' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("is_string")).to eq("function_is_string")
-  end
+  it { is_expected.to run.with_params(3).and_return(false) }
+  it { is_expected.to run.with_params('3').and_return(false) }
+  it { is_expected.to run.with_params(-3).and_return(false) }
+  it { is_expected.to run.with_params('-3').and_return(false) }
 
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_is_string([]) }.to( raise_error(Puppet::ParseError))
-  end
+  it { is_expected.to run.with_params(3.7).and_return(false) }
+  it { is_expected.to run.with_params('3.7').and_return(false) }
+  it { is_expected.to run.with_params(-3.7).and_return(false) }
+  it { is_expected.to run.with_params('3.7').and_return(false) }
 
-  it "should return true if a string" do
-    result = scope.function_is_string(["asdf"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if an integer" do
-    result = scope.function_is_string(["3"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if a float" do
-    result = scope.function_is_string(["3.23"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return false if an array" do
-    result = scope.function_is_string([["a","b","c"]])
-    expect(result).to(eq(false))
-  end
+  it { is_expected.to run.with_params([]).and_return(false) }
+  it { is_expected.to run.with_params([1]).and_return(false) }
+  it { is_expected.to run.with_params({}).and_return(false) }
+  it { is_expected.to run.with_params(true).and_return(false) }
+  it { is_expected.to run.with_params(false).and_return(false) }
+  it { is_expected.to run.with_params('one').and_return(true) }
+  it { is_expected.to run.with_params('0001234').and_return(true) }
 end
index 4a9ae87a21818cbd4fcb33ee7892b635320c4bc8..6c109d1c2a9fd1f878decee47d970f419045a6e7 100755 (executable)
@@ -1,40 +1,19 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the join_keys_to_values function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("join_keys_to_values")).to eq("function_join_keys_to_values")
-  end
-
-  it "should raise a ParseError if there are fewer than two arguments" do
-    expect { scope.function_join_keys_to_values([{}]) }.to raise_error Puppet::ParseError
-  end
-
-  it "should raise a ParseError if there are greater than two arguments" do
-    expect { scope.function_join_keys_to_values([{}, 'foo', 'bar']) }.to raise_error Puppet::ParseError
-  end
-
-  it "should raise a TypeError if the first argument is an array" do
-    expect { scope.function_join_keys_to_values([[1,2], ',']) }.to raise_error TypeError
-  end
-
-  it "should raise a TypeError if the second argument is an array" do
-    expect { scope.function_join_keys_to_values([{}, [1,2]]) }.to raise_error TypeError
-  end
-
-  it "should raise a TypeError if the second argument is a number" do
-    expect { scope.function_join_keys_to_values([{}, 1]) }.to raise_error TypeError
-  end
-
-  it "should return an empty array given an empty hash" do
-    result = scope.function_join_keys_to_values([{}, ":"])
-    expect(result).to eq([])
-  end
-
-  it "should join hash's keys to its values" do
-    result = scope.function_join_keys_to_values([{'a'=>1,2=>'foo',:b=>nil}, ":"])
-    expect(result).to match_array(['a:1','2:foo','b:'])
+describe 'join_keys_to_values' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Takes exactly two arguments/) }
+  it { is_expected.to run.with_params({}, '', '').and_raise_error(Puppet::ParseError, /Takes exactly two arguments/) }
+  it { is_expected.to run.with_params('one', '').and_raise_error(TypeError, /The first argument must be a hash/) }
+  it { is_expected.to run.with_params({}, 2).and_raise_error(TypeError, /The second argument must be a string/) }
+
+  it { is_expected.to run.with_params({}, '').and_return([]) }
+  it { is_expected.to run.with_params({}, ':').and_return([]) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return(['keyvalue']) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }, ':').and_return(['key:value']) }
+  it { is_expected.to run.with_params({ 'key' => nil }, ':').and_return(['key:']) }
+  it 'should run join_keys_to_values(<hash with multiple keys>, ":") and return the proper array' do
+    result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2' }, ':'])
+    expect(result.sort).to eq(['key1:value1', 'key2:value2'].sort)
   end
 end
index 793c36fa32da6f2975d056a47811ecf928eeccfe..a3005714ba9b2825489131eab723fa0f9c93c533 100755 (executable)
@@ -1,19 +1,19 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the join function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'join' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the second.")
+    is_expected.to run.with_params([], '', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Requires array to work with/) }
+  it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, /Requires string to work with/) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("join")).to eq("function_join")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_join([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should join an array into a string" do
-    result = scope.function_join([["a","b","c"], ":"])
-    expect(result).to(eq("a:b:c"))
-  end
+  it { is_expected.to run.with_params([]).and_return('') }
+  it { is_expected.to run.with_params([], ':').and_return('') }
+  it { is_expected.to run.with_params(['one']).and_return('one') }
+  it { is_expected.to run.with_params(['one'], ':').and_return('one') }
+  it { is_expected.to run.with_params(['one', 'two', 'three']).and_return('onetwothree') }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ':').and_return('one:two:three') }
 end
index f2e7d428ebb7ceeef2567eb05cb1a84b85929086..2e009dcc8f506fb8df106a0f4d0e46dc76b7d592 100755 (executable)
@@ -1,21 +1,19 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the keys function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("keys")).to eq("function_keys")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_keys([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return an array of keys when given a hash" do
-    result = scope.function_keys([{'a'=>1, 'b'=>2}])
-    # =~ performs 'array with same elements' (set) matching
-    # For more info see RSpec::Matchers::MatchArray
-    expect(result).to match_array(['a','b'])
+describe 'keys' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+  it { is_expected.to run.with_params({}).and_return([]) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['key']) }
+  it 'should return the array of keys' do
+    result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2' }])
+    expect(result).to match_array(['key1', 'key2'])
   end
 end
index 4b8d40623639fc67d6036730b86370dbfa1a7928..ffc714d11a46ae32b7bf0e9e334b01d6e984a3f6 100755 (executable)
@@ -1,31 +1,24 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the loadyaml function" do
-  include PuppetlabsSpec::Files
-
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("loadyaml")).to eq("function_loadyaml")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_loadyaml([]) }.to raise_error(Puppet::ParseError)
+describe 'loadyaml' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  context 'when a non-existing file is specified' do
+    let(:filename) { '/tmp/doesnotexist' }
+    before {
+      File.expects(:exists?).with(filename).returns(false).once
+      YAML.expects(:load_file).never
+    }
+    it { is_expected.to run.with_params(filename).and_return(nil) }
   end
-
-  it "should return nil when file does not exist" do
-    yaml_file = tmpfilename ('yamlfile')
-    result = scope.function_loadyaml([yaml_file])
-    expect(result).to(eq(nil))
-  end
-
-  it "should convert YAML file to a data structure" do
-    yaml_file = tmpfilename ('yamlfile')
-    File.open(yaml_file, 'w') do |fh|
-      fh.write("---\n aaa: 1\n bbb: 2\n ccc: 3\n ddd: 4\n")
-    end
-    result = scope.function_loadyaml([yaml_file])
-    expect(result).to eq({"aaa" => 1, "bbb" => 2, "ccc" => 3, "ddd" => 4 })
+  context 'when an existing file is specified' do
+    let(:filename) { '/tmp/doesexist' }
+    let(:data) { { 'key' => 'value' } }
+    before {
+      File.expects(:exists?).with(filename).returns(true).once
+      YAML.expects(:load_file).with(filename).returns(data).once
+    }
+    it { is_expected.to run.with_params(filename).and_return(data) }
   end
 end
index 68cca1c5d269fe276ea2f0f9d610cf0f0b3ac9f7..981794edff4b5819c90cc0243a12b21069b0f73e 100755 (executable)
@@ -1,28 +1,34 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the lstrip function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("lstrip")).to eq("function_lstrip")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_lstrip([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should lstrip a string" do
-    result = scope.function_lstrip(["  asdf"])
-    expect(result).to(eq('asdf'))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new("  asdf")
-    result = scope.function_lstrip([value])
-    result.should(eq("asdf"))
-  end
+describe 'lstrip' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+  it { is_expected.to run.with_params('').and_return('') }
+  it { is_expected.to run.with_params(' ').and_return('') }
+  it { is_expected.to run.with_params('     ').and_return('') }
+  it { is_expected.to run.with_params("\t").and_return('') }
+  it { is_expected.to run.with_params("\t ").and_return('') }
+  it { is_expected.to run.with_params('one').and_return('one') }
+  it { is_expected.to run.with_params(' one').and_return('one') }
+  it { is_expected.to run.with_params('     one').and_return('one') }
+  it { is_expected.to run.with_params("\tone").and_return('one') }
+  it { is_expected.to run.with_params("\t one").and_return('one') }
+  it { is_expected.to run.with_params('one ').and_return('one ') }
+  it { is_expected.to run.with_params(' one ').and_return('one ') }
+  it { is_expected.to run.with_params('     one ').and_return('one ') }
+  it { is_expected.to run.with_params("\tone ").and_return('one ') }
+  it { is_expected.to run.with_params("\t one ").and_return('one ') }
+  it { is_expected.to run.with_params("one \t").and_return("one \t") }
+  it { is_expected.to run.with_params(" one \t").and_return("one \t") }
+  it { is_expected.to run.with_params("     one \t").and_return("one \t") }
+  it { is_expected.to run.with_params("\tone \t").and_return("one \t") }
+  it { is_expected.to run.with_params("\t one \t").and_return("one \t") }
+  it { is_expected.to run.with_params(' o n e ').and_return('o n e ') }
+  it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return('one ') }
 end
index c3d8a132fad918efd726ef12d34d716baae90c2a..66fb0c8694034f11cbaa5375a1bbe09c016519d0 100755 (executable)
@@ -1,27 +1,21 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the max function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("max")).to eq("function_max")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_max([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should be able to compare strings" do
-    expect(scope.function_max(["albatross","dog","horse"])).to(eq("horse"))
-  end
-
-  it "should be able to compare numbers" do
-    expect(scope.function_max([6,8,4])).to(eq(8))
-  end
+describe 'max' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(1).and_return(1) }
+  it { is_expected.to run.with_params(1, 2).and_return(2) }
+  it { is_expected.to run.with_params(1, 2, 3).and_return(3) }
+  it { is_expected.to run.with_params(3, 2, 1).and_return(3) }
+  it { is_expected.to run.with_params('one').and_return('one') }
+  it { is_expected.to run.with_params('one', 'two').and_return('two') }
+  it { is_expected.to run.with_params('one', 'two', 'three').and_return('two') }
+  it { is_expected.to run.with_params('three', 'two', 'one').and_return('two') }
 
-  it "should be able to compare a number with a stringified number" do
-    expect(scope.function_max([1,"2"])).to(eq("2"))
+  describe 'implementation artifacts' do
+    it { is_expected.to run.with_params(1, 'one').and_return('one') }
+    it { is_expected.to run.with_params('1', 'one').and_return('one') }
+    it { is_expected.to run.with_params('1.3e1', '1.4e0').and_return('1.4e0') }
+    it { is_expected.to run.with_params(1.3e1, 1.4e0).and_return(1.3e1) }
   end
 end
index 1a1d7c66025b852b37420a652bf3cc038e08e012..527f887fa2c87b6ab1e7811a02e096a074eceed2 100755 (executable)
@@ -1,34 +1,21 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the member function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("member")).to eq("function_member")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_member([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if a member is in an array" do
-    result = scope.function_member([["a","b","c"], "a"])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if a member is not in an array" do
-    result = scope.function_member([["a","b","c"], "d"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return true if a member array is in an array" do
-    result = scope.function_member([["a","b","c"], ["a", "b"]])
-    expect(result).to(eq(true))
-  end
-
-  it "should return false if a member array is not in an array" do
-    result = scope.function_member([["a","b","c"], ["d", "e"]])
-    expect(result).to(eq(false))
-  end
+describe 'member' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params([], [], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params([], '').and_return(false) }
+  it { is_expected.to run.with_params([], ['']).and_return(false) }
+  it { is_expected.to run.with_params([''], '').and_return(true) }
+  it { is_expected.to run.with_params([''], ['']).and_return(true) }
+  it { is_expected.to run.with_params([], 'one').and_return(false) }
+  it { is_expected.to run.with_params([], ['one']).and_return(false) }
+  it { is_expected.to run.with_params(['one'], 'one').and_return(true) }
+  it { is_expected.to run.with_params(['one'], ['one']).and_return(true) }
+  it { is_expected.to run.with_params(['one', 'two', 'three', 'four'], ['four', 'two']).and_return(true) }
+  it { is_expected.to run.with_params(['one', 'two', 'three', 'four'], ['four', 'five']).and_return(false) }
 end
index 2abf97622418cc0c4f0a194dc1d63d924e8db2ba..7b53363ed5dfabc5d64c19b00dd906a6a538700e 100755 (executable)
@@ -1,52 +1,25 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:merge) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe 'when calling merge from puppet' do
-    it "should not compile when no arguments are passed" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = '$x = merge()'
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
-
-    it "should not compile when 1 argument is passed" do
-      skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./
-      Puppet[:code] = "$my_hash={'one' => 1}\n$x = merge($my_hash)"
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
-  end
-
-  describe 'when calling merge on the scope instance' do
-    it 'should require all parameters are hashes' do
-      expect { new_hash = scope.function_merge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/)
-      expect { new_hash = scope.function_merge([{}, 2])}.to raise_error(Puppet::ParseError, /unexpected argument type Fixnum/)
-    end
-
-    it 'should accept empty strings as puppet undef' do
-      expect { new_hash = scope.function_merge([{}, ''])}.not_to raise_error
-    end
-
-    it 'should be able to merge two hashes' do
-      new_hash = scope.function_merge([{'one' => '1', 'two' => '1'}, {'two' => '2', 'three' => '2'}])
-      expect(new_hash['one']).to   eq('1')
-      expect(new_hash['two']).to   eq('2')
-      expect(new_hash['three']).to eq('2')
-    end
-
-    it 'should merge multiple hashes' do
-      hash = scope.function_merge([{'one' => 1}, {'one' => '2'}, {'one' => '3'}])
-      expect(hash['one']).to eq('3')
-    end
-
-    it 'should accept empty hashes' do
-      expect(scope.function_merge([{},{},{}])).to eq({})
-    end
+describe 'merge' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params({}, 'two').and_raise_error(Puppet::ParseError, /unexpected argument type String/) }
+  it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) }
+  it {
+    pending 'should not special case this'
+    is_expected.to run.with_params({}).and_return({})
+  }
+  it { is_expected.to run.with_params({}, {}).and_return({}) }
+  it { is_expected.to run.with_params({}, {}, {}).and_return({}) }
+  describe 'should accept empty strings as puppet undef' do
+    it { is_expected.to run.with_params({}, '').and_return({}) }
   end
+  it { is_expected.to run.with_params({ 'key' => 'value' }, {}).and_return({ 'key' => 'value' }) }
+  it { is_expected.to run.with_params({}, { 'key' => 'value' }).and_return({ 'key' => 'value' }) }
+  it { is_expected.to run.with_params({ 'key' => 'value1' }, { 'key' => 'value2' }).and_return({ 'key' => 'value2' }) }
+  it {
+    is_expected.to run \
+      .with_params({ 'key1' => 'value1' }, { 'key2' => 'value2' }, { 'key3' => 'value3' }) \
+      .and_return({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' })
+  }
 end
index 35a08900bfeff332d396d4e1ebd437ca5a10bbe6..c840a72c9065601a5b10906d12b1f82bfd423bcb 100755 (executable)
@@ -1,27 +1,21 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the min function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("min")).to eq("function_min")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_min([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should be able to compare strings" do
-    expect(scope.function_min(["albatross","dog","horse"])).to(eq("albatross"))
-  end
-
-  it "should be able to compare numbers" do
-    expect(scope.function_min([6,8,4])).to(eq(4))
-  end
+describe 'min' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(1).and_return(1) }
+  it { is_expected.to run.with_params(1, 2).and_return(1) }
+  it { is_expected.to run.with_params(1, 2, 3).and_return(1) }
+  it { is_expected.to run.with_params(3, 2, 1).and_return(1) }
+  it { is_expected.to run.with_params('one').and_return('one') }
+  it { is_expected.to run.with_params('one', 'two').and_return('one') }
+  it { is_expected.to run.with_params('one', 'two', 'three').and_return('one') }
+  it { is_expected.to run.with_params('three', 'two', 'one').and_return('one') }
 
-  it "should be able to compare a number with a stringified number" do
-    expect(scope.function_min([1,"2"])).to(eq(1))
+  describe 'implementation artifacts' do
+    it { is_expected.to run.with_params(1, 'one').and_return(1) }
+    it { is_expected.to run.with_params('1', 'one').and_return('1') }
+    it { is_expected.to run.with_params('1.3e1', '1.4e0').and_return('1.3e1') }
+    it { is_expected.to run.with_params(1.3e1, 1.4e0).and_return(1.4e0) }
   end
 end
index d0ba935488ceacce70e65472dab72a67930a5ae4..494afff9fc3ef9f9622eb97f46f1807487f3a259 100755 (executable)
@@ -1,67 +1,22 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the num2bool function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("num2bool")).to eq("function_num2bool")
-  end
-
-  it "should raise a ParseError if there are no arguments" do
-    expect { scope.function_num2bool([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if there are more than 1 arguments" do
-    expect { scope.function_num2bool(["foo","bar"]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if passed something non-numeric" do
-    expect { scope.function_num2bool(["xyzzy"]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return true if passed string 1" do
-    result = scope.function_num2bool(["1"])
-    expect(result).to(be_truthy)
-  end
-
-  it "should return true if passed string 1.5" do
-    result = scope.function_num2bool(["1.5"])
-    expect(result).to(be_truthy)
-  end
-
-  it "should return true if passed number 1" do
-    result = scope.function_num2bool([1])
-    expect(result).to(be_truthy)
-  end
-
-  it "should return false if passed string 0" do
-    result = scope.function_num2bool(["0"])
-    expect(result).to(be_falsey)
-  end
-
-  it "should return false if passed number 0" do
-    result = scope.function_num2bool([0])
-    expect(result).to(be_falsey)
-  end
-
-  it "should return false if passed string -1" do
-    result = scope.function_num2bool(["-1"])
-    expect(result).to(be_falsey)
-  end
-
-  it "should return false if passed string -1.5" do
-    result = scope.function_num2bool(["-1.5"])
-    expect(result).to(be_falsey)
-  end
-
-  it "should return false if passed number -1" do
-    result = scope.function_num2bool([-1])
-    expect(result).to(be_falsey)
-  end
-
-  it "should return false if passed float -1.5" do
-    result = scope.function_num2bool([-1.5])
-    expect(result).to(be_falsey)
-  end
+describe 'num2bool' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('abc').and_raise_error(Puppet::ParseError, /does not look like a number/) }
+  it { is_expected.to run.with_params(1).and_return(true) }
+  it { is_expected.to run.with_params('1').and_return(true) }
+  it { is_expected.to run.with_params(1.5).and_return(true) }
+  it { is_expected.to run.with_params('1.5').and_return(true) }
+  it { is_expected.to run.with_params(-1).and_return(false) }
+  it { is_expected.to run.with_params('-1').and_return(false) }
+  it { is_expected.to run.with_params(-1.5).and_return(false) }
+  it { is_expected.to run.with_params('-1.5').and_return(false) }
+  it { is_expected.to run.with_params(0).and_return(false) }
+  it { is_expected.to run.with_params('0').and_return(false) }
+  it { is_expected.to run.with_params([]).and_return(false) }
+  it { is_expected.to run.with_params('[]').and_raise_error(Puppet::ParseError, /does not look like a number/) }
+  it { is_expected.to run.with_params({}).and_return(false) }
+  it { is_expected.to run.with_params('{}').and_raise_error(Puppet::ParseError, /does not look like a number/) }
 end
index 1dd41b9601add366ed2b53466174b8f3e129c6c1..436566e79c935622015a1e7c25435529c9f7ae13 100755 (executable)
@@ -1,22 +1,9 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the parsejson function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("parsejson")).to eq("function_parsejson")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_parsejson([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should convert JSON to a data structure" do
-    json = <<-EOS
-["aaa","bbb","ccc"]
-EOS
-    result = scope.function_parsejson([json])
-    expect(result).to(eq(['aaa','bbb','ccc']))
-  end
+describe 'parsejson' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('["one"').and_raise_error(PSON::ParserError) }
+  it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) }
 end
index e5f145ba1cad3742478019aadd74310cc59bc147..fb635f8ee382ed4712d7d60148f7af30be96dc21 100755 (executable)
@@ -1,24 +1,14 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the parseyaml function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("parseyaml")).to eq("function_parseyaml")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_parseyaml([]) }.to( raise_error(Puppet::ParseError))
+describe 'parseyaml' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) }
+  context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+    it { is_expected.to run.with_params('["one"').and_raise_error(Psych::SyntaxError) }
   end
-
-  it "should convert YAML to a data structure" do
-    yaml = <<-EOS
-- aaa
-- bbb
-- ccc
-EOS
-    result = scope.function_parseyaml([yaml])
-    expect(result).to(eq(['aaa','bbb','ccc']))
+  context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
+    it { is_expected.to run.with_params('["one"').and_raise_error(ArgumentError) }
   end
 end
index db10cc3547b11c8464219323c7b9033bb7def759..e2fc64a118a3f1a3bf0bb844a5309428fe7ba6c7 100755 (executable)
@@ -1,58 +1,24 @@
-#!/usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the pick_default function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("pick_default")).to eq("function_pick_default")
-  end
-
-  it 'should return the correct value' do
-    expect(scope.function_pick_default(['first', 'second'])).to eq('first')
-  end
-
-  it 'should return the correct value if the first value is empty' do
-    expect(scope.function_pick_default(['', 'second'])).to eq('second')
-  end
-
-  it 'should skip empty string values' do
-    expect(scope.function_pick_default(['', 'first'])).to eq('first')
-  end
-
-  it 'should skip :undef values' do
-    expect(scope.function_pick_default([:undef, 'first'])).to eq('first')
-  end
-
-  it 'should skip :undefined values' do
-    expect(scope.function_pick_default([:undefined, 'first'])).to eq('first')
-  end
-
-  it 'should return the empty string if it is the last possibility' do
-    expect(scope.function_pick_default([:undef, :undefined, ''])).to eq('')
-  end
-
-  it 'should return :undef if it is the last possibility' do
-    expect(scope.function_pick_default(['', :undefined, :undef])).to eq(:undef)
-  end
-
-  it 'should return :undefined if it is the last possibility' do
-    expect(scope.function_pick_default([:undef, '', :undefined])).to eq(:undefined)
-  end
-
-  it 'should return the empty string if it is the only possibility' do
-    expect(scope.function_pick_default([''])).to eq('')
-  end
-
-  it 'should return :undef if it is the only possibility' do
-    expect(scope.function_pick_default([:undef])).to eq(:undef)
-  end
-
-  it 'should return :undefined if it is the only possibility' do
-    expect(scope.function_pick_default([:undefined])).to eq(:undefined)
-  end
-
-  it 'should error if no values are passed' do
-    expect { scope.function_pick_default([]) }.to raise_error(Puppet::Error, /Must receive at least one argument./)
+describe 'pick_default' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::Error, /Must receive at least one argument/) }
+
+  it { is_expected.to run.with_params('one', 'two').and_return('one') }
+  it { is_expected.to run.with_params('', 'two').and_return('two') }
+  it { is_expected.to run.with_params(:undef, 'two').and_return('two') }
+  it { is_expected.to run.with_params(:undefined, 'two').and_return('two') }
+  it { is_expected.to run.with_params(nil, 'two').and_return('two') }
+
+  [ '', :undef, :undefined, nil, {}, [], 1, 'default' ].each do |value|
+    describe "when providing #{value.inspect} as default" do
+      it { is_expected.to run.with_params('one', value).and_return('one') }
+      it { is_expected.to run.with_params([], value).and_return([]) }
+      it { is_expected.to run.with_params({}, value).and_return({}) }
+      it { is_expected.to run.with_params(value, value).and_return(value) }
+      it { is_expected.to run.with_params(:undef, value).and_return(value) }
+      it { is_expected.to run.with_params(:undefined, value).and_return(value) }
+      it { is_expected.to run.with_params(nil, value).and_return(value) }
+    end
   end
 end
index 8be8f5875329c76ebdafb531f2f3f54a68350927..2c7caa87e3ae03454ba5df9d6fd8696652b69983 100755 (executable)
@@ -1,34 +1,12 @@
-#!/usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the pick function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("pick")).to eq("function_pick")
-  end
-
-  it 'should return the correct value' do
-    expect(scope.function_pick(['first', 'second'])).to eq('first')
-  end
-
-  it 'should return the correct value if the first value is empty' do
-    expect(scope.function_pick(['', 'second'])).to eq('second')
-  end
-
-  it 'should remove empty string values' do
-    expect(scope.function_pick(['', 'first'])).to eq('first')
-  end
-
-  it 'should remove :undef values' do
-    expect(scope.function_pick([:undef, 'first'])).to eq('first')
-  end
-
-  it 'should remove :undefined values' do
-    expect(scope.function_pick([:undefined, 'first'])).to eq('first')
-  end
-
-  it 'should error if no values are passed' do
-    expect { scope.function_pick([]) }.to( raise_error(Puppet::ParseError, "pick(): must receive at least one non empty value"))
-  end
+describe 'pick' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /must receive at least one non empty value/) }
+  it { is_expected.to run.with_params('', nil, :undef, :undefined).and_raise_error(Puppet::ParseError, /must receive at least one non empty value/) }
+  it { is_expected.to run.with_params('one', 'two').and_return('one') }
+  it { is_expected.to run.with_params('', 'two').and_return('two') }
+  it { is_expected.to run.with_params(:undef, 'two').and_return('two') }
+  it { is_expected.to run.with_params(:undefined, 'two').and_return('two') }
+  it { is_expected.to run.with_params(nil, 'two').and_return('two') }
 end
index aec8a7d983c09bc6ae911f4fed31fe0b920fccdb..37610221a43207d0761e6a187c12ccfcb8ae04cf 100755 (executable)
@@ -1,33 +1,28 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the prefix function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "raises a ParseError if there is less than 1 arguments" do
-    expect { scope.function_prefix([]) }.to raise_error(Puppet::ParseError, /number of arguments/)
-  end
-
-  it "raises an error if the first argument is not an array" do
-    expect {
-      scope.function_prefix([Object.new])
-    }.to raise_error(Puppet::ParseError, /expected first argument to be an Array/)
-  end
-
-
-  it "raises an error if the second argument is not a string" do
-    expect {
-      scope.function_prefix([['first', 'second'], 42])
-    }.to raise_error(Puppet::ParseError, /expected second argument to be a String/)
-  end
-
-  it "returns a prefixed array" do
-    result = scope.function_prefix([['a','b','c'], 'p'])
-    expect(result).to(eq(['pa','pb','pc']))
-  end
-
-  it "returns a prefixed hash" do
-    result = scope.function_prefix([{'a' => 'b','b' => 'c','c' => 'd'}, 'p'])
-    expect(result).to(eq({'pa'=>'b','pb'=>'c','pc'=>'d'}))
-  end
+describe 'prefix' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the second.")
+    is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /expected first argument to be an Array or a Hash/) }
+  it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, /expected second argument to be a String/) }
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(['one', 2]).and_return(['one', '2']) }
+  it { is_expected.to run.with_params([], '').and_return([]) }
+  it { is_expected.to run.with_params([''], '').and_return(['']) }
+  it { is_expected.to run.with_params(['one'], 'pre').and_return(['preone']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], 'pre').and_return(['preone', 'pretwo', 'prethree']) }
+  it { is_expected.to run.with_params({}).and_return({}) }
+  it { is_expected.to run.with_params({ 'key1' => 'value1', 2 => 3}).and_return({ 'key1' => 'value1', '2' => 3 }) }
+  it { is_expected.to run.with_params({}, '').and_return({}) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return({ 'key' => 'value' }) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }, 'pre').and_return({ 'prekey' => 'value' }) }
+  it {
+    is_expected.to run \
+      .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'pre') \
+      .and_return({ 'prekey1' => 'value1', 'prekey2' => 'value2', 'prekey3' => 'value3' })
+  }
 end
index c90282ed70e9fbfd9ec4babeacaf77516c21f874..a13be6439c71b5a13c5b800ce8de54229c79d264 100644 (file)
@@ -1,17 +1,13 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:private) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  subject do
-    function_name = Puppet::Parser::Functions.function(:private)
-    scope.method(function_name)
-  end
-
+describe 'private' do
   it 'should issue a warning' do
     scope.expects(:warning).with("private() DEPRECATED: This function will cease to function on Puppet 4; please use assert_private() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.")
-    subject.call []
+    begin
+      subject.call []
+    rescue
+      # ignore this
+    end
   end
 
   context "when called from inside module" do
index 337809085c6721a4e432860c890c6464606393be..df5348c90c7a1e9c37971f8fa81e5218b7045ae8 100644 (file)
@@ -1,96 +1,69 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the pw_hash function" do
+describe 'pw_hash' do
 
-  before :all do
-    @enhanced_salts_supported = RUBY_PLATFORM == 'java'
-  end
-
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("pw_hash")).to eq("function_pw_hash")
-  end
+  it { is_expected.not_to eq(nil) }
 
-  it "should raise an ArgumentError if there are less than 3 arguments" do
-    expect { scope.function_pw_hash([]) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) )
-    expect { scope.function_pw_hash(['password']) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) )
-    expect { scope.function_pw_hash(['password', 'sha-512']) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) )
+  context 'when there are less than 3 arguments' do
+    it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('password').and_raise_error(ArgumentError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('password', 'sha-256').and_raise_error(ArgumentError, /wrong number of arguments/i) }
   end
 
-  it "should raise an ArgumentError if there are more than 3 arguments" do
-    expect { scope.function_pw_hash(['password', 'sha-512', 'salt', 5]) }.to( raise_error(ArgumentError, /[Ww]rong number of arguments/) )
+  context 'when there are more than 3 arguments' do
+    it { is_expected.to run.with_params('password', 'sha-256', 'salt', 'extra').and_raise_error(ArgumentError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('password', 'sha-256', 'salt', 'extra', 'extra').and_raise_error(ArgumentError, /wrong number of arguments/i) }
   end
 
-  it "should raise an ArgumentError if the first argument is not a string" do
-    expect { scope.function_pw_hash([['password'], 'sha-512', 'salt']) }.to( raise_error(ArgumentError, /first argument must be a string/) )
-    # in Puppet 3, numbers are passed as strings, so we can't test that
+  context 'when the first argument is not a string' do
+    it { is_expected.to run.with_params([], 'sha-256', 'salt').and_raise_error(ArgumentError, /first argument must be a string/) }
+    it { is_expected.to run.with_params({}, 'sha-256', 'salt').and_raise_error(ArgumentError, /first argument must be a string/) }
+    it { is_expected.to run.with_params(1, 'sha-256', 'salt').and_raise_error(ArgumentError, /first argument must be a string/) }
+    it { is_expected.to run.with_params(true, 'sha-256', 'salt').and_raise_error(ArgumentError, /first argument must be a string/) }
   end
 
-  it "should return nil if the first argument is empty" do
-    expect(scope.function_pw_hash(['', 'sha-512', 'salt'])).to eq(nil)
+  context 'when the first argument is undefined' do
+    it { is_expected.to run.with_params('', 'sha-256', 'salt').and_return(nil) }
+    it { is_expected.to run.with_params(nil, 'sha-256', 'salt').and_return(nil) }
   end
 
-  it "should return nil if the first argument is undef" do
-    expect(scope.function_pw_hash([nil, 'sha-512', 'salt'])).to eq(nil)
+  context 'when the second argument is not a string' do
+    it { is_expected.to run.with_params('password', [], 'salt').and_raise_error(ArgumentError, /second argument must be a string/) }
+    it { is_expected.to run.with_params('password', {}, 'salt').and_raise_error(ArgumentError, /second argument must be a string/) }
+    it { is_expected.to run.with_params('password', 1, 'salt').and_raise_error(ArgumentError, /second argument must be a string/) }
+    it { is_expected.to run.with_params('password', true, 'salt').and_raise_error(ArgumentError, /second argument must be a string/) }
   end
 
-  it "should raise an ArgumentError if the second argument is an invalid hash type" do
-    expect { scope.function_pw_hash(['', 'invalid', 'salt']) }.to( raise_error(ArgumentError, /not a valid hash type/) )
+  context 'when the second argument is not one of the supported hashing algorithms' do
+    it { is_expected.to run.with_params('password', 'no such algo', 'salt').and_raise_error(ArgumentError, /is not a valid hash type/) }
   end
 
-  it "should raise an ArgumentError if the second argument is not a string" do
-    expect { scope.function_pw_hash(['', [], 'salt']) }.to( raise_error(ArgumentError, /second argument must be a string/) )
+  context 'when the third argument is not a string' do
+    it { is_expected.to run.with_params('password', 'sha-256', []).and_raise_error(ArgumentError, /third argument must be a string/) }
+    it { is_expected.to run.with_params('password', 'sha-256', {}).and_raise_error(ArgumentError, /third argument must be a string/) }
+    it { is_expected.to run.with_params('password', 'sha-256', 1).and_raise_error(ArgumentError, /third argument must be a string/) }
+    it { is_expected.to run.with_params('password', 'sha-256', true).and_raise_error(ArgumentError, /third argument must be a string/) }
   end
 
-  it "should raise an ArgumentError if the third argument is not a string" do
-    expect { scope.function_pw_hash(['password', 'sha-512', ['salt']]) }.to( raise_error(ArgumentError, /third argument must be a string/) )
-    # in Puppet 3, numbers are passed as strings, so we can't test that
+  context 'when the third argument is empty' do
+    it { is_expected.to run.with_params('password', 'sha-512', '').and_raise_error(ArgumentError, /third argument must not be empty/) }
   end
 
-  it "should raise an ArgumentError if the third argument is empty" do
-    expect { scope.function_pw_hash(['password', 'sha-512', '']) }.to( raise_error(ArgumentError, /third argument must not be empty/) )
+  context 'when the third argument contains invalid characters' do
+    it { is_expected.to run.with_params('password', 'sha-512', 'one%').and_raise_error(ArgumentError, /characters in salt must be in the set/) }
   end
 
-  it "should raise an ArgumentError if the third argument has invalid characters" do
-    expect { scope.function_pw_hash(['password', 'sha-512', '%']) }.to( raise_error(ArgumentError, /characters in salt must be in the set/) )
-  end
+  context 'when running on a platform with a weak String#crypt implementation' do
+    before(:each) { allow_any_instance_of(String).to receive(:crypt).with('$1$1').and_return('a bad hash') }
 
-  it "should fail on platforms with weak implementations of String#crypt" do
-    String.any_instance.expects(:crypt).with('$1$1').returns('$1SoNol0Ye6Xk')
-    expect { scope.function_pw_hash(['password', 'sha-512', 'salt']) }.to( raise_error(Puppet::ParseError, /system does not support enhanced salts/) )
+    it { is_expected.to run.with_params('password', 'sha-512', 'salt').and_raise_error(Puppet::ParseError, /system does not support enhanced salts/) }
   end
 
-  if @enhanced_salts_supported
+  if RUBY_PLATFORM == 'java' or 'test'.crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.'
     describe "on systems with enhanced salts support" do
-      it "should return a hashed password" do
-        result = scope.function_pw_hash(['password', 'sha-512', 'salt'])
-        expect(result).to eql('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.')
-      end
-
-      it "should use the specified salt" do
-        result = scope.function_pw_hash(['password', 'sha-512', 'salt'])
-        expect(result).to match('salt')
-      end
-
-      it "should use the specified hash type" do
-        resultmd5 = scope.function_pw_hash(['password', 'md5', 'salt'])
-        resultsha256 = scope.function_pw_hash(['password', 'sha-256', 'salt'])
-        resultsha512 = scope.function_pw_hash(['password', 'sha-512', 'salt'])
-
-        expect(resultmd5).to eql('$1$salt$qJH7.N4xYta3aEG/dfqo/0')
-        expect(resultsha256).to eql('$5$salt$Gcm6FsVtF/Qa77ZKD.iwsJlCVPY0XSMgLJL0Hnww/c1')
-        expect(resultsha512).to eql('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.')
-      end
-
-      it "should generate a valid hash" do
-        password_hash = scope.function_pw_hash(['password', 'sha-512', 'salt'])
-
-        hash_parts = password_hash.match(%r{\A\$(.*)\$([a-zA-Z0-9./]+)\$([a-zA-Z0-9./]+)\z})
-
-        expect(hash_parts).not_to eql(nil)
-      end
+      it { is_expected.to run.with_params('password', 'md5', 'salt').and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') }
+      it { is_expected.to run.with_params('password', 'sha-256', 'salt').and_return('$5$salt$Gcm6FsVtF/Qa77ZKD.iwsJlCVPY0XSMgLJL0Hnww/c1') }
+      it { is_expected.to run.with_params('password', 'sha-512', 'salt').and_return('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.') }
     end
   end
 end
index f18b89e1a10c916de4543c59c0271151b52d9d14..492cad40b12898e2066883f87882399db868d86f 100755 (executable)
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the range function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'range' do
+  it { is_expected.not_to eq(nil) }
 
-  it "exists" do
-    expect(Puppet::Parser::Functions.function("range")).to eq("function_range")
+  describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it {
+      pending("Current implementation ignores parameters after the third.")
+      is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+    }
+    it { is_expected.to run.with_params('1..2..3').and_raise_error(Puppet::ParseError, /Unable to compute range/i) }
+    it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /Unknown range format/i) }
   end
 
-  it "raises a ParseError if there is less than 1 arguments" do
-    expect { scope.function_range([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments.*0 for 1/
+  describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params().and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params('').and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params({}).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params([]).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params(true).and_raise_error(ArgumentError) }
+    it {                                        is_expected.to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, []).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, {}).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, true).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError) }
+    it { pending "the puppet 4 implementation"; is_expected.to run.with_params('1..2..3').and_raise_error(ArgumentError) }
   end
 
-  describe 'with a letter range' do
-    it "returns a letter range" do
-      result = scope.function_range(["a","d"])
-      expect(result).to eq ['a','b','c','d']
-    end
-
-    it "returns a letter range given a step of 1" do
-      result = scope.function_range(["a","d","1"])
-      expect(result).to eq ['a','b','c','d']
-    end
-
-    it "returns a stepped letter range" do
-      result = scope.function_range(["a","d","2"])
-      expect(result).to eq ['a','c']
-    end
-
-    it "returns a stepped letter range given a negative step" do
-      result = scope.function_range(["a","d","-2"])
-      expect(result).to eq ['a','c']
-    end
+  context 'with characters as bounds' do
+    it { is_expected.to run.with_params('d', 'a').and_return([]) }
+    it { is_expected.to run.with_params('a', 'a').and_return(['a']) }
+    it { is_expected.to run.with_params('a', 'b').and_return(['a', 'b']) }
+    it { is_expected.to run.with_params('a', 'd').and_return(['a', 'b', 'c', 'd']) }
+    it { is_expected.to run.with_params('a', 'd', 1).and_return(['a', 'b', 'c', 'd']) }
+    it { is_expected.to run.with_params('a', 'd', '1').and_return(['a', 'b', 'c', 'd']) }
+    it { is_expected.to run.with_params('a', 'd', 2).and_return(['a', 'c']) }
+    it { is_expected.to run.with_params('a', 'd', -2).and_return(['a', 'c']) }
+    it { is_expected.to run.with_params('a', 'd', 3).and_return(['a', 'd']) }
+    it { is_expected.to run.with_params('a', 'd', 4).and_return(['a']) }
   end
 
-  describe 'with a number range' do
-    it "returns a number range" do
-      result = scope.function_range(["1","4"])
-      expect(result).to eq [1,2,3,4]
-    end
+  context 'with strings as bounds' do
+    it { is_expected.to run.with_params('onea', 'oned').and_return(['onea', 'oneb', 'onec', 'oned']) }
+    it { is_expected.to run.with_params('two', 'one').and_return([]) }
+    it { is_expected.to run.with_params('true', 'false').and_return([]) }
+    it { is_expected.to run.with_params('false', 'true').and_return(['false']) }
+  end
 
-    it "returns a number range given a step of 1" do
-      result = scope.function_range(["1","4","1"])
-      expect(result).to eq [1,2,3,4]
-    end
+  context 'with integers as bounds' do
+    it { is_expected.to run.with_params(4, 1).and_return([]) }
+    it { is_expected.to run.with_params(1, 1).and_return([1]) }
+    it { is_expected.to run.with_params(1, 2).and_return([1, 2]) }
+    it { is_expected.to run.with_params(1, 4).and_return([1, 2, 3, 4]) }
+    it { is_expected.to run.with_params(1, 4, 1).and_return([1, 2, 3, 4]) }
+    it { is_expected.to run.with_params(1, 4, '1').and_return([1, 2, 3, 4]) }
+    it { is_expected.to run.with_params(1, 4, 2).and_return([1, 3]) }
+    it { is_expected.to run.with_params(1, 4, -2).and_return([1, 3]) }
+    it { is_expected.to run.with_params(1, 4, 3).and_return([1, 4]) }
+    it { is_expected.to run.with_params(1, 4, 4).and_return([1]) }
+  end
 
-    it "returns a stepped number range" do
-      result = scope.function_range(["1","4","2"])
-      expect(result).to eq [1,3]
-    end
+  context 'with integers as strings as bounds' do
+    it { is_expected.to run.with_params('4', '1').and_return([]) }
+    it { is_expected.to run.with_params('1', '1').and_return([1]) }
+    it { is_expected.to run.with_params('1', '2').and_return([1, 2]) }
+    it { is_expected.to run.with_params('1', '4').and_return([1, 2, 3, 4]) }
+    it { is_expected.to run.with_params('1', '4', 1).and_return([1, 2, 3, 4]) }
+    it { is_expected.to run.with_params('1', '4', '1').and_return([1, 2, 3, 4]) }
+    it { is_expected.to run.with_params('1', '4', 2).and_return([1, 3]) }
+    it { is_expected.to run.with_params('1', '4', -2).and_return([1, 3]) }
+    it { is_expected.to run.with_params('1', '4', 3).and_return([1, 4]) }
+    it { is_expected.to run.with_params('1', '4', 4).and_return([1]) }
+  end
 
-    it "returns a stepped number range given a negative step" do
-      result = scope.function_range(["1","4","-2"])
-      expect(result).to eq [1,3]
-    end
+  context 'with prefixed numbers as strings as bounds' do
+    it { is_expected.to run.with_params('host01', 'host04').and_return(['host01', 'host02', 'host03', 'host04']) }
+    it { is_expected.to run.with_params('01', '04').and_return([1, 2, 3, 4]) }
   end
 
-  describe 'with a numeric-like string range' do
-    it "works with padded hostname like strings" do
-      expected = ("host01".."host10").to_a
-      expect(scope.function_range(["host01","host10"])).to eq expected
-    end
+  context 'with dash-range syntax' do
+    it { is_expected.to run.with_params('4-1').and_return([]) }
+    it { is_expected.to run.with_params('1-1').and_return([1]) }
+    it { is_expected.to run.with_params('1-2').and_return([1, 2]) }
+    it { is_expected.to run.with_params('1-4').and_return([1, 2, 3, 4]) }
+  end
 
-    it "coerces zero padded digits to integers" do
-      expected = (0..10).to_a
-      expect(scope.function_range(["00", "10"])).to eq expected
-    end
+  context 'with two-dot-range syntax' do
+    it { is_expected.to run.with_params('4..1').and_return([]) }
+    it { is_expected.to run.with_params('1..1').and_return([1]) }
+    it { is_expected.to run.with_params('1..2').and_return([1, 2]) }
+    it { is_expected.to run.with_params('1..4').and_return([1, 2, 3, 4]) }
   end
 
-  describe 'with a ruby-like range' do
-    it "returns a number range" do
-      result = scope.function_range(["1..4"])
-      expect(result).to eq [1,2,3,4]
-    end
+  context 'with three-dot-range syntax' do
+    it { is_expected.to run.with_params('4...1').and_return([]) }
+    it { is_expected.to run.with_params('1...1').and_return([]) }
+    it { is_expected.to run.with_params('1...2').and_return([1]) }
+    it { is_expected.to run.with_params('1...3').and_return([1, 2]) }
+    it { is_expected.to run.with_params('1...5').and_return([1, 2, 3, 4]) }
   end
 
-  describe 'with a numeric range' do
-    it "returns a range of numbers" do
-      expected = (1..10).to_a
-      expect(scope.function_range([1,10])).to eq expected
-    end
-    it "returns a range of numbers with step parameter" do
-      expected = (1..10).step(2).to_a
-      expect(scope.function_range([1,10,2])).to eq expected
-    end
-    it "works with mixed numeric like strings and numeric arguments" do
-      expected = (1..10).to_a
-      expect(scope.function_range(['1',10])).to eq expected
-      expect(scope.function_range([1,'10'])).to eq expected
-    end
+  describe 'when passing mixed arguments as bounds' do
+    it {
+      pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially')
+      is_expected.to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
+    }
+    it {
+      pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially')
+      is_expected.to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
+    }
+    it {
+      pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially')
+      is_expected.to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, /cannot interpolate between numeric and non-numeric bounds/)
+    }
   end
 end
index 88a992efc9ff46e3b42d896b4c1a3a75cc7fb506..4863050753aaa17c14a9a41b11727b80d9e169a3 100755 (executable)
@@ -1,20 +1,19 @@
-#!/usr/bin/env ruby
-
 require 'spec_helper'
 
-describe "the reject function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("reject")).to eq("function_reject")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_reject([]) }.to( raise_error(Puppet::ParseError))
-  end
+describe 'reject' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params([], 'pattern', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
-  it "should reject contents from an array" do
-    result = scope.function_reject([["1111", "aaabbb","bbbccc","dddeee"], "bbb"])
-    expect(result).to(eq(["1111", "dddeee"]))
-  end
+  it {
+    pending("reject does not actually check this, and raises NoMethodError instead")
+    is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /first argument not an array/)
+  }
+  it {
+    pending("reject does not actually check this, and raises NoMethodError instead")
+    is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, /first argument not an array/)
+  }
+  it { is_expected.to run.with_params([], 'two').and_return([]) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], 'two').and_return(['one', 'three']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], 't(wo|hree)').and_return(['one']) }
 end
index 1f047943fe63ceaa74a5e92e632868c53ddc6eb8..e00dee92e7ad6ea85ce40aadad673b7a4693acbe 100755 (executable)
@@ -1,28 +1,31 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the reverse function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'reverse' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+  it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(['a']).and_return(['a']) }
+  it { is_expected.to run.with_params(['one']).and_return(['one']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(['three', 'two', 'one']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(['four', 'three', 'two', 'one']) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("reverse")).to eq("function_reverse")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_reverse([]) }.to( raise_error(Puppet::ParseError))
-  end
+  it { is_expected.to run.with_params('').and_return('') }
+  it { is_expected.to run.with_params('a').and_return('a') }
+  it { is_expected.to run.with_params('abc').and_return('cba') }
+  it { is_expected.to run.with_params('abcd').and_return('dcba') }
 
-  it "should reverse a string" do
-    result = scope.function_reverse(["asdfghijkl"])
-    expect(result).to(eq('lkjihgfdsa'))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
+  context 'when using a class extending String' do
+    it 'should call its reverse method' do
+      value = AlsoString.new('asdfghjkl')
+      value.expects(:reverse).returns('foo')
+      expect(subject).to run.with_params(value).and_return('foo')
     end
-
-    value = AlsoString.new('asdfghjkl')
-    result = scope.function_reverse([value])
-    result.should(eq('lkjhgfdsa'))
   end
 end
index f6b483896f10c9cac8810423d6bb166092e8f4f1..d2efac8ea273d45cbf0ecd41b3e512f3397feb5d 100755 (executable)
@@ -1,33 +1,34 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the rstrip function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("rstrip")).to eq("function_rstrip")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_rstrip([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should rstrip a string" do
-    result = scope.function_rstrip(["asdf  "])
-    expect(result).to(eq('asdf'))
-  end
-
-  it "should rstrip each element in an array" do
-    result = scope.function_rstrip([["a ","b ", "c "]])
-    expect(result).to(eq(['a','b','c']))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new('asdf ')
-    result = scope.function_rstrip([value])
-    result.should(eq('asdf'))
-  end
+describe 'rstrip' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+  it { is_expected.to run.with_params('').and_return('') }
+  it { is_expected.to run.with_params(' ').and_return('') }
+  it { is_expected.to run.with_params('     ').and_return('') }
+  it { is_expected.to run.with_params("\t").and_return('') }
+  it { is_expected.to run.with_params("\t ").and_return('') }
+  it { is_expected.to run.with_params('one').and_return('one') }
+  it { is_expected.to run.with_params(' one').and_return(' one') }
+  it { is_expected.to run.with_params('     one').and_return('     one') }
+  it { is_expected.to run.with_params("\tone").and_return("\tone") }
+  it { is_expected.to run.with_params("\t one").and_return("\t one") }
+  it { is_expected.to run.with_params('one ').and_return('one') }
+  it { is_expected.to run.with_params(' one ').and_return(' one') }
+  it { is_expected.to run.with_params('     one ').and_return('     one') }
+  it { is_expected.to run.with_params("\tone ").and_return("\tone") }
+  it { is_expected.to run.with_params("\t one ").and_return("\t one") }
+  it { is_expected.to run.with_params("one\t").and_return('one') }
+  it { is_expected.to run.with_params(" one\t").and_return(' one') }
+  it { is_expected.to run.with_params("     one\t").and_return('     one') }
+  it { is_expected.to run.with_params("\tone\t").and_return("\tone") }
+  it { is_expected.to run.with_params("\t one\t").and_return("\t one") }
+  it { is_expected.to run.with_params(' o n e ').and_return(' o n e') }
+  it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return(' one') }
 end
index a62c1fb5e3f2799d505286649599f4f843e4b437..ebc3a732dadea5536f5d62bfcaa80bd52c4e6ff2 100755 (executable)
@@ -1,33 +1,33 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the shuffle function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'shuffle' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+  it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("shuffle")).to eq("function_shuffle")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_shuffle([]) }.to( raise_error(Puppet::ParseError))
-  end
+  context 'when running with a specific seed' do
+    # make tests deterministic
+    before(:each) { srand(2) }
 
-  it "should shuffle a string and the result should be the same size" do
-    result = scope.function_shuffle(["asdf"])
-    expect(result.size).to(eq(4))
-  end
+    it { is_expected.to run.with_params([]).and_return([]) }
+    it { is_expected.to run.with_params(['a']).and_return(['a']) }
+    it { is_expected.to run.with_params(['one']).and_return(['one']) }
+    it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(['two', 'one', 'three']) }
+    it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(['four', 'three', 'two', 'one']) }
 
-  it "should shuffle a string but the sorted contents should still be the same" do
-    result = scope.function_shuffle(["adfs"])
-    expect(result.split("").sort.join("")).to(eq("adfs"))
-  end
+    it { is_expected.to run.with_params('').and_return('') }
+    it { is_expected.to run.with_params('a').and_return('a') }
+    it { is_expected.to run.with_params('abc').and_return('bac') }
+    it { is_expected.to run.with_params('abcd').and_return('dcba') }
 
-  it "should accept objects which extend String" do
-    class AlsoString < String
+    context 'when using a class extending String' do
+      it { is_expected.to run.with_params(AlsoString.new('asdfghjkl')).and_return('lkhdsfajg') }
     end
-
-    value = AlsoString.new('asdf')
-    result = scope.function_shuffle([value])
-    result.size.should(eq(4))
   end
 end
index 18eb48f96d8d7d48bb319b0322e7285880d89542..6b64866830b8ffd174565b39f1d90f7c0433bf73 100755 (executable)
@@ -1,24 +1,32 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the size function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'size' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Unknown type given/) }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Unknown type given/) }
+  it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Unknown type given/) }
+  it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, /Requires either string or array to work/) }
+  it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, /Requires either string or array to work/) }
+  it { is_expected.to run.with_params([]).and_return(0) }
+  it { is_expected.to run.with_params(['a']).and_return(1) }
+  it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(3) }
+  it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(4) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("size")).to eq("function_size")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_size([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return the size of a string" do
-    result = scope.function_size(["asdf"])
-    expect(result).to(eq(4))
-  end
+  it { is_expected.to run.with_params('').and_return(0) }
+  it { is_expected.to run.with_params('a').and_return(1) }
+  it { is_expected.to run.with_params('abc').and_return(3) }
+  it { is_expected.to run.with_params('abcd').and_return(4) }
 
-  it "should return the size of an array" do
-    result = scope.function_size([["a","b","c"]])
-    expect(result).to(eq(3))
+  context 'when using a class extending String' do
+    it 'should call its size method' do
+      value = AlsoString.new('asdfghjkl')
+      value.expects(:size).returns('foo')
+      expect(subject).to run.with_params(value).and_return('foo')
+    end
   end
 end
index 4c2a66cf832db08a26e57a6548c5c3f954e3f57b..9abd039c13e7fd0e5bdcc62d35983be590bb8143 100755 (executable)
@@ -1,24 +1,24 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the sort function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("sort")).to eq("function_sort")
-  end
-
-  it "should raise a ParseError if there is not 1 arguments" do
-    expect { scope.function_sort(['','']) }.to( raise_error(Puppet::ParseError))
+describe 'sort' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { pending('stricter input checking'); is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /requires string or array/) }
+    it { pending('stricter input checking'); is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /requires string or array/) }
+    it { pending('stricter input checking'); is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /requires string or array/) }
   end
 
-  it "should sort an array" do
-    result = scope.function_sort([["a","c","b"]])
-    expect(result).to(eq(['a','b','c']))
+  context 'when called with an array' do
+    it { is_expected.to run.with_params([]).and_return([]) }
+    it { is_expected.to run.with_params(['a']).and_return(['a']) }
+    it { is_expected.to run.with_params(['c', 'b', 'a']).and_return(['a', 'b', 'c']) }
   end
 
-  it "should sort a string" do
-    result = scope.function_sort(["acb"])
-    expect(result).to(eq('abc'))
+  context 'when called with a string' do
+    it { is_expected.to run.with_params('').and_return('') }
+    it { is_expected.to run.with_params('a').and_return('a') }
+    it { is_expected.to run.with_params('cbda').and_return('abcd') }
   end
 end
index cd0eb37fc5a2ec217a868cb744b1ca3c6aaf100b..7f09c30ff3aa6c1ed388a2bd8f735963bba9e028 100755 (executable)
@@ -1,24 +1,44 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the squeeze function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'squeeze' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('', '', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(1).and_raise_error(NoMethodError) }
+  it { is_expected.to run.with_params({}).and_raise_error(NoMethodError) }
+  it { is_expected.to run.with_params(true).and_raise_error(NoMethodError) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("squeeze")).to eq("function_squeeze")
+  context 'when squeezing a single string' do
+    it { is_expected.to run.with_params('').and_return('') }
+    it { is_expected.to run.with_params('a').and_return('a') }
+    it { is_expected.to run.with_params('aaaaaaaaa').and_return('a') }
+    it { is_expected.to run.with_params('aaaaaaaaa', 'a').and_return('a') }
+    it { is_expected.to run.with_params('aaaaaaaaabbbbbbbbbbcccccccccc', 'b-c').and_return('aaaaaaaaabc') }
   end
 
-  it "should raise a ParseError if there is less than 2 arguments" do
-    expect { scope.function_squeeze([]) }.to( raise_error(Puppet::ParseError))
+  context 'when squeezing values in an array' do
+    it {
+      is_expected.to run \
+        .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc']) \
+        .and_return( ['', 'a', 'a',         'abc'])
+    }
+    it {
+      is_expected.to run \
+        .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'a') \
+        .and_return( ['', 'a', 'a',         'abbbbbbbbbbcccccccccc'])
+    }
+    it {
+      is_expected.to run \
+        .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'b-c') \
+        .and_return( ['', 'a', 'aaaaaaaaa', 'aaaaaaaaabc'])
+    }
   end
 
-  it "should squeeze a string" do
-    result = scope.function_squeeze(["aaabbbbcccc"])
-    expect(result).to(eq('abc'))
-  end
-
-  it "should squeeze all elements in an array" do
-    result = scope.function_squeeze([["aaabbbbcccc","dddfff"]])
-    expect(result).to(eq(['abc','df']))
+  context 'when using a class extending String' do
+    it 'should call its squeeze method' do
+      value = AlsoString.new('aaaaaaaaa')
+      value.expects(:squeeze).returns('foo')
+      expect(subject).to run.with_params(value).and_return('foo')
+    end
   end
 end
index 1d205d75d4769f6ca9dcc83acc23c0a51c8eed24..3b439b249b456b6c744b80b4da362cd7366dc8a8 100755 (executable)
@@ -1,31 +1,23 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the str2bool function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'str2bool' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params('true', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Unknown type of boolean given/) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("str2bool")).to eq("function_str2bool")
+  describe 'when testing values that mean "true"' do
+    [ '1', 't', 'y', 'true', 'yes', true ].each do |value|
+      it { is_expected.to run.with_params(value).and_return(true) }
+    end
   end
 
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_str2bool([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should convert string 'true' to true" do
-    result = scope.function_str2bool(["true"])
-    expect(result).to(eq(true))
-  end
-
-  it "should convert string 'undef' to false" do
-    result = scope.function_str2bool(["undef"])
-    expect(result).to(eq(false))
-  end
-
-  it "should return the boolean it was called with" do
-    result = scope.function_str2bool([true])
-    expect(result).to(eq(true))
-    result = scope.function_str2bool([false])
-    expect(result).to(eq(false))
+  describe 'when testing values that mean "false"' do
+    [ '', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined' ].each do |value|
+      it { is_expected.to run.with_params(value).and_return(false) }
+    end
   end
 end
index ab7f57f113f2ba10900a9b6ea33e090a603af6f5..2e1e818b5f7919e4b223e409c58701925173c654 100755 (executable)
@@ -1,45 +1,17 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the str2saltedsha512 function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'str2saltedsha512' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('password', 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires a String argument/) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("str2saltedsha512")).to eq("function_str2saltedsha512")
-  end
-
-  it "should raise a ParseError if there is less than 1 argument" do
-    expect { scope.function_str2saltedsha512([]) }.to( raise_error(Puppet::ParseError) )
-  end
-
-  it "should raise a ParseError if there is more than 1 argument" do
-    expect { scope.function_str2saltedsha512(['foo', 'bar', 'baz']) }.to( raise_error(Puppet::ParseError) )
-  end
-
-  it "should return a salted-sha512 password hash 136 characters in length" do
-    result = scope.function_str2saltedsha512(["password"])
-    expect(result.length).to(eq(136))
-  end
-
-  it "should raise an error if you pass a non-string password" do
-    expect { scope.function_str2saltedsha512([1234]) }.to( raise_error(Puppet::ParseError) )
-  end
-
-  it "should generate a valid password" do
-    # Allow the function to generate a password based on the string 'password'
-    password_hash = scope.function_str2saltedsha512(["password"])
-
-    # Separate the Salt and Password from the Password Hash
-    salt     = password_hash[0..7]
-    password = password_hash[8..-1]
-
-    # Convert the Salt and Password from Hex to Binary Data
-    str_salt     = Array(salt.lines).pack('H*')
-    str_password = Array(password.lines).pack('H*')
+  context 'when running with a specific seed' do
+    # make tests deterministic
+    before(:each) { srand(2) }
 
-    # Combine the Binary Salt with 'password' and compare the end result
-    saltedpass    = Digest::SHA512.digest(str_salt + 'password')
-    result        = (str_salt + saltedpass).unpack('H*')[0]
-    expect(result).to eq(password_hash)
+    it { is_expected.to run.with_params('').and_return('0f8a612f4eeed08e47b3875d00f33c5688f7926298f2d9b5fe19d1323f910bc78b6f7b5892596d2fabaa65e7a8d99b3768c102610cf0432c4827eee01f09451e3fae4f7a') }
+    it { is_expected.to run.with_params('password').and_return('0f8a612f43134376566c5707718d600effcfb17581fc9d3fa64d7f447dfda317c174ffdb498d2c5bd5c2075dab41c9d7ada5afbdc6b55354980eb5ba61802371e6b64956') }
+    it { is_expected.to run.with_params('verylongpassword').and_return('0f8a612f7a448537540e062daa8621f9bae326ca8ccb899e1bdb10e7c218cebfceae2530b856662565fdc4d81e986fc50cfbbc46d50436610ed9429ff5e43f2c45b5d039') }
   end
 end
index ebec54b80fc624028aa780746ae1a88d2a9536eb..e76774aa33e1406194b3041d22ec5fb47eede033 100755 (executable)
@@ -1,9 +1,6 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the strftime function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
+describe 'strftime' do
   it "should exist" do
     expect(Puppet::Parser::Functions.function("strftime")).to eq("function_strftime")
   end
index 4ac8daf8638288cae5eb5104306a822757e3a110..689b6dd0c12a1b5da104aed7f1977372b81ae5b0 100755 (executable)
@@ -1,27 +1,34 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the strip function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("strip")).to eq("function_strip")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_strip([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should strip a string" do
-    result = scope.function_strip([" ab cd "])
-    expect(result).to(eq('ab cd'))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new(' as df ')
-    result = scope.function_strip([value])
-    result.should(eq('as df'))
-  end
+describe 'strip' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work with/) }
+  it { is_expected.to run.with_params('').and_return('') }
+  it { is_expected.to run.with_params(' ').and_return('') }
+  it { is_expected.to run.with_params('     ').and_return('') }
+  it { is_expected.to run.with_params("\t").and_return('') }
+  it { is_expected.to run.with_params("\t ").and_return('') }
+  it { is_expected.to run.with_params('one').and_return('one') }
+  it { is_expected.to run.with_params(' one').and_return('one') }
+  it { is_expected.to run.with_params('     one').and_return('one') }
+  it { is_expected.to run.with_params("\tone").and_return('one') }
+  it { is_expected.to run.with_params("\t one").and_return('one') }
+  it { is_expected.to run.with_params('one ').and_return('one') }
+  it { is_expected.to run.with_params(' one ').and_return('one') }
+  it { is_expected.to run.with_params('     one ').and_return('one') }
+  it { is_expected.to run.with_params("\tone ").and_return('one') }
+  it { is_expected.to run.with_params("\t one ").and_return('one') }
+  it { is_expected.to run.with_params("one \t").and_return('one') }
+  it { is_expected.to run.with_params(" one \t").and_return('one') }
+  it { is_expected.to run.with_params("     one \t").and_return('one') }
+  it { is_expected.to run.with_params("\tone \t").and_return('one') }
+  it { is_expected.to run.with_params("\t one \t").and_return('one') }
+  it { is_expected.to run.with_params(' o n e ').and_return('o n e') }
+  it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return('one') }
 end
index c7783c64d81d147b0d58adf8a679abd1f7f81310..b48a4ebf6a0df4627e69b379639f7a22fb4e5649 100755 (executable)
@@ -1,27 +1,44 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the suffix function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "raises a ParseError if there is less than 1 arguments" do
-    expect { scope.function_suffix([]) }.to raise_error(Puppet::ParseError, /number of arguments/)
-  end
-
-  it "raises an error if the first argument is not an array" do
-    expect {
-      scope.function_suffix([Object.new])
-    }.to raise_error(Puppet::ParseError, /expected first argument to be an Array/)
-  end
-
-  it "raises an error if the second argument is not a string" do
-    expect {
-      scope.function_suffix([['first', 'second'], 42])
-    }.to raise_error(Puppet::ParseError, /expected second argument to be a String/)
-  end
-
-  it "returns a suffixed array" do
-    result = scope.function_suffix([['a','b','c'], 'p'])
-    expect(result).to(eq(['ap','bp','cp']))
-  end
+describe 'suffix' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the second.")
+    is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /expected first argument to be an Array/) }
+  it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, /expected second argument to be a String/) }
+  it { is_expected.to run.with_params([]).and_return([]) }
+  it { is_expected.to run.with_params(['one', 2]).and_return(['one', '2']) }
+  it { is_expected.to run.with_params([], '').and_return([]) }
+  it { is_expected.to run.with_params([''], '').and_return(['']) }
+  it { is_expected.to run.with_params(['one'], 'post').and_return(['onepost']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], 'post').and_return(['onepost', 'twopost', 'threepost']) }
+  it {
+    pending("implementation of Hash functionality matching prefix")
+    is_expected.to run.with_params({}).and_return({})
+  }
+  it {
+    pending("implementation of Hash functionality matching prefix")
+    is_expected.to run.with_params({ 'key1' => 'value1', 2 => 3}).and_return({ 'key1' => 'value1', '2' => 3 })
+  }
+  it {
+    pending("implementation of Hash functionality matching prefix")
+    is_expected.to run.with_params({}, '').and_return({})
+  }
+  it {
+    pending("implementation of Hash functionality matching prefix")
+    is_expected.to run.with_params({ 'key' => 'value' }, '').and_return({ 'key' => 'value' })
+  }
+  it {
+    pending("implementation of Hash functionality matching prefix")
+    is_expected.to run.with_params({ 'key' => 'value' }, 'post').and_return({ 'keypost' => 'value' })
+  }
+  it {
+    pending("implementation of Hash functionality matching prefix")
+    is_expected.to run \
+      .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'pre') \
+      .and_return({ 'key1post' => 'value1', 'key2post' => 'value2', 'key3post' => 'value3' })
+  }
 end
index 791d1dfaebc0c6974dc3f9f9ea7e4987f04628fe..c175a15889ea743d8fa152c56866967401627d67 100755 (executable)
@@ -1,28 +1,40 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the swapcase function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("swapcase")).to eq("function_swapcase")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_swapcase([]) }.to( raise_error(Puppet::ParseError))
+describe 'swapcase' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+  it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+  it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+  describe 'with strings as inputs' do
+    it { is_expected.to run.with_params('').and_return('') }
+    it { is_expected.to run.with_params('one').and_return('ONE') }
+    it { is_expected.to run.with_params('ONE').and_return('one') }
+    it { is_expected.to run.with_params('oNe').and_return('OnE') }
   end
-
-  it "should swapcase a string" do
-    result = scope.function_swapcase(["aaBBccDD"])
-    expect(result).to(eq('AAbbCCdd'))
+  describe 'with arrays as inputs' do
+    it { is_expected.to run.with_params([]).and_return([]) }
+    describe 'only containing strings' do
+      it { is_expected.to run.with_params(['']).and_return(['']) }
+      it { is_expected.to run.with_params(['one']).and_return(['ONE']) }
+      it { is_expected.to run.with_params(['ONE']).and_return(['one']) }
+      it { is_expected.to run.with_params(['oNe']).and_return(['OnE']) }
+      it { is_expected.to run.with_params(['one', 'ONE']).and_return(['ONE', 'one']) }
+      it { is_expected.to run.with_params(['ONE', 'OnE']).and_return(['one', 'oNe']) }
+      it { is_expected.to run.with_params(['oNe', 'one']).and_return(['OnE', 'ONE']) }
+    end
+    describe 'containing mixed types' do
+      it { is_expected.to run.with_params(['OnE', {}]).and_return(['oNe', {}]) }
+      it { is_expected.to run.with_params(['OnE', 1]).and_return(['oNe', 1]) }
+      it { is_expected.to run.with_params(['OnE', []]).and_return(['oNe', []]) }
+      it { is_expected.to run.with_params(['OnE', ['two']]).and_return(['oNe', ['two']]) }
+    end
   end
-
   it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new("aaBBccDD")
-    result = scope.function_swapcase([value])
-    result.should(eq("AAbbCCdd"))
+    is_expected.to run.with_params(AlsoString.new("OnE")).and_return('oNe')
   end
 end
index 6e22515f1a6da3897d182332b211b67c8f40ba37..2875e25ac7a84ff463f49f589d755ab01a804fad 100755 (executable)
@@ -1,29 +1,28 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the time function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'time' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("time")).to eq("function_time")
-  end
-
-  it "should raise a ParseError if there is more than 2 arguments" do
-    expect { scope.function_time(['','']) }.to( raise_error(Puppet::ParseError))
-  end
+  context 'when running at a specific time' do
+    before(:each) {
+      # get a value before stubbing the function
+      test_time = Time.utc(2006, 10, 13, 8, 15, 11, '+01:00')
+      Time.expects(:new).with().returns(test_time).once
+    }
+    it { is_expected.to run.with_params().and_return(1160727311) }
+    it { is_expected.to run.with_params('').and_return(1160727311) }
+    it { is_expected.to run.with_params([]).and_return(1160727311) }
+    it { is_expected.to run.with_params({}).and_return(1160727311) }
+    it { is_expected.to run.with_params('foo').and_return(1160727311) }
+    it { is_expected.to run.with_params('UTC').and_return(1160727311) }
 
-  it "should return a number" do
-    result = scope.function_time([])
-    expect(result).to be_an(Integer)
-  end
-
-  it "should be higher then when I wrote this test" do
-    result = scope.function_time([])
-    expect(result).to(be > 1311953157)
-  end
+    context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+      it { is_expected.to run.with_params('America/Los_Angeles').and_return(1160727311) }
+    end
 
-  it "should be lower then 1.5 trillion" do
-    result = scope.function_time([])
-    expect(result).to(be < 1500000000)
+    context 'when running on ruby 1.8.7, which garbles the TZ', :if => RUBY_VERSION == '1.8.7' do
+      it { is_expected.to run.with_params('America/Los_Angeles').and_return(1160702111) }
+    end
   end
 end
index 0f6ade912c105c2d8710bb3bd94b08f78b73de08..2be23ff2d0039d8cc224580a77f40aaf352d789e 100755 (executable)
@@ -1,83 +1,72 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the to_bytes function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("to_bytes")).to eq("function_to_bytes")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_to_bytes([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should convert kB to B" do
-    result = scope.function_to_bytes(["4 kB"])
-    expect(result).to(eq(4096))
-  end
-
-  it "should convert MB to B" do
-    result = scope.function_to_bytes(["4 MB"])
-    expect(result).to(eq(4194304))
-  end
-
-  it "should convert GB to B" do
-    result = scope.function_to_bytes(["4 GB"])
-    expect(result).to(eq(4294967296))
-  end
-
-  it "should convert TB to B" do
-    result = scope.function_to_bytes(["4 TB"])
-    expect(result).to(eq(4398046511104))
-  end
-
-  it "should convert PB to B" do
-    result = scope.function_to_bytes(["4 PB"])
-    expect(result).to(eq(4503599627370496))
-  end
-
-  it "should convert PB to B" do
-    result = scope.function_to_bytes(["4 EB"])
-    expect(result).to(eq(4611686018427387904))
-  end
-
-  it "should work without B in unit" do
-    result = scope.function_to_bytes(["4 k"])
-    expect(result).to(eq(4096))
-  end
-
-  it "should work without a space before unit" do
-    result = scope.function_to_bytes(["4k"])
-    expect(result).to(eq(4096))
-  end
-
-  it "should work without a unit" do
-    result = scope.function_to_bytes(["5678"])
-    expect(result).to(eq(5678))
-  end
-
-  it "should convert fractions" do
-    result = scope.function_to_bytes(["1.5 kB"])
-    expect(result).to(eq(1536))
-  end
-
-  it "should convert scientific notation" do
-    result = scope.function_to_bytes(["1.5e2 B"])
-    expect(result).to(eq(150))
-  end
-
-  it "should do nothing with a positive number" do
-    result = scope.function_to_bytes([5678])
-    expect(result).to(eq(5678))
-  end
-
-  it "should should raise a ParseError if input isn't a number" do
-    expect { scope.function_to_bytes(["foo"]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should should raise a ParseError if prefix is unknown" do
-    expect { scope.function_to_bytes(["5 uB"]) }.to( raise_error(Puppet::ParseError))
+describe 'to_bytes' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params('1', 'extras').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params([]).and_raise_error(TypeError, /(can't convert|no implicit conversion of) Array (in)?to String/) }
+  it { is_expected.to run.with_params({}).and_raise_error(TypeError, /(can't convert|no implicit conversion of) Hash (in)?to String/) }
+  it { is_expected.to run.with_params(true).and_raise_error(TypeError, /(can't convert|no implicit conversion of) (TrueClass|true) (in)?to String/) }
+
+  describe 'when passing numbers' do
+    it { is_expected.to run.with_params(0).and_return(0) }
+    it { is_expected.to run.with_params(1).and_return(1) }
+    it { is_expected.to run.with_params(-1).and_return(-1) }
+    it { is_expected.to run.with_params(1.1).and_return(1.1) }
+    it { is_expected.to run.with_params(-1.1).and_return(-1.1) }
+  end
+
+  describe 'when passing numbers as strings' do
+    describe 'without a unit' do
+      it { is_expected.to run.with_params('1').and_return(1) }
+      it { is_expected.to run.with_params('-1').and_return(-1) }
+      # these are so wrong
+      it { is_expected.to run.with_params('1.1').and_return(1) }
+      it { is_expected.to run.with_params('-1.1').and_return(-1) }
+    end
+
+    describe 'with a unit' do
+      it { is_expected.to run.with_params('1k').and_return(1024) }
+      it { is_expected.to run.with_params('-1kB').and_return(-1024) }
+      it { is_expected.to run.with_params('1k').and_return(1024) }
+      it { is_expected.to run.with_params('1M').and_return(1024*1024) }
+      it { is_expected.to run.with_params('1G').and_return(1024*1024*1024) }
+      it { is_expected.to run.with_params('1T').and_return(1024*1024*1024*1024) }
+      it { is_expected.to run.with_params('1P').and_return(1024*1024*1024*1024*1024) }
+      it { is_expected.to run.with_params('1E').and_return(1024*1024*1024*1024*1024*1024) }
+      it { is_expected.to run.with_params('1.5e3M').and_return(1572864000) }
+
+      it { is_expected.to run.with_params('4k').and_return(4*1024) }
+      it { is_expected.to run.with_params('-4kB').and_return(4*-1024) }
+      it { is_expected.to run.with_params('4k').and_return(4*1024) }
+      it { is_expected.to run.with_params('4M').and_return(4*1024*1024) }
+      it { is_expected.to run.with_params('4G').and_return(4*1024*1024*1024) }
+      it { is_expected.to run.with_params('4T').and_return(4*1024*1024*1024*1024) }
+      it { is_expected.to run.with_params('4P').and_return(4*1024*1024*1024*1024*1024) }
+      it { is_expected.to run.with_params('4E').and_return(4*1024*1024*1024*1024*1024*1024) }
+
+      # these are so wrong
+      it { is_expected.to run.with_params('1.0001 k').and_return(1024) }
+      it { is_expected.to run.with_params('-1.0001 kB').and_return(-1024) }
+    end
+
+    describe 'with a unknown unit' do
+      it { is_expected.to run.with_params('1KB').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+      it { is_expected.to run.with_params('1K').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+      it { is_expected.to run.with_params('1mb').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+      it { is_expected.to run.with_params('1m').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+      it { is_expected.to run.with_params('1%').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+      it { is_expected.to run.with_params('1 p').and_raise_error(Puppet::ParseError, /Unknown prefix/) }
+    end
+  end
+
+  # these are so wrong
+  describe 'when passing random stuff' do
+    it { is_expected.to run.with_params('-1....1').and_return(-1) }
+    it { is_expected.to run.with_params('-1.e.e.e.1').and_return(-1) }
+    it { is_expected.to run.with_params('-1+1').and_return(-1) }
+    it { is_expected.to run.with_params('1-1').and_return(1) }
+    it { is_expected.to run.with_params('1 kaboom').and_return(1024) }
+    it { is_expected.to run.with_params('kaboom').and_return(0) }
   end
 end
index d21236a6197abe4eaf2f7f0fa9d68d67308f8eea..c3eb1deee69e1f1747b6dd2a0d5f1b708a80cf4e 100644 (file)
@@ -1,8 +1,6 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the type3x function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'type3x' do
   it "should exist" do
     expect(Puppet::Parser::Functions.function("type3x")).to eq("function_type3x")
   end
index b8234600cc9d02bc1d339468da1edb5448e06910..f77099031e0f0fb99ff03b7e8caf89f52f85047c 100644 (file)
@@ -1,13 +1,23 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe 'type_of', :if => Puppet.version.to_f >= 4.0 do
-  it 'gives the type of a string' do
-    expect(subject.call_function('type_of', 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType)
+if ENV["FUTURE_PARSER"] == 'yes'
+  describe 'type_of' do
+    pending 'teach rspec-puppet to load future-only functions under 3.7.5'
   end
+end
+
+if Puppet.version.to_f >= 4.0
+  describe 'type_of' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+    it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) }
+
+    it 'gives the type of a string' do
+      expect(subject.call({}, 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType)
+    end
 
-  it 'gives the type of an integer' do
-    expect(subject.call_function('type_of', 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType)
+    it 'gives the type of an integer' do
+      expect(subject.call({}, 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType)
+    end
   end
 end
index b683fcfa4973175e6c5f2a14835fed29087880cf..4288df09b199235d2c9adc83e6f17c22b345c93b 100755 (executable)
@@ -1,8 +1,6 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the type function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+describe 'type' do
   it "should exist" do
     expect(Puppet::Parser::Functions.function("type")).to eq("function_type")
   end
index 706f4cbcd04942c2cd07a5a5d869839c22d13731..970e1fe5046dc29a7e7726fdabf8e0d7b623ffae 100755 (executable)
@@ -1,19 +1,23 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the union function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("union")).to eq("function_union")
-  end
-
-  it "should raise a ParseError if there are fewer than 2 arguments" do
-    expect { scope.function_union([]) }.to( raise_error(Puppet::ParseError) )
+describe 'union' do
+  describe 'argument checking' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
+    it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
+    it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
   end
 
-  it "should join two arrays together" do
-    result = scope.function_union([["a","b","c"],["b","c","d"]])
-    expect(result).to(eq(["a","b","c","d"]))
-  end
+  it { is_expected.to run.with_params([], []).and_return([]) }
+  it { is_expected.to run.with_params([], ['one']).and_return(['one']) }
+  it { is_expected.to run.with_params(['one'], []).and_return(['one']) }
+  it { is_expected.to run.with_params(['one'], ['one']).and_return(['one']) }
+  it { is_expected.to run.with_params(['one'], ['two']).and_return(['one', 'two']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
+  it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
+  it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['one', 'two', 'three']) }
+  it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return(['1', '2', '3', 1, 2]) end
 end
index 7cd3a566f38e90e2a19a085dc4bd50e4b5c023d5..24257a018be2ed65a92765a2cb0902173cf5e2e8 100755 (executable)
@@ -1,33 +1,27 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the unique function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("unique")).to eq("function_unique")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_unique([]) }.to( raise_error(Puppet::ParseError))
+describe 'unique' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it {
+      pending("Current implementation ignores parameters after the first.")
+      is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+    }
+    it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+    it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+    it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
   end
 
-  it "should remove duplicate elements in a string" do
-    result = scope.function_unique(["aabbc"])
-    expect(result).to(eq('abc'))
+  context 'when called with an array' do
+    it { is_expected.to run.with_params([]).and_return([]) }
+    it { is_expected.to run.with_params(['a']).and_return(['a']) }
+    it { is_expected.to run.with_params(['a', 'b', 'a']).and_return(['a', 'b']) }
   end
 
-  it "should remove duplicate elements in an array" do
-    result = scope.function_unique([["a","a","b","b","c"]])
-    expect(result).to(eq(['a','b','c']))
-  end
-
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new('aabbc')
-    result = scope.function_unique([value])
-    result.should(eq('abc'))
+  context 'when called with a string' do
+    it { is_expected.to run.with_params('').and_return('') }
+    it { is_expected.to run.with_params('a').and_return('a') }
+    it { is_expected.to run.with_params('aaba').and_return('ab') }
   end
 end
index 0689099cd4d8b10f4335f02d1d393a2c54aa5363..3b7b02d472e0105fb24ceec84e6004bee22b4631 100755 (executable)
@@ -1,58 +1,26 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the upcase function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("upcase")).to eq("function_upcase")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_upcase([]) }.to(raise_error(Puppet::ParseError))
+describe 'upcase' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires an array, hash or object that responds to upcase/) }
+    it { is_expected.to run.with_params([1]).and_raise_error(Puppet::ParseError, /Requires an array, hash or object that responds to upcase/) }
   end
 
-  it "should upcase a string" do
-    result = scope.function_upcase(["abc"])
-    expect(result).to(eq('ABC'))
+  describe 'normal string handling' do
+    it { is_expected.to run.with_params("abc").and_return("ABC") }
+    it { is_expected.to run.with_params("Abc").and_return("ABC") }
+    it { is_expected.to run.with_params("ABC").and_return("ABC") }
   end
 
-  it "should do nothing if a string is already upcase" do
-    result = scope.function_upcase(["ABC"])
-    expect(result).to(eq('ABC'))
+  describe 'handling classes derived from String' do
+    it { is_expected.to run.with_params(AlsoString.new("ABC")).and_return("ABC") }
   end
 
-  it "should accept objects which extend String" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new('abc')
-    result = scope.function_upcase([value])
-    result.should(eq('ABC'))
-  end
-
-  it 'should accept hashes and return uppercase' do
-    expect(
-        scope.function_upcase([{'test' => %w(this that and other thing)}])
-    ).to eq({'TEST' => %w(THIS THAT AND OTHER THING)})
-  end
-
-  if :test.respond_to?(:upcase)
-    it 'should accept hashes of symbols' do
-      expect(
-          scope.function_upcase([{:test => [:this, :that, :other]}])
-      ).to eq({:TEST => [:THIS, :THAT, :OTHER]})
-    end
-    it 'should return upcase symbol' do
-      expect(
-          scope.function_upcase([:test])
-      ).to eq(:TEST)
-    end
-    it 'should return mixed objects in upcease' do
-      expect(
-          scope.function_upcase([[:test, 'woot']])
-      ).to eq([:TEST, 'WOOT'])
-
-    end
+  describe 'strings in arrays handling' do
+    it { is_expected.to run.with_params([]).and_return([]) }
+    it { is_expected.to run.with_params(["One", "twO"]).and_return(["ONE", "TWO"]) }
   end
 end
index d0f37de1d4fcbe3e212389751f0115a0bfb32ef0..f05ec088ca7c119cae1fe95fb732a806532219d2 100755 (executable)
@@ -1,40 +1,36 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the uriescape function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("uriescape")).to eq("function_uriescape")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_uriescape([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should uriescape a string" do
-    result = scope.function_uriescape([":/?#[]@!$&'()*+,;= \"{}"])
-    expect(result).to(eq(':/?%23[]@!$&\'()*+,;=%20%22%7B%7D'))
+describe 'uriescape' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it {
+      pending("Current implementation ignores parameters after the first.")
+      is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+    }
+    it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+    it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
+    it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) }
   end
 
-  it "should uriescape an array of strings, while not touching up nonstrings" do
-    teststring = ":/?#[]@!$&'()*+,;= \"{}"
-    expectstring = ':/?%23[]@!$&\'()*+,;=%20%22%7B%7D'
-    result = scope.function_uriescape([[teststring, teststring, 1]])
-    expect(result).to(eq([expectstring, expectstring, 1]))
-  end
-
-  it "should do nothing if a string is already safe" do
-    result = scope.function_uriescape(["ABCdef"])
-    expect(result).to(eq('ABCdef'))
+  describe 'handling normal strings' do
+    it 'should call ruby\'s URI.escape function' do
+      URI.expects(:escape).with('uri_string').returns('escaped_uri_string').once
+      is_expected.to run.with_params('uri_string').and_return('escaped_uri_string') 
+    end
   end
 
-  it "should accept objects which extend String" do
-    class AlsoString < String
+  describe 'handling classes derived from String' do
+    it 'should call ruby\'s URI.escape function' do
+      uri_string = AlsoString.new('uri_string')
+      URI.expects(:escape).with(uri_string).returns('escaped_uri_string').once
+      is_expected.to run.with_params(uri_string).and_return("escaped_uri_string") 
     end
+  end
 
-    value = AlsoString.new('abc')
-    result = scope.function_uriescape([value])
-    result.should(eq('abc'))
+  describe 'strings in arrays handling' do
+    it { is_expected.to run.with_params([]).and_return([]) }
+    it { is_expected.to run.with_params(["one}", "two"]).and_return(["one%7D", "two"]) }
+    it { is_expected.to run.with_params(["one}", 1, true, {}, "two"]).and_return(["one%7D", 1, true, {}, "two"]) }
   end
 end
index 36c836bdb7b2a7f34c2203e4854d76dcbee95d4c..4a8404d813d0281b89e46582359004b9c40e7e7d 100755 (executable)
@@ -1,67 +1,32 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_absolute_path) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  # The subject of these examples is the method itself.
-  subject do
-    # This makes sure the function is loaded within each test
-    function_name = Puppet::Parser::Functions.function(:validate_absolute_path)
-    scope.method(function_name)
+describe 'validate_absolute_path' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
   end
 
-  describe "Valid Paths" do
-    def self.valid_paths
-      %w{
-        C:/
-        C:\\
-        C:\\WINDOWS\\System32
-        C:/windows/system32
-        X:/foo/bar
-        X:\\foo\\bar
-        /var/tmp
-        /var/lib/puppet
-        /var/opt/../lib/puppet
-      }
-    end
-
-    context "Without Puppet::Util.absolute_path? (e.g. Puppet <= 2.6)" do
-      before :each do
-        # The intent here is to mock Puppet to behave like Puppet 2.6 does.
-        # Puppet 2.6 does not have the absolute_path? method.  This is only a
-        # convenience test, stdlib should be run with the Puppet 2.6.x in the
-        # $LOAD_PATH in addition to 2.7.x and master.
-        Puppet::Util.expects(:respond_to?).with(:absolute_path?).returns(false)
-      end
-      valid_paths.each do |path|
-        it "validate_absolute_path(#{path.inspect}) should not fail" do
-          expect { subject.call [path] }.not_to raise_error
-        end
-      end
-      valid_paths do
-        it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
-          expect { subject.call [valid_paths] }.not_to raise_error
-        end
-      end
-    end
-
-    context "Puppet without mocking" do
-      valid_paths.each do |path|
-        it "validate_absolute_path(#{path.inspect}) should not fail" do
-          expect { subject.call [path] }.not_to raise_error
-        end
-      end
-      valid_paths do
-        it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
-          expect { subject.call [valid_paths] }.not_to raise_error
-        end
-      end
+  describe "valid paths handling" do
+    %w{
+      C:/
+      C:\\
+      C:\\WINDOWS\\System32
+      C:/windows/system32
+      X:/foo/bar
+      X:\\foo\\bar
+      \\\\host\\windows
+      //host/windows
+      /
+      /var/tmp
+      /var/opt/../lib/puppet
+    }.each do |path|
+      it { is_expected.to run.with_params(path) }
+      it { is_expected.to run.with_params(['/tmp', path]) }
     end
   end
 
-  describe 'Invalid paths' do
-    context 'Garbage inputs' do
+  describe 'invalid path handling' do
+    context 'garbage inputs' do
       [
         nil,
         [ nil ],
@@ -70,33 +35,26 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
         { },
         '',
       ].each do |path|
-        it "validate_absolute_path(#{path.inspect}) should fail" do
-          expect { subject.call [path] }.to raise_error Puppet::ParseError
-        end
+        it { is_expected.to run.with_params(path).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
+        it { is_expected.to run.with_params([path]).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
+        it { is_expected.to run.with_params(['/tmp', path]).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
       end
     end
 
-    context 'Relative paths' do
-      def self.rel_paths 
-        %w{
-          relative1
-          .
-          ..
-          ./foo
-          ../foo
-          etc/puppetlabs/puppet
-          opt/puppet/bin
-        }
-      end
-      rel_paths.each do |path|
-        it "validate_absolute_path(#{path.inspect}) should fail" do
-          expect { subject.call [path] }.to raise_error Puppet::ParseError
-        end
-      end
-      rel_paths do
-        it "validate_absolute_path(#{rel_paths.inspect}) should fail" do
-          expect { subject.call [rel_paths] }.to raise_error Puppet::ParseError
-        end
+    context 'relative paths' do
+      %w{
+        relative1
+        .
+        ..
+        ./foo
+        ../foo
+        etc/puppetlabs/puppet
+        opt/puppet/bin
+        relative\\windows
+      }.each do |path|
+        it { is_expected.to run.with_params(path).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
+        it { is_expected.to run.with_params([path]).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
+        it { is_expected.to run.with_params(['/tmp', path]).and_raise_error(Puppet::ParseError, /is not an absolute path/) }
       end
     end
   end
index 4b31cfde42fcb152eef0e8022c8e153c0d302c23..4ee7754f8f7aa159419d10791c4a8531727b6d5a 100755 (executable)
@@ -1,38 +1,27 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_array) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-  describe 'when calling validate_array from puppet' do
-
-    %w{ true false }.each do |the_string|
-      it "should not compile when #{the_string} is a string" do
-        Puppet[:code] = "validate_array('#{the_string}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
-      end
+describe 'validate_array' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
-      it "should not compile when #{the_string} is a bare word" do
-        Puppet[:code] = "validate_array(#{the_string})"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
-      end
+    describe 'valid inputs' do
+      it { is_expected.to run.with_params([]) }
+      it { is_expected.to run.with_params(['one']) }
+      it { is_expected.to run.with_params([], ['two']) }
+      it { is_expected.to run.with_params(['one'], ['two']) }
     end
 
-    it "should compile when multiple array arguments are passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = [ ]
-        $bar = [ 'one', 'two' ]
-        validate_array($foo, $bar)
-      ENDofPUPPETcode
-      scope.compiler.compile
-    end
-
-    it "should not compile when an undef variable is passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = undef
-        validate_array($foo)
-      ENDofPUPPETcode
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
+    describe 'invalid inputs' do
+      it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not an Array/) }
+      it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not an Array/) }
+      it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not an Array/) }
+      it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not an Array/) }
+      it { is_expected.to run.with_params([], {}).and_raise_error(Puppet::ParseError, /is not an Array/) }
+      it { is_expected.to run.with_params([], 1).and_raise_error(Puppet::ParseError, /is not an Array/) }
+      it { is_expected.to run.with_params([], true).and_raise_error(Puppet::ParseError, /is not an Array/) }
+      it { is_expected.to run.with_params([], 'one').and_raise_error(Puppet::ParseError, /is not an Array/) }
     end
   end
 end
+
index 99523ab7762c43babb38a8591b21ceca0778c0cd..4236649d68ac1d9dcfe72ede51bd9cdb19c11f8d 100755 (executable)
@@ -1,49 +1,29 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.features.augeas? do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  # The subject of these examplres is the method itself.
-  subject do
-    # This makes sure the function is loaded within each test
-    function_name = Puppet::Parser::Functions.function(:validate_augeas)
-    scope.method(function_name)
-  end
-
-  context 'Using Puppet::Parser::Scope.new' do
-
-    describe 'Garbage inputs' do
-      inputs = [
-        [ nil ],
-        [ [ nil ] ],
-        [ { 'foo' => 'bar' } ],
-        [ { } ],
-        [ '' ],
-        [ "one", "one", "MSG to User", "4th arg" ],
-      ]
-
-      inputs.each do |input|
-        it "validate_augeas(#{input.inspect}) should fail" do
-          expect { subject.call [input] }.to raise_error Puppet::ParseError
-        end
-      end
+describe 'validate_augeas' do
+  unless Puppet.features.augeas?
+    skip "ruby-augeas not installed"
+  else
+    describe 'signature validation' do
+      it { is_expected.not_to eq(nil) }
+      it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+      it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+      it { is_expected.to run.with_params('', '', [], '', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+      it { is_expected.to run.with_params('one', 'one', 'MSG to User', '4th arg').and_raise_error(NoMethodError) }
     end
 
-    describe 'Valid inputs' do
+    describe 'valid inputs' do
       inputs = [
         [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns' ],
         [ "proc /proc   proc    nodev,noexec,nosuid     0       0\n", 'Fstab.lns'],
       ]
 
       inputs.each do |input|
-        it "validate_augeas(#{input.inspect}) should not fail" do
-          expect { subject.call input }.not_to raise_error
-        end
+        it { is_expected.to run.with_params(*input) }
       end
     end
 
-    describe "Valid inputs which should raise an exception without a message" do
+    describe 'valid inputs which fail augeas validation' do
       # The intent here is to make sure valid inputs raise exceptions when they
       # don't specify an error message to display.  This is the behvior in
       # 2.2.x and prior.
@@ -53,13 +33,11 @@ describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.fea
       ]
 
       inputs.each do |input|
-        it "validate_augeas(#{input.inspect}) should fail" do
-          expect { subject.call input }.to raise_error /validate_augeas.*?matched less than it should/
-        end
+        it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, /validate_augeas.*?matched less than it should/) }
       end
     end
 
-    describe "Nicer Error Messages" do
+    describe "when specifying nice error messages" do
       # The intent here is to make sure the function returns the 4th argument
       # in the exception thrown
       inputs = [
@@ -68,35 +46,29 @@ describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.fea
       ]
 
       inputs.each do |input|
-        it "validate_augeas(#{input.inspect}) should fail" do
-          expect { subject.call input }.to raise_error /#{input[3]}/
-        end
+        it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, /#{input[3]}/) }
       end
     end
 
-    describe "Passing simple unit tests" do
+    describe "matching additional tests" do
       inputs = [
         [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
         [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
       ]
 
       inputs.each do |input|
-        it "validate_augeas(#{input.inspect}) should fail" do
-          expect { subject.call input }.not_to raise_error
-        end
+        it { is_expected.to run.with_params(*input) }
       end
     end
 
-    describe "Failing simple unit tests" do
+    describe "failing additional tests" do
       inputs = [
         [ "foobar:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
         [ "root:x:0:0:root:/root:/bin/sh\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
       ]
 
       inputs.each do |input|
-        it "validate_augeas(#{input.inspect}) should fail" do
-          expect { subject.call input }.to raise_error /testing path/
-        end
+        it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, /testing path/) }
       end
     end
   end
index a352d3b550b352976680f202814d496d3dcda532..d9cdf572e8e3b1c97c504337e5617332fcfa3c4b 100755 (executable)
@@ -1,51 +1,25 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_bool) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-  describe 'when calling validate_bool from puppet' do
-
-    %w{ true false }.each do |the_string|
-
-      it "should not compile when #{the_string} is a string" do
-        Puppet[:code] = "validate_bool('#{the_string}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
-      end
-
-      it "should compile when #{the_string} is a bare word" do
-        Puppet[:code] = "validate_bool(#{the_string})"
-        scope.compiler.compile
-      end
-
-    end
-
-    it "should not compile when an arbitrary string is passed" do
-      Puppet[:code] = 'validate_bool("jeff and dan are awesome")'
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
-    end
-
-    it "should not compile when no arguments are passed" do
-      Puppet[:code] = 'validate_bool()'
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
+describe 'validate_bool' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  end
 
-    it "should compile when multiple boolean arguments are passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = true
-        $bar = false
-        validate_bool($foo, $bar, true, false)
-      ENDofPUPPETcode
-      scope.compiler.compile
-    end
+  describe 'acceptable values' do
+    it { is_expected.to run.with_params(true) }
+    it { is_expected.to run.with_params(false) }
+    it { is_expected.to run.with_params(true, false, false, true) }
+  end
 
-    it "should compile when multiple boolean arguments are passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = true
-        $bar = false
-        validate_bool($foo, $bar, true, false, 'jeff')
-      ENDofPUPPETcode
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
-    end
+  describe 'validation failures' do
+    it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a boolean/) }
+    it { is_expected.to run.with_params(true, 'one').and_raise_error(Puppet::ParseError, /is not a boolean/) }
+    it { is_expected.to run.with_params('one', false).and_raise_error(Puppet::ParseError, /is not a boolean/) }
+    it { is_expected.to run.with_params("true").and_raise_error(Puppet::ParseError, /is not a boolean/) }
+    it { is_expected.to run.with_params("false").and_raise_error(Puppet::ParseError, /is not a boolean/) }
+    it { is_expected.to run.with_params(true, "false").and_raise_error(Puppet::ParseError, /is not a boolean/) }
+    it { is_expected.to run.with_params("true", false).and_raise_error(Puppet::ParseError, /is not a boolean/) }
+    it { is_expected.to run.with_params("true", false, false, false, false, false).and_raise_error(Puppet::ParseError, /is not a boolean/) }
   end
 end
index 7cb9782d60ec9303c577c981f57a0fee6e655780..ab0cbc9a7bc798c362dc958b2233952043feb456 100755 (executable)
@@ -1,85 +1,35 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-TESTEXE = File.exists?('/usr/bin/test') ? '/usr/bin/test' : '/bin/test'
-TOUCHEXE = File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch'
-
-describe Puppet::Parser::Functions.function(:validate_cmd) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  subject do
-    function_name = Puppet::Parser::Functions.function(:validate_cmd)
-    scope.method(function_name)
+describe 'validate_cmd' do
+  let(:touch) { File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch' }
+
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('', '', '', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it {
+      pending('should implement stricter type checking')
+      is_expected.to run.with_params([], '', '').and_raise_error(Puppet::ParseError, /content must be a string/)
+    }
+    it {
+      pending('should implement stricter type checking')
+      is_expected.to run.with_params('', [], '').and_raise_error(Puppet::ParseError, /checkscript must be a string/)
+    }
+    it {
+      pending('should implement stricter type checking')
+      is_expected.to run.with_params('', '', []).and_raise_error(Puppet::ParseError, /custom error message must be a string/)
+    }
   end
 
-  context 'with no % placeholder' do
-    describe "with an explicit failure message" do
-      it "prints the failure message on error" do
-        expect {
-          subject.call ['', '/bin/false', 'failure message!']
-        }.to raise_error Puppet::ParseError, /failure message!/
-      end
-    end
-
-    describe "on validation failure" do
-      it "includes the command error output" do
-        expect {
-          subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
-        }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
-      end
-
-      it "includes the command return value" do
-        expect {
-          subject.call ['', '/cant/run/this']
-        }.to raise_error Puppet::ParseError, /returned 1\b/
-      end
-    end
-
-    describe "when performing actual validation" do
-      it "can positively validate file content" do
-        expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
-      end
-
-      it "can negatively validate file content" do
-        expect {
-          subject.call ["", "#{TESTEXE} -s"]
-        }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
-      end
+  context 'when validation fails' do
+    context 'with % placeholder' do
+      it { is_expected.to run.with_params('', "#{touch} % /no/such/file").and_raise_error(Puppet::ParseError, /Execution of '#{touch} \S+ \/no\/such\/file' returned 1:.*(cannot touch|o such file or)/) }
+      it { is_expected.to run.with_params('', "#{touch} % /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
     end
-  end
-
-  context 'with % placeholder' do
-    describe "with an explicit failure message" do
-      it "prints the failure message on error" do
-        expect {
-          subject.call ['', '/bin/false % -f', 'failure message!']
-        }.to raise_error Puppet::ParseError, /failure message!/
-      end
-    end
-    describe "on validation failure" do
-      it "includes the command error output" do
-        expect {
-          subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
-        }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
-      end
-
-      it "includes the command return value" do
-        expect {
-          subject.call ['', '/cant/run/this % -z']
-        }.to raise_error Puppet::ParseError, /Execution of '\/cant\/run\/this .+ -z' returned 1/
-      end
-    end
-
-    describe "when performing actual validation" do
-      it "can positively validate file content" do
-        expect { subject.call ["non-empty", "#{TESTEXE} -s %"] }.to_not raise_error
-      end
-
-      it "can negatively validate file content" do
-        expect {
-          subject.call ["", "#{TESTEXE} -s %"]
-        }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
-      end
+    context 'without % placeholder' do
+      it { is_expected.to run.with_params('', "#{touch} /no/such/file").and_raise_error(Puppet::ParseError, /Execution of '#{touch} \/no\/such\/file \S+' returned 1:.*(cannot touch|o such file or)/) }
+      it { is_expected.to run.with_params('', "#{touch} /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
     end
   end
 end
index a0c35c2304ff1e69d18f72896d216dfcfb816f7d..2e8e59fb8ccd67f52f74742becd00e9585993f4f 100755 (executable)
@@ -1,43 +1,26 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_hash) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe 'when calling validate_hash from puppet' do
-
-    %w{ true false }.each do |the_string|
-
-      it "should not compile when #{the_string} is a string" do
-        Puppet[:code] = "validate_hash('#{the_string}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
-      end
-
-      it "should not compile when #{the_string} is a bare word" do
-        Puppet[:code] = "validate_hash(#{the_string})"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
-      end
+describe 'validate_hash' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
+    describe 'valid inputs' do
+      it { is_expected.to run.with_params({}) }
+      it { is_expected.to run.with_params({'key' => 'value'}) }
+      it { is_expected.to run.with_params({}, {'key' => 'value'}) }
+      it { is_expected.to run.with_params({'key1' => 'value1'}, {'key2' => 'value2'}) }
     end
 
-    it "should compile when multiple hash arguments are passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = {}
-        $bar = { 'one' => 'two' }
-        validate_hash($foo, $bar)
-      ENDofPUPPETcode
-      scope.compiler.compile
+    describe 'invalid inputs' do
+      it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+      it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+      it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+      it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a Hash/) }
+      it { is_expected.to run.with_params({}, []).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+      it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+      it { is_expected.to run.with_params({}, true).and_raise_error(Puppet::ParseError, /is not a Hash/) }
+      it { is_expected.to run.with_params({}, 'one').and_raise_error(Puppet::ParseError, /is not a Hash/) }
     end
-
-    it "should not compile when an undef variable is passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = undef
-        validate_hash($foo)
-      ENDofPUPPETcode
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
-    end
-
   end
-
 end
index e95da6a8cd2b7a4040dd190ac8a62ce645b94c47..4c0a9d7d4f164dc0ee3bfb9e8d322308c7bfb41b 100755 (executable)
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_integer) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe 'when calling validate_integer from puppet without any argument or to many' do
-    it "should not compile when no argument is passed" do
-      Puppet[:code] = "validate_integer()"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
-    end
-    it "should not compile when more than three arguments are passed" do
-      Puppet[:code] = "validate_integer(1, 1, 1, 1)"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
-    end
-  end
-
-  describe 'when calling validate_integer from puppet only with input' do
-    %w{ 1 -1 }.each do |the_number|
-      it "should compile when #{the_number} is an encapsulated integer" do
-        Puppet[:code] = "validate_integer('#{the_number}')"
-        scope.compiler.compile
-      end
-      it "should compile when #{the_number} is an bare integer" do
-        Puppet[:code] = "validate_integer(#{the_number})"
-        scope.compiler.compile
-      end
-    end
-
-    %w{ [1,2,3,4,5] ['1','2','3','4','5'] }.each do |the_number|
-      it "should compile when multiple Integer arguments are passed in an Array" do
-        Puppet[:code] = "validate_integer(#{the_number})"
-        scope.compiler.compile
-      end
-    end
-
-    %w{ true false iAmAString 1test 7.0 -7.0 }.each do |the_number|
-      it "should not compile when #{the_number} is in a string" do
-        Puppet[:code] = "validate_integer('#{the_number}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
-      end
-
-      it "should not compile when #{the_number} is a bare word" do
-        Puppet[:code] = "validate_integer(#{the_number})"
-        expect { scope.compiler.compile }.to raise_error
-      end
-    end
-
-    it "should not compile when an Integer is part of a larger String" do
-      Puppet[:code] = "validate_integer('1 test')"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
-    end
+describe 'validate_integer' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
-    it "should not compile when an Array with a non-Integer value is passed" do
-      Puppet[:code] = "validate_integer([1, '-7.0'])"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /at array position 1 to be an Integer/)
+    [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', 7.0, -7.0, {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x'].each do |invalid|
+      it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, /to be an Integer/) }
+      it { is_expected.to run.with_params(invalid, 10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
+      it { is_expected.to run.with_params(invalid, 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
+      it { is_expected.to run.with_params([0, 1, 2, invalid, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
     end
 
-    it "should not compile when a Hash is passed" do
-      Puppet[:code] = "validate_integer({ 1 => 2 })"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer or Array/)
+    context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+      it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be an Integer/) }
     end
 
-    it "should not compile when a Hash is passed as Array" do
-      Puppet[:code] = "validate_integer([{ 1 => 2 }])"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
+    context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do
+      it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
+      it { is_expected.to run.with_params([0, 1, 2, {0=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
     end
 
-    it "should not compile when an explicitly undef variable is passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = undef
-        validate_integer($foo)
-      ENDofPUPPETcode
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
-    end
+    it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, /to be unset or an Integer/) }
+    it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, /to be unset or an Integer/) }
+    it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, /second argument to be larger than third argument/) }
+  end
 
-    it "should not compile when an undefined variable is passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        validate_integer($foobarbazishouldnotexist)
-      ENDofPUPPETcode
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
-    end
+  context 'with no range constraints' do
+    it { is_expected.to run.with_params(1) }
+    it { is_expected.to run.with_params(-1) }
+    it { is_expected.to run.with_params('1') }
+    it { is_expected.to run.with_params('-1') }
+    it { is_expected.to run.with_params([1, 2, 3, 4]) }
+    it { is_expected.to run.with_params([1, '2', '3', 4]) }
   end
 
-  describe 'when calling validate_integer from puppet with input and a maximum' do
-    max = 10
-    %w{ 1 -1 }.each do |the_number|
-      it "should compile when #{the_number} is lower than a maximum of #{max}" do
-        Puppet[:code] = "validate_integer(#{the_number},#{max})"
-        scope.compiler.compile
+  context "with a maximum limit of 10" do
+    describe 'rejects numbers greater than the limit' do
+      it { is_expected.to run.with_params(11, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+      it { is_expected.to run.with_params(100, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+      it { is_expected.to run.with_params(2**65, 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+      it { is_expected.to run.with_params([1,2,10,100], 10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+    end
+
+    describe 'accepts numbers less or equal to the limit' do
+      it { is_expected.to run.with_params(10, 10) }
+      it { is_expected.to run.with_params(1, 10) }
+      it { is_expected.to run.with_params(-1, 10) }
+      it { is_expected.to run.with_params('1', 10) }
+      it { is_expected.to run.with_params('-1', 10) }
+      it { is_expected.to run.with_params([1, 2, 3, 4], 10) }
+      it { is_expected.to run.with_params([1, '2', '3', 4], 10) }
+    end
+
+    context "with a minimum limit of -10" do
+      describe 'rejects numbers greater than the upper limit' do
+        it { is_expected.to run.with_params(11, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+        it { is_expected.to run.with_params(100, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+        it { is_expected.to run.with_params(2**65, 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+        it { is_expected.to run.with_params([1,2,10,100], 10, -10).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
       end
-    end
 
-    it "should compile when an Integer is equal the maximum" do
-      Puppet[:code] = "validate_integer(#{max},#{max})"
-      scope.compiler.compile
-    end
-
-    it "should not compile when #{max+1} is greater than a maximum of #{max}" do
-      Puppet[:code] = "validate_integer(#{max+1},#{max})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
-    end
-
-    %w{ [-10,1,2,3,4,5,10] ['-10','1','2','3','4','5','10'] }.each do |the_number|
-      it "should compile when each element of #{the_number} is lower than a maximum of #{max}" do
-        Puppet[:code] = "validate_integer(#{the_number},#{max})"
-        scope.compiler.compile
+      describe 'rejects numbers smaller than the lower limit' do
+        it { is_expected.to run.with_params(-11, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+        it { is_expected.to run.with_params(-100, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+        it { is_expected.to run.with_params(-2**65, 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+        it { is_expected.to run.with_params([-10, 1,2,10,-100], 10, -10).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
       end
-    end
-
-    it "should not compile when an element of an Array [-10,1,2,3,4,5,#{max+1}] is greater than a maximum of #{max}" do
-      Puppet[:code] = "validate_integer([-10,1,2,3,4,5,#{max+1}],#{max})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
-    end
 
-    %w{ true false iAmAString 1test 7.0 -7.0 }.each do |the_max|
-      it "should not compile when a non-Integer maximum #{the_max}, encapsulated in a String, is passed" do
-        Puppet[:code] = "validate_integer(1,'#{the_max}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
+      describe 'accepts numbers between and including the limits' do
+        it { is_expected.to run.with_params(10, 10, -10) }
+        it { is_expected.to run.with_params(-10, 10, -10) }
+        it { is_expected.to run.with_params(1, 10, -10) }
+        it { is_expected.to run.with_params(-1, 10, -10) }
+        it { is_expected.to run.with_params('1', 10, -10) }
+        it { is_expected.to run.with_params('-1', 10, -10) }
+        it { is_expected.to run.with_params([1, 2, 3, 4], 10, -10) }
+        it { is_expected.to run.with_params([1, '2', '3', 4], 10, -10) }
       end
-      it "should not compile when a non-Integer maximum #{the_max} bare word is passed" do
-        Puppet[:code] = "validate_integer(1,#{the_max})"
-        expect { scope.compiler.compile }.to raise_error
-      end
-    end
-
-    it "should not compile when an explicitly undefined variable is passed as maximum and no minimum is passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = undef
-        validate_integer(10, $foo)
-      ENDofPUPPETcode
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
-    end
-    it "should not compile when an explicitly undef is passed as maximum and no minimum is passed" do
-      Puppet[:code] = "validate_integer(10, undef)"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
-    end
-    it "should not compile when an empty string is passed as maximum and no minimum is passed" do
-      Puppet[:code] = "validate_integer(10, '')"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
-    end
-    it "should not compile when an undefined variable for a maximum is passed" do
-      Puppet[:code] = "validate_integer(10, $foobarbazishouldnotexist)"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
     end
   end
 
-  describe 'when calling validate_integer from puppet with input, a maximum and a minimum' do
-    it "should not compile when a minimum larger than maximum is passed" do
-      Puppet[:code] = "validate_integer(1,1,2)"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /second argument to be larger than third argument/)
-    end
+  it { is_expected.to run.with_params(10, 10, 10) }
 
-    max = 10
-    min = -10
-    %w{ 1 -1 }.each do |the_number|
-      it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
-        Puppet[:code] = "validate_integer(#{the_number},#{max},#{min})"
-        scope.compiler.compile
-      end
-    end
-
-    it "should compile when an Integer is equal the minimum" do
-      Puppet[:code] = "validate_integer(#{min},#{max},#{min})"
-      scope.compiler.compile
-    end
-
-    it "should compile when an Integer is equal the minimum and maximum" do
-      Puppet[:code] = "validate_integer(#{max},#{max},#{max})"
-      scope.compiler.compile
-    end
-
-    it "should compile when an empty maximum is passed and the Integer is greater than the minimum" do
-      Puppet[:code] = "validate_integer(#{max},'',#{min})"
-      scope.compiler.compile
-    end
-    it "should compile when an explicitly undefined maximum is passed and the Integer is greater than the minimum" do
-      Puppet[:code] = "validate_integer(#{max},undef,#{min})"
-      scope.compiler.compile
-    end
-    it "should compile when an explicitly undefined variable is passed for maximum and the Integer is greater than the minimum" do
-      Puppet[:code] = <<-"ENDofPUPPETcode"
-        $foo = undef
-        validate_integer(#{max}, $foo, #{min})
-      ENDofPUPPETcode
-      scope.compiler.compile
-    end
-    it "should not compile when no maximum value is given and the Integer is greater than the minimum" do
-      Puppet[:code] = "validate_integer(#{max},,#{min})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::Error, /Syntax error at ','/)
-    end
-
-    it "should not compile when #{min-1} is lower than a minimum of #{min}" do
-      Puppet[:code] = "validate_integer(#{min-1},#{max},#{min})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
-    end
-
-    %w{ [-10,1,2,3,4,5,10] ['-10','1','2','3','4','5','10'] }.each do |the_number|
-      it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
-        Puppet[:code] = "validate_integer(#{the_number},#{max},#{min})"
-        scope.compiler.compile
-      end
-    end
-
-    it "should not compile when an element of an Array [#{min-1},1,2,3,4,5,10] is lower than a minimum of #{min}" do
-      Puppet[:code] = "validate_integer([#{min-1},1,2,3,4,5,10],#{max},#{min})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
-    end
-
-    %w{ true false iAmAString 1test 7.0 -7.0 }.each do |the_min|
-      it "should not compile when a non-Integer minimum #{the_min}, encapsulated in a String, is passed" do
-        Puppet[:code] = "validate_integer(1,#{max},'#{the_min}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or an Integer/)
-      end
-      it "should not compile when a non-Integer minimum #{the_min} bare word is passed" do
-        Puppet[:code] = "validate_integer(1,#{max},#{the_min})"
-        expect { scope.compiler.compile }.to raise_error
-      end
-    end
+  describe 'empty upper limit is interpreted as infinity' do
+    it { is_expected.to run.with_params(11, '', 10) }
   end
 end
index 27ea4feeff225394676e07b93a498dbf6927ac1c..b6170d438765d4bb163e20b5c7210a42a3d912df 100755 (executable)
@@ -1,62 +1,40 @@
-#! /usr/bin/env ruby -S rspec
-
-require "spec_helper"
-
-describe Puppet::Parser::Functions.function(:validate_ipv4_address) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe "when calling validate_ipv4_address from puppet" do
-    describe "when given IPv4 address strings" do
-      it "should compile with one argument" do
-        Puppet[:code] = "validate_ipv4_address('1.2.3.4')"
-        scope.compiler.compile
-      end
-
-      it "should compile with multiple arguments" do
-        Puppet[:code] = "validate_ipv4_address('1.2.3.4', '5.6.7.8')"
-        scope.compiler.compile
-      end
-    end
-
-    describe "when given an IPv6 address" do
-      it "should not compile" do
-        Puppet[:code] = "validate_ipv4_address('3ffe:505')"
-        expect {
-          scope.compiler.compile
-        }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
-      end
-    end
-
-    describe "when given other strings" do
-      it "should not compile" do
-        Puppet[:code] = "validate_ipv4_address('hello', 'world')"
-        expect {
-          scope.compiler.compile
-        }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
-      end
-    end
-
-    describe "when given numbers" do
-      it "should not compile" do
-        Puppet[:code] = "validate_ipv4_address(1, 2)"
-        expect { scope.compiler.compile }.to raise_error
-      end
-    end
-
-    describe "when given booleans" do
-      it "should not compile" do
-        Puppet[:code] = "validate_ipv4_address(true, false)"
-        expect {
-          scope.compiler.compile
-        }.to raise_error(Puppet::ParseError, /is not a string/)
+require 'spec_helper'
+
+describe 'validate_ipv4_address' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+    describe 'valid inputs' do
+      it { is_expected.to run.with_params('0.0.0.0') }
+      it { is_expected.to run.with_params('8.8.8.8') }
+      it { is_expected.to run.with_params('127.0.0.1') }
+      it { is_expected.to run.with_params('10.10.10.10') }
+      it { is_expected.to run.with_params('194.232.104.150') }
+      it { is_expected.to run.with_params('244.24.24.24') }
+      it { is_expected.to run.with_params('255.255.255.255') }
+      it { is_expected.to run.with_params('1.2.3.4', '5.6.7.8') }
+      context 'with netmasks' do
+        it { is_expected.to run.with_params('8.8.8.8/0') }
+        it { is_expected.to run.with_params('8.8.8.8/16') }
+        it { is_expected.to run.with_params('8.8.8.8/32') }
+        it { is_expected.to run.with_params('8.8.8.8/255.255.0.0') }
       end
     end
 
-    it "should not compile when no arguments are passed" do
-      Puppet[:code] = "validate_ipv4_address()"
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+    describe 'invalid inputs' do
+      it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
+      it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
+      it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
+      it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
+      it { is_expected.to run.with_params('affe::beef').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
+      it { is_expected.to run.with_params('1.2.3.4', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params('1.2.3.4', 1).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params('1.2.3.4', true).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params('1.2.3.4', 'one').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) }
     end
   end
 end
index e87b3726ad522f709b44a16fd6a9e54997cc1678..7aaf0060a83bf055ff1781e8212e6524ed8d5a02 100755 (executable)
@@ -1,65 +1,32 @@
-#! /usr/bin/env ruby -S rspec
-
-require "spec_helper"
-
-describe Puppet::Parser::Functions.function(:validate_ipv6_address) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe "when calling validate_ipv6_address from puppet" do
-    describe "when given IPv6 address strings" do
-      it "should compile with one argument" do
-        Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::')"
-        scope.compiler.compile
-      end
-
-      it "should compile with multiple arguments" do
-        Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::', '3ffe:0505:0001::')"
-        scope.compiler.compile
-      end
-    end
-
-    describe "when given an ipv4 address" do
-      it "should not compile" do
-        Puppet[:code] = "validate_ipv6_address('1.2.3.4')"
-        expect {
-          scope.compiler.compile
-        }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
-      end
+require 'spec_helper'
+
+describe 'validate_ipv6_address' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+    describe 'valid inputs' do
+      it { is_expected.to run.with_params('3ffe:0505:0002::') }
+      it { is_expected.to run.with_params('3ffe:0505:0002::', '3ffe:0505:0002::2') }
+      it { is_expected.to run.with_params('::1/64') }
+      it { is_expected.to run.with_params('fe80::a00:27ff:fe94:44d6/64') }
     end
 
-    describe "when given other strings" do
-      it "should not compile" do
-        Puppet[:code] = "validate_ipv6_address('hello', 'world')"
-        expect {
-          scope.compiler.compile
-        }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
+    describe 'invalid inputs' do
+      it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+      it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+      it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+      it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+      it { is_expected.to run.with_params('affe:beef').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+      it { is_expected.to run.with_params('::1', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params('::1', true).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params('::1', 'one').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) }
+      context 'unless running on ruby 1.8.7', :if => RUBY_VERSION != '1.8.7' do
+        it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
+        it { is_expected.to run.with_params('::1', 1).and_raise_error(Puppet::ParseError, /is not a string/) }
       end
     end
-
-    # 1.8.7 is EOL'd and also absolutely insane about ipv6
-    unless RUBY_VERSION == '1.8.7'
-      describe "when given numbers" do
-        it "should not compile" do
-          Puppet[:code] = "validate_ipv6_address(1, 2)"
-          expect { scope.compiler.compile }.to raise_error
-        end
-      end
-    end
-
-    describe "when given booleans" do
-      it "should not compile" do
-        Puppet[:code] = "validate_ipv6_address(true, false)"
-        expect {
-          scope.compiler.compile
-        }.to raise_error(Puppet::ParseError, /is not a string/)
-      end
-    end
-
-    it "should not compile when no arguments are passed" do
-      Puppet[:code] = "validate_ipv6_address()"
-      expect {
-        scope.compiler.compile
-      }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
-    end
   end
 end
index c99d879e9ba5c584da04271833a0e854b4749678..9b8eb0eebb44d3606bf2788463c5df04e641a8bf 100755 (executable)
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_numeric) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe 'when calling validate_numeric from puppet without any argument or to many' do
-    it "should not compile when no argument is passed" do
-      Puppet[:code] = "validate_numeric()"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
-    end
-    it "should not compile when more than three arguments are passed" do
-      Puppet[:code] = "validate_numeric(1, 1, 1, 1)"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /Wrong number of arguments/)
-    end
-  end
-
-  describe 'when calling validate_numeric from puppet only with input' do
-    %w{ 1 -1 1.0 -1.0 }.each do |the_number|
-      it "should compile when #{the_number} is an encapsulated numeric" do
-        Puppet[:code] = "validate_numeric('#{the_number}')"
-        scope.compiler.compile
-      end
-      it "should compile when #{the_number} is a bare numeric" do
-        Puppet[:code] = "validate_numeric(#{the_number})"
-        scope.compiler.compile
-      end
-    end
-
-    %w{ [1,2,3,4,5] ['1','2','3','4','5'] [1.1,2.2,3.3,4.4,5.5] ['1.1','2.2','3.3','4.4','5.5'] }.each do |the_number|
-      it "should compile when multiple Numeric arguments are passed in an Array" do
-        Puppet[:code] = "validate_numeric(#{the_number})"
-        scope.compiler.compile
-      end
-    end
-
-    %w{ true false iAmAString 1test }.each do |the_number|
-      it "should not compile when #{the_number} is in a string" do
-        Puppet[:code] = "validate_numeric('#{the_number}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
-      end
-
-      it "should not compile when #{the_number} is a bare word" do
-        Puppet[:code] = "validate_numeric(#{the_number})"
-        expect { scope.compiler.compile }.to raise_error
-      end
-    end
-
-    it "should not compile when a Numeric is part of a larger String" do
-      Puppet[:code] = "validate_numeric('1.0 test')"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
-    end
+describe 'validate_numeric' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
-    it "should not compile when an Array with a non-Numeric value is passed" do
-      Puppet[:code] = "validate_numeric([1, 'test'])"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /at array position 1 to be a Numeric/)
+    [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x'].each do |invalid|
+      it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
+      it { is_expected.to run.with_params(invalid, 10.0).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
+      it { is_expected.to run.with_params(invalid, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
     end
 
-    it "should not compile when a Hash is passed" do
-      Puppet[:code] = "validate_numeric({ 1 => 2 })"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric or Array/)
+    context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+      it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, /to be a Numeric/) }
     end
 
-    it "should not compile when a Hash is passed in an Array" do
-      Puppet[:code] = "validate_numeric([{ 1 => 2 }])"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
+    context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do
+      it { is_expected.to run.with_params([0, 1, 2, {1=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
+      it { is_expected.to run.with_params([0, 1, 2, {0=>2}, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) }
     end
 
-    it "should not compile when an explicitly undef variable is passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = undef
-        validate_numeric($foo)
-      ENDofPUPPETcode
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
-    end
+    it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, /to be unset or a Numeric/) }
+    it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, /to be unset or a Numeric/) }
+    it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, /second argument to be larger than third argument/) }
+  end
 
-    it "should not compile when an undefined variable is passed" do
-      Puppet[:code] = 'validate_numeric($foobarbazishouldnotexist)'
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
-    end
+  context 'with no range constraints' do
+    it { is_expected.to run.with_params(1) }
+    it { is_expected.to run.with_params(-1) }
+    it { is_expected.to run.with_params('1') }
+    it { is_expected.to run.with_params('-1') }
+    it { is_expected.to run.with_params([1, 2, 3, 4]) }
+    it { is_expected.to run.with_params([1, '2', '3', 4]) }
   end
 
-  describe 'when calling validate_numeric from puppet with input and a maximum' do
-    max = 10
-    %w{ 1 -1 1.0 -1.0 }.each do |the_number|
-      it "should compile when #{the_number} is lower than a maximum of #{max}" do
-        Puppet[:code] = "validate_numeric(#{the_number},#{max})"
-        scope.compiler.compile
+  context "with a maximum limit of 10.0" do
+    describe 'rejects numbers greater than the limit' do
+      it { is_expected.to run.with_params(11, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+      it { is_expected.to run.with_params(100, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+      it { is_expected.to run.with_params(2**65, 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+      it { is_expected.to run.with_params([1,2,10.0,100], 10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+    end
+
+    describe 'accepts numbers less or equal to the limit' do
+      it { is_expected.to run.with_params(10.0, 10.0) }
+      it { is_expected.to run.with_params(1, 10.0) }
+      it { is_expected.to run.with_params(-1, 10.0) }
+      it { is_expected.to run.with_params('1', 10.0) }
+      it { is_expected.to run.with_params('-1', 10.0) }
+      it { is_expected.to run.with_params([1, 2, 3, 4], 10.0) }
+      it { is_expected.to run.with_params([1, '2', '3', 4], 10.0) }
+    end
+
+    context "with a minimum limit of -10.0" do
+      describe 'rejects numbers greater than the upper limit' do
+        it { is_expected.to run.with_params(11, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+        it { is_expected.to run.with_params(100, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+        it { is_expected.to run.with_params(2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
+        it { is_expected.to run.with_params([1,2,10.0,100], 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be smaller or equal/) }
       end
-    end
 
-    it "should compile when a Numeric is equal the maximum" do
-      Puppet[:code] = "validate_numeric(#{max},#{max})"
-      scope.compiler.compile
-    end
-
-    it "should not compile when #{max+1} is greater than a maximum of #{max}" do
-      Puppet[:code] = "validate_numeric(#{max+1},#{max})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
-    end
-
-    %w{ [-10,1,2,3,4,5,10] ['-10','1','2','3','4','5','10'] }.each do |the_number|
-      it "should compile when each element of #{the_number} is lower than a maximum of #{max}" do
-        Puppet[:code] = "validate_numeric(#{the_number},#{max})"
-        scope.compiler.compile
+      describe 'rejects numbers smaller than the lower limit' do
+        it { is_expected.to run.with_params(-11, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+        it { is_expected.to run.with_params(-100, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+        it { is_expected.to run.with_params(-2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
+        it { is_expected.to run.with_params([-10.0, 1,2,10.0,-100], 10.0, -10.0).and_raise_error(Puppet::ParseError, /to be greater or equal/) }
       end
-    end
-
-    it "should not compile when an element of an Array [-10,1,2,3,4,5,#{max+1}] is greater than a maximum of #{max}" do
-      Puppet[:code] = "validate_numeric([-10,1,2,3,4,5,#{max+1}],#{max})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be smaller or equal to/)
-    end
 
-    %w{ true false iAmAString 1test }.each do |the_max|
-      it "should not compile when a non-Numeric maximum #{the_max}, encapsulated in a String, is passed" do
-        Puppet[:code] = "validate_numeric(1,'#{the_max}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
+      describe 'accepts numbers between and including the limits' do
+        it { is_expected.to run.with_params(10.0, 10.0, -10.0) }
+        it { is_expected.to run.with_params(-10.0, 10.0, -10.0) }
+        it { is_expected.to run.with_params(1, 10.0, -10.0) }
+        it { is_expected.to run.with_params(-1, 10.0, -10.0) }
+        it { is_expected.to run.with_params('1', 10.0, -10.0) }
+        it { is_expected.to run.with_params('-1', 10.0, -10.0) }
+        it { is_expected.to run.with_params([1, 2, 3, 4], 10.0, -10.0) }
+        it { is_expected.to run.with_params([1, '2', '3', 4], 10.0, -10.0) }
       end
-      it "should not compile when a non-Numeric maximum #{the_max} bare word is passed" do
-        Puppet[:code] = "validate_numeric(1,#{the_max})"
-        expect { scope.compiler.compile }.to raise_error
-      end
-    end
-
-    it "should not compile when an explicitly undefined variable is passed as maximum and no minimum is passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = undef
-        validate_numeric(10, $foo)
-      ENDofPUPPETcode
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
-    end
-    it "should not compile when an explicitly undef is passed as maximum and no minimum is passed" do
-      Puppet[:code] = "validate_numeric(10, undef)"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
-    end
-    it "should not compile when an empty string is passed as maximum and no minimum is passed" do
-      Puppet[:code] = "validate_numeric(10, '')"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
-    end
-    it "should not compile when an undefined variable for a maximum is passed" do
-      Puppet[:code] = "validate_numeric(10, $foobarbazishouldnotexist)"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
     end
   end
 
-  describe 'when calling validate_numeric from puppet with input, a maximum and a minimum' do
-    it "should not compile when a minimum larger than maximum is passed" do
-      Puppet[:code] = "validate_numeric(1,1,2)"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /second argument to be larger than third argument/)
-    end
+  it { is_expected.to run.with_params(10.0, 10.0, 10.0) }
 
-    max = 10
-    min = -10
-    %w{ 1 -1 }.each do |the_number|
-      it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
-        Puppet[:code] = "validate_numeric(#{the_number},#{max},#{min})"
-        scope.compiler.compile
-      end
-    end
-
-    it "should compile when a Numeric is equal the minimum" do
-      Puppet[:code] = "validate_numeric(#{min},#{max},#{min})"
-      scope.compiler.compile
-    end
-
-    it "should compile when a Numeric is equal the minimum and maximum" do
-      Puppet[:code] = "validate_numeric(#{max},#{max},#{max})"
-      scope.compiler.compile
-    end
-
-    it "should compile when an empty maximum is passed and the Numeric is greater than the minimum" do
-      Puppet[:code] = "validate_numeric(#{max}.1,'',#{min})"
-      scope.compiler.compile
-    end
-    it "should compile when an explicitly undefined maximum is passed and the Numeric is greater than the minimum" do
-      Puppet[:code] = "validate_numeric(#{max}.1,undef,#{min})"
-      scope.compiler.compile
-    end
-    it "should compile when an explicitly undefined variable is passed for maximum and the Numeric is greater than the minimum" do
-      Puppet[:code] = <<-"ENDofPUPPETcode"
-        $foo = undef
-        validate_numeric(#{max}.1, $foo, #{min})
-      ENDofPUPPETcode
-      scope.compiler.compile
-    end
-    it "should not compile when no maximum value is given and the Numeric is greater than the minimum" do
-      Puppet[:code] = "validate_numeric(#{max}.1,,#{min})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::Error, /Syntax error at ','/)
-    end
-
-    it "should not compile when #{min-1} is lower than a minimum of #{min}" do
-      Puppet[:code] = "validate_numeric(#{min-1.0},#{max},#{min})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
-    end
-
-    %w{ [-10,1,2,3,4,5,10] ['-10.0','1','2','3','4','5','10.0'] }.each do |the_number|
-      it "should compile when each element of #{the_number} is within a range from #{min} to #{max}" do
-        Puppet[:code] = "validate_numeric(#{the_number},#{max},#{min})"
-        scope.compiler.compile
-      end
-    end
-
-    it "should not compile when an element of an Array [#{min-1.1},1,2,3,4,5,10.0] is lower than a minimum of #{min}" do
-      Puppet[:code] = "validate_numeric([#{min-1},1,2,3,4,5,10],#{max},#{min})"
-      expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be greater or equal to/)
-    end
-
-    %w{ true false iAmAString 1test }.each do |the_min|
-      it "should not compile when a non-Numeric minimum #{the_min}, encapsulated in a String, is passed" do
-        Puppet[:code] = "validate_numeric(1,#{max},'#{the_min}')"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be unset or a Numeric/)
-      end
-      it "should not compile when a non-Numeric minimum #{the_min} bare word is passed" do
-        Puppet[:code] = "validate_numeric(1,#{max},#{the_min})"
-        expect { scope.compiler.compile }.to raise_error
-      end
-    end
+  describe 'empty upper limit is interpreted as infinity' do
+    it { is_expected.to run.with_params(11, '', 10.0) }
   end
 end
index d29988bf0068b932ede2e386c45133368aa5a67f..42b10491713feb6e67ce3afcd255305ffe2b0797 100755 (executable)
@@ -1,77 +1,46 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_re) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  # The subject of these examplres is the method itself.
-  subject do
-    # This makes sure the function is loaded within each test
-    function_name = Puppet::Parser::Functions.function(:validate_re)
-    scope.method(function_name)
-  end
-
-  context 'Using Puppet::Parser::Scope.new' do
-
-    describe 'Garbage inputs' do
-      inputs = [
-        [ nil ],
-        [ [ nil ] ],
-        [ { 'foo' => 'bar' } ],
-        [ { } ],
-        [ '' ],
-        [ "one", "one", "MSG to User", "4th arg" ],
-      ]
-
-      inputs.each do |input|
-        it "validate_re(#{input.inspect}) should fail" do
-          expect { subject.call [input] }.to raise_error Puppet::ParseError
-        end
-      end
-    end
-
-    describe 'Valid inputs' do
-      inputs = [
-        [ '/full/path/to/something', '^/full' ],
-        [ '/full/path/to/something', 'full' ],
-        [ '/full/path/to/something', ['full', 'absent'] ],
-        [ '/full/path/to/something', ['full', 'absent'], 'Message to the user' ],
-      ]
-
-      inputs.each do |input|
-        it "validate_re(#{input.inspect}) should not fail" do
-          expect { subject.call input }.not_to raise_error
-        end
-      end
-    end
-    describe "Valid inputs which should raise an exception without a message" do
-      # The intent here is to make sure valid inputs raise exceptions when they
-      # don't specify an error message to display.  This is the behvior in
-      # 2.2.x and prior.
-      inputs = [
-        [ "hello", [ "bye", "later", "adios" ] ],
-        [ "greetings", "salutations" ],
-      ]
-
-      inputs.each do |input|
-        it "validate_re(#{input.inspect}) should fail" do
-          expect { subject.call input }.to raise_error /validate_re.*?does not match/
-        end
-      end
+describe 'validate_re' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('', '', '', 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+    describe 'valid inputs' do
+      it { is_expected.to run.with_params('', '') }
+      it { is_expected.to run.with_params('', ['']) }
+      it { is_expected.to run.with_params('', [''], 'custom error') }
+      it { is_expected.to run.with_params('one', '^one') }
+      it { is_expected.to run.with_params('one', [ '^one', '^two' ]) }
+      it { is_expected.to run.with_params('one', [ '^one', '^two' ], 'custom error') }
     end
-    describe "Nicer Error Messages" do
-      # The intent here is to make sure the function returns the 3rd argument
-      # in the exception thrown
-      inputs = [
-        [ "hello", [ "bye", "later", "adios" ], "MSG to User" ],
-        [ "greetings", "salutations", "Error, greetings does not match salutations" ],
-      ]
 
-      inputs.each do |input|
-        it "validate_re(#{input.inspect}) should fail" do
-          expect { subject.call input }.to raise_error /#{input[2]}/
-        end
-      end
+    describe 'invalid inputs' do
+      it {
+        pending('should implement stricter type checking')
+        is_expected.to run.with_params([], '').and_raise_error(Puppet::ParseError, /is not a String/)
+      }
+      it {
+        pending('should implement stricter type checking')
+        is_expected.to run.with_params('', {}).and_raise_error(Puppet::ParseError, /is not an Array/)
+      }
+      it {
+        pending('should implement stricter type checking')
+        is_expected.to run.with_params('', '', []).and_raise_error(Puppet::ParseError, /is not a String/)
+      }
+      it {
+        pending('should implement stricter type checking')
+        is_expected.to run.with_params(nil, nil).and_raise_error(Puppet::ParseError, /is not a String/)
+      }
+      it { is_expected.to run.with_params('', []).and_raise_error(Puppet::ParseError, /does not match/) }
+      it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /does not match/) }
+      it { is_expected.to run.with_params('', 'two').and_raise_error(Puppet::ParseError, /does not match/) }
+      it { is_expected.to run.with_params('', ['two']).and_raise_error(Puppet::ParseError, /does not match/) }
+      it { is_expected.to run.with_params('', ['two'], 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
+      it { is_expected.to run.with_params('notone', '^one').and_raise_error(Puppet::ParseError, /does not match/) }
+      it { is_expected.to run.with_params('notone', [ '^one', '^two' ]).and_raise_error(Puppet::ParseError, /does not match/) }
+      it { is_expected.to run.with_params('notone', [ '^one', '^two' ], 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
     end
   end
 end
index e23f61a20e126ac233f2ff21980ef46e2900da10..391f83a2ab411d0917af3afb1afd5866ae6c3b82 100755 (executable)
@@ -1,67 +1,61 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe "the validate_slength function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("validate_slength")).to eq("function_validate_slength")
+describe 'validate_slength' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('', 2, 3, 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /second argument to be a positive Numeric/) }
+    it { is_expected.to run.with_params('', -1).and_raise_error(Puppet::ParseError, /second argument to be a positive Numeric/) }
+    it { is_expected.to run.with_params('', 1, '').and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
+    it { is_expected.to run.with_params('', 1, -1).and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
+    it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, /argument to be larger than third argument/) }
   end
 
-  describe "validating the input argument types" do
-    it "raises an error if there are less than two arguments" do
-      expect { scope.function_validate_slength([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
-    end
-
-    it "raises an error if there are more than three arguments" do
-      expect { scope.function_validate_slength(['input', 1, 2, 3]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
-    end
-
-    it "raises an error if the first argument is not a string" do
-      expect { scope.function_validate_slength([Object.new, 2, 1]) }.to raise_error Puppet::ParseError, /Expected first argument.*got .*Object/
-    end
-
-    it "raises an error if the second argument cannot be cast to an Integer" do
-      expect { scope.function_validate_slength(['input', Object.new]) }.to raise_error Puppet::ParseError, /Expected second argument.*got .*Object/
-    end
-
-    it "raises an error if the third argument cannot be cast to an Integer" do
-      expect { scope.function_validate_slength(['input', 1, Object.new]) }.to raise_error Puppet::ParseError, /Expected third argument.*got .*Object/
+  context "with a maximum length of 10" do
+    describe 'rejects strings longer than the limit' do
+      it { is_expected.to run.with_params('1234567890a', 10).and_raise_error(Puppet::ParseError, /Expected length/) }
+      it { is_expected.to run.with_params('1234567890abcdef', 10).and_raise_error(Puppet::ParseError, /Expected length/) }
+      it { is_expected.to run.with_params([ 'one', '1234567890abcdef' ], 10).and_raise_error(Puppet::ParseError, /Expected length/) }
     end
 
-    it "raises an error if the second argument is smaller than the third argument" do
-      expect { scope.function_validate_slength(['input', 1, 2]) }.to raise_error Puppet::ParseError, /Expected second argument to be larger than third argument/
+    describe 'accepts strings shorter or equal to the limit' do
+      it { is_expected.to run.with_params('1234567890', 10) }
+      it { is_expected.to run.with_params('12345', 10) }
+      it { is_expected.to run.with_params([ 'one', 'two' ], 10) }
     end
-  end
-
-  describe "validating the input string length" do
-    describe "when the input is a string" do
-      it "fails validation if the string is larger than the max length" do
-        expect { scope.function_validate_slength(['input', 1]) }.to raise_error Puppet::ParseError, /Expected length .* between 0 and 1, was 5/
-      end
-
-      it "fails validation if the string is less than the min length" do
-        expect { scope.function_validate_slength(['input', 10, 6]) }.to raise_error Puppet::ParseError, /Expected length .* between 6 and 10, was 5/
-      end
 
-      it "doesn't raise an error if the string is under the max length" do
-        scope.function_validate_slength(['input', 10])
+    context "with a minimum length of 5" do
+      describe 'rejects strings longer than the upper limit' do
+        it { is_expected.to run.with_params('1234567890a', 10, 5).and_raise_error(Puppet::ParseError, /Expected length/) }
+        it { is_expected.to run.with_params('1234567890abcdef', 10, 5).and_raise_error(Puppet::ParseError, /Expected length/) }
       end
 
-      it "doesn't raise an error if the string is equal to the max length" do
-        scope.function_validate_slength(['input', 5])
+      describe 'rejects numbers shorter than the lower limit' do
+        it { is_expected.to run.with_params('one', 10, 5).and_raise_error(Puppet::ParseError, /Expected length/) }
+        it { is_expected.to run.with_params(['12345678', 'two'], 10, 5).and_raise_error(Puppet::ParseError, /Expected length/) }
       end
 
-      it "doesn't raise an error if the string is equal to the min length" do
-        scope.function_validate_slength(['input', 10, 5])
+      describe 'accepts strings of length between and including the limits' do
+        it { is_expected.to run.with_params('12345', 10, 5) }
+        it { is_expected.to run.with_params('123456', 10, 5) }
+        it { is_expected.to run.with_params('1234567', 10, 5) }
+        it { is_expected.to run.with_params('12345678', 10, 5) }
+        it { is_expected.to run.with_params('123456789', 10, 5) }
+        it { is_expected.to run.with_params('1234567890', 10, 5) }
+        it { is_expected.to run.with_params(['1233456', '12345678'], 10, 5) }
       end
     end
+  end
 
-    describe "when the input is an array" do
-      it "fails validation if one of the array elements is not a string" do
-        expect { scope.function_validate_slength([["a", "b", Object.new], 2]) }.to raise_error Puppet::ParseError, /Expected element at array position 2 .*String, got .*Object/
-      end
-    end
+  describe 'corner cases' do
+    it { pending('this should work'); is_expected.to run.with_params('', 0, 0) }
+    it { is_expected.to run.with_params('1234567890', 10, 10) }
+  end
+
+  describe 'empty upper limit is interpreted as infinity' do
+    it { pending('not implemented'); is_expected.to run.with_params('1234567890ab', '', 10) }
+    it { pending('not implemented'); is_expected.to run.with_params('12345678', '', 10).and_raise_error(Puppet::ParseError, /Expected length/) }
   end
 end
index 3b4fb3e1da154496fb5eabfe5bb61176f88db70d..f0c500eb1e2867262b6c263ac9d6f077f591e231 100755 (executable)
@@ -1,60 +1,21 @@
-#! /usr/bin/env ruby -S rspec
-
 require 'spec_helper'
 
-describe Puppet::Parser::Functions.function(:validate_string) do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  describe 'when calling validate_string from puppet' do
-
-    %w{ foo bar baz }.each do |the_string|
-
-      it "should compile when #{the_string} is a string" do
-        Puppet[:code] = "validate_string('#{the_string}')"
-        scope.compiler.compile
-      end
-
-      it "should compile when #{the_string} is a bare word" do
-        Puppet[:code] = "validate_string(#{the_string})"
-        scope.compiler.compile
-      end
-
-    end
-
-    %w{ true false }.each do |the_string|
-      it "should compile when #{the_string} is a string" do
-        Puppet[:code] = "validate_string('#{the_string}')"
-        scope.compiler.compile
-      end
-
-      it "should not compile when #{the_string} is a bare word" do
-        Puppet[:code] = "validate_string(#{the_string})"
-        expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/)
-      end
-    end
-
-    it "should compile when multiple string arguments are passed" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = ''
-        $bar = 'two'
-        validate_string($foo, $bar)
-      ENDofPUPPETcode
-      scope.compiler.compile
-    end
+describe 'validate_string' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
 
-    it "should compile when an explicitly undef variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        $foo = undef
-        validate_string($foo)
-      ENDofPUPPETcode
-      scope.compiler.compile
+    describe 'valid inputs' do
+      it { is_expected.to run.with_params('') }
+      it { is_expected.to run.with_params('one') }
+      it { is_expected.to run.with_params('one', 'two') }
     end
 
-    it "should compile when an undefined variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do
-      Puppet[:code] = <<-'ENDofPUPPETcode'
-        validate_string($foobarbazishouldnotexist)
-      ENDofPUPPETcode
-      scope.compiler.compile
+    describe 'invalid inputs' do
+      it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
+      it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
     end
   end
 end
index 86e3c31c6e67c7467f3a57e57139d1ea4a410b05..a8348f395cac7e192f14b1b1106caa512732475c 100755 (executable)
@@ -1,38 +1,49 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the values_at function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("values_at")).to eq("function_values_at")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_values_at([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should raise a ParseError if you try to use a range where stop is greater then start" do
-    expect { scope.function_values_at([['a','b'],["3-1"]]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return a value at from an array" do
-    result = scope.function_values_at([['a','b','c'],"1"])
-    expect(result).to(eq(['b']))
-  end
-
-  it "should return a value at from an array when passed a range" do
-    result = scope.function_values_at([['a','b','c'],"0-1"])
-    expect(result).to(eq(['a','b']))
+describe 'values_at' do
+  describe 'signature validation' do
+    it { is_expected.not_to eq(nil) }
+    it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+    it {
+      pending("Current implementation ignores parameters after the first two.")
+      is_expected.to run.with_params([], 0, 1).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+    }
+    it { is_expected.to run.with_params('', 1).and_raise_error(Puppet::ParseError, /Requires array/i) }
+    it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, /Requires array/i) }
+    it { is_expected.to run.with_params(true, 1).and_raise_error(Puppet::ParseError, /Requires array/i) }
+    it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, /Requires array/i) }
+    it { is_expected.to run.with_params([0,1,2], 'two').and_raise_error(Puppet::ParseError, /Unknown format of given index/) }
+    it { is_expected.to run.with_params([0,1,2], []).and_raise_error(Puppet::ParseError, /provide at least one positive index/) }
+    it { is_expected.to run.with_params([0,1,2], '-1-1').and_raise_error(Puppet::ParseError, /Unknown format of given index/) }
+    it { is_expected.to run.with_params([0,1,2], '2-1').and_raise_error(Puppet::ParseError, /Stop index in given indices range is smaller than the start index/) }
   end
 
-  it "should return chosen values from an array when passed number of indexes" do
-    result = scope.function_values_at([['a','b','c'],["0","2"]])
-    expect(result).to(eq(['a','c']))
+  context 'when requesting a single item' do
+    it { is_expected.to run.with_params([0, 1, 2], -1).and_raise_error(Puppet::ParseError, /Unknown format of given index/) }
+    it { is_expected.to run.with_params([0, 1, 2], 0).and_return([0]) }
+    it { is_expected.to run.with_params([0, 1, 2], 1).and_return([1]) }
+    it { is_expected.to run.with_params([0, 1, 2], [1]).and_return([1]) }
+    it { is_expected.to run.with_params([0, 1, 2], '1').and_return([1]) }
+    it { is_expected.to run.with_params([0, 1, 2], '1-1').and_return([1]) }
+    it { is_expected.to run.with_params([0, 1, 2], 2).and_return([2]) }
+    it { is_expected.to run.with_params([0, 1, 2], 3).and_raise_error(Puppet::ParseError, /index exceeds array size/) }
   end
 
-  it "should return chosen values from an array when passed ranges and multiple indexes" do
-    result = scope.function_values_at([['a','b','c','d','e','f','g'],["0","2","4-5"]])
-    expect(result).to(eq(['a','c','e','f']))
+  context 'when requesting multiple items' do
+    it { is_expected.to run.with_params([0, 1, 2], [1, -1]).and_raise_error(Puppet::ParseError, /Unknown format of given index/) }
+    it { is_expected.to run.with_params([0, 1, 2], [0, 2]).and_return([0, 2]) }
+    it { is_expected.to run.with_params([0, 1, 2], ['0-2', 1, 2]).and_return([0, 1, 2, 1, 2]) }
+    it { is_expected.to run.with_params([0, 1, 2], [3, 2]).and_raise_error(Puppet::ParseError, /index exceeds array size/) }
+
+    describe 'different range syntaxes' do
+      it { is_expected.to run.with_params([0, 1, 2], '0-2').and_return([0, 1, 2]) }
+      it { is_expected.to run.with_params([0, 1, 2], '0..2').and_return([0, 1, 2]) }
+      it { is_expected.to run.with_params([0, 1, 2], '0...2').and_return([0, 1]) }
+      it {
+        pending('fix this bounds check')
+        is_expected.to run.with_params([0, 1, 2], '0...3').and_return([0, 1, 2])
+      }
+    end
   end
 end
index 08d21b037067251150c01dda8bede003ef1d27a8..4abf0bd74b1d02ed64134f1d8b86fe0193cbac17 100755 (executable)
@@ -1,31 +1,19 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the values function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should exist" do
-    expect(Puppet::Parser::Functions.function("values")).to eq("function_values")
-  end
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_values([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should return values from a hash" do
-    result = scope.function_values([{'a'=>'1','b'=>'2','c'=>'3'}])
-    # =~ is the RSpec::Matchers::MatchArray matcher.
-    # A.K.A. "array with same elements" (multiset) matching
-    expect(result).to match_array(%w{ 1 2 3 })
-  end
-
-  it "should return a multiset" do
-    result = scope.function_values([{'a'=>'1','b'=>'3','c'=>'3'}])
-    expect(result).to     match_array(%w{ 1 3 3 })
-    expect(result).not_to match_array(%w{ 1 3 })
-  end
-
-  it "should raise a ParseError unless a Hash is provided" do
-    expect { scope.function_values([['a','b','c']]) }.to( raise_error(Puppet::ParseError))
+describe 'values' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the first.")
+    is_expected.to run.with_params({}, 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+  it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /Requires hash to work with/) }
+  it { is_expected.to run.with_params({}).and_return([]) }
+  it { is_expected.to run.with_params({ 'key' => 'value' }).and_return(['value']) }
+  it 'should return the array of values' do
+    result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2', 'duplicate_value_key' => 'value2' }])
+    expect(result).to match_array(['value1', 'value2', 'value2'])
   end
 end
index f265fcee49a3be49259d3ca23067b84c22eca811..abca7ee86fc5c4fa4b368fa8be65a678a0ce06d2 100755 (executable)
@@ -1,31 +1,15 @@
-#! /usr/bin/env ruby -S rspec
 require 'spec_helper'
 
-describe "the zip function" do
-  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
-
-  it "should raise a ParseError if there is less than 1 arguments" do
-    expect { scope.function_zip([]) }.to( raise_error(Puppet::ParseError))
-  end
-
-  it "should be able to zip an array" do
-    result = scope.function_zip([['1','2','3'],['4','5','6']])
-    expect(result).to(eq([["1", "4"], ["2", "5"], ["3", "6"]]))
-    result = scope.function_zip([['1','2','3'],['4','5','6'], false])
-    result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]]))
-  end
-
-  it "should be able to zip an array and flatten" do
-    result = scope.function_zip([['1','2','3'],['4','5','6'], true])
-    result.should(eq(["1", "4", "2", "5", "3", "6"]))
-  end
-
-  it "should accept objects which extend String for the second argument" do
-    class AlsoString < String
-    end
-
-    value = AlsoString.new('false')
-    result = scope.function_zip([['1','2','3'],['4','5','6'],value])
-    result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]]))
-  end
+describe 'zip' do
+  it { is_expected.not_to eq(nil) }
+  it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+  it {
+    pending("Current implementation ignores parameters after the third.")
+    is_expected.to run.with_params([], [], true, []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
+  }
+  it { is_expected.to run.with_params([], []).and_return([]) }
+  it { is_expected.to run.with_params([1,2,3], [4,5,6]).and_return([[1,4], [2,5], [3,6]]) }
+  it { is_expected.to run.with_params([1,2,3], [4,5,6], false).and_return([[1,4], [2,5], [3,6]]) }
+  it { is_expected.to run.with_params([1,2,3], [4,5,6], true).and_return([1, 4, 2, 5, 3, 6]) }
 end
diff --git a/spec/puppetlabs_spec_helper_clone.rb b/spec/puppetlabs_spec_helper_clone.rb
new file mode 100644 (file)
index 0000000..6a94a3b
--- /dev/null
@@ -0,0 +1,34 @@
+#This file pulls in only the minimum necessary to let unmigrated specs still work
+
+# Define the main module namespace for use by the helper modules
+module PuppetlabsSpec
+  # FIXTURE_DIR represents the standard locations of all fixture data. Normally
+  # this represents <project>/spec/fixtures. This will be used by the fixtures
+  # library to find relative fixture data.
+  FIXTURE_DIR = File.join("spec", "fixtures") unless defined?(FIXTURE_DIR)
+end
+
+# Require all necessary helper libraries so they can be used later
+require 'puppetlabs_spec_helper/puppetlabs_spec/files'
+require 'puppetlabs_spec_helper/puppetlabs_spec/fixtures'
+#require 'puppetlabs_spec_helper/puppetlabs_spec/puppet_internals'
+require 'puppetlabs_spec_helper/puppetlabs_spec/matchers'
+
+RSpec.configure do |config|
+  # Include PuppetlabsSpec helpers so they can be called at convenience
+  config.extend PuppetlabsSpec::Files
+  config.extend PuppetlabsSpec::Fixtures
+  config.include PuppetlabsSpec::Fixtures
+
+  config.parser = 'future' if ENV['FUTURE_PARSER'] == 'yes'
+  config.strict_variables = true if ENV['STRICT_VARIABLES'] == 'yes'
+  config.stringify_facts = false if ENV['STRINGIFY_FACTS'] == 'no'
+  config.trusted_node_data = true if ENV['TRUSTED_NODE_DATA'] == 'yes'
+  config.ordering = ENV['ORDERING'] if ENV['ORDERING']
+
+  # This will cleanup any files that were created with tmpdir or tmpfile
+  config.after :each do
+    PuppetlabsSpec::Files.cleanup
+  end
+end
+
index be392fd02d5dd7d96961be059d84966940e2d421..f941c1c60135adf8960be0403d8f2891ad6a0c81 100755 (executable)
@@ -18,11 +18,17 @@ require 'puppet_spec/matchers'
 require 'puppet_spec/database'
 require 'monkey_patches/alias_should_to_must'
 require 'mocha/api'
+#require 'puppetlabs_spec_helper/module_spec_helper'
+require 'puppetlabs_spec_helper_clone'
 
 # hack to enable all the expect syntax (like allow_any_instance_of) in rspec-puppet examples
 RSpec::Mocks::Syntax.enable_expect(RSpec::Puppet::ManifestMatchers)
 
 RSpec.configure do |config|
+  config.module_path = File.join(File.dirname(File.expand_path(__FILE__)), 'fixtures', 'modules')
+  config.manifest_dir = File.join(File.dirname(File.expand_path(__FILE__)), 'fixtures', 'manifests')
+  config.environmentpath = spec_path = File.expand_path(File.join(Dir.pwd, 'spec'))
+
   config.add_setting :puppet_future
   #config.puppet_future = (ENV['FUTURE_PARSER'] == 'yes' or Puppet.version.to_f >= 4.0)
   config.puppet_future = Puppet.version.to_f >= 4.0
@@ -35,8 +41,6 @@ RSpec.configure do |config|
     Facter.clear
     Facter.clear_messages
 
-    Puppet[:parser] = 'future' if ENV['FUTURE_PARSER'] == 'yes'
-
     RSpec::Mocks.setup
   end
 
@@ -45,3 +49,7 @@ RSpec.configure do |config|
     RSpec::Mocks.teardown
   end
 end
+
+# Helper class to test handling of arguments which are derived from string
+class AlsoString < String
+end
index c06137d7e2a93c0a7176fd5d6720f1c2f9c0a700..c278b79849f019aee604f50bd1be20075c15677e 100755 (executable)
@@ -25,6 +25,7 @@ describe Facter::Util::PuppetSettings do
       before :each do
         Puppet.expects(:[]).with(:vardir).returns vardir
       end
+
       it 'should yield to the block' do
         subject.with_puppet { Puppet[:vardir] }
       end