Skip to content

Commit

Permalink
Add test case for desired behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
bas smit committed Mar 4, 2018
1 parent 5976962 commit 36a5517
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions spec/puppet-lint/plugins/check_parameter_types_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require 'spec_helper'

describe 'parameter_types' do
let(:msg) { 'missing datatype for parameter spec::%s' }

[ 'class', 'define' ].each do | rt |
context "#{rt} without parameters" do
let(:code) { 'class spec() {}' }
it 'should not be a problem' do
expect(problems).to have(0).problem
end
end
context "simple #{rt} without type" do
let(:code) { 'class spec($foo) { }' }
it 'should be a problem' do
expect(problems).to have(1).problem
expect(problems).to contain_warning(msg % :foo).on_line(1)
end
end

context "#{rt} with many params without type" do
let(:code) do
<<-EOL
class spec (
$attr1,
$attr2,
$attr3
) { }
EOL
end
it 'should be a problem' do
expect(problems).to have(3).problem
end
end
context "#{rt} with many params without type on one line" do
let(:code) { 'class spec ($attr1, $attr2, $attr3 ) { }' }
it 'should be a problem' do
expect(problems).to have(3).problem
end
end
context "#{rt} with many params and defaults without type" do
let(:code) do
<<-EOL
class spec (
$attr0,
$attr1 = 1,
$attr2 = $attr1,
$attr3 = [ 'array', 'with', 'entries'],
$attr4 = { 'key' => 'value' }
$attr5 = {
'key' => 'value',
'key2' => [
'val1',
'val2',
],
},
) { }
EOL
end
it 'should be a problem' do
expect(problems).to have(5).problem
end
end

context "#{rt} with some attributes typed" do
let(:code) do
<<-EOL
class spec (
Integer $attr1,
String $attr2 = 'foo',
$attr3 = undef,
$attr4 = { 'key' => 'value' }
Array[Struct[{
name => String[1],
source_url => String[1],
delete => Optional[Boolean],
exclude => Optional[Variant[String,Array[String]]],
include => Optional[Variant[String,Array[String]]],
sync_hour => Optional[String],
}]] $repos = [],
Hash $attr5 = {
'key' => 'value',
'key2' => [
'val1',
'val2',
],
},
) { }
EOL
end
it 'should be a problem' do
expect(problems).to have(2).problem
end
end
context "#{rt} with some attributes typed on one line" do
let(:code) { 'class spec(Integer $attr1, $attr2 = 5, Variant[String,Integer] $attr 3, $attr4 = [1,2]) { }' }
it 'should be a problem' do
expect(problems).to have(2).problem
end
end

context "#{rt} with all attributes typed" do
let(:code) do
<<-EOL
class spec (
Integer $attr1,
String $attr2 = 'foo',
Optional[String] $attr3 = undef,
Optional[Variant[Integer,Array[String] $attr4 = undef,
Stdlib::MyType $attr5 = undef,
) { }
EOL
end
it 'should not be a problem' do
expect(problems).to have(0).problem
end
end
context "#{rt} with all attributes typed complex" do
let(:code) do
<<-EOL
class spec (
Integer $attr1,
String $attr2 = 'foo',
Optional[String] $attr3 = undef,
Optional[Variant[Integer,Array[String] $attr4,
Array[Struct[{
name => String[1],
source_url => String[1],
delete => Optional[Boolean],
exclude => Optional[Variant[String,Array[String]]],
include => Optional[Variant[String,Array[String]]],
sync_hour => Optional[String],
}]] $repos = [],
) { }
EOL
end
it 'should not be a problem' do
expect(problems).to have(0).problem
end
end
end
end

0 comments on commit 36a5517

Please sign in to comment.