1 #! /usr/bin/env ruby -S rspec
4 describe Puppet::Parser::Functions.function(:validate_re) do
5 let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
7 # The subject of these examplres is the method itself.
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)
14 context 'Using Puppet::Parser::Scope.new' do
16 describe 'Garbage inputs' do
20 [ { 'foo' => 'bar' } ],
23 [ "one", "one", "MSG to User", "4th arg" ],
26 inputs.each do |input|
27 it "validate_re(#{input.inspect}) should fail" do
28 expect { subject.call [input] }.to raise_error Puppet::ParseError
33 describe 'Valid inputs' do
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' ],
41 inputs.each do |input|
42 it "validate_re(#{input.inspect}) should not fail" do
43 expect { subject.call input }.not_to raise_error
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
52 [ "hello", [ "bye", "later", "adios" ] ],
53 [ "greetings", "salutations" ],
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/
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
66 [ "hello", [ "bye", "later", "adios" ], "MSG to User" ],
67 [ "greetings", "salutations", "Error, greetings does not match salutations" ],
70 inputs.each do |input|
71 it "validate_re(#{input.inspect}) should fail" do
72 expect { subject.call input }.to raise_error /#{input[2]}/