2 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
3 require 'rubygems/user_interaction'
5 class TestGemStreamUI < RubyGemTestCase
11 @tty = true unless defined? @tty
15 alias_method :isatty, :tty?
21 @cfg = Gem.configuration
29 @sui = Gem::StreamUI.new @in, @out, @err
34 expected_answer = "Arthur, King of the Britons"
35 @in.string = "#{expected_answer}\n"
36 actual_answer = @sui.ask("What is your name?")
37 assert_equal expected_answer, actual_answer
45 answer = @sui.ask("what is your favorite color?")
46 assert_equal nil, answer
50 def test_ask_yes_no_no_tty_with_default
54 answer = @sui.ask_yes_no("do coconuts migrate?", false)
55 assert_equal false, answer
57 answer = @sui.ask_yes_no("do coconuts migrate?", true)
58 assert_equal true, answer
62 def test_ask_yes_no_no_tty_without_default
66 assert_raises(Gem::OperationNotSupportedError) do
67 @sui.ask_yes_no("do coconuts migrate?")
72 def test_choose_from_list
76 result = @sui.choose_from_list 'which one?', %w[foo bar]
78 assert_equal ['foo', 0], result
79 assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
82 def test_choose_from_list_EOF
83 result = @sui.choose_from_list 'which one?', %w[foo bar]
85 assert_equal [nil, nil], result
86 assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
89 def test_proress_reporter_silent_nil
91 reporter = @sui.progress_reporter 10, 'hi'
92 assert_kind_of Gem::StreamUI::SilentProgressReporter, reporter
95 def test_proress_reporter_silent_false
97 reporter = @sui.progress_reporter 10, 'hi'
98 assert_kind_of Gem::StreamUI::SilentProgressReporter, reporter
99 assert_equal "", @out.string
102 def test_proress_reporter_simple
104 reporter = @sui.progress_reporter 10, 'hi'
105 assert_kind_of Gem::StreamUI::SimpleProgressReporter, reporter
106 assert_equal "hi\n", @out.string
109 def test_proress_reporter_verbose
111 reporter = @sui.progress_reporter 10, 'hi'
112 assert_kind_of Gem::StreamUI::VerboseProgressReporter, reporter
113 assert_equal "hi\n", @out.string