From: "Mauricio Fernández" Date: 2004-08-28T06:05:57+09:00 Subject: Re: Literate testing? On Sat, Aug 28, 2004 at 05:40:33AM +0900, Massimiliano Mirra - bard wrote: > > I was documenting a class and found myself often copying unit tests > and pasting them as examples. I think I also read something about > tests embedded in doc strings in Python. So in the interest of DRY I > figured something like this could be desirable: > > class Calculator > extend Literate > > example > # Addition > add(2, 8) == 10 > end > > def add(a, b) > return a + b > end > end > > I then came up with a stupid but working proof of concept: > > class Calculator > extend Literate > > example(__FILE__, __LINE__) do |calc| > # Addition > calc.add(2, 8) == 10 > end I believe you can make it look a bit better by using Kernel#caller and Module#method_added; it would also be nice to generate test cases for Test::Unit (for instance by defining the test_* methods in MyClass::LiterateTests so that they can be included in a test suite later). Reminds me of Florian Gro�' 'test extractor' [109712]. It works "the other way around" though, by extracting the tests from the comments (similar to what you describe for Python). -- Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com