RAA - xmpfilter/0.3.1

xmpfilter / 0.3.1

Short description: Automagic test assertions/RSpec expectations, code annotation
Category: Application/Development
Status: stable
Created: 2006-10-16 10:43:44 GMT
Last update: 2006-10-27 23:16:12 GMT
Owner: mfp (Mauricio Fernandez) (Projects of this owner)
Homepage: http://eigenclass.org/hiki.rb?xmpfilter
Download: http://eigenclass.org/static/xmpfilter/xmpfilter-0.3.1.tar.gz
License: Ruby's
Dependency:
None
Description:

xmpfilter.rb is a small tool that can be used to

  • generate Test::Unit assertions and RSpec expectations semi-automatically
  • annotate source code with intermediate results (a bit like irb —simple-prompt but only for the lines explicitly marked with # =>) Very useful for example code (such as postings to ruby-talk).

Example: code annotation

Just add "# =>" markers to the lines whose values you want to be shown:

 a, b = "foo", "baz"
 a + b                                             # =>
 a.size                                            # =>

will be expanded to (in one keypress in a decent editor, see README.emacs and README.vim)

 a, b = "foo", "baz"
 a + b                                             # => "foobaz"
 a.size                                            # => 3

This saves much cut&pasting when you’re posting to ruby-talk/ruby-core (I use it all the time).

Example: assertion generation

xmpfilter.rb can generate assertions based on the current behavior of the code to be tested (iow. the current behavior is assumed to be correct and is used to generate assertions which won’t be modified by further runs of xmpfilter.rb), making it quite useful for regression testing.

Imagine you have a ComplexClass you want to test. You might start with

    class TestComplexClass < Test::Unit::TestCase
      def setup; @o = ComplexClass.new("foo", false) end
    end

and then want to add some tests:

  def test_insertion
    @o.insert "bar"
    @o.insert "baz"
    # ... assertions here
  end

At this point, you want to add several assertions to verify that the values returned by @o.size, @o.last, @o.first, @o.complex_computation and @o.last(2) are correct. You can just write the following and feed the file to xmpfilter.rb in -u mode (the # => markers can also be inserted by xmpfilter.rb, see README.vim for more information:

  def test_insertion
    @o.insert "bar"
    @o.insert "baz"
    @o.size                                        # =>
    @o.last                                        # =>
    @o.first                                       # =>
    @o.complex_computation                         # =>
    @o.last(2)                                     # =>
  end

xmpfilter.rb will run the test and remember what happened in each marked line, and then rewrite the code so that it looks for instance like

  def test_insertion
    @o.insert "bar"
    @o.insert "baz"
    assert_equal(2, @o.size)
    assert_equal("baz", @o.last)
    assert_equal("bar", @o.first)
    assert_in_delta(3.14159265358979, @o.complex_computation, 0.0001)
    assert_equal(["baz", "bar"], @o.last(2))
  end

As you can see, it can save some typing.

You can edit the generated assertions as you want: xmpfilter.rb will not modify lines without the "# =>" marker. xmpfilter.rb can be used repeatedly as you add more assertions. Imagine you want to verify that @o.last(3) raises an ArgumentError. You can simply add one line marked with # => :

  ...
    assert_in_delta(3.14159265358979, @o.complex_computation, 0.0001)
    assert_equal(["baz", "bar"], @o.last(2))
    @o.last(3)                                     # =>
  end

and have it expanded by xmpfilter.rb:

  ...
    assert_in_delta(3.14159265358979, @o.complex_computation, 0.0001)
    assert_equal(["baz", "bar"], @o.last(2))
    assert_raise(ArgumentError){ @o.last(3) }
  end
Versions: [0.3.1 (2006-10-27)] [0.3.0 (2006-10-16)]

Edit this project (for project owner)

back to RAA top