From 190b9438c58eab2322bdf216be1aaae109d14072 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 3 Jan 2013 13:53:03 +0000 Subject: Add test/validation for is_numeric if created from an arithmetical operation --- spec/unit/puppet/parser/functions/is_numeric_spec.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/is_numeric_spec.rb b/spec/unit/puppet/parser/functions/is_numeric_spec.rb index 4078b37..d7440fb 100644 --- a/spec/unit/puppet/parser/functions/is_numeric_spec.rb +++ b/spec/unit/puppet/parser/functions/is_numeric_spec.rb @@ -22,6 +22,16 @@ describe "the is_numeric function" do result.should(eq(true)) end + it "should return true if an integer is created from an arithmetical operation" do + result = scope.function_is_numeric([3*2]) + result.should(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]) + result.should(eq(true)) + end + it "should return false if a string" do result = scope.function_is_numeric(["asdf"]) result.should(eq(false)) -- cgit v1.2.3 From b86f5dc1293ad431581afbb8f294560f3cf34d43 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 3 Jan 2013 14:02:58 +0000 Subject: Add test/validation for is_integer if created from an arithmetical operation --- spec/unit/puppet/parser/functions/is_integer_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/is_integer_spec.rb b/spec/unit/puppet/parser/functions/is_integer_spec.rb index 5afbba4..4335795 100644 --- a/spec/unit/puppet/parser/functions/is_integer_spec.rb +++ b/spec/unit/puppet/parser/functions/is_integer_spec.rb @@ -26,4 +26,9 @@ describe "the is_integer function" do result = scope.function_is_integer(["asdf"]) result.should(eq(false)) end + + it "should return true if an integer is created from an arithmetical operation" do + result = scope.function_is_integer([3*2]) + result.should(eq(true)) + end end -- cgit v1.2.3 From a773281760469f2b104c89b2bfb760c0e3013422 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 3 Jan 2013 14:05:29 +0000 Subject: Add test/validation for is_float if created from an arithmetical operation --- spec/unit/puppet/parser/functions/is_float_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/is_float_spec.rb b/spec/unit/puppet/parser/functions/is_float_spec.rb index 2f527d9..b7d73b0 100644 --- a/spec/unit/puppet/parser/functions/is_float_spec.rb +++ b/spec/unit/puppet/parser/functions/is_float_spec.rb @@ -26,4 +26,8 @@ describe "the is_float function" do result = scope.function_is_float(["3"]) result.should(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]) + result.should(eq(true)) + end end -- cgit v1.2.3 From 6902cc582eef6eb59cd5252208eca5ac608995bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Thu, 6 Dec 2012 11:01:19 +0100 Subject: Add validate_cmd function --- .../puppet/parser/functions/validate_cmd_spec.rb | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/validate_cmd_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/validate_cmd_spec.rb b/spec/unit/puppet/parser/functions/validate_cmd_spec.rb new file mode 100644 index 0000000..0730a59 --- /dev/null +++ b/spec/unit/puppet/parser/functions/validate_cmd_spec.rb @@ -0,0 +1,81 @@ +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_cmd) 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_cmd) + 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_cmd(#{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', '/bin/echo' ], + [ '/full/path/to/something', '/bin/cat' ], + ] + + inputs.each do |input| + it "validate_cmd(#{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", "/bin/false" ], + ] + + inputs.each do |input| + it "validate_cmd(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /validate_cmd.*?failed to validate content with command/ + end + end + 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_cmd(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /#{input[2]}/ + end + end + end + + describe "Test output message" do + it "validate_cmd('whatever', 'kthnksbye') should fail" do + expect { subject.call ['whatever', 'kthnksbye'] }.to raise_error /kthnksbye.*not found/ + end + end + end +end -- cgit v1.2.3 From 3a97c2314c67245be3190cc485cff63e40a833fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Thu, 6 Dec 2012 11:33:43 +0100 Subject: Add validate_augeas function --- .../parser/functions/validate_augeas_spec.rb | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/validate_augeas_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/validate_augeas_spec.rb b/spec/unit/puppet/parser/functions/validate_augeas_spec.rb new file mode 100644 index 0000000..ab5c140 --- /dev/null +++ b/spec/unit/puppet/parser/functions/validate_augeas_spec.rb @@ -0,0 +1,102 @@ +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 + end + + 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 + 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 = [ + [ "root:x:0:0:root\n", 'Passwd.lns' ], + [ "127.0.1.1\n", 'Hosts.lns' ], + ] + + 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 + end + end + + describe "Nicer Error Messages" do + # The intent here is to make sure the function returns the 3rd argument + # in the exception thrown + inputs = [ + [ "root:x:0:0:root\n", 'Passwd.lns', [], 'Failed to validate passwd content' ], + [ "127.0.1.1\n", 'Hosts.lns', [], 'Wrong hosts content' ], + ] + + inputs.each do |input| + it "validate_augeas(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /#{input[2]}/ + end + end + end + + describe "Passing simple unit 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 + end + end + + describe "Failing simple unit 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 + end + end + end +end -- cgit v1.2.3 From 5f22933e75058310af06a993d663be2da7759c3d Mon Sep 17 00:00:00 2001 From: stephen Date: Tue, 16 Oct 2012 11:54:14 +0100 Subject: ammend .should raise_error to .to raise_error --- spec/unit/puppet/parser/functions/get_module_path_spec.rb | 6 +++--- spec/unit/puppet/parser/functions/merge_spec.rb | 2 +- spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb | 6 +++--- spec/unit/puppet/parser/functions/validate_array_spec.rb | 6 +++--- spec/unit/puppet/parser/functions/validate_bool_spec.rb | 8 ++++---- spec/unit/puppet/parser/functions/validate_hash_spec.rb | 6 +++--- spec/unit/puppet/parser/functions/validate_string_spec.rb | 2 +- 7 files changed, 18 insertions(+), 18 deletions(-) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/get_module_path_spec.rb b/spec/unit/puppet/parser/functions/get_module_path_spec.rb index e761706..486bef6 100644 --- a/spec/unit/puppet/parser/functions/get_module_path_spec.rb +++ b/spec/unit/puppet/parser/functions/get_module_path_spec.rb @@ -15,11 +15,11 @@ describe Puppet::Parser::Functions.function(:get_module_path) do end it 'should only allow one argument' do - expect { scope.function_get_module_path([]) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) - expect { scope.function_get_module_path(['1','2','3']) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) + 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']) }.should raise_error(Puppet::ParseError, /Could not find module/) + 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" } diff --git a/spec/unit/puppet/parser/functions/merge_spec.rb b/spec/unit/puppet/parser/functions/merge_spec.rb index db7d837..04169e7 100644 --- a/spec/unit/puppet/parser/functions/merge_spec.rb +++ b/spec/unit/puppet/parser/functions/merge_spec.rb @@ -25,7 +25,7 @@ describe Puppet::Parser::Functions.function(:merge) do describe 'when calling merge on the scope instance' do it 'should require all parameters are hashes' do - expect { new_hash = scope.function_merge([{}, '2'])}.should raise_error(Puppet::ParseError, /unexpected argument type String/) + expect { new_hash = scope.function_merge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/) end it 'should be able to merge two hashes' do diff --git a/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb b/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb index a692c31..df8fb8e 100644 --- a/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb +++ b/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb @@ -9,11 +9,11 @@ describe "the str2saltedsha512 function" do end it "should raise a ParseError if there is less than 1 argument" do - expect { scope.function_str2saltedsha512([]) }.should( raise_error(Puppet::ParseError) ) + 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']) }.should( raise_error(Puppet::ParseError) ) + 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 @@ -22,7 +22,7 @@ describe "the str2saltedsha512 function" do end it "should raise an error if you pass a non-string password" do - expect { scope.function_str2saltedsha512([1234]) }.should( raise_error(Puppet::ParseError) ) + expect { scope.function_str2saltedsha512([1234]) }.to( raise_error(Puppet::ParseError) ) end it "should generate a valid password" do diff --git a/spec/unit/puppet/parser/functions/validate_array_spec.rb b/spec/unit/puppet/parser/functions/validate_array_spec.rb index 8eee72a..4b31cfd 100644 --- a/spec/unit/puppet/parser/functions/validate_array_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_array_spec.rb @@ -9,12 +9,12 @@ describe Puppet::Parser::Functions.function(:validate_array) 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 }.should raise_error(Puppet::ParseError, /is not an Array/) + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/) end it "should not compile when #{the_string} is a bare word" do Puppet[:code] = "validate_array(#{the_string})" - expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not an Array/) + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/) end end @@ -32,7 +32,7 @@ describe Puppet::Parser::Functions.function(:validate_array) do $foo = undef validate_array($foo) ENDofPUPPETcode - expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not an Array/) + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/) end end end diff --git a/spec/unit/puppet/parser/functions/validate_bool_spec.rb b/spec/unit/puppet/parser/functions/validate_bool_spec.rb index 31ab8fb..261fb23 100644 --- a/spec/unit/puppet/parser/functions/validate_bool_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_bool_spec.rb @@ -10,7 +10,7 @@ describe Puppet::Parser::Functions.function(:validate_bool) do it "should not compile when #{the_string} is a string" do Puppet[:code] = "validate_bool('#{the_string}')" - expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/) + 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 @@ -22,12 +22,12 @@ describe Puppet::Parser::Functions.function(:validate_bool) do it "should not compile when an arbitrary string is passed" do Puppet[:code] = 'validate_bool("jeff and dan are awesome")' - expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/) + 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 }.should raise_error(Puppet::ParseError, /wrong number of arguments/) + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /wrong number of arguments/) end it "should compile when multiple boolean arguments are passed" do @@ -45,7 +45,7 @@ describe Puppet::Parser::Functions.function(:validate_bool) do $bar = false validate_bool($foo, $bar, true, false, 'jeff') ENDofPUPPETcode - expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/) + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/) end end end diff --git a/spec/unit/puppet/parser/functions/validate_hash_spec.rb b/spec/unit/puppet/parser/functions/validate_hash_spec.rb index 06d77a1..a0c35c2 100644 --- a/spec/unit/puppet/parser/functions/validate_hash_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_hash_spec.rb @@ -11,12 +11,12 @@ describe Puppet::Parser::Functions.function(:validate_hash) do it "should not compile when #{the_string} is a string" do Puppet[:code] = "validate_hash('#{the_string}')" - expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a Hash/) + 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 }.should raise_error(Puppet::ParseError, /is not a Hash/) + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/) end end @@ -35,7 +35,7 @@ describe Puppet::Parser::Functions.function(:validate_hash) do $foo = undef validate_hash($foo) ENDofPUPPETcode - expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a Hash/) + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/) end end diff --git a/spec/unit/puppet/parser/functions/validate_string_spec.rb b/spec/unit/puppet/parser/functions/validate_string_spec.rb index 007b4ca..3b4fb3e 100644 --- a/spec/unit/puppet/parser/functions/validate_string_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_string_spec.rb @@ -29,7 +29,7 @@ describe Puppet::Parser::Functions.function(:validate_string) do it "should not compile when #{the_string} is a bare word" do Puppet[:code] = "validate_string(#{the_string})" - expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a string/) + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/) end end -- cgit v1.2.3 From 683ac8f8aa5f349ece98165d92a8d271b99b855e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Tue, 5 Feb 2013 09:01:48 +0100 Subject: validate_cmd: Use Puppet::Util.execute --- spec/unit/puppet/parser/functions/validate_cmd_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/validate_cmd_spec.rb b/spec/unit/puppet/parser/functions/validate_cmd_spec.rb index 0730a59..69ea7f4 100644 --- a/spec/unit/puppet/parser/functions/validate_cmd_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_cmd_spec.rb @@ -74,7 +74,7 @@ describe Puppet::Parser::Functions.function(:validate_cmd) do describe "Test output message" do it "validate_cmd('whatever', 'kthnksbye') should fail" do - expect { subject.call ['whatever', 'kthnksbye'] }.to raise_error /kthnksbye.*not found/ + expect { subject.call ['whatever', 'kthnksbye'] }.to raise_error /kthnksbye.* returned 1/ end end end -- cgit v1.2.3 From 3cef5d9e3323115c896e76f9e217cac061ab156f Mon Sep 17 00:00:00 2001 From: fatmcgav Date: Tue, 12 Feb 2013 15:07:18 +0000 Subject: (#19201) Add concat function to join two arrays Without this patch applied there is no easy way to append one array to another. This is a problem because it is often desirable to join two arrays without flattening the contents into a single, one dimensional array. This patch addresses the problem by adding a `concat()` function which takes two arguments. The arguments will be concatenated together and a new array returned to the caller. Reviewed-by: Jeff McCune --- spec/unit/puppet/parser/functions/concat_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/concat_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/concat_spec.rb b/spec/unit/puppet/parser/functions/concat_spec.rb new file mode 100644 index 0000000..123188b --- /dev/null +++ b/spec/unit/puppet/parser/functions/concat_spec.rb @@ -0,0 +1,15 @@ +#! /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 there is less than 1 arguments" do + lambda { scope.function_concat([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should be able to concat an array" do + result = scope.function_concat([['1','2','3'],['4','5','6']]) + result.should(eq(['1','2','3','4','5','6'])) + end +end -- cgit v1.2.3 From 95cf3fed689a72c6a7c6641fdf462770f94112ca Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Thu, 14 Feb 2013 11:57:35 -0700 Subject: (#19272) Add has_element() function It is exceptionally difficult to determine if an array contains an element matching a specific value without an iteration or loop construct. This function is the Puppet equivalent of Array.includes?(foo) in Ruby. The implementation is a verbatim copy of has_key() with the minor modifications needed to support arrays instead of hashes. --- .../puppet/parser/functions/has_element_spec.rb | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/has_element_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/has_element_spec.rb b/spec/unit/puppet/parser/functions/has_element_spec.rb new file mode 100644 index 0000000..b5270d2 --- /dev/null +++ b/spec/unit/puppet/parser/functions/has_element_spec.rb @@ -0,0 +1,42 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:has_element) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe 'when calling has_element from puppet' do + it "should not compile when no arguments are passed" do + pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = '$x = has_element()' + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should not compile when 1 argument is passed" do + pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = "$x = has_element('foo')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should require the first value to be an Array" do + pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = "$x = has_element('foo', 'bar')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /expects the first argument to be an array/) + end + end + + describe 'when calling the function has_element from a scope instance' do + it 'should detect existing elements' do + scope.function_has_element([['one'], 'one']).should be_true + end + + it 'should detect existing elements' do + scope.function_has_element([['one'], 'two']).should be_false + end + end +end -- cgit v1.2.3 From a45d4f13070cd05c029b96c789f499133721d523 Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Fri, 15 Feb 2013 11:56:16 -0800 Subject: Revert "Merge pull request #130 from jhoblitt/has_element" This reverts commit f7a18189ec338b01b0fc89d75def832753af3868, reversing changes made to 36a7b29630a4d4de17af79b5dd4e9491ec20b123. I'm reverting this change because of concerns raised by Peter Meier that it duplicates the "in" operator in the DSL. The "in" operator is new information that I did not posses when I made the decision to merge. Because of this new information I'm un-merging and continuing the discussion in the comments of https://projects.puppetlabs.com/issues/19272 Reference: GH-130 --- .../puppet/parser/functions/has_element_spec.rb | 42 ---------------------- 1 file changed, 42 deletions(-) delete mode 100644 spec/unit/puppet/parser/functions/has_element_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/has_element_spec.rb b/spec/unit/puppet/parser/functions/has_element_spec.rb deleted file mode 100644 index b5270d2..0000000 --- a/spec/unit/puppet/parser/functions/has_element_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -#! /usr/bin/env ruby -S rspec -require 'spec_helper' - -describe Puppet::Parser::Functions.function(:has_element) do - let(:scope) { PuppetlabsSpec::PuppetInternals.scope } - - describe 'when calling has_element from puppet' do - it "should not compile when no arguments are passed" do - pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ - Puppet[:code] = '$x = has_element()' - expect { - scope.compiler.compile - }.to raise_error(Puppet::ParseError, /wrong number of arguments/) - end - - it "should not compile when 1 argument is passed" do - pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ - Puppet[:code] = "$x = has_element('foo')" - expect { - scope.compiler.compile - }.to raise_error(Puppet::ParseError, /wrong number of arguments/) - end - - it "should require the first value to be an Array" do - pending("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ - Puppet[:code] = "$x = has_element('foo', 'bar')" - expect { - scope.compiler.compile - }.to raise_error(Puppet::ParseError, /expects the first argument to be an array/) - end - end - - describe 'when calling the function has_element from a scope instance' do - it 'should detect existing elements' do - scope.function_has_element([['one'], 'one']).should be_true - end - - it 'should detect existing elements' do - scope.function_has_element([['one'], 'two']).should be_false - end - end -end -- cgit v1.2.3 From e80207bede37e498c32bea6d56c46f1dd709721e Mon Sep 17 00:00:00 2001 From: Uwe Stuehler Date: Tue, 23 Oct 2012 16:43:03 +0200 Subject: Fix number of arguments check in flatten() The function only uses the first argument, so raise an error with too few arguments *and* with too many arguments. --- spec/unit/puppet/parser/functions/flatten_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/flatten_spec.rb b/spec/unit/puppet/parser/functions/flatten_spec.rb index d4dfd20..dba7a6b 100755 --- a/spec/unit/puppet/parser/functions/flatten_spec.rb +++ b/spec/unit/puppet/parser/functions/flatten_spec.rb @@ -11,6 +11,10 @@ describe "the flatten function" do lambda { scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) end + it "should raise a ParseError if there is more than 1 argument" do + lambda { scope.function_flatten([[], []]) }.should( raise_error(Puppet::ParseError)) + end + it "should flatten a complex data structure" do result = scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) result.should(eq(["a","b","c","d","e","f","g"])) -- cgit v1.2.3 From 5a11279cc54e7f9f5d168c75728f990d0bd0b694 Mon Sep 17 00:00:00 2001 From: Uwe Stuehler Date: Tue, 23 Oct 2012 16:43:03 +0200 Subject: Fix number of arguments check in flatten() The function only uses the first argument, so raise an error with too few arguments *and* with too many arguments. --- spec/unit/puppet/parser/functions/flatten_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/flatten_spec.rb b/spec/unit/puppet/parser/functions/flatten_spec.rb index d4dfd20..dba7a6b 100755 --- a/spec/unit/puppet/parser/functions/flatten_spec.rb +++ b/spec/unit/puppet/parser/functions/flatten_spec.rb @@ -11,6 +11,10 @@ describe "the flatten function" do lambda { scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) end + it "should raise a ParseError if there is more than 1 argument" do + lambda { scope.function_flatten([[], []]) }.should( raise_error(Puppet::ParseError)) + end + it "should flatten a complex data structure" do result = scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) result.should(eq(["a","b","c","d","e","f","g"])) -- cgit v1.2.3 From 5d5a4d466ed6530fa8b68162e96bdaaaf35066e1 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Mon, 17 Dec 2012 06:22:36 -0700 Subject: str2bool should return a boolean if called with a boolean --- spec/unit/puppet/parser/functions/str2bool_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/str2bool_spec.rb b/spec/unit/puppet/parser/functions/str2bool_spec.rb index 2782bbe..ef6350f 100644 --- a/spec/unit/puppet/parser/functions/str2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/str2bool_spec.rb @@ -21,4 +21,11 @@ describe "the str2bool function" do result = scope.function_str2bool(["undef"]) result.should(eq(false)) end + + it "should return the boolean it was called with" do + result = scope.function_str2bool([true]) + result.should(eq(true)) + result = scope.function_str2bool([false]) + result.should(eq(false)) + end end -- cgit v1.2.3 From 961dcab15d3cf00eabeb5a496efc9da051169241 Mon Sep 17 00:00:00 2001 From: Eric Shamow Date: Tue, 3 Apr 2012 22:30:46 -0400 Subject: (#13610) Add is_function_available to stdlib This function provides a simple wrapper around Puppet::Parser::Functions.function for access within Puppet manifests. This will allow users to check whether or not a plugin or functionality such as hiera is installed on the server. --- .../parser/functions/is_function_available.rb | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/is_function_available.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/is_function_available.rb b/spec/unit/puppet/parser/functions/is_function_available.rb new file mode 100644 index 0000000..bd40c51 --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_function_available.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env 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 + Puppet::Parser::Functions.function("is_function_available").should == "function_is_function_available" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_function_available([]) }.should( 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']) + result.should(eq(false)) + end + + it "should return true if an available function is passed" do + result = @scope.function_is_function_available(['require']) + result.should(eq(true)) + end + +end -- cgit v1.2.3 From 05273419e1c8b34115ede15b1d8a8739f6a0db00 Mon Sep 17 00:00:00 2001 From: Kristof Willaert Date: Tue, 19 Mar 2013 10:00:57 +0100 Subject: Add floor function implementation and unit tests --- spec/unit/puppet/parser/functions/floor_spec.rb | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/floor_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/floor_spec.rb b/spec/unit/puppet/parser/functions/floor_spec.rb new file mode 100644 index 0000000..dbc8c77 --- /dev/null +++ b/spec/unit/puppet/parser/functions/floor_spec.rb @@ -0,0 +1,39 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the floor function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("floor").should == "function_floor" + end + + it "should raise a ParseError if there is less than 1 argument" do + lambda { scope.function_floor([]) }.should( raise_error(Puppet::ParseError, /Wrong number of arguments/)) + end + + it "should should raise a ParseError if input isn't numeric (eg. String)" do + lambda { scope.function_floor(["foo"]) }.should( raise_error(Puppet::ParseError, /Wrong argument type/)) + end + + it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do + lambda { scope.function_floor([true]) }.should( 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]) + result.is_a?(Integer).should(eq(true)) + end + + it "should return the input when an integer is passed" do + result = scope.function_floor([7]) + result.should(eq(7)) + end + + it "should return the largest integer less than or equal to the input" do + result = scope.function_floor([3.8]) + result.should(eq(3)) + end +end + -- cgit v1.2.3 From 88a93ac6cdf38045e1cf29325a70e5e4143016b3 Mon Sep 17 00:00:00 2001 From: Richard Soderberg Date: Tue, 26 Mar 2013 15:45:40 -0700 Subject: add suffix function to accompany the prefix function --- spec/unit/puppet/parser/functions/suffix_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/suffix_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/suffix_spec.rb b/spec/unit/puppet/parser/functions/suffix_spec.rb new file mode 100644 index 0000000..c28f719 --- /dev/null +++ b/spec/unit/puppet/parser/functions/suffix_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the suffix function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("suffix").should == "function_suffix" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { scope.function_suffix([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return a suffixed array" do + result = scope.function_suffix([['a','b','c'], 'p']) + result.should(eq(['ap','bp','cp'])) + end +end -- cgit v1.2.3 From ff5dd5d75adb3723e106ca20bac4e68466395a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 20 Mar 2013 16:36:20 +0100 Subject: Allow comparisons of Numeric and number as String Puppet passes numbers as String to functions, but it makes more sense to compare them as Numeric. But sometimes Puppet passes them as the wrong type, see: https://projects.puppetlabs.com/issues/19812 --- spec/unit/puppet/parser/functions/max_spec.rb | 4 ++++ spec/unit/puppet/parser/functions/min_spec.rb | 4 ++++ 2 files changed, 8 insertions(+) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/max_spec.rb b/spec/unit/puppet/parser/functions/max_spec.rb index 604927e..ff6f2b3 100755 --- a/spec/unit/puppet/parser/functions/max_spec.rb +++ b/spec/unit/puppet/parser/functions/max_spec.rb @@ -20,4 +20,8 @@ describe "the max function" do it "should be able to compare numbers" do scope.function_max([6,8,4]).should(eq(8)) end + + it "should be able to compare a number with a stringified number" do + scope.function_max([1,"2"]).should(eq("2")) + end end diff --git a/spec/unit/puppet/parser/functions/min_spec.rb b/spec/unit/puppet/parser/functions/min_spec.rb index 781422c..71d593e 100755 --- a/spec/unit/puppet/parser/functions/min_spec.rb +++ b/spec/unit/puppet/parser/functions/min_spec.rb @@ -20,4 +20,8 @@ describe "the min function" do it "should be able to compare numbers" do scope.function_min([6,8,4]).should(eq(4)) end + + it "should be able to compare a number with a stringified number" do + scope.function_min([1,"2"]).should(eq(1)) + end end -- cgit v1.2.3 From e6916f83fd35989db4b86dfb10716c9198994389 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 10:04:05 -0400 Subject: Enable num2bool to accept numeric input Also ignore rspec fixtures directory --- spec/unit/puppet/parser/functions/num2bool_spec.rb | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb index 640c689..e51ee45 100644 --- a/spec/unit/puppet/parser/functions/num2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -12,13 +12,33 @@ describe "the num2bool function" do lambda { scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError)) end - it "should return true if 1" do + it "should return true if passed string 1" do result = scope.function_num2bool(["1"]) result.should(be_true) end - it "should return false if 0" do + it "should return true if passed number 1" do + result = scope.function_num2bool([1]) + result.should(be_true) + end + + it "should return false if passed string 0" do result = scope.function_num2bool(["0"]) result.should(be_false) end + + it "should return false if passed number 0" do + result = scope.function_num2bool([0]) + result.should(be_false) + end + + it "should return false if passed string -1" do + result = scope.function_num2bool(["-1"]) + result.should(be_false) + end + + it "should return false if passed number -1" do + result = scope.function_num2bool([-1]) + result.should(be_false) + end end -- cgit v1.2.3 From 4a5218a8af8c3ffaf9ea2348a3900b19d6a95416 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 12:03:33 -0400 Subject: Reworked number-handling logic No more coercing to String and regex matching. Instead, we now coerce to Integer at the beginning or raise an error if we cannot coerce to Integer. A consequence of this change is that the function will now accept blatantly non-numeric strings as input, and return false. This seems a bit goofy to me, but it's how String#to_i works. If we really don't like this, then I'm open to suggestions. --- spec/unit/puppet/parser/functions/num2bool_spec.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb index e51ee45..5ce981e 100644 --- a/spec/unit/puppet/parser/functions/num2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -8,10 +8,14 @@ describe "the num2bool function" do Puppet::Parser::Functions.function("num2bool").should == "function_num2bool" end - it "should raise a ParseError if there is less than 1 arguments" do + it "should raise a ParseError if there are no arguments" do lambda { scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError)) end + it "should raise a ParseError if there are more than 1 arguments" do + lambda { scope.function_num2bool(["foo","bar"]) }.should( raise_error(Puppet::ParseError)) + end + it "should return true if passed string 1" do result = scope.function_num2bool(["1"]) result.should(be_true) @@ -41,4 +45,9 @@ describe "the num2bool function" do result = scope.function_num2bool([-1]) result.should(be_false) end + + it "should return false if passed something non-numeric" do + result = scope.function_num2bool(["xyzzy"]) + result.should(be_false) + end end -- cgit v1.2.3 From 8d217f0012fef332642faf485ad187773a95bcc1 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 15:06:36 -0400 Subject: (19864) num2bool match fix This is a bit more heavy-handed than I might like, but it does appear to do the right things: * accepts numeric input appropriately, truncating floats * matches string input against a regex, then coerces number-looking strings to int * makes a best effort to coerce anything else to a string, then subjects it to the same treatment * raises an error in the event of incorrect number of arguments or non-number-looking strings I've also included some additional unit tests. --- spec/unit/puppet/parser/functions/num2bool_spec.rb | 34 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb index 5ce981e..038881f 100644 --- a/spec/unit/puppet/parser/functions/num2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -16,6 +16,10 @@ describe "the num2bool function" do lambda { scope.function_num2bool(["foo","bar"]) }.should( raise_error(Puppet::ParseError)) end + it "should raise a ParseError if passed something non-numeric" do + lambda { scope.function_num2bool(["xyzzy"]) }.should( raise_error(Puppet::ParseError)) + end + it "should return true if passed string 1" do result = scope.function_num2bool(["1"]) result.should(be_true) @@ -26,6 +30,16 @@ describe "the num2bool function" do result.should(be_true) end + it "should return true if passed array with string 1" do + result = scope.function_num2bool([["1"]]) + result.should(be_true) + end + + it "should return true if passed array with number 1" do + result = scope.function_num2bool([[1]]) + result.should(be_true) + end + it "should return false if passed string 0" do result = scope.function_num2bool(["0"]) result.should(be_false) @@ -36,6 +50,16 @@ describe "the num2bool function" do result.should(be_false) end + it "should return false if passed array with string 0" do + result = scope.function_num2bool([["0"]]) + result.should(be_false) + end + + it "should return false if passed array with number 0" do + result = scope.function_num2bool([[0]]) + result.should(be_false) + end + it "should return false if passed string -1" do result = scope.function_num2bool(["-1"]) result.should(be_false) @@ -46,8 +70,14 @@ describe "the num2bool function" do result.should(be_false) end - it "should return false if passed something non-numeric" do - result = scope.function_num2bool(["xyzzy"]) + it "should return false if passed array with string -1" do + result = scope.function_num2bool([["-1"]]) result.should(be_false) end + + it "should return false if passed array with number -1" do + result = scope.function_num2bool([[-1]]) + result.should(be_false) + end + end -- cgit v1.2.3 From c372f177708df4c844337e9901646b7b84b86cd8 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Mon, 1 Apr 2013 11:44:09 -0400 Subject: Cleanup per adrianthebo suggestions * use Float() to process string arguments * get rid of doubly nested arrays * removing needless ternary operator * improving error message handling --- spec/unit/puppet/parser/functions/num2bool_spec.rb | 36 ++++++---------------- 1 file changed, 10 insertions(+), 26 deletions(-) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb index 038881f..b56196d 100644 --- a/spec/unit/puppet/parser/functions/num2bool_spec.rb +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -25,18 +25,13 @@ describe "the num2bool function" do result.should(be_true) end - it "should return true if passed number 1" do - result = scope.function_num2bool([1]) - result.should(be_true) - end - - it "should return true if passed array with string 1" do - result = scope.function_num2bool([["1"]]) + it "should return true if passed string 1.5" do + result = scope.function_num2bool(["1.5"]) result.should(be_true) end - it "should return true if passed array with number 1" do - result = scope.function_num2bool([[1]]) + it "should return true if passed number 1" do + result = scope.function_num2bool([1]) result.should(be_true) end @@ -50,34 +45,23 @@ describe "the num2bool function" do result.should(be_false) end - it "should return false if passed array with string 0" do - result = scope.function_num2bool([["0"]]) - result.should(be_false) - end - - it "should return false if passed array with number 0" do - result = scope.function_num2bool([[0]]) - result.should(be_false) - end - it "should return false if passed string -1" do result = scope.function_num2bool(["-1"]) result.should(be_false) end - it "should return false if passed number -1" do - result = scope.function_num2bool([-1]) + it "should return false if passed string -1.5" do + result = scope.function_num2bool(["-1.5"]) result.should(be_false) end - it "should return false if passed array with string -1" do - result = scope.function_num2bool([["-1"]]) + it "should return false if passed number -1" do + result = scope.function_num2bool([-1]) result.should(be_false) end - it "should return false if passed array with number -1" do - result = scope.function_num2bool([[-1]]) + it "should return false if passed float -1.5" do + result = scope.function_num2bool([-1.5]) result.should(be_false) end - end -- cgit v1.2.3 From 7a2fb80834d3ec4d21efe58e5e8af188e5bdd07c Mon Sep 17 00:00:00 2001 From: Amos Shapira Date: Sun, 31 Mar 2013 23:37:30 +1100 Subject: (#19998) Implement any2array This change is to implement a new function "any2array", which will take any argument or arguments and create an array which contains it. If the argument is a single array then it will be returned as-is. If the argument is a single hash then it will be converted into an array. Otherwise (if there are more than one argument, or the only argument is not an array or a hash) the function will return an array containing all the arguments. --- .../unit/puppet/parser/functions/any2array_spec.rb | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/any2array_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/any2array_spec.rb b/spec/unit/puppet/parser/functions/any2array_spec.rb new file mode 100644 index 0000000..b266e84 --- /dev/null +++ b/spec/unit/puppet/parser/functions/any2array_spec.rb @@ -0,0 +1,55 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the any2array function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("any2array").should == "function_any2array" + end + + it "should return an empty array if there is less than 1 argument" do + result = scope.function_any2array([]) + result.should(eq([])) + end + + it "should convert boolean true to [ true ] " do + result = scope.function_any2array([true]) + result.should(eq([true])) + end + + it "should convert one object to [object]" do + result = scope.function_any2array(['one']) + result.should(eq(['one'])) + end + + it "should convert multiple objects to [objects]" do + result = scope.function_any2array(['one', 'two']) + result.should(eq(['one', 'two'])) + end + + it "should return empty array it was called with" do + result = scope.function_any2array([[]]) + result.should(eq([])) + end + + it "should return one-member array it was called with" do + result = scope.function_any2array([['string']]) + result.should(eq(['string'])) + end + + it "should return multi-member array it was called with" do + result = scope.function_any2array([['one', 'two']]) + result.should(eq(['one', 'two'])) + end + + it "should return members of a hash it was called with" do + result = scope.function_any2array([{ 'key' => 'value' }]) + result.should(eq(['key', 'value'])) + end + + it "should return an empty array if it was called with an empty hash" do + result = scope.function_any2array([{ }]) + result.should(eq([])) + end +end -- cgit v1.2.3 From f28550e78996f908b076cdc9aebcbe584c777cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 10 Apr 2013 14:38:14 +0200 Subject: Add a count function Similar to the ruby count method on arrays. --- spec/unit/puppet/parser/functions/count_spec.rb | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/count_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/count_spec.rb b/spec/unit/puppet/parser/functions/count_spec.rb new file mode 100644 index 0000000..5e4a8b9 --- /dev/null +++ b/spec/unit/puppet/parser/functions/count_spec.rb @@ -0,0 +1,27 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the count function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("count").should == "function_count" + end + + it "should raise a ArgumentError if there is more than 2 arguments" do + lambda { scope.function_count(['foo', 'bar', 'baz']) }.should( raise_error(ArgumentError)) + end + + it "should be able to count arrays" do + scope.function_count([["1","2","3"]]).should(eq(3)) + end + + it "should be able to count matching elements in arrays" do + scope.function_count([["1", "2", "2"], "2"]).should(eq(2)) + end + + it "should not count :undef, nil or empty strings" do + scope.function_count([["foo","bar",:undef,nil,""]]).should(eq(2)) + end +end -- cgit v1.2.3 From 435226abfdf001bf2475be56d77027c638e93b70 Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Wed, 10 Apr 2013 14:33:08 -0700 Subject: (maint) Add the behavior for count() with arrays and hashes Without this patch the expected behavior of the count() function when dealing with an out of bound array index and with a hash key that does not exist is implicitly encoded in the spec examples. This is a problem because the expected behavior is not clear for something similar to the following example: node default { $ary = [ 1, 2, 3 ] $ary_undef = $ary[100] $hsh = { 'one' => 1 } $hsh_undef = $hsh['dne'] $count = count(['hi', $ary_undef, $hsh_undef]) notice "Count is ${count}" } This patch addresses the problem by making the expected behavior explicit in the examples. --- spec/unit/puppet/parser/functions/count_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/count_spec.rb b/spec/unit/puppet/parser/functions/count_spec.rb index 5e4a8b9..2453815 100644 --- a/spec/unit/puppet/parser/functions/count_spec.rb +++ b/spec/unit/puppet/parser/functions/count_spec.rb @@ -21,7 +21,11 @@ describe "the count function" do scope.function_count([["1", "2", "2"], "2"]).should(eq(2)) end - it "should not count :undef, nil or empty strings" do - scope.function_count([["foo","bar",:undef,nil,""]]).should(eq(2)) + it "should not count nil or empty strings" do + scope.function_count([["foo","bar",nil,""]]).should(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 end -- cgit v1.2.3 From 2ba9e4721b6d96f0b9e66bbb2f8b09c9c413efe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Thu, 2 May 2013 15:10:59 +0200 Subject: Add a dirname function --- spec/unit/puppet/parser/functions/dirname_spec.rb | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 spec/unit/puppet/parser/functions/dirname_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/dirname_spec.rb b/spec/unit/puppet/parser/functions/dirname_spec.rb new file mode 100755 index 0000000..fb3b4fe --- /dev/null +++ b/spec/unit/puppet/parser/functions/dirname_spec.rb @@ -0,0 +1,24 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the dirname function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("dirname").should == "function_dirname" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { scope.function_dirname([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return dirname for an absolute path" do + result = scope.function_dirname(['/path/to/a/file.ext']) + result.should(eq('/path/to/a')) + end + + it "should return dirname for a relative path" do + result = scope.function_dirname(['path/to/a/file.ext']) + result.should(eq('path/to/a')) + end +end -- cgit v1.2.3 From b975bd66aa7345a5eeb54eb79dd0f708934609f8 Mon Sep 17 00:00:00 2001 From: Martin Hellmich Date: Wed, 8 May 2013 18:20:56 +0200 Subject: Added rspec tests for the new behaviour of merge accepting empty strings added test that '' is accepted changed a test that a number is correctly rejected with a type error --- spec/unit/puppet/parser/functions/merge_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/merge_spec.rb b/spec/unit/puppet/parser/functions/merge_spec.rb index 04169e7..8a170bb 100644 --- a/spec/unit/puppet/parser/functions/merge_spec.rb +++ b/spec/unit/puppet/parser/functions/merge_spec.rb @@ -26,6 +26,11 @@ describe Puppet::Parser::Functions.function(:merge) do 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(Puppet::ParseError, /unexpected argument type String/) end it 'should be able to merge two hashes' do -- cgit v1.2.3 From 737aa31546e71e9febea2199582510ef88a2560c Mon Sep 17 00:00:00 2001 From: Alex Cline Date: Mon, 13 May 2013 12:09:33 -0400 Subject: (#20684) Add array comparison functions, difference, intersection and union. Included is code, tests and documentation for the difference, intersection and union functions for comparing arrays. --- spec/unit/puppet/parser/functions/difference_spec.rb | 19 +++++++++++++++++++ .../unit/puppet/parser/functions/intersection_spec.rb | 19 +++++++++++++++++++ spec/unit/puppet/parser/functions/union_spec.rb | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 spec/unit/puppet/parser/functions/difference_spec.rb create mode 100644 spec/unit/puppet/parser/functions/intersection_spec.rb create mode 100644 spec/unit/puppet/parser/functions/union_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/difference_spec.rb b/spec/unit/puppet/parser/functions/difference_spec.rb new file mode 100644 index 0000000..9feff09 --- /dev/null +++ b/spec/unit/puppet/parser/functions/difference_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the difference function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("difference").should == "function_difference" + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + lambda { scope.function_difference([]) }.should( raise_error(Puppet::ParseError) ) + end + + it "should return the difference between two arrays" do + result = scope.function_difference([["a","b","c"],["b","c","d"]]) + result.should(eq(["a"])) + end +end diff --git a/spec/unit/puppet/parser/functions/intersection_spec.rb b/spec/unit/puppet/parser/functions/intersection_spec.rb new file mode 100644 index 0000000..fd44f7f --- /dev/null +++ b/spec/unit/puppet/parser/functions/intersection_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the intersection function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("intersection").should == "function_intersection" + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + lambda { scope.function_intersection([]) }.should( raise_error(Puppet::ParseError) ) + end + + it "should return the intersection of two arrays" do + result = scope.function_intersection([["a","b","c"],["b","c","d"]]) + result.should(eq(["b","c"])) + end +end diff --git a/spec/unit/puppet/parser/functions/union_spec.rb b/spec/unit/puppet/parser/functions/union_spec.rb new file mode 100644 index 0000000..0d282ca --- /dev/null +++ b/spec/unit/puppet/parser/functions/union_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the union function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("union").should == "function_union" + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + lambda { scope.function_union([]) }.should( raise_error(Puppet::ParseError) ) + end + + it "should join two arrays together" do + result = scope.function_union([["a","b","c"],["b","c","d"]]) + result.should(eq(["a","b","c","d"])) + end +end -- cgit v1.2.3 From 3707c47dcdf154b2396d9240fc90f76450890b71 Mon Sep 17 00:00:00 2001 From: fiddyspence Date: Fri, 24 May 2013 16:33:24 +0100 Subject: Adding base64 function Adding base64 function and spec test. Included a bonus fix to validate_slength_spec.rb to put the expectation message in the right place. --- spec/unit/puppet/parser/functions/base64_spec.rb | 34 ++++++++++++++++++++++ .../parser/functions/validate_slength_spec.rb | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100755 spec/unit/puppet/parser/functions/base64_spec.rb (limited to 'spec/unit/puppet/parser') diff --git a/spec/unit/puppet/parser/functions/base64_spec.rb b/spec/unit/puppet/parser/functions/base64_spec.rb new file mode 100755 index 0000000..5faa5e6 --- /dev/null +++ b/spec/unit/puppet/parser/functions/base64_spec.rb @@ -0,0 +1,34 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the base64 function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("base64").should == "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']) + result.should =~ /\AdGhlc3RyaW5n\n\Z/ + end + it "should decode a base64 encoded string" do + result = scope.function_base64(["decode",'dGhlc3RyaW5n']) + result.should == 'thestring' + end +end diff --git a/spec/unit/puppet/parser/functions/validate_slength_spec.rb b/spec/unit/puppet/parser/functions/validate_slength_spec.rb index eccf908..b363e7a 100755 --- a/spec/unit/puppet/parser/functions/validate_slength_spec.rb +++ b/spec/unit/puppet/parser/functions/validate_slength_spec.rb @@ -35,7 +35,7 @@ describe "the validate_slength function" do end it "should fail if you pass something other than a string or array" do - expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError), /please pass a string, or an array of strings/) + expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError, /please pass a string, or an array of strings/)) end it "should not fail if string is smaller or equal to size" do -- cgit v1.2.3