]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/spec/unit/puppet/parser/functions/validate_string_spec.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / spec / unit / puppet / parser / functions / validate_string_spec.rb
1 require 'puppet'
2
3 # We don't need this for the basic tests we're doing
4 # require 'spec_helper'
5
6 # Dan mentioned that Nick recommended the function method call
7 # to return the string value for the test description.
8 # this will not even try the test if the function cannot be
9 # loaded.
10 describe Puppet::Parser::Functions.function(:validate_string) do
11
12   # Pulled from Dan's create_resources function
13   def get_scope
14     @topscope = Puppet::Parser::Scope.new
15     # This is necessary so we don't try to use the compiler to discover our parent.
16     @topscope.parent = nil
17     @scope = Puppet::Parser::Scope.new
18     @scope.compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("floppy", :environment => 'production'))
19     @scope.parent = @topscope
20     @compiler = @scope.compiler
21   end
22
23   describe 'when calling validate_string from puppet' do
24
25     %w{ foo bar baz }.each do |the_string|
26
27       it "should compile when #{the_string} is a string" do
28         Puppet[:code] = "validate_string('#{the_string}')"
29         get_scope
30         @scope.compiler.compile
31       end
32
33       it "should compile when #{the_string} is a bare word" do
34         Puppet[:code] = "validate_string(#{the_string})"
35         get_scope
36         @scope.compiler.compile
37       end
38
39     end
40
41     %w{ true false }.each do |the_string|
42       it "should compile when #{the_string} is a string" do
43         Puppet[:code] = "validate_string('#{the_string}')"
44         get_scope
45         @scope.compiler.compile
46       end
47
48       it "should not compile when #{the_string} is a bare word" do
49         Puppet[:code] = "validate_string(#{the_string})"
50         get_scope
51         expect { @scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a string/)
52       end
53     end
54
55     it "should compile when multiple string arguments are passed" do
56       Puppet[:code] = <<-'ENDofPUPPETcode'
57         $foo = ''
58         $bar = 'two'
59         validate_string($foo, $bar)
60       ENDofPUPPETcode
61       get_scope
62       @scope.compiler.compile
63     end
64
65     it "should compile when an explicitly undef variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do
66       Puppet[:code] = <<-'ENDofPUPPETcode'
67         $foo = undef
68         validate_string($foo)
69       ENDofPUPPETcode
70       get_scope
71       @scope.compiler.compile
72     end
73
74     it "should compile when an undefined variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do
75       Puppet[:code] = <<-'ENDofPUPPETcode'
76         validate_string($foobarbazishouldnotexist)
77       ENDofPUPPETcode
78       get_scope
79       @scope.compiler.compile
80     end
81   end
82 end
83