]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/functions/validate_slength_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / validate_slength_spec.rb
1 #! /usr/bin/env ruby -S rspec
2
3 require 'spec_helper'
4
5 describe "the validate_slength function" do
6   let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7
8   it "should exist" do
9     expect(Puppet::Parser::Functions.function("validate_slength")).to eq("function_validate_slength")
10   end
11
12   describe "validating the input argument types" do
13     it "raises an error if there are less than two arguments" do
14       expect { scope.function_validate_slength([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
15     end
16
17     it "raises an error if there are more than three arguments" do
18       expect { scope.function_validate_slength(['input', 1, 2, 3]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
19     end
20
21     it "raises an error if the first argument is not a string" do
22       expect { scope.function_validate_slength([Object.new, 2, 1]) }.to raise_error Puppet::ParseError, /Expected first argument.*got .*Object/
23     end
24
25     it "raises an error if the second argument cannot be cast to an Integer" do
26       expect { scope.function_validate_slength(['input', Object.new]) }.to raise_error Puppet::ParseError, /Expected second argument.*got .*Object/
27     end
28
29     it "raises an error if the third argument cannot be cast to an Integer" do
30       expect { scope.function_validate_slength(['input', 1, Object.new]) }.to raise_error Puppet::ParseError, /Expected third argument.*got .*Object/
31     end
32
33     it "raises an error if the second argument is smaller than the third argument" do
34       expect { scope.function_validate_slength(['input', 1, 2]) }.to raise_error Puppet::ParseError, /Expected second argument to be larger than third argument/
35     end
36   end
37
38   describe "validating the input string length" do
39     describe "when the input is a string" do
40       it "fails validation if the string is larger than the max length" do
41         expect { scope.function_validate_slength(['input', 1]) }.to raise_error Puppet::ParseError, /Expected length .* between 0 and 1, was 5/
42       end
43
44       it "fails validation if the string is less than the min length" do
45         expect { scope.function_validate_slength(['input', 10, 6]) }.to raise_error Puppet::ParseError, /Expected length .* between 6 and 10, was 5/
46       end
47
48       it "doesn't raise an error if the string is under the max length" do
49         scope.function_validate_slength(['input', 10])
50       end
51
52       it "doesn't raise an error if the string is equal to the max length" do
53         scope.function_validate_slength(['input', 5])
54       end
55
56       it "doesn't raise an error if the string is equal to the min length" do
57         scope.function_validate_slength(['input', 10, 5])
58       end
59     end
60
61     describe "when the input is an array" do
62       it "fails validation if one of the array elements is not a string" do
63         expect { scope.function_validate_slength([["a", "b", Object.new], 2]) }.to raise_error Puppet::ParseError, /Expected element at array position 2 .*String, got .*Object/
64       end
65     end
66   end
67 end