From 36a5517e7567a15322b0ad2c48005573b60dfe8b Mon Sep 17 00:00:00 2001 From: bas smit Date: Sat, 3 Mar 2018 21:33:53 +0100 Subject: [PATCH] Add test case for desired behaviour --- .../plugins/check_parameter_types_spec.rb | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 spec/puppet-lint/plugins/check_parameter_types_spec.rb diff --git a/spec/puppet-lint/plugins/check_parameter_types_spec.rb b/spec/puppet-lint/plugins/check_parameter_types_spec.rb new file mode 100644 index 0000000..be0b286 --- /dev/null +++ b/spec/puppet-lint/plugins/check_parameter_types_spec.rb @@ -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