diff options
author | TP Honey <tphoney@users.noreply.github.com> | 2015-05-05 14:35:48 +0100 |
---|---|---|
committer | TP Honey <tphoney@users.noreply.github.com> | 2015-05-05 14:35:48 +0100 |
commit | 7181e4ebcaf59cb16e7166aa254cbb637590423a (patch) | |
tree | 3b45b6b6e4ef0a249096ed827d2599e80100ec57 /spec/functions | |
parent | c7a23b226d5293e24cc52229c6162425ad473b6f (diff) | |
parent | d4f3d57f1678ae03a58a17181f863c44c248f09b (diff) | |
download | puppet-stdlib-7181e4ebcaf59cb16e7166aa254cbb637590423a.tar.gz puppet-stdlib-7181e4ebcaf59cb16e7166aa254cbb637590423a.tar.bz2 |
Merge pull request #443 from DavidS/prep-work-for-new-specs
Prep work for new specs
Diffstat (limited to 'spec/functions')
-rwxr-xr-x | spec/functions/basename_spec.rb | 46 | ||||
-rwxr-xr-x | spec/functions/bool2str_spec.rb | 46 | ||||
-rwxr-xr-x | spec/functions/camelcase_spec.rb | 24 | ||||
-rw-r--r-- | spec/functions/type_of_spec.rb | 33 | ||||
-rwxr-xr-x | spec/functions/validate_integer_spec.rb | 6 | ||||
-rwxr-xr-x | spec/functions/validate_ipv4_address_spec.rb | 4 | ||||
-rwxr-xr-x | spec/functions/validate_ipv6_address_spec.rb | 4 | ||||
-rwxr-xr-x | spec/functions/validate_numeric_spec.rb | 6 |
8 files changed, 157 insertions, 12 deletions
diff --git a/spec/functions/basename_spec.rb b/spec/functions/basename_spec.rb new file mode 100755 index 0000000..8a2d0dc --- /dev/null +++ b/spec/functions/basename_spec.rb @@ -0,0 +1,46 @@ +#! /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 +end diff --git a/spec/functions/bool2str_spec.rb b/spec/functions/bool2str_spec.rb new file mode 100755 index 0000000..b878891 --- /dev/null +++ b/spec/functions/bool2str_spec.rb @@ -0,0 +1,46 @@ +#! /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)) + end +end diff --git a/spec/functions/camelcase_spec.rb b/spec/functions/camelcase_spec.rb new file mode 100755 index 0000000..70382ad --- /dev/null +++ b/spec/functions/camelcase_spec.rb @@ -0,0 +1,24 @@ +#! /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 +end diff --git a/spec/functions/type_of_spec.rb b/spec/functions/type_of_spec.rb new file mode 100644 index 0000000..8afb624 --- /dev/null +++ b/spec/functions/type_of_spec.rb @@ -0,0 +1,33 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +if ENV["FUTURE_PARSER"] == 'yes' or Puppet.version >= "4" + require 'puppet/pops' + require 'puppet/loaders' + + describe 'the type_of function' do + before(:all) do + loaders = Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, [File.join(fixtures, "modules")])) + Puppet.push_context({:loaders => loaders}, "test-examples") + end + + after(:all) do + Puppet::Pops::Loaders.clear + Puppet::pop_context() + end + + let(:func) do + # Load the function from the environment modulepath's modules (ie, fixtures) + Puppet.lookup(:loaders).private_environment_loader.load(:function, 'type_of') + end + + it 'gives the type of a string' do + expect(func.call({}, 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType) + end + + it 'gives the type of an integer' do + expect(func.call({}, 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType) + end + end +end diff --git a/spec/functions/validate_integer_spec.rb b/spec/functions/validate_integer_spec.rb index dff3415..3865c4f 100755 --- a/spec/functions/validate_integer_spec.rb +++ b/spec/functions/validate_integer_spec.rb @@ -43,7 +43,7 @@ describe Puppet::Parser::Functions.function(:validate_integer) do 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(Puppet::ParseError, /to be an Integer/) + expect { scope.compiler.compile }.to raise_error end end @@ -117,7 +117,7 @@ describe Puppet::Parser::Functions.function(:validate_integer) do 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(Puppet::ParseError, /to be unset or an Integer/) + expect { scope.compiler.compile }.to raise_error end end @@ -212,7 +212,7 @@ describe Puppet::Parser::Functions.function(:validate_integer) do 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(Puppet::ParseError, /to be unset or an Integer/) + expect { scope.compiler.compile }.to raise_error end end end diff --git a/spec/functions/validate_ipv4_address_spec.rb b/spec/functions/validate_ipv4_address_spec.rb index 45401a4..27ea4fe 100755 --- a/spec/functions/validate_ipv4_address_spec.rb +++ b/spec/functions/validate_ipv4_address_spec.rb @@ -39,9 +39,7 @@ describe Puppet::Parser::Functions.function(:validate_ipv4_address) do describe "when given numbers" do it "should not compile" do Puppet[:code] = "validate_ipv4_address(1, 2)" - expect { - scope.compiler.compile - }.to raise_error(Puppet::ParseError, /is not a valid IPv4 address/) + expect { scope.compiler.compile }.to raise_error end end diff --git a/spec/functions/validate_ipv6_address_spec.rb b/spec/functions/validate_ipv6_address_spec.rb index a839d90..e87b372 100755 --- a/spec/functions/validate_ipv6_address_spec.rb +++ b/spec/functions/validate_ipv6_address_spec.rb @@ -41,9 +41,7 @@ describe Puppet::Parser::Functions.function(:validate_ipv6_address) do describe "when given numbers" do it "should not compile" do Puppet[:code] = "validate_ipv6_address(1, 2)" - expect { - scope.compiler.compile - }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) + expect { scope.compiler.compile }.to raise_error end end end diff --git a/spec/functions/validate_numeric_spec.rb b/spec/functions/validate_numeric_spec.rb index c8b0e4d..1623a3d 100755 --- a/spec/functions/validate_numeric_spec.rb +++ b/spec/functions/validate_numeric_spec.rb @@ -43,7 +43,7 @@ describe Puppet::Parser::Functions.function(:validate_numeric) do 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(Puppet::ParseError, /to be a Numeric/) + expect { scope.compiler.compile }.to raise_error end end @@ -115,7 +115,7 @@ describe Puppet::Parser::Functions.function(:validate_numeric) do 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(Puppet::ParseError, /to be unset or a Numeric/) + expect { scope.compiler.compile }.to raise_error end end @@ -210,7 +210,7 @@ describe Puppet::Parser::Functions.function(:validate_numeric) do 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(Puppet::ParseError, /to be unset or a Numeric/) + expect { scope.compiler.compile }.to raise_error end end end |