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