Guest User

Untitled

a guest
Feb 20th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. =begin
  2. MicroTest Version 0.2 (c) by manveru
  3.  
  4. MicroTest is aiming to be a replacement to big monolithic tests and provides
  5. the perfect solution to most of my needs.
  6. If you don't like it - don't think about it :)
  7.  
  8. Example usage:
  9.  
  10. {
  11. :foo.to_s => 'foo',
  12. 'foo'.to_sym => :foo
  13. }.test
  14.  
  15. {
  16. :foo => 'foo',
  17. :bar => 'bar'
  18. }.test :to_s
  19.  
  20. {
  21. :foo => 'foo',
  22. 'bar' => :bar
  23. }.test do |test|
  24. test == test.to_sym ? test.to_s : test.to_sym
  25. end
  26.  
  27. TODO:
  28. * Something autotest-like, basic structure for doing that is already
  29. implemented, just have to integrate it...
  30.  
  31. * Maybe providing more customization of how the tests may look like
  32. atm i think it's a pretty good mix, but of course there is always
  33. room for improvment
  34. =end
  35.  
  36. require 'micro/opt'
  37.  
  38. module Micro
  39. module Test
  40. def test(method = nil, &block)
  41. return nil unless $microtest_run
  42.  
  43. errors, parameters = [], []
  44.  
  45. setup = delete :test_setup
  46. teardown = delete :test_teardown
  47. description = delete :description
  48.  
  49. $stdout.sync = true
  50. puts "[ #{description} ]".center(80, '-')
  51.  
  52. setup.call if setup
  53.  
  54. each do |input, expected|
  55. begin
  56. error = nil
  57. result = if block_given?
  58. yield input
  59. elsif method.nil?
  60. input
  61. else
  62. input.instance_eval method.to_s unless method.nil?
  63. end
  64. rescue Object => error # catch everything you can
  65. if error.class == expected
  66. result = error.class
  67. error = nil
  68. end
  69. end
  70.  
  71. if error || result != expected
  72. message_long = "#{result.inspect} != #{expected.inspect}"
  73. message_short = 'E'
  74. errors << error
  75. parameters << { :input => input, :result => result, :expected => expected }
  76. else
  77. message_long = "#{result.inspect} == #{expected.inspect}"
  78. message_short = '.'
  79. end
  80.  
  81. unless $microtest_silent
  82. $microtest_verbose ? puts(message_long) : print(message_short)
  83. end
  84. end
  85.  
  86. unless $microtest_silent || $microtest_micro
  87. unless $microtest_verbose
  88. print "\n"
  89. errors.zip(parameters).each do |error|
  90. error, params = error[0], error[1]
  91. puts "\nError while asserting " +
  92. "#{params[:input].inspect} == " +
  93. "#{params[:expected].inspect} #=> " +
  94. "#{params[:result].inspect}"
  95. if error
  96. puts error
  97. puts error.backtrace.map{|e| " #{e}"}.join("\n")
  98. else
  99. puts "Given : #{params[:input].inspect}"
  100. puts "Result : #{params[:result].inspect}"
  101. puts "Expected: #{params[:expected].inspect}"
  102. end
  103. end
  104. end
  105.  
  106. total, failed, success = size, parameters.size, (size - parameters.size)
  107.  
  108. puts (%{#{total} Tests. } +
  109. %{[#{failed}/#{total} (#{"%.2f" % ((failed.to_f/total)*100)}%) failed.] } +
  110. %{[#{success}/#{total} (#{"%.2f" % ((success.to_f/total)*100)}%) passed.]}).center(80, ' ')
  111. end
  112.  
  113. puts '' unless $microtest_silent
  114.  
  115. teardown.call if teardown
  116. end
  117. end
  118. end
  119.  
  120. class Hash
  121. include Micro::Test
  122. end
  123.  
  124. ruleset = {
  125. :help => lambda{
  126. puts %{Usage:
  127. ruby foo.rb test [silent|micro|minimal|verbose]
  128. run the ruby-file with the test-option to activate the tests
  129. Options:
  130. silent => no output at all
  131. micro => micro output, only: . = success; F = failure
  132. minimal => minimal output, gives you additional a little statistic
  133. verbose => verbose output, as much info as you can handle
  134. }
  135. exit;
  136. },
  137. :test => lambda{ $microtest_run = true },
  138. :micro => lambda{ $microtest_micro = true },
  139. :silent => lambda{ $microtest_silent = true },
  140. :minimal => lambda{ $microtest_minimal = true },
  141. :verbose => lambda{ $microtest_verbose = true }
  142. }
  143.  
  144. ruleset.each do |option, action|
  145. action.call if Micro::Opt.parse(ruleset.keys, ARGV).include? option
  146. end
Add Comment
Please, Sign In to add comment