Skip to content

Instantly share code, notes, and snippets.

@lsegal
Created December 28, 2011 07:43
Show Gist options
  • Save lsegal/1527001 to your computer and use it in GitHub Desktop.
Save lsegal/1527001 to your computer and use it in GitHub Desktop.
module TestCaseMixin
include YARD::CodeObjects
def create_test_method(name)
test_name = 'test_' + name.to_s.gsub(/\W/, '_')
register MethodObject.new(namespace, test_name, :instance)
end
end
class LegacyTestCaseHandler < YARD::Handlers::Ruby::Legacy::Base
include TestCaseMixin
handles /^test(?:\(|\s)/
namespace_only
process { create_test_method tokval(statement.tokens[2], :attr) }
end
class TestCaseHandler < YARD::Handlers::Ruby::Base
include TestCaseMixin
handles method_call(:test)
namespace_only
process { create_test_method statement.parameters[0].jump(:tstring_content, :ident).source }
end
require 'yard'
require './a_yard_handler'
YARD.parse('test/test_something.rb')
obj = YARD::Registry.at('MyTestClass#test_something_1')
obj.source # => "def test_something_1\n assert_equals 1, 1\n assert_equals 'foo', 'bar'\nend"
obj.line # => 2
class MyTestClass
def test_something_1
assert_equals 1, 1
assert_equals 'foo', 'bar'
end
end
@qrush
Copy link

qrush commented Dec 28, 2011

Hey Loren, this doesn't work with dynamically created methods. m supports ActiveSupport::TestCase's #test method, like here:

https://github.com/qrush/m/blob/master/test/examples/active_support_example_test.rb

Basically, doing YARD::Registry.at("ActiveSupportExampleTest#test_carrot") doesn't work for me.

@qrush
Copy link

qrush commented Dec 28, 2011

Nevermind, I just found

http://yardoc.org/guides/extending-yard/writing-handlers.html

And that works great. :)

@qrush
Copy link

qrush commented Dec 28, 2011

Having trouble with tests defined dynamically now:

https://github.com/qrush/m/blob/master/test/examples/multiple_example_test.rb

I can just wait until you're up :)

@qrush
Copy link

qrush commented Dec 28, 2011

Here's my progress so far, I think I'd prefer to use YARD if it works :)

https://github.com/qrush/m/tree/yard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment