Reworked semi-automagic test generator and code annotator
I cleanup up my semi-automagic test generator and xmp filter for Ruby, and added some code to make it win32-friendly*1.
If you missed it the first times I wrote about it: xmpfilter.rb is basically a source code filter than can generate assertions for your tests, and annotate the code to show intermediate results. This picture illustrates the former:
The code on the right of the above picture was generated by
xmpfilter.rb -u code.rb
The quick explanation is that you add # => to the lines you want to test, the macro executes that line and sees what the value currently is, and that value becomes the asserted value for the future. So if anything changes, you will know, but if your current implementation is broken, your test will also be broken.
This can save quite some typing when you're adding regression tests, or when you're developing the code and the tests in parallel. It can (and should) be used incrementally: you expand the assertions once and then keep editing the generated code (not the original one!), repeating this as many times as needed while you're writing the tests --- think of it as an intelligent kind of macro expansion. It should only take about 1 keypress under a sane editor (I show how to do it with vim below).
This is something I use all the time when posting on ruby-talk; xmpfilter -a will expand
a = 1 10.times do |i| i ** 2 # => a += i end A = 1 A = 1
a = 1 10.times do |i| i ** 2 # => 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 a += i end A = 1 A = 1 # !> already initialized constant A
If you use vim, this xmpfilter can be used very conveniently with something like
" I actually prefer ggVG to %; it's a sort of poor man's visual bell nmap <silent> <F11> mzggVG!xmpfilter.rb -a<cr>'z imap <silent> <F11> <ESC><F11>a nmap <silent> <S-F11> mzggVG!xmpfilter.rb -u<cr>'z imap <silent> <S-F11> <ESC><S-F11>axmpfilter.rb
The online darcs repository with the development version can be found at http://eigenclass.org/repos/xmpfilter .
This is cool. It took me a few minutes to wrap my head around it though. The quick explanation is that you add # => lines you want to test, the macro executes that line and sees what the value currently is, and that value becomes the asserted vakye for the future. So if anything changes, you will know, but if your currently implementation is broken, your test will also be broken.
Err... "asserted value". Vakye sounds like something from the Ring cycle.
I'm adding your explanation to the posting, thank you :)
"added some code to make it win32-friendly*1. [I have no winbox to test it, though]"
There is a Windows system here running Ruby via the One Click installer (r184-19final) that you can use at second-hand if that helps...
Initial comments -
1. your sample code doesn't get the Constant violation flagged up
C:\RubyTests>mf_xmpfilter -a mf_code1.rb a = 1 10.times do |i| i ** 2 # => 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 a += i end A = 1 A = 1 C:\RubyTests>
2. with no options supplied, the parser defaults to providing Help, not Annotate as documented.
C:\RubyTests>mf_xmpfilter mf_code1.rb Usage: xmpfilter.rb [options] [inputfile] [-- cmdline args] Modes: -a, --annotations Annotate code (default) -u, --unittest Complete Test::Unit assertions. -m, --markers Add # => markers. Interpreter options: -I PATH Add PATH to $LOAD_PATH -r LIB Require LIB before execution. -S, --interpreter FILE Use interpreter FILE. -l, --min-line-length N Align markers to N spaces. --[no]-delimiters Put delimiters around code. (default: disabled) -h, --help Show this message -v, --version Show version information C:\RubyTests>
Pursue this by private email (you have my Gmail) if you wish.
As ever, if I've misunderstood how to use this, please educate me!
Thank you for the bug report. I fixed the bug involving missing warnings, and changed the code to match the help message (I might change it the other way around if waiting for input from stdin proves to be confusing).
You can find the updated version in the online darcs repository at http://eigenclass.org/repos/xmpfilter/ .
*1 I have no winbox to test it, though
- 271 http://platypope.org/blog/2006/9/8/i-need-a-cool-european-accent
- 47 http://platypope.org/blog
- 17 http://www.artima.com/forums/flat.jsp?forum=123&thread=168557
- 12 http://www.artima.com/buzz/community.jsp?forum=123
- 11 http://anarchaia.org
- 10 http://www.approximity.com/cgi-bin/blogtariAgile/index.rb
- 7 http://www.anarchaia.org
- 7 http://planetruby.0x42.net
- 6 http://www.rubycorner.com
- 4 http://anarchaia.org/archive/2006/07/16.html