unitirb


C

Caleb Clausen

I'm releasing this little script I wrote called unitirb in the hopes
that other people might find it useful. It's a hacky little
interactive unit test creator. I like it, anyway. Let's call this one
0.1.

Here's how to use it:

% unitirb -r rubylib testname.rb

unitirb starts up a regular irb session, and logs the commands and
their output to a test file named on the command line. The file can be
an existing unitirb test, or a new one is created if none exists. Once
you exit the irb session, you can re-run the session like this:

% ruby testname.rb

The commands are re-run and the output compared to what was originally
obtained. If there's any difference, the test fails.

Unitirb session scripts use Test::Unit, but they aren't too pretty.

There are lots of ways to fool unitirb, I'm sure. I've tried to handle
the most obvious and common stuff, but I can't get all the cases.
Here's one I didn't: rand. Don't do stuff like that, this is just a
stupid little script.

If you accidently hit return at the wrong time and cause a syntax
error, the errored code will be copied into the session anyway...
you'll have to remove it by hand before your test works.

I'm using (a mangled form of) #inspect to dump the results of commands
in a form that can be re-read later in another process. It would be
nice to use yaml for this instead, but I found that yaml doesn't
handle a bunch of classes that I want to work in unitirb.


unitirb:

#!/usr/bin/env ruby
#
# unitirb.rb - interactive ruby unit test creation
# =09$Release Version: 0.7.3 $
# =09$Revision: 1.1 $
# =09$Date: 2005/6/6 02:00:18 $
# Copyright 2005 by Caleb Clausen
# based on irb.rb
# =09by Keiju ISHITSUKA([email protected])
#

ARGV.push "unit" if /^-/=3D=3D=3DARGV.last

def_inspect=3D<<end
class Object
#define a more stable version of inspect (for testing purposes)
alias pristine_inspect inspect
def inspect
res=3Dpristine_inspect
res[/^#</] or return res
res=3D["#<",self.class,": ",instance_variables.sort.collect{|v|=20
[v,"=3D",instance_variable_get(v).inspect," "]
}]
res.last.pop
res.push('>')
res.to_s
end
end
class MatchData
def inspect
'#<MatchData: >'
end
end
end

name=3DARGV.pop
/\.rb$/=3D=3D=3Dname or name=3D"test_#{name}.rb"
print "#{File.exists?(name)?"Append":"Creat"}ing file #{name}\n"
$UnitCode=3DFile.open(name,'a')
$UnitCode.pos=3D=3D0 and $UnitCode.print "require 'test/unit'\n",def_inspec=
t
$SuiteName=3D"T#{$UnitCode.pos}"
$UnitCode.print <<end
class #{$SuiteName} < Test::Unit::TestCase
define_method :test_unnamed do begin
end

END {=20
$UnitCode.print %{ rescue Test::Unit::AssertionFailedError:
raise\n rescue e: assert false, "Exception: #{ e.inspect}";\n =20
end end\n end\n}
$UnitCode.close=20
}

eval def_inspect

require "irb"

module IRB
class WorkSpace
alias evaluate__nonunit evaluate
def evaluate(context,code,file=3D__FILE__,line=3D__LINE__)
/^(__? =3D |[\s\t\v\f\r\n]*exit[\s\t\v\f\r\n]*$)/=3D=3D=3Dcode and
return(evaluate__nonunit context,code,file,line)
if /^[\s\t\v\f\r\n]*require(_gem)?[^a-zA-Z_0-9?!]/=3D=3D=3Dcode
$UnitCode.print(" #{code}")
return(evaluate__nonunit context,code,file,line)
end
$UnitCode.print(" _=3D#{code}")
result=3Devaluate__nonunit context,code,file,line
$UnitCode.print(' assert_equal
"'+result.inspect.gsub(/([\\"])/){|k|"\\"+k}+'"'+", _.inspect\n\n")
return result
end
end
end

if __FILE__ =3D=3D $0
IRB.start(__FILE__)
else
# check -e option
if /^-e$/ =3D~ $0
IRB.start(__FILE__)
else
IRB.setup(__FILE__)
end
end
 
Ad

Advertisements


Top