#!/usr/bin/env ruby
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'spec'
require 'mocha'
require 'puppet'
describe Puppet::Parser::Interpreter do
it "should create a parser with code passed in at initialization time" do
interp = Puppet::Parser::Interpreter.new :Code => :some_code
parser = mock('parser1')
parser.expects(:code=).with(:some_code)
parser.expects(:parse)
Puppet::Parser::Parser.expects(:new).with(:first).returns(parser)
interp.send(:create_parser, :first).object_id.should equal(parser.object_id)
end
it "should create a parser with a file passed in at initialization time" do
interp = Puppet::Parser::Interpreter.new :Manifest => :a_file
parser = mock('parser2')
parser.expects(:file=).with(:a_file)
parser.expects(:parse)
Puppet::Parser::Parser.expects(:new).with(:second).returns(parser)
interp.send(:create_parser, :second).should equal(parser)
end
it "should create a parser when passed neither code nor file" do
interp = Puppet::Parser::Interpreter.new
parser = mock('parser3')
parser.expects(:parse)
Puppet::Parser::Parser.expects(:new).with(:third).returns(parser)
interp.send(:create_parser, :third).should equal(parser)
end
it "should return nothing when new parsers fail" do
interp = Puppet::Parser::Interpreter.new
parser = mock('parser4')
Puppet::Parser::Parser.expects(:new).with(:fourth).raises(ArgumentError)
assert_nil(interp.send(:create_parser, :fourth), "Returned something after a failed parser creation")
end
it "it should an exception when nothing is there and nil is returned" do
interp = Puppet::Parser::Interpreter.new
interp.expects(:create_parser).with(:first).returns(nil)
interp.should raise_error(Puppet::Error)
end
it "should create a new parser and return it when the parser does not need reparsing" do
parser = mock('parser1')
interp = Puppet::Parser::Interpreter.new
interp.expects(:create_parser).with(:first).returns(parser)
interp.send(:parser, :first).should equal(parser)
parser.expects(:reparse?).returns(false)
parser.expects(:clear)
interp.send(:parser, :first).should equal(parser)
end
it "should create a new parser when reparse is true" do
interp = Puppet::Parser::Interpreter.new
oldparser = mock('oldparser')
newparser = mock('newparser')
oldparser.expects(:reparse?).returns(true)
oldparser.expects(:reparse?).clear
interp.expects(:create_parser).with(:first).returns(oldparser)
interp.send(:parser, :first).should equal(oldparser)
interp.expects(:create_parser).with(:first).returns(newparser)
interp.send(:parser, :first).should equal(newparser)
end
it "should keep the old parser if create_parser doesn't return anything." do
interp = Puppet::Parser::Interpreter.new
oldparser = mock('oldparser')
newparser = mock('newparser')
oldparser.expects(:reparse?).returns(true)
oldparser.expects(:reparse?).clear
interp.expects(:create_parser).with(:first).returns(oldparser)
interp.send(:parser, :first).should equal(oldparser)
newparser.expects(:reparse?).returns(true)
interp.expects(:create_parser).with(:first).returns(nil)
interp.send(:parser, :first).should equal(newparser)
end
it "should create a new parser when we ask for a different env" do
newparser = mock('parser3')
interp.expects(:create_parser).with(:second).returns(newparser)
interp.send(:parser, :second).should equal(newparser)
end
end