You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_interface.rb 2.0 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # frozen_string_literal: true
  2. require 'timeout'
  3. module Cucumber
  4. class Runtime
  5. module UserInterface
  6. attr_writer :visitor
  7. # Suspends execution and prompts +question+ to the console (STDOUT).
  8. # An operator (manual tester) can then enter a line of text and hit
  9. # <ENTER>. The entered text is returned, and both +question+ and
  10. # the result is added to the output using #puts.
  11. #
  12. # If you want a beep to happen (to grab the manual tester's attention),
  13. # just prepend ASCII character 7 to the question:
  14. #
  15. # ask("#{7.chr}How many cukes are in the external system?")
  16. #
  17. # If that doesn't issue a beep, you can shell out to something else
  18. # that makes a sound before invoking #ask.
  19. #
  20. def ask(question, timeout_seconds)
  21. $stdout.puts(question)
  22. $stdout.flush
  23. puts(question)
  24. answer = if Cucumber::JRUBY
  25. jruby_gets(timeout_seconds)
  26. else
  27. mri_gets(timeout_seconds)
  28. end
  29. raise("Waited for input for #{timeout_seconds} seconds, then timed out.") unless answer
  30. puts(answer)
  31. answer
  32. end
  33. # Embed +src+ of MIME type +mime_type+ into the output. The +src+ argument may
  34. # be a path to a file, or if it's an image it may also be a Base64 encoded image.
  35. # The embedded data may or may not be ignored, depending on what kind of formatter(s) are active.
  36. #
  37. def attach(src, media_type)
  38. @visitor.attach(src, media_type)
  39. end
  40. private
  41. def mri_gets(timeout_seconds)
  42. Timeout.timeout(timeout_seconds) do
  43. $stdin.gets
  44. end
  45. rescue Timeout::Error
  46. nil
  47. end
  48. def jruby_gets(timeout_seconds)
  49. answer = nil
  50. t = java.lang.Thread.new do
  51. answer = $stdin.gets
  52. end
  53. t.start
  54. t.join(timeout_seconds * 1000)
  55. answer
  56. end
  57. end
  58. end
  59. end

No Description

Contributors (1)