Skip to content

Instantly share code, notes, and snippets.

@outoftime
Created January 29, 2009 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save outoftime/54846 to your computer and use it in GitHub Desktop.
Save outoftime/54846 to your computer and use it in GitHub Desktop.
Tiny framework for one-line tests in Test::Unit
# Tiny framework for writing one-line tests. Just call expect with a block. The
# block evaluates to true if the test passes and false if it doesn't.
#
# Example:
# class ExpectTest < Test::Unit::TestCase
# include Expect
#
# expect { 1 == 1 }
# expect { 1 == 2 }
# end
#
# Output:
# Loaded suite expect_test
# Started
# .F
# Finished in 0.00058 seconds.
#
# 1) Failure:
# test: (1 == 2) (SomeTest) [./expect.rb:20]:
# expected (1 == 2)
#
# 2 tests, 2 assertions, 1 failures, 0 errors
require 'rubygems'
require 'parse_tree'
require 'ruby2ruby'
module Expect
def self.last_statement_in_block(&block)
temp_class = Class.new
temp_class.module_eval do
define_method :block_method, &block
end
block_contents = Ruby2Ruby.translate(temp_class, :block_method).split("\n")
block_contents.pop
block_contents.last.gsub!(/^ /, '')
end
def self.included(base)
def base.expect(&block)
s = Expect.last_statement_in_block(&block)
define_method("test: #{s} ".to_sym) do
assert_block("expected #{s}", &block)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment