3 describe Puppet::Parser::Functions.function(:has_key) do
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.
9 topscope = Puppet::Parser::Scope.new
10 # This is necessary so we don't try to use the compiler to discover our parent.
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
18 scope = Puppet::Parser::Scope.new
19 scope.stubs(:environment).returns(Puppet::Node::Environment.new('production'))
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/)
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/)
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/)
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
41 it 'should detect existing keys' do
42 scope.function_has_key([{'one' => 1}, 'two']).should be_false