diff --git a/spec/lib/strings_spec/parsing.rb b/spec/lib/strings_spec/parsing.rb new file mode 100644 index 0000000..2570e28 --- /dev/null +++ b/spec/lib/strings_spec/parsing.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +module StringsSpec + module Parsing + + def parse(string, parser = :ruby) + Registry.clear + YARD::Parser::SourceParser.parse_string(string, parser) + end + + RSpec::Matchers.define :document_a do |arguments| + match do |actual| + compare_values(actual).empty? + end + + failure_message do |actual| + mismatches = compare_values(actual) + mismatches.collect do |key, value| + "Expected #{key} to be <#{value[1]}>, but got <#{value[0]}>." + end.join("\n") + end + + def compare_values(actual) + mismatched_arguments = {} + expected.each do |key, value| + actual_value = actual.send(key) + if actual_value != value + mismatched_arguments[key] = [actual_value, value] + end + end + mismatched_arguments + end + end + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 311484f..7e24a2f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,10 +13,3 @@ RSpec.configure do |config| config.mock_with :mocha end -# Borrowed from YARD spec helper -def parse_file(file, thisfile = __FILE__, log_level = log.level, ext = '.pp') - Registry.clear - path = File.join(File.dirname(thisfile), 'examples', file.to_s + ext) - YARD::Parser::SourceParser.parse(path, [], log_level) -end - diff --git a/spec/unit/puppetx/yardoc/yard/defined_type_handler_spec.rb b/spec/unit/puppetx/yardoc/yard/defined_type_handler_spec.rb new file mode 100644 index 0000000..7d347f2 --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/defined_type_handler_spec.rb @@ -0,0 +1,61 @@ +require 'spec_helper' +require 'puppetx/yardoc/yard/handlers/defined_type_handler' +require 'strings_spec/parsing' + + +describe "DefinedTypeHanlder" do + include StringsSpec::Parsing + + def the_definedtype() + Registry.at("foo::bar") + end + + it "should parse single-line documentation strings before a given defined type" do + comment = "Definition: foo::bar" + puppet_code = <<-PUPPET + # #{comment} + define foo::bar ($baz) { } + PUPPET + + parse(puppet_code, :puppet) + + expect(the_definedtype).to document_a(:type => :definedtype, :docstring => comment) + end + + it "should parse multi-line documentation strings before a given defined type" do + puppet_code = <<-PUPPET + # Definition: foo::bar + # + # This class does some stuff + define foo::bar ($baz) { } + PUPPET + + parse(puppet_code, :puppet) + + comment = "Definition: foo::bar\nThis class does some stuff" + expect(the_definedtype).to document_a(:type => :definedtype, :docstring => comment) + end + + it "should not parse documentation before a function if it is followed by a new line" do + puppet_code = <<-PUPPET + # Definition: foo::bar + + define foo::bar ($baz) { } + PUPPET + + parse(puppet_code, :puppet) + + expect(the_definedtype).to document_a(:type => :definedtype, :docstring => "") + end + + it "should not add anything to the Registry if incorrect puppet code is present" do + puppet_code = <<-PUPPET + # Definition: foo::bar + This is not puppet code + PUPPET + + parse(puppet_code, :puppet) + + expect(Registry.all).to be_empty + end +end diff --git a/spec/unit/puppetx/yardoc/yard/future_parser_function_handler_spec.rb b/spec/unit/puppetx/yardoc/yard/future_parser_function_handler_spec.rb new file mode 100644 index 0000000..c62bee7 --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/future_parser_function_handler_spec.rb @@ -0,0 +1,65 @@ +require 'spec_helper' +require 'puppetx/yardoc/yard/handlers/future_parser_function_handler' +require 'strings_spec/parsing' + +describe "FutureParserDispatchHandler" do + include StringsSpec::Parsing + + def the_method() + Registry.at("FutureParserFunctions#the_function") + end + + def the_namespace() + Registry.at("FutureParserFunctions") + end + + it "should parse single-line documentation strings before a given function" do + comment = "The summary" + parse <<-RUBY + # #{comment} + Puppet::Functions.create_function(:the_function) do + end + RUBY + + expect(the_method).to document_a(:type => :method, :docstring => comment) + expect(the_namespace).to document_a(:type => :puppetnamespace) + end + + it "should parse multi-line documentation strings before a given function" do + parse <<-RUBY + # The summary + # + # The longer description + Puppet::Functions.create_function(:the_function) do + end + RUBY + + comment = "The summary\n\nThe longer description" + expect(the_method).to document_a(:type => :method, :docstring => comment) + expect(the_namespace).to document_a(:type => :puppetnamespace) + end + + it "should not parse documentation before a function if it is followed by two new lines" do + parse <<-RUBY + # The summary + # + # The longer description + + + Puppet::Functions.create_function(:the_function) do + end + RUBY + + expect(the_method).to document_a(:type => :method, :docstring => "") + expect(the_namespace).to document_a(:type => :puppetnamespace) + end + + it "should not add anything to the Registry if incorrect ruby code is present" do + parse <<-RUBY + # The summary + This is not ruby code + RUBY + + expect(Registry.all).to be_empty + end +end diff --git a/spec/unit/puppetx/yardoc/yard/handlers_spec.rb b/spec/unit/puppetx/yardoc/yard/handlers_spec.rb deleted file mode 100644 index 4ba051b..0000000 --- a/spec/unit/puppetx/yardoc/yard/handlers_spec.rb +++ /dev/null @@ -1,243 +0,0 @@ -require 'spec_helper' -require 'puppetx/yardoc/yard/handlers' - -describe Puppetx::Yardoc::YARD::Handlers do - - # TODO: Relocate/refactor helper methods - def parse(string, parser = :ruby) - Registry.clear - YARD::Parser::SourceParser.parse_string(string, parser) - end - - RSpec::Matchers.define :document_a do |arguments| - match do |actual| - compare_values(actual).empty? - end - - failure_message do |actual| - mismatches = compare_values(actual) - mismatches.collect do |key, value| - "Expected #{key} to be <#{value[1]}>, but got <#{value[0]}>." - end.join("\n") - end - - def compare_values(actual) - mismatched_arguments = {} - expected.each do |key, value| - actual_value = actual.send(key) - if actual_value != value - mismatched_arguments[key] = [actual_value, value] - end - end - mismatched_arguments - end - end - - #TODO: Split up tests for each handler into their own files - describe "DefinedTypeHanlder" do - def the_definedtype() - Registry.at("foo::bar") - end - - it "should parse single-line documentation strings before a given defined type" do - comment = "Definition: foo::bar" - puppet_code = <<-PUPPET - # #{comment} - define foo::bar ($baz) { } - PUPPET - - parse(puppet_code, :puppet) - - expect(the_definedtype).to document_a(:type => :definedtype, :docstring => comment) - end - - it "should parse multi-line documentation strings before a given defined type" do - puppet_code = <<-PUPPET - # Definition: foo::bar - # - # This class does some stuff - define foo::bar ($baz) { } - PUPPET - - parse(puppet_code, :puppet) - - comment = "Definition: foo::bar\nThis class does some stuff" - expect(the_definedtype).to document_a(:type => :definedtype, :docstring => comment) - end - - it "should not parse documentation before a function if it is followed by a new line" do - puppet_code = <<-PUPPET - # Definition: foo::bar - - define foo::bar ($baz) { } - PUPPET - - parse(puppet_code, :puppet) - - expect(the_definedtype).to document_a(:type => :definedtype, :docstring => "") - end - it "should not add anything to the Registry if incorrect puppet code is present" do - puppet_code = <<-PUPPET - # Definition: foo::bar - This is not puppet code - PUPPET - - parse(puppet_code, :puppet) - - expect(Registry.all).to be_empty - end - end - - describe "FutureParserDispatchHandler" do - def the_method() - Registry.at("FutureParserFunctions#the_function") - end - - def the_namespace() - Registry.at("FutureParserFunctions") - end - - it "should parse single-line documentation strings before a given function" do - comment = "The summary" - parse <<-RUBY - # #{comment} - Puppet::Functions.create_function(:the_function) do - end - RUBY - - expect(the_method).to document_a(:type => :method, :docstring => comment) - expect(the_namespace).to document_a(:type => :puppetnamespace) - end - - it "should parse multi-line documentation strings before a given function" do - parse <<-RUBY - # The summary - # - # The longer description - Puppet::Functions.create_function(:the_function) do - end - RUBY - - comment = "The summary\n\nThe longer description" - expect(the_method).to document_a(:type => :method, :docstring => comment) - expect(the_namespace).to document_a(:type => :puppetnamespace) - end - - it "should not parse documentation before a function if it is followed by two new lines" do - parse <<-RUBY - # The summary - # - # The longer description - - - Puppet::Functions.create_function(:the_function) do - end - RUBY - - expect(the_method).to document_a(:type => :method, :docstring => "") - expect(the_namespace).to document_a(:type => :puppetnamespace) - end - - it "should not add anything to the Registry if incorrect ruby code is present" do - parse <<-RUBY - # The summary - This is not ruby code - RUBY - - expect(Registry.all).to be_empty - end - end - - describe "ParserFunctionHanlder" do - def the_method() - Registry.at("ParserFunctions#the_function") - end - - def the_namespace() - Registry.at("ParserFunctions") - end - - it "should parse single-line documentation strings before a given function" do - comment = "The summary" - parse <<-RUBY - # #{comment} - newfunction(:the_function, :type => rvalue) do |args| - end - RUBY - - expect(the_method).to document_a(:type => :method, :docstring => comment) - expect(the_namespace).to document_a(:type => :puppetnamespace) - end - - it "should parse multi-line documentation strings before a given function" do - parse <<-RUBY - # The summary - # - # The longer description - newfunction(:the_function, :type => rvalue) do |args| - end - RUBY - - comment = "The summary\n\nThe longer description" - expect(the_method).to document_a(:type => :method, :docstring => comment) - expect(the_namespace).to document_a(:type => :puppetnamespace) - end - - it "should not parse documentation before a function if it is followed by two new lines" do - parse <<-RUBY - # The summary - - - newfunction(:the_function, :type => rvalue) do |args| - end - RUBY - - expect(the_method).to document_a(:type => :method, :docstring => "") - expect(the_namespace).to document_a(:type => :puppetnamespace) - end - end - - describe "HostClassDefintion" do - def the_hostclass() - Registry.at("foo::bar") - end - - it "should parse single-line documentation strings before a given class" do - comment = "Class: foo::bar" - puppet_code = <<-PUPPET - # #{comment} - class foo::bar { } - PUPPET - - parse(puppet_code, :puppet) - - expect(the_hostclass).to document_a(:type => :hostclass, :docstring => comment) - end - - it "should parse multi-line documentation strings before a given class" do - puppet_code = <<-PUPPET - # Class: foo::bar - # - # This class does some stuff - class foo::bar { } - PUPPET - - parse(puppet_code, :puppet) - - comment = "Class: foo::bar\nThis class does some stuff" - expect(the_hostclass).to document_a(:type => :hostclass, :docstring => comment) - end - - it "should not parse documentation before a class if it is followed by a new line" do - puppet_code = <<-PUPPET - # Class: foo::bar - - class foo::bar { } - PUPPET - - parse(puppet_code, :puppet) - - expect(the_hostclass).to document_a(:type => :hostclass, :docstring => "") - end - end -end diff --git a/spec/unit/puppetx/yardoc/yard/host_class_handler_spec.rb b/spec/unit/puppetx/yardoc/yard/host_class_handler_spec.rb new file mode 100644 index 0000000..e594373 --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/host_class_handler_spec.rb @@ -0,0 +1,49 @@ +require 'spec_helper' +require 'puppetx/yardoc/yard/handlers/host_class_handler' +require 'strings_spec/parsing' + +describe "HostClassDefintion" do + include StringsSpec::Parsing + + def the_hostclass() + Registry.at("foo::bar") + end + + it "should parse single-line documentation strings before a given class" do + comment = "Class: foo::bar" + puppet_code = <<-PUPPET + # #{comment} + class foo::bar { } + PUPPET + + parse(puppet_code, :puppet) + + expect(the_hostclass).to document_a(:type => :hostclass, :docstring => comment) + end + + it "should parse multi-line documentation strings before a given class" do + puppet_code = <<-PUPPET + # Class: foo::bar + # + # This class does some stuff + class foo::bar { } + PUPPET + + parse(puppet_code, :puppet) + + comment = "Class: foo::bar\nThis class does some stuff" + expect(the_hostclass).to document_a(:type => :hostclass, :docstring => comment) + end + + it "should not parse documentation before a class if it is followed by a new line" do + puppet_code = <<-PUPPET + # Class: foo::bar + + class foo::bar { } + PUPPET + + parse(puppet_code, :puppet) + + expect(the_hostclass).to document_a(:type => :hostclass, :docstring => "") + end +end diff --git a/spec/unit/puppetx/yardoc/yard/parser_function_handler_spec.rb b/spec/unit/puppetx/yardoc/yard/parser_function_handler_spec.rb new file mode 100644 index 0000000..b84141e --- /dev/null +++ b/spec/unit/puppetx/yardoc/yard/parser_function_handler_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' +require 'puppetx/yardoc/yard/handlers/parser_function_handler' +require 'strings_spec/parsing' + +describe "ParserFunctionHanlder" do + include StringsSpec::Parsing + + def the_method() + Registry.at("ParserFunctions#the_function") + end + + def the_namespace() + Registry.at("ParserFunctions") + end + + it "should parse single-line documentation strings before a given function" do + comment = "The summary" + parse <<-RUBY + # #{comment} + newfunction(:the_function, :type => rvalue) do |args| + end + RUBY + + expect(the_method).to document_a(:type => :method, :docstring => comment) + expect(the_namespace).to document_a(:type => :puppetnamespace) + end + + it "should parse multi-line documentation strings before a given function" do + parse <<-RUBY + # The summary + # + # The longer description + newfunction(:the_function, :type => rvalue) do |args| + end + RUBY + + comment = "The summary\n\nThe longer description" + expect(the_method).to document_a(:type => :method, :docstring => comment) + expect(the_namespace).to document_a(:type => :puppetnamespace) + end + + it "should not parse documentation before a function if it is followed by two new lines" do + parse <<-RUBY + # The summary + + + newfunction(:the_function, :type => rvalue) do |args| + end + RUBY + + expect(the_method).to document_a(:type => :method, :docstring => "") + expect(the_namespace).to document_a(:type => :puppetnamespace) + end +end