From aed91b0fb2fe3c2cd3a74e2e5bc23c8bd95ef1db Mon Sep 17 00:00:00 2001 From: Seba Gamboa Date: Wed, 23 Sep 2015 14:24:03 -0300 Subject: Making specs works --- lib/yard/handlers.rb | 2 + lib/yard/handlers/c.rb | 5 ++ lib/yard/handlers/c/mruby_base.rb | 12 +++ lib/yard/handlers/c/mruby_class_handler.rb | 35 ++++++++ lib/yard/handlers/c/mruby_method_handler.rb | 21 +++++ lib/yard/handlers/c/mruby_module_handler.rb | 34 ++++++++ lib/yard/handlers/header.rb | 1 + lib/yard/handlers/header/base.rb | 11 +++ lib/yard/handlers/header/define_handler.rb | 0 lib/yard/handlers/header/function_handler.rb | 0 lib/yard/handlers/header/header_handler.rb | 0 lib/yard/mruby.rb | 2 +- lib/yard/mruby/handlers.rb | 2 - lib/yard/mruby/handlers/header.rb | 1 - lib/yard/mruby/handlers/header/base.rb | 11 --- lib/yard/mruby/handlers/header/define_handler.rb | 0 lib/yard/mruby/handlers/header/function_handler.rb | 0 lib/yard/mruby/handlers/header/header_handler.rb | 0 lib/yard/mruby/handlers/source.rb | 5 -- lib/yard/mruby/handlers/source/base.rb | 18 ----- lib/yard/mruby/handlers/source/class_handler.rb | 35 -------- lib/yard/mruby/handlers/source/method_handler.rb | 22 ----- lib/yard/mruby/handlers/source/module_handler.rb | 34 -------- spec/handlers/c/mruby_class_handler_spec.rb | 32 ++++++++ spec/handlers/c/spec_helper.rb | 13 +++ spec/handlers/spec_helper.rb | 31 ++++++++ spec/spec_helper.rb | 93 ++++++++++++++++++++++ spec/yard/mruby_spec.rb | 11 --- 28 files changed, 291 insertions(+), 140 deletions(-) create mode 100644 lib/yard/handlers.rb create mode 100644 lib/yard/handlers/c.rb create mode 100644 lib/yard/handlers/c/mruby_base.rb create mode 100644 lib/yard/handlers/c/mruby_class_handler.rb create mode 100644 lib/yard/handlers/c/mruby_method_handler.rb create mode 100644 lib/yard/handlers/c/mruby_module_handler.rb create mode 100644 lib/yard/handlers/header.rb create mode 100644 lib/yard/handlers/header/base.rb create mode 100644 lib/yard/handlers/header/define_handler.rb create mode 100644 lib/yard/handlers/header/function_handler.rb create mode 100644 lib/yard/handlers/header/header_handler.rb delete mode 100644 lib/yard/mruby/handlers.rb delete mode 100644 lib/yard/mruby/handlers/header.rb delete mode 100644 lib/yard/mruby/handlers/header/base.rb delete mode 100644 lib/yard/mruby/handlers/header/define_handler.rb delete mode 100644 lib/yard/mruby/handlers/header/function_handler.rb delete mode 100644 lib/yard/mruby/handlers/header/header_handler.rb delete mode 100644 lib/yard/mruby/handlers/source.rb delete mode 100644 lib/yard/mruby/handlers/source/base.rb delete mode 100644 lib/yard/mruby/handlers/source/class_handler.rb delete mode 100644 lib/yard/mruby/handlers/source/method_handler.rb delete mode 100644 lib/yard/mruby/handlers/source/module_handler.rb create mode 100644 spec/handlers/c/mruby_class_handler_spec.rb create mode 100644 spec/handlers/c/spec_helper.rb create mode 100644 spec/handlers/spec_helper.rb delete mode 100644 spec/yard/mruby_spec.rb diff --git a/lib/yard/handlers.rb b/lib/yard/handlers.rb new file mode 100644 index 0000000..41a1f73 --- /dev/null +++ b/lib/yard/handlers.rb @@ -0,0 +1,2 @@ +require_relative 'handlers/c' +#require_relative 'handlers/header' diff --git a/lib/yard/handlers/c.rb b/lib/yard/handlers/c.rb new file mode 100644 index 0000000..61c4943 --- /dev/null +++ b/lib/yard/handlers/c.rb @@ -0,0 +1,5 @@ +require_relative 'c/mruby_base' +require_relative 'c/mruby_class_handler' +# require_relative 'c/mruby_module_handler' +require_relative 'c/mruby_method_handler' + diff --git a/lib/yard/handlers/c/mruby_base.rb b/lib/yard/handlers/c/mruby_base.rb new file mode 100644 index 0000000..83c237a --- /dev/null +++ b/lib/yard/handlers/c/mruby_base.rb @@ -0,0 +1,12 @@ +module YARD::Handlers::C + class MRubyBase < Base + DEFAULT_NAMESPACES = { + 'mrb->object_class' => 'Object' + } + + def namespace_for_variable(var) + return DEFAULT_NAMESPACES[var] if DEFAULT_NAMESPACES[var] + super + end + end +end diff --git a/lib/yard/handlers/c/mruby_class_handler.rb b/lib/yard/handlers/c/mruby_class_handler.rb new file mode 100644 index 0000000..9e3d1f5 --- /dev/null +++ b/lib/yard/handlers/c/mruby_class_handler.rb @@ -0,0 +1,35 @@ +module YARD::Handlers::C + class MRubyClassHandler < MRubyBase + + TOP_LEVEL_CLASS = /([\w]+)\s*=\s*mrb_define_class\s* + \( + \s*\w+\s*, + \s*"(\w+)"\s*, + \s*([\w\->]+)\s* + \) + /mx + + NAMESPACED_CLASS = /([\w]+)\s*=\s*mrb_define_class_under\s* + \( + \s*\w+\s*, + \s*(\w+)\s*, + \s*"(\w+)"\s*, + \s*([\w\->]+)\s* + \) + /mx + + handles TOP_LEVEL_CLASS + handles NAMESPACED_CLASS + + statement_class BodyStatement + + process do + statement.source.scan(TOP_LEVEL_CLASS) do |var_name, class_name, parent| + handle_class(var_name, class_name, parent) + end + statement.source.scan(NAMESPACED_CLASS) do |var_name, in_module, class_name, parent| + handle_class(var_name, class_name, parent, in_module) + end + end + end +end diff --git a/lib/yard/handlers/c/mruby_method_handler.rb b/lib/yard/handlers/c/mruby_method_handler.rb new file mode 100644 index 0000000..01fb345 --- /dev/null +++ b/lib/yard/handlers/c/mruby_method_handler.rb @@ -0,0 +1,21 @@ +module YARD::Handlers::C + class MRubyMethodHandler < MRubyBase + MATCH1 = /mrb_define_method\s* + \( + \s*\w+\s*, + \s*(\w+)\s*, + \s*"(\w+)"\s*, + \s*(\w+)\s*, + /mx + + handles MATCH1 + statement_class BodyStatement + + process do + statement.source.scan(MATCH1) do |var_name, name, func_name| + handle_method(nil, var_name, name, func_name) + end + end + + end +end diff --git a/lib/yard/handlers/c/mruby_module_handler.rb b/lib/yard/handlers/c/mruby_module_handler.rb new file mode 100644 index 0000000..c9243ac --- /dev/null +++ b/lib/yard/handlers/c/mruby_module_handler.rb @@ -0,0 +1,34 @@ +module YARD::MRuby::Handlers::Source + class ModuleHandler < Base + + TOP_LEVEL_MODULE = /\*([\w]+)\s*=\s*mrb_define_module\s* + \( + \s*\w+\s*, + \s*"(\w+)"\s* + \) + /mx + + NAMESPACED_MODULE = /\*([\w]+)\s*=\s*mrb_define_module_under\s* + \( + \s*\w+\s*, + \s*(\w+)\s*, + \s*"(\w+)"\s* + \) + /mx + + handles TOP_LEVEL_MODULE + handles NAMESPACED_MODULE + + statement_class BodyStatement + + process do + statement.source.scan(TOP_LEVEL_MODULE) do |var_name, module_name| + handle_module(var_name, module_name) + end + statement.source.scan(NAMESPACED_MODULE) do |var_name, in_module, module_name| + handle_module(var_name, module_name, in_module) + end + end + end +end + diff --git a/lib/yard/handlers/header.rb b/lib/yard/handlers/header.rb new file mode 100644 index 0000000..bb39563 --- /dev/null +++ b/lib/yard/handlers/header.rb @@ -0,0 +1 @@ + require_relative 'header/base' diff --git a/lib/yard/handlers/header/base.rb b/lib/yard/handlers/header/base.rb new file mode 100644 index 0000000..35859f7 --- /dev/null +++ b/lib/yard/handlers/header/base.rb @@ -0,0 +1,11 @@ +module YARD + module MRuby + module Handlers + module Header + class Base < YARD::Handlers::Base + include YARD::Parser::C + end + end + end + end +end diff --git a/lib/yard/handlers/header/define_handler.rb b/lib/yard/handlers/header/define_handler.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/yard/handlers/header/function_handler.rb b/lib/yard/handlers/header/function_handler.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/yard/handlers/header/header_handler.rb b/lib/yard/handlers/header/header_handler.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/yard/mruby.rb b/lib/yard/mruby.rb index d94cd78..683d3c5 100644 --- a/lib/yard/mruby.rb +++ b/lib/yard/mruby.rb @@ -1,3 +1,3 @@ require_relative "mruby/version" require_relative "mruby/code_objects" -require_relative "mruby/handlers" +require_relative "handlers" diff --git a/lib/yard/mruby/handlers.rb b/lib/yard/mruby/handlers.rb deleted file mode 100644 index cb0c898..0000000 --- a/lib/yard/mruby/handlers.rb +++ /dev/null @@ -1,2 +0,0 @@ -require_relative 'handlers/source' -require_relative 'handlers/header' diff --git a/lib/yard/mruby/handlers/header.rb b/lib/yard/mruby/handlers/header.rb deleted file mode 100644 index bb39563..0000000 --- a/lib/yard/mruby/handlers/header.rb +++ /dev/null @@ -1 +0,0 @@ - require_relative 'header/base' diff --git a/lib/yard/mruby/handlers/header/base.rb b/lib/yard/mruby/handlers/header/base.rb deleted file mode 100644 index 35859f7..0000000 --- a/lib/yard/mruby/handlers/header/base.rb +++ /dev/null @@ -1,11 +0,0 @@ -module YARD - module MRuby - module Handlers - module Header - class Base < YARD::Handlers::Base - include YARD::Parser::C - end - end - end - end -end diff --git a/lib/yard/mruby/handlers/header/define_handler.rb b/lib/yard/mruby/handlers/header/define_handler.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/yard/mruby/handlers/header/function_handler.rb b/lib/yard/mruby/handlers/header/function_handler.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/yard/mruby/handlers/header/header_handler.rb b/lib/yard/mruby/handlers/header/header_handler.rb deleted file mode 100644 index e69de29..0000000 diff --git a/lib/yard/mruby/handlers/source.rb b/lib/yard/mruby/handlers/source.rb deleted file mode 100644 index af8b0db..0000000 --- a/lib/yard/mruby/handlers/source.rb +++ /dev/null @@ -1,5 +0,0 @@ - require_relative 'source/base' - require_relative 'source/class_handler' - require_relative 'source/module_handler' - require_relative 'source/method_handler' - diff --git a/lib/yard/mruby/handlers/source/base.rb b/lib/yard/mruby/handlers/source/base.rb deleted file mode 100644 index 4578079..0000000 --- a/lib/yard/mruby/handlers/source/base.rb +++ /dev/null @@ -1,18 +0,0 @@ -module YARD - module MRuby - module Handlers - module Source - class Base < YARD::Handlers::C::Base - DEFAULT_NAMESPACES = { - 'mrb->object_class' => 'Object' - } - - def namespace_for_variable(var) - return DEFAULT_NAMESPACES[var] if DEFAULT_NAMESPACES[var] - super - end - end - end - end - end -end diff --git a/lib/yard/mruby/handlers/source/class_handler.rb b/lib/yard/mruby/handlers/source/class_handler.rb deleted file mode 100644 index 185aaa6..0000000 --- a/lib/yard/mruby/handlers/source/class_handler.rb +++ /dev/null @@ -1,35 +0,0 @@ -module YARD::MRuby::Handlers::Source - class ClassHandler < Base - - TOP_LEVEL_CLASS = /\*([\w]+)\s*=\s*mrb_define_class\s* - \( - \s*\w+\s*, - \s*"(\w+)"\s*, - \s*([\w\->]+)\s* - \) - /mx - - NAMESPACED_CLASS = /\*([\w]+)\s*=\s*mrb_define_class_under\s* - \( - \s*\w+\s*, - \s*(\w+)\s*, - \s*"(\w+)"\s*, - \s*([\w\->]+)\s* - \) - /mx - - handles TOP_LEVEL_CLASS - handles NAMESPACED_CLASS - - statement_class BodyStatement - - process do - statement.source.scan(TOP_LEVEL_CLASS) do |var_name, class_name, parent| - handle_class(var_name, class_name, parent) - end - statement.source.scan(NAMESPACED_CLASS) do |var_name, in_module, class_name, parent| - handle_class(var_name, class_name, parent, in_module) - end - end - end -end diff --git a/lib/yard/mruby/handlers/source/method_handler.rb b/lib/yard/mruby/handlers/source/method_handler.rb deleted file mode 100644 index f3eab8c..0000000 --- a/lib/yard/mruby/handlers/source/method_handler.rb +++ /dev/null @@ -1,22 +0,0 @@ -module YARD::MRuby::Handlers::Source - class MethodHandler < Base - MATCH1 = /mrb_define_method\s* - \( - \s*\w+\s*, - \s*(\w+)\s*, - \s*"(\w+)"\s*, - \s*(\w+)\s*, - /mx - - handles MATCH1 - statement_class BodyStatement - - process do - puts statement.inspect - statement.source.scan(MATCH1) do |var_name, name, func_name| - handle_method(nil, var_name, name, func_name) - end - end - - end -end diff --git a/lib/yard/mruby/handlers/source/module_handler.rb b/lib/yard/mruby/handlers/source/module_handler.rb deleted file mode 100644 index c9243ac..0000000 --- a/lib/yard/mruby/handlers/source/module_handler.rb +++ /dev/null @@ -1,34 +0,0 @@ -module YARD::MRuby::Handlers::Source - class ModuleHandler < Base - - TOP_LEVEL_MODULE = /\*([\w]+)\s*=\s*mrb_define_module\s* - \( - \s*\w+\s*, - \s*"(\w+)"\s* - \) - /mx - - NAMESPACED_MODULE = /\*([\w]+)\s*=\s*mrb_define_module_under\s* - \( - \s*\w+\s*, - \s*(\w+)\s*, - \s*"(\w+)"\s* - \) - /mx - - handles TOP_LEVEL_MODULE - handles NAMESPACED_MODULE - - statement_class BodyStatement - - process do - statement.source.scan(TOP_LEVEL_MODULE) do |var_name, module_name| - handle_module(var_name, module_name) - end - statement.source.scan(NAMESPACED_MODULE) do |var_name, in_module, module_name| - handle_module(var_name, module_name, in_module) - end - end - end -end - diff --git a/spec/handlers/c/mruby_class_handler_spec.rb b/spec/handlers/c/mruby_class_handler_spec.rb new file mode 100644 index 0000000..521ad62 --- /dev/null +++ b/spec/handlers/c/mruby_class_handler_spec.rb @@ -0,0 +1,32 @@ +require_relative 'spec_helper' + +describe YARD::Handlers::C::MRubyClassHandler do + it "should register classes" do + parse_init 'cFoo = mrb_define_class(mrb, "Foo", mrb->object_class);' + + expect(Registry.at('Foo').type).to be :class + end + + it "should register classes under namespaces" do + parse_init 'cFoo = mrb_define_class_under(mrb, cBar, "Foo", rb_cObject);' + expect(Registry.at('Bar::Foo').type).to be :class + end + + it "should remember symbol defined with class" do + parse_init(<<-eof) + cXYZ = mrb_define_class(mrb, "Foo", mrb->object_class); + mrb_define_method(mrb, cXYZ, "bar", bar, MRB_ARGS_NONE()); + eof + expect(Registry.at('Foo').type).to be :class + expect(Registry.at('Foo#bar')).not_to be_nil + end + + it "should lookup superclass symbol name" do + parse_init(<<-eof) + cXYZ = mrb_define_class(mrb,"Foo", mrb->object_class); + cBar = mrb_define_class(mrb,"Bar", cXYZ); + eof + expect(Registry.at('Bar').superclass).to eq Registry.at('Foo') + end + +end diff --git a/spec/handlers/c/spec_helper.rb b/spec/handlers/c/spec_helper.rb new file mode 100644 index 0000000..9424e3e --- /dev/null +++ b/spec/handlers/c/spec_helper.rb @@ -0,0 +1,13 @@ +require_relative '../spec_helper' + +def parse(src, file = '(stdin)') + YARD::Registry.clear + parser = YARD::Parser::SourceParser.new(:c) + parser.file = file + parser.parse(StringIO.new(src)) +end + +def parse_init(src) + YARD::Registry.clear + YARD.parse_string("void mrb_foo_gem_init(mrb_state *mrb) {\n#{src}\n}", :c) +end diff --git a/spec/handlers/spec_helper.rb b/spec/handlers/spec_helper.rb new file mode 100644 index 0000000..1eba194 --- /dev/null +++ b/spec/handlers/spec_helper.rb @@ -0,0 +1,31 @@ +require_relative '../spec_helper' +require 'stringio' + +def undoc_error(code) + lambda { StubbedSourceParser.parse_string(code) }.should raise_error(Parser::UndocumentableError) +end + +def with_parser(parser_type, &block) + tmp = StubbedSourceParser.parser_type + StubbedSourceParser.parser_type = parser_type + yield + StubbedSourceParser.parser_type = tmp +end + +class StubbedProcessor < YARD::Handlers::Processor + def process(statements) + statements.each_with_index do |stmt, index| + find_handlers(stmt).each do |handler| + handler.new(self, stmt).process + end + end + end +end + +class StubbedSourceParser < YARD::Parser::SourceParser + StubbedSourceParser.parser_type = :ruby + def post_process + post = StubbedProcessor.new(self) + post.process(@parser.enumerator) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9a01ce9..dab8698 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,95 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) + +require 'yard' require 'yard/mruby' + +def parse_file(file, thisfile = __FILE__, log_level = log.level, ext = '.rb.txt') + Registry.clear + path = File.join(File.dirname(thisfile), 'examples', file.to_s + ext) + YARD::Parser::SourceParser.parse(path, [], log_level) +end + +def described_in_docs(klass, meth, file = nil) + YARD::Tags::Library.define_tag "RSpec Specification", :it, :with_raw_title_and_text + + # Parse the file (could be multiple files) + if file + filename = File.join(YARD::ROOT, file) + YARD::Parser::SourceParser.new.parse(filename) + else + underscore = klass.class_name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.gsub('::', '/') + $".find_all {|p| p.include? underscore }.each do |filename| + next unless File.exist? filename + YARD::Parser::SourceParser.new.parse(filename) + end + end + + # Get the object + objname = klass.name + (meth[0,1] == '#' ? meth : '::' + meth) + obj = Registry.at(objname) + raise "Cannot find object #{objname} described by spec." unless obj + raise "#{obj.path} has no @it tags to spec." unless obj.has_tag? :it + + # Run examples + describe(klass, meth) do + obj.tags(:it).each do |it| + path = File.relative_path(YARD::ROOT, obj.file) + it(it.name + " (from #{path}:#{obj.line})") do + begin + eval(it.text) + rescue => e + e.set_backtrace(["#{path}:#{obj.line}:in @it tag specification"]) + raise e + end + end + end + end +end + +def docspec(objname = self.class.description, klass = self.class.described_type) + # Parse the file (could be multiple files) + underscore = klass.class_name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.gsub('::', '/') + $".find_all {|p| p.include? underscore }.each do |filename| + filename = File.join(YARD::ROOT, filename) + next unless File.exist? filename + YARD::Parser::SourceParser.new.parse(filename) + end + + # Get the object + objname = klass.name + objname if objname =~ /^[^A-Z]/ + obj = Registry.at(objname) + raise "Cannot find object #{objname} described by spec." unless obj + raise "#{obj.path} has no @example tags to spec." unless obj.has_tag? :example + + # Run examples + obj.tags(:example).each do |exs| + exs.text.split(/\n/).each do |ex| + begin + hash = eval("{ #{ex} }") + hash.keys.first.should == hash.values.first + rescue => e + raise e, "#{e.message}\nInvalid spec example in #{objname}:\n\n\t#{ex}\n" + end + end + end +end + +module Kernel + require 'cgi' + + def p(*args) + puts args.map {|arg| CGI.escapeHTML(arg.inspect) }.join("
\n") + args.first + end + + def puts(str = '') + STDOUT.puts str + "
\n" + str + end +end if ENV['TM_APP_PATH'] + +RSpec.configure do |config| + config.before(:each) { log.io = StringIO.new } +end + +Registry = YARD::Registry diff --git a/spec/yard/mruby_spec.rb b/spec/yard/mruby_spec.rb deleted file mode 100644 index 3b6f756..0000000 --- a/spec/yard/mruby_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'spec_helper' - -describe Yard::Mruby do - it 'has a version number' do - expect(Yard::Mruby::VERSION).not_to be nil - end - - it 'does something useful' do - expect(false).to eq(true) - end -end -- cgit v1.2.3