From: Florian Gross Date: 2004-11-26T07:52:58+09:00 Subject: Re: [RDOC] Using unit-tests as examples for a documentation Dave Thomas wrote: > On Nov 25, 2004, at 15:32, benny wrote: >> Now the idea: Why not use the examples in the unit-tests (at least the >> "positive ones") as link to a popup window "examples" in the >> HTML-documentation created by rdoc? > > This has been raised before. The problem is that it's hard to work out > which tests test which methods: method naming conventions would help, > but even so you quite often have three test methods testing one > production method. > > An alternative might be to embed the UTs in the comment for the method, > and then have TestUnit extract and run them automatically. I personally > quite like that approach, although it would need some special markup > that RDoc doesn't yet support. I'm currently using indentation as a sample code marker in test-extract.rb and would welcome official tags for this. (Maybe prefixing each line of code with optional whitespace and "|"?) Integrating support for this into RDoc might also have other benefits for output formatting and so on. Here's a sample of what I am currently doing: > # Causes a pattern which would otherwise match > # greedily to match non-greedily. > # > # This can only be applied to #count and #optional > # based constructs. > # > # re_greedy = Regexp::English.new do > # literal("<") + something + literal(">") > # end > # re_non_greedy = Regexp::English.new do > # literal("<") + something.minimal + literal(">") > # end > # str = "" > # re_greedy.match(str)[0] # => "" > # re_non_greedy.match(str)[0] # => "" > # > # This method will do nothing when applied to a Node > # which is already minimal: > # > # re_minimal = Regexp::English.something.minimal > # re_minimal.minimal == re_minimal # => true > # > # However, it will raise a NameError Exception when > # you try to call it on a Regexp Node that can't be > # made non-greedy: > # > # re = Regexp::English.literal("foo") > # re.minimal # raises NameError > def minimal > > end The next version of test/unit will also feature a new syntax for this. That one will use sample { assert_equal(code, result) } style test cases directly before the method IIRC.