]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/spec/unit/puppet/parser/functions/has_key_spec.rb
d1dcd15865c8e433ecf364ea7714e69ce4926cf7
[dsa-puppet.git] / modules / stdlib / spec / unit / puppet / parser / functions / has_key_spec.rb
1 require 'puppet'
2 require 'mocha'
3 describe Puppet::Parser::Functions.function(:has_key) do
4
5   # Pulled from Dan's create_resources function
6   # TODO - this should be moved to spec_helper since the
7   # logic is likely to be applied to multiple rspec files.
8   let(:compiler) {
9     topscope = Puppet::Parser::Scope.new
10     # This is necessary so we don't try to use the compiler to discover our parent.
11     topscope.parent = nil
12     my_scope = Puppet::Parser::Scope.new
13     my_scope.compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("floppy", :environment => 'production'))
14     my_scope.parent = topscope
15     compiler = my_scope.compiler
16   }
17   let(:scope) {
18     scope = Puppet::Parser::Scope.new
19     scope.stubs(:environment).returns(Puppet::Node::Environment.new('production'))
20     scope
21   }
22
23   describe 'when calling has_key from puppet' do
24     it "should not compile when no arguments are passed" do
25       Puppet[:code] = 'has_key()'
26       expect { compiler.compile }.should raise_error(Puppet::ParseError, /wrong number of arguments/)
27     end
28     it "should not compile when 1 argument is passed" do
29       Puppet[:code] = "has_key('foo')"
30       expect { compiler.compile }.should raise_error(Puppet::ParseError, /wrong number of arguments/)
31     end
32     it "should require the first value to be a Hash" do
33       Puppet[:code] = "has_key('foo', 'bar')"
34       expect { compiler.compile }.should raise_error(Puppet::ParseError, /expects the first argument to be a hash/)
35     end
36   end
37   describe 'when calling the function has_key from a scope instance' do
38     it 'should detect existing keys' do
39       scope.function_has_key([{'one' => 1}, 'one']).should be_true
40     end
41     it 'should detect existing keys' do
42       scope.function_has_key([{'one' => 1}, 'two']).should be_false
43     end
44   end
45
46 end