Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =begin
- MicroTest Version 0.2 (c) by manveru
- MicroTest is aiming to be a replacement to big monolithic tests and provides
- the perfect solution to most of my needs.
- If you don't like it - don't think about it :)
- Example usage:
- {
- :foo.to_s => 'foo',
- 'foo'.to_sym => :foo
- }.test
- {
- :foo => 'foo',
- :bar => 'bar'
- }.test :to_s
- {
- :foo => 'foo',
- 'bar' => :bar
- }.test do |test|
- test == test.to_sym ? test.to_s : test.to_sym
- end
- TODO:
- * Something autotest-like, basic structure for doing that is already
- implemented, just have to integrate it...
- * Maybe providing more customization of how the tests may look like
- atm i think it's a pretty good mix, but of course there is always
- room for improvment
- =end
- require 'micro/opt'
- module Micro
- module Test
- def test(method = nil, &block)
- return nil unless $microtest_run
- errors, parameters = [], []
- setup = delete :test_setup
- teardown = delete :test_teardown
- description = delete :description
- $stdout.sync = true
- puts "[ #{description} ]".center(80, '-')
- setup.call if setup
- each do |input, expected|
- begin
- error = nil
- result = if block_given?
- yield input
- elsif method.nil?
- input
- else
- input.instance_eval method.to_s unless method.nil?
- end
- rescue Object => error # catch everything you can
- if error.class == expected
- result = error.class
- error = nil
- end
- end
- if error || result != expected
- message_long = "#{result.inspect} != #{expected.inspect}"
- message_short = 'E'
- errors << error
- parameters << { :input => input, :result => result, :expected => expected }
- else
- message_long = "#{result.inspect} == #{expected.inspect}"
- message_short = '.'
- end
- unless $microtest_silent
- $microtest_verbose ? puts(message_long) : print(message_short)
- end
- end
- unless $microtest_silent || $microtest_micro
- unless $microtest_verbose
- print "\n"
- errors.zip(parameters).each do |error|
- error, params = error[0], error[1]
- puts "\nError while asserting " +
- "#{params[:input].inspect} == " +
- "#{params[:expected].inspect} #=> " +
- "#{params[:result].inspect}"
- if error
- puts error
- puts error.backtrace.map{|e| " #{e}"}.join("\n")
- else
- puts "Given : #{params[:input].inspect}"
- puts "Result : #{params[:result].inspect}"
- puts "Expected: #{params[:expected].inspect}"
- end
- end
- end
- total, failed, success = size, parameters.size, (size - parameters.size)
- puts (%{#{total} Tests. } +
- %{[#{failed}/#{total} (#{"%.2f" % ((failed.to_f/total)*100)}%) failed.] } +
- %{[#{success}/#{total} (#{"%.2f" % ((success.to_f/total)*100)}%) passed.]}).center(80, ' ')
- end
- puts '' unless $microtest_silent
- teardown.call if teardown
- end
- end
- end
- class Hash
- include Micro::Test
- end
- ruleset = {
- :help => lambda{
- puts %{Usage:
- ruby foo.rb test [silent|micro|minimal|verbose]
- run the ruby-file with the test-option to activate the tests
- Options:
- silent => no output at all
- micro => micro output, only: . = success; F = failure
- minimal => minimal output, gives you additional a little statistic
- verbose => verbose output, as much info as you can handle
- }
- exit;
- },
- :test => lambda{ $microtest_run = true },
- :micro => lambda{ $microtest_micro = true },
- :silent => lambda{ $microtest_silent = true },
- :minimal => lambda{ $microtest_minimal = true },
- :verbose => lambda{ $microtest_verbose = true }
- }
- ruleset.each do |option, action|
- action.call if Micro::Opt.parse(ruleset.keys, ARGV).include? option
- end
Add Comment
Please, Sign In to add comment