(PDOC-88) Add additional spec tests to increase code coverage

This commit is contained in:
Will Hopper 2016-09-29 14:42:27 -07:00
parent f72b7995fe
commit ef6db6df41
2 changed files with 145 additions and 29 deletions

View File

@ -27,6 +27,34 @@ SOURCE
end end
end end
describe 'parsing a provider with an invalid docstring assignment' do
let(:source) { <<-SOURCE
Puppet::Type.type(:custom).provide :linux do
@doc = 123
end
SOURCE
}
it 'should log an error' do
expect { subject }.to output(/Failed to parse docstring/).to_stdout_from_any_process
end
end
describe 'parsing a provider with a valid docstring assignment' do
let(:source) { <<-SOURCE
Puppet::Type.type(:custom).provide :linux do
@doc = 'An example provider on Linux.'
end
SOURCE
}
it 'should correctly detect the docstring' do
expect(subject.size).to eq(1)
object = subject.first
expect(object.docstring).to eq('An example provider on Linux.')
end
end
describe 'parsing a provider definition' do describe 'parsing a provider definition' do
let(:source) { <<-SOURCE let(:source) { <<-SOURCE
Puppet::Type.type(:custom).provide :linux do Puppet::Type.type(:custom).provide :linux do

View File

@ -27,8 +27,38 @@ SOURCE
end end
end end
describe 'parsing a type with an invalid docstring assignment' do
let(:source) { <<-SOURCE
Puppet::Type.newtype(:database) do
@doc = 123
end
SOURCE
}
it 'should log an error' do
expect { subject }.to output(/Failed to parse docstring/).to_stdout_from_any_process
end
end
describe 'parsing a type with a valid docstring assignment' do
let(:source) { <<-SOURCE
Puppet::Type.newtype(:database) do
@doc = 'An example database server resource type.'
end
SOURCE
}
it 'should correctly detect the docstring' do
expect(subject.size).to eq(1)
object = subject.first
expect(object.docstring).to eq('An example database server resource type.')
end
end
describe 'parsing a type definition' do describe 'parsing a type definition' do
let(:source) { <<-SOURCE let(:source) { <<-SOURCE
# @!puppet.type.param [value1, value2] dynamic_param Documentation for a dynamic parameter.
# @!puppet.type.property [foo, bar] dynamic_prop Documentation for a dynamic property.
Puppet::Type.newtype(:database) do Puppet::Type.newtype(:database) do
desc 'An example database server resource type.' desc 'An example database server resource type.'
feature :encryption, 'The provider supports encryption.', methods: [:encrypt] feature :encryption, 'The provider supports encryption.', methods: [:encrypt]
@ -54,6 +84,12 @@ Puppet::Type.newtype(:database) do
defaultto false defaultto false
end end
newparam(:backup) do
desc 'How often to backup the database.'
defaultto :never
newvalues(:daily, :monthly, :never)
end
newproperty(:file) do newproperty(:file) do
desc 'The database file to use.' desc 'The database file to use.'
end end
@ -80,47 +116,99 @@ SOURCE
tags = object.docstring.tags(:api) tags = object.docstring.tags(:api)
expect(tags.size).to eq(1) expect(tags.size).to eq(1)
expect(tags[0].text).to eq('public') expect(tags[0].text).to eq('public')
expect(object.properties.size).to eq(3) expect(object.properties.size).to eq(4)
expect(object.properties[0].name).to eq('ensure') expect(object.properties[0].name).to eq('dynamic_prop')
expect(object.properties[0].docstring).to eq('What state the database should be in.') expect(object.properties[0].docstring).to eq('Documentation for a dynamic property.')
expect(object.properties[0].isnamevar).to eq(false) expect(object.properties[0].isnamevar).to eq(false)
expect(object.properties[0].default).to eq('up') expect(object.properties[0].values).to eq(%w(foo bar))
expect(object.properties[0].values).to eq(%w(present absent up down)) expect(object.properties[1].name).to eq('ensure')
expect(object.properties[0].aliases).to eq({ 'down' => 'absent', 'up' => 'present' }) expect(object.properties[1].docstring).to eq('What state the database should be in.')
expect(object.properties[1].name).to eq('file')
expect(object.properties[1].docstring).to eq('The database file to use.')
expect(object.properties[1].isnamevar).to eq(false) expect(object.properties[1].isnamevar).to eq(false)
expect(object.properties[1].default).to be_nil expect(object.properties[1].default).to eq('up')
expect(object.properties[1].values).to eq([]) expect(object.properties[1].values).to eq(%w(present absent up down))
expect(object.properties[1].aliases).to eq({}) expect(object.properties[1].aliases).to eq({ 'down' => 'absent', 'up' => 'present' })
expect(object.properties[2].name).to eq('log_level') expect(object.properties[2].name).to eq('file')
expect(object.properties[2].docstring).to eq('The log level to use.') expect(object.properties[2].docstring).to eq('The database file to use.')
expect(object.properties[2].isnamevar).to eq(false) expect(object.properties[2].isnamevar).to eq(false)
expect(object.properties[2].default).to eq('warn') expect(object.properties[2].default).to be_nil
expect(object.properties[2].values).to eq(%w(debug warn error)) expect(object.properties[2].values).to eq([])
expect(object.properties[2].aliases).to eq({}) expect(object.properties[2].aliases).to eq({})
expect(object.parameters.size).to eq(3) expect(object.properties[3].name).to eq('log_level')
expect(object.parameters[0].name).to eq('address') expect(object.properties[3].docstring).to eq('The log level to use.')
expect(object.parameters[0].docstring).to eq('The database server name.') expect(object.properties[3].isnamevar).to eq(false)
expect(object.parameters[0].isnamevar).to eq(true) expect(object.properties[3].default).to eq('warn')
expect(object.parameters[0].default).to be_nil expect(object.properties[3].values).to eq(%w(debug warn error))
expect(object.parameters[0].values).to eq([]) expect(object.properties[3].aliases).to eq({})
expect(object.parameters[0].aliases).to eq({}) expect(object.parameters.size).to eq(5)
expect(object.parameters[1].name).to eq('encryption_key') expect(object.parameters[0].name).to eq('dynamic_param')
expect(object.parameters[1].docstring).to eq('The encryption key to use.') expect(object.parameters[0].docstring).to eq('Documentation for a dynamic parameter.')
expect(object.parameters[1].isnamevar).to eq(false) expect(object.parameters[0].isnamevar).to eq(false)
expect(object.parameters[0].values).to eq(%w(value1 value2))
expect(object.parameters[1].name).to eq('address')
expect(object.parameters[1].docstring).to eq('The database server name.')
expect(object.parameters[1].isnamevar).to eq(true)
expect(object.parameters[1].default).to be_nil expect(object.parameters[1].default).to be_nil
expect(object.parameters[1].values).to eq([]) expect(object.parameters[1].values).to eq([])
expect(object.parameters[1].aliases).to eq({}) expect(object.parameters[1].aliases).to eq({})
expect(object.parameters[2].name).to eq('encrypt') expect(object.parameters[2].name).to eq('encryption_key')
expect(object.parameters[2].docstring).to eq('Whether or not to encrypt the database.') expect(object.parameters[2].docstring).to eq('The encryption key to use.')
expect(object.parameters[2].isnamevar).to eq(false) expect(object.parameters[2].isnamevar).to eq(false)
expect(object.parameters[2].default).to eq('false') expect(object.parameters[2].default).to be_nil
expect(object.parameters[2].values).to eq(%w(true false yes no)) expect(object.parameters[2].values).to eq([])
expect(object.parameters[2].aliases).to eq({}) expect(object.parameters[2].aliases).to eq({})
expect(object.parameters[3].name).to eq('encrypt')
expect(object.parameters[3].docstring).to eq('Whether or not to encrypt the database.')
expect(object.parameters[3].isnamevar).to eq(false)
expect(object.parameters[3].default).to eq('false')
expect(object.parameters[3].values).to eq(%w(true false yes no))
expect(object.parameters[3].aliases).to eq({})
expect(object.parameters[4].name).to eq('backup')
expect(object.parameters[4].docstring).to eq('How often to backup the database.')
expect(object.parameters[4].isnamevar).to eq(false)
expect(object.parameters[4].default).to eq('never')
expect(object.parameters[4].values).to eq(%w(daily monthly never))
expect(object.features.size).to eq(1) expect(object.features.size).to eq(1)
expect(object.features[0].name).to eq('encryption') expect(object.features[0].name).to eq('encryption')
expect(object.features[0].docstring).to eq('The provider supports encryption.') expect(object.features[0].docstring).to eq('The provider supports encryption.')
end end
end end
describe 'parsing an ensurable type with default ensure values' do
let(:source) { <<-SOURCE
Puppet::Type.newtype(:database) do
desc 'An example database server resource type.'
ensurable
end
SOURCE
}
it 'should register a type object with default ensure values' do
expect(subject.size).to eq(1)
object = subject.first
expect(object.properties[0].name).to eq('ensure')
expect(object.properties[0].docstring).to eq('The basic property that the resource should be in.')
expect(object.properties[0].default).to eq('present')
expect(object.properties[0].values).to eq(%w(present absent))
end
end
describe 'parsing a type with a parameter with the name of "name"' do
let(:source) { <<-SOURCE
Puppet::Type.newtype(:database) do
desc 'An example database server resource type.'
newparam(:name) do
desc 'The database server name.'
end
end
SOURCE
}
it 'should register a type object with the "name" parameter as the namevar' do
expect(subject.size).to eq(1)
object = subject.first
expect(object.parameters.size).to eq(1)
expect(object.parameters[0].name).to eq('name')
expect(object.parameters[0].isnamevar).to eq(true)
end
end
end end