summaryrefslogtreecommitdiffhomepage
path: root/spec/spec_helper.rb
diff options
context:
space:
mode:
authorSeba Gamboa <[email protected]>2015-09-23 14:24:03 -0300
committerSeba Gamboa <[email protected]>2015-09-23 14:24:03 -0300
commitaed91b0fb2fe3c2cd3a74e2e5bc23c8bd95ef1db (patch)
tree880d0eb2d6ec5d949f4a0ae72980687dd9041740 /spec/spec_helper.rb
parent731c5326b47b34364b2a8f69c6d558f52957a562 (diff)
downloadyard-mruby-aed91b0fb2fe3c2cd3a74e2e5bc23c8bd95ef1db.tar.gz
yard-mruby-aed91b0fb2fe3c2cd3a74e2e5bc23c8bd95ef1db.zip
Making specs works
Diffstat (limited to 'spec/spec_helper.rb')
-rw-r--r--spec/spec_helper.rb93
1 files changed, 93 insertions, 0 deletions
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("<br/>\n")
+ args.first
+ end
+
+ def puts(str = '')
+ STDOUT.puts str + "<br/>\n"
+ str
+ end
+end if ENV['TM_APP_PATH']
+
+RSpec.configure do |config|
+ config.before(:each) { log.io = StringIO.new }
+end
+
+Registry = YARD::Registry