eigenclass logo
MAIN  Index  Search  Changes  PageRank  Login

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:

screenshot.png

The code on the right of the above picture was generated by

 xmpfilter.rb -u code.rb

update.png evan explains:

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).

Intermediate result annotations

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

into

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

Vim mappings

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>a

Download

xmpfilter.rb

update.png The online darcs repository with the development version can be found at http://eigenclass.org/repos/xmpfilter .


No Title - evan (2006-07-16 (Sun) 14:00:17)

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.

evan 2006-07-16 (Sun) 14:01:16

Err... "asserted value". Vakye sounds like something from the Ring cycle.

mfp 2006-07-16 (Sun) 16:25:01

I'm adding your explanation to the posting, thank you :)


xmpfilter - DeeJay (2006-07-16 (Sun) 10:34:22)

"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!

DeeJay/smartGPX

mfp 2006-07-16 (Sun) 12:17:22

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/ .


Last modified:2006/07/15 10:01:58
Keyword(s):[blog] [ruby] [frontpage] [xmp] [code] [filter] [test] [assertion] [generator] [release]
References:

*1 I have no winbox to test it, though