Created
June 14, 2009 18:56
-
-
Save lsegal/129778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'yard' | |
class ContextObject < YARD::CodeObjects::NamespaceObject | |
attr_accessor :tests | |
def initialize(*args) | |
super | |
@tests = [] | |
end | |
def type; :context end | |
def path; "__TestContext::" + super end | |
end | |
class ContextHandler < YARD::Handlers::Ruby::Base | |
handles method_call(:context) | |
def process | |
name = statement[1].jump(:string_content).source | |
ns = ContextObject.new(:root, name) | |
parse_block(statement[-1][1], :namespace => ns) | |
end | |
end | |
class ExpectHandler < YARD::Handlers::Ruby::Base | |
handles method_call(:expect) | |
def process | |
return unless namespace.is_a?(ContextObject) | |
namespace.tests << statement[-1][1].source | |
end | |
end | |
class LegacyContextHandler < YARD::Handlers::Ruby::Legacy::Base | |
handles /\Acontext\b/ | |
def process | |
name = tokval_list(statement.tokens[2..-1]).first | |
parse_block :namespace => ContextObject.new(namespace, name) | |
end | |
def parse_block(opts = {}) | |
push_state(opts) do | |
blk = YARD::Parser::Ruby::Legacy::StatementList.new(statement.block[1..-2]) | |
parser.process(blk) | |
end | |
end | |
end | |
class LegacyExpectHandler < YARD::Handlers::Ruby::Legacy::Base | |
handles /\Aexpect\b/ | |
def process | |
return unless namespace.is_a?(ContextObject) | |
namespace.tests << statement.block[1..-3].to_s.strip | |
end | |
end | |
YARD::Parser::SourceParser.parse_string <<-eof | |
context "A blog post" do | |
setup do | |
@author = create_person | |
@post = create_post :author => @author | |
end | |
expect { @post.to be_editable_by(@author) } | |
expect { @post.not_to be_editable_by(@somebody_else) } | |
end | |
eof | |
YARD::Registry.all(:context).each do |context| | |
context.tests.each do |test| | |
puts "test: #{context.name} expect #{test}" | |
end | |
end | |
# test: A blog post expect @post.to be_editable_by(@author) | |
# test: A blog post expect @post.not_to be_editable_by(@somebody_else) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment