|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Puppet::Parser::Functions.function(:validate_re) do |
| 4 | + before :all do |
| 5 | + Puppet::Parser::Functions.autoloader.loadall |
| 6 | + end |
| 7 | + |
| 8 | + let(:scope) do |
| 9 | + scope = Puppet::Parser::Scope.new |
| 10 | + end |
| 11 | + |
| 12 | + # The subject of these examplres is the method itself. |
| 13 | + subject do |
| 14 | + scope.method :function_validate_re |
| 15 | + end |
| 16 | + |
| 17 | + context 'Using Puppet::Parser::Scope.new' do |
| 18 | + |
| 19 | + describe 'Garbage inputs' do |
| 20 | + inputs = [ |
| 21 | + [ nil ], |
| 22 | + [ [ nil ] ], |
| 23 | + [ { 'foo' => 'bar' } ], |
| 24 | + [ { } ], |
| 25 | + [ '' ], |
| 26 | + [ "one", "one", "MSG to User", "4th arg" ], |
| 27 | + ] |
| 28 | + |
| 29 | + inputs.each do |input| |
| 30 | + it "validate_re(#{input.inspect}) should fail" do |
| 31 | + expect { subject.call [input] }.to raise_error Puppet::ParseError |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + describe 'Valid inputs' do |
| 37 | + inputs = [ |
| 38 | + [ '/full/path/to/something', '^/full' ], |
| 39 | + [ '/full/path/to/something', 'full' ], |
| 40 | + [ '/full/path/to/something', ['full', 'absent'] ], |
| 41 | + [ '/full/path/to/something', ['full', 'absent'], 'Message to the user' ], |
| 42 | + ] |
| 43 | + |
| 44 | + inputs.each do |input| |
| 45 | + it "validate_re(#{input.inspect}) should not fail" do |
| 46 | + expect { subject.call input }.not_to raise_error |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + describe "Valid inputs which should raise an exception without a message" do |
| 51 | + # The intent here is to make sure valid inputs raise exceptions when they |
| 52 | + # don't specify an error message to display. This is the behvior in |
| 53 | + # 2.2.x and prior. |
| 54 | + inputs = [ |
| 55 | + [ "hello", [ "bye", "later", "adios" ] ], |
| 56 | + [ "greetings", "salutations" ], |
| 57 | + ] |
| 58 | + |
| 59 | + inputs.each do |input| |
| 60 | + it "validate_re(#{input.inspect}) should fail" do |
| 61 | + expect { subject.call input }.to raise_error /validate_re.*?does not match/ |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + describe "Nicer Error Messages" do |
| 66 | + # The intent here is to make sure the function returns the 3rd argument |
| 67 | + # in the exception thrown |
| 68 | + inputs = [ |
| 69 | + [ "hello", [ "bye", "later", "adios" ], "MSG to User" ], |
| 70 | + [ "greetings", "salutations", "Error, greetings does not match salutations" ], |
| 71 | + ] |
| 72 | + |
| 73 | + inputs.each do |input| |
| 74 | + it "validate_re(#{input.inspect}) should fail" do |
| 75 | + expect { subject.call input }.to raise_error /#{input[2]}/ |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments