Guest User

Untitled

a guest
Feb 19th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # detective
  3. # Tests by asking the user questions
  4. #
  5. # NOTE: requires ruby 1.9.2 and the rainbow gem.
  6. #
  7. # Usage:
  8. # detective [list of ruby test files]
  9. #
  10. # Syntax:
  11. # describe "A test" do
  12. # instruct "Go to google"
  13. # ask("What is written on the first button?").expect("Google Search")
  14. # end
  15. #
  16. #
  17. require 'fiber'
  18. require 'rainbow'
  19.  
  20. module Detective
  21. # Ask question. e.g.
  22. # ask "Where were you last night?"
  23. def ask(question)
  24. @last_question = question
  25. @last_result = Fiber.yield [:ask, question]
  26. return self
  27. end
  28.  
  29. # Instruct the user. e.g.
  30. # instruct "You have the right to remain silent"
  31. def instruct(question)
  32. Fiber.yield [:instruct, question]
  33. return self
  34. end
  35.  
  36. # Expect a result from the last question. e.g.
  37. # ask("When did you last see M. Ratchett alive?").expect("last evening")
  38. def expect(result)
  39. if (@last_result||'').strip != result.to_s.downcase
  40. Fiber.yield [:fail, @last_question, @last_result, result]
  41. end
  42. end
  43.  
  44. # Describe a test. e.g.
  45. # describe("The suspect") { ask("How many fingers am i holding up?").expect(1) }
  46. def describe test, &block
  47. Fiber.yield [:describe, test]
  48. yield
  49. Fiber.yield [:describe_end, test]
  50. end
  51.  
  52. alias_method :it, :describe
  53. end
  54.  
  55. def run_suite(files)
  56. fib = Fiber.new do
  57. include Detective
  58. files.each { |f| require f }
  59. end
  60.  
  61. total_test = 0
  62. total_pass = 0
  63. test_results = []
  64.  
  65. begin
  66. result = nil
  67. describe_stack = []
  68. while (command = fib.resume(result)) and !command.nil? and command != :end
  69. result = nil
  70. case command[0]
  71. when :instruct
  72. if describe_stack[-1][1]
  73. puts "#{command[1]} [press RETURN to continue]"
  74. result = STDIN.readline
  75. end
  76. when :ask
  77. if describe_stack[-1][1]
  78. puts "#{command[1]}: "
  79. result = STDIN.readline.downcase
  80. end
  81. when :describe
  82. describe_stack << [command[1], true]
  83. puts "\n#{describe_stack.map{|d|d[0]}.join(' ')}.\n"
  84. total_test += 1
  85. when :describe_end
  86. test_results << ["#{describe_stack.map{|d|d[0]}.join(' ')}.", describe_stack[-1][1]]
  87. total_pass += 1 if describe_stack.pop[1]
  88. when :fail
  89. describe_stack[-1][1] = false
  90. end
  91. end
  92. rescue FiberError
  93. rescue Exception => e
  94. puts e.inspect
  95. puts e.backtrace
  96. end
  97.  
  98. puts "\n"
  99. test_results.reverse.each do |test|
  100. puts test[0].color(test[1] ? :green : :red)
  101. end
  102. puts "\n#{total_pass} / #{total_test} Tests passed".color(total_pass == total_test ? :green : :red)
  103.  
  104. exit total_pass == total_test ? 0 : 1
  105. end
  106.  
  107. run_suite(ARGV)
Add Comment
Please, Sign In to add comment