|
9 | 9 | Puppet::Parser::Functions.function("validate_slength").should == "function_validate_slength"
|
10 | 10 | end
|
11 | 11 |
|
12 |
| - it "should raise a ParseError if there is less than 2 arguments" do |
13 |
| - expect { scope.function_validate_slength([]) }.to(raise_error(Puppet::ParseError)) |
14 |
| - expect { scope.function_validate_slength(["asdf"]) }.to(raise_error(Puppet::ParseError)) |
15 |
| - end |
| 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 | 16 |
|
17 |
| - it "should raise a ParseError if argument 2 doesn't convert to a fixnum" do |
18 |
| - expect { scope.function_validate_slength(["moo",["2"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/)) |
19 |
| - end |
| 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 | 20 |
|
21 |
| - it "should raise a ParseError if argument 2 converted, but to 0, e.g. a string" do |
22 |
| - expect { scope.function_validate_slength(["moo","monkey"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/)) |
23 |
| - end |
| 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 | 24 |
|
25 |
| - it "should raise a ParseError if argument 2 converted, but to 0" do |
26 |
| - expect { scope.function_validate_slength(["moo","0"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/)) |
27 |
| - end |
| 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 | 28 |
|
29 |
| - it "should raise a ParseError if argument 3 doesn't convert to a fixnum" do |
30 |
| - expect { scope.function_validate_slength(["moo",2,["3"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/)) |
31 |
| - end |
| 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 | 32 |
|
33 |
| - it "should raise a ParseError if argument 3 converted, but to 0, e.g. a string" do |
34 |
| - expect { scope.function_validate_slength(["moo",2,"monkey"]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/)) |
| 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 |
35 | 36 | end
|
36 | 37 |
|
37 |
| - it "should fail if string greater then size" do |
38 |
| - expect { scope.function_validate_slength(["test", 2]) }.to(raise_error(Puppet::ParseError, /It should have been between 0 and 2/)) |
39 |
| - end |
| 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 |
40 | 43 |
|
41 |
| - it "should fail if the min length is larger than the max length" do |
42 |
| - expect { scope.function_validate_slength(["test", 10, 15]) }.to(raise_error(Puppet::ParseError, /pass a min length that is smaller than the max/)) |
43 |
| - end |
| 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 |
44 | 47 |
|
45 |
| - it "should fail if you pass an array of something other than strings" do |
46 |
| - expect { scope.function_validate_slength([["moo",["moo"],Hash.new["moo" => 7]], 7]) }.to(raise_error(Puppet::ParseError, /is not a string, it's a/)) |
47 |
| - end |
| 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 |
48 | 51 |
|
49 |
| - it "should fail if you pass something other than a string or array" do |
50 |
| - expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError, /please pass a string, or an array of strings/)) |
51 |
| - end |
| 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 |
52 | 55 |
|
53 |
| - it "should not fail if string is smaller or equal to size" do |
54 |
| - expect { scope.function_validate_slength(["test", 5]) }.to_not(raise_error(Puppet::ParseError)) |
55 |
| - end |
| 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 |
56 | 60 |
|
57 |
| - it "should not fail if array of string is are all smaller or equal to size" do |
58 |
| - expect { scope.function_validate_slength([["moo","foo","bar"], 5]) }.to_not(raise_error(Puppet::ParseError)) |
| 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 |
59 | 66 | end
|
60 | 67 | end
|
0 commit comments