summaryrefslogtreecommitdiff
path: root/spec/functions/values_at_spec.rb
diff options
context:
space:
mode:
authorHunter Haugen <hunter@puppetlabs.com>2015-06-01 13:36:25 -0700
committerHunter Haugen <hunter@puppetlabs.com>2015-06-01 13:36:25 -0700
commita383705fdb133978e53503b7e01012367fac139d (patch)
treef044a3b9c65db499ae279a08328c18200201d094 /spec/functions/values_at_spec.rb
parent1ae9058518d042ea81d42f46cebbb040b35a2b4d (diff)
parent18d4c21418e8d188ae659a92ff3a8df04d711f6a (diff)
downloadpuppet-stdlib-a383705fdb133978e53503b7e01012367fac139d.tar.gz
puppet-stdlib-a383705fdb133978e53503b7e01012367fac139d.tar.bz2
Merge pull request #464 from DavidS/modules-1882-convert-to-rspec
(MODULES-1882) convert function tests to rspec-puppet
Diffstat (limited to 'spec/functions/values_at_spec.rb')
-rwxr-xr-xspec/functions/values_at_spec.rb71
1 files changed, 41 insertions, 30 deletions
diff --git a/spec/functions/values_at_spec.rb b/spec/functions/values_at_spec.rb
index 86e3c31..a8348f3 100755
--- a/spec/functions/values_at_spec.rb
+++ b/spec/functions/values_at_spec.rb
@@ -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