~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/io/console/test_io_console.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
begin
 
2
  require 'io/console'
 
3
  require 'test/unit'
 
4
  require 'pty'
 
5
rescue LoadError
 
6
end
 
7
 
 
8
class TestIO_Console < Test::Unit::TestCase
 
9
  def test_raw
 
10
    helper {|m, s|
 
11
      s.print "abc\n"
 
12
      assert_equal("abc\r\n", m.gets)
 
13
      s.raw {
 
14
        s.print "def\n"
 
15
        assert_equal("def\n", m.gets)
 
16
      }
 
17
      s.print "ghi\n"
 
18
      assert_equal("ghi\r\n", m.gets)
 
19
    }
 
20
  end
 
21
 
 
22
  def test_echo
 
23
    helper {|m, s|
 
24
      assert(s.echo?)
 
25
      m.print "a"
 
26
      assert_equal("a", m.readpartial(10))
 
27
    }
 
28
  end
 
29
 
 
30
  def test_noecho
 
31
    helper {|m, s|
 
32
      s.noecho {
 
33
        assert(!s.echo?)
 
34
        m.print "a"
 
35
        sleep 0.1
 
36
      }
 
37
      m.print "b"
 
38
      assert_equal("b", m.readpartial(10))
 
39
    }
 
40
  end
 
41
 
 
42
  def test_noecho2
 
43
    helper {|m, s|
 
44
      assert(s.echo?)
 
45
      m.print "a\n"
 
46
      sleep 0.1
 
47
      s.print "b\n"
 
48
      sleep 0.1
 
49
      assert_equal("a\r\nb\r\n", m.readpartial(10))
 
50
      assert_equal("a\n", s.readpartial(10))
 
51
      s.noecho {
 
52
        assert(!s.echo?)
 
53
        m.print "a\n"
 
54
        s.print "b\n"
 
55
        assert_equal("b\r\n", m.readpartial(10))
 
56
        assert_equal("a\n", s.readpartial(10))
 
57
      }
 
58
      assert(s.echo?)
 
59
      m.print "a\n"
 
60
      sleep 0.1
 
61
      s.print "b\n"
 
62
      sleep 0.1
 
63
      assert_equal("a\r\nb\r\n", m.readpartial(10))
 
64
      assert_equal("a\n", s.readpartial(10))
 
65
    }
 
66
  end
 
67
 
 
68
  def test_setecho
 
69
    helper {|m, s|
 
70
      assert(s.echo?)
 
71
      s.echo = false
 
72
      m.print "a"
 
73
      sleep 0.1
 
74
      s.echo = true
 
75
      m.print "b"
 
76
      assert_equal("b", m.readpartial(10))
 
77
    }
 
78
  end
 
79
 
 
80
  def test_setecho2
 
81
    helper {|m, s|
 
82
      assert(s.echo?)
 
83
      m.print "a\n"
 
84
      sleep 0.1
 
85
      s.print "b\n"
 
86
      sleep 0.1
 
87
      assert_equal("a\r\nb\r\n", m.readpartial(10))
 
88
      assert_equal("a\n", s.readpartial(10))
 
89
      s.echo = false
 
90
      assert(!s.echo?)
 
91
      m.print "a\n"
 
92
      s.print "b\n"
 
93
      assert_equal("b\r\n", m.readpartial(10))
 
94
      assert_equal("a\n", s.readpartial(10))
 
95
      s.echo = true
 
96
      assert(s.echo?)
 
97
      m.print "a\n"
 
98
      sleep 0.1
 
99
      s.print "b\n"
 
100
      sleep 0.1
 
101
      assert_equal("a\r\nb\r\n", m.readpartial(10))
 
102
      assert_equal("a\n", s.readpartial(10))
 
103
    }
 
104
  end
 
105
 
 
106
  def test_iflush
 
107
    helper {|m, s|
 
108
      m.print "a"
 
109
      s.iflush
 
110
      m.print "b\n"
 
111
      assert_equal("b\n", s.readpartial(10))
 
112
    }
 
113
  end
 
114
 
 
115
  def test_oflush
 
116
    helper {|m, s|
 
117
      s.print "a"
 
118
      s.oflush # oflush may be issued after "a" is already sent.
 
119
      s.print "b"
 
120
      assert_include(["b", "ab"], m.readpartial(10))
 
121
    }
 
122
  end
 
123
 
 
124
  def test_ioflush
 
125
    helper {|m, s|
 
126
      m.print "a"
 
127
      s.ioflush
 
128
      m.print "b\n"
 
129
      assert_equal("b\n", s.readpartial(10))
 
130
    }
 
131
  end
 
132
 
 
133
  def test_ioflush2
 
134
    helper {|m, s|
 
135
      s.print "a"
 
136
      s.ioflush # ioflush may be issued after "a" is already sent.
 
137
      s.print "b"
 
138
      assert_include(["b", "ab"], m.readpartial(10))
 
139
    }
 
140
  end
 
141
 
 
142
  def test_winsize
 
143
    helper {|m, s|
 
144
      begin
 
145
        assert_equal([0, 0], s.winsize)
 
146
      rescue Errno::EINVAL # OpenSolaris 2009.06 TIOCGWINSZ causes Errno::EINVAL before TIOCSWINSZ.
 
147
      end
 
148
    }
 
149
  end
 
150
 
 
151
  if IO.console
 
152
    def test_sync
 
153
      assert(IO.console.sync, "console should be unbuffered")
 
154
    end
 
155
  else
 
156
    def test_sync
 
157
      r, w, pid = PTY.spawn(EnvUtil.rubybin, "-rio/console", "-e", "p IO.console.class")
 
158
      con = r.gets.chomp
 
159
      Process.wait(pid)
 
160
      assert_match("File", con)
 
161
    end
 
162
  end
 
163
 
 
164
  private
 
165
  def helper
 
166
    m, s = PTY.open
 
167
  rescue RuntimeError
 
168
    skip $!
 
169
  else
 
170
    yield m, s
 
171
  ensure
 
172
    m.close if m
 
173
    s.close if s
 
174
  end
 
175
end if defined?(PTY) and defined?(IO::console)
 
176
 
 
177
class TestIO_Console < Test::Unit::TestCase
 
178
  require_relative '../../ruby/envutil'
 
179
 
 
180
  case
 
181
  when Process.respond_to?(:daemon)
 
182
    noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"]
 
183
  when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty?
 
184
    dir, base = File.split(EnvUtil.rubybin)
 
185
    noctty = [File.join(dir, base.sub(/ruby/, rubyw))]
 
186
  end
 
187
 
 
188
  if noctty
 
189
    require 'tempfile'
 
190
    NOCTTY = noctty
 
191
    def test_noctty
 
192
      t = Tempfile.new("console")
 
193
      t.close
 
194
      t2 = Tempfile.new("console")
 
195
      t2.close
 
196
      cmd = NOCTTY + [
 
197
        '-rio/console',
 
198
        '-e', 'open(ARGV[0], "w") {|f| f.puts IO.console.inspect}',
 
199
        '-e', 'File.unlink(ARGV[1])',
 
200
        '--', t.path, t2.path]
 
201
      system(*cmd)
 
202
      sleep 0.1 while File.exist?(t2.path)
 
203
      t.open
 
204
      assert_equal("nil", t.gets.chomp)
 
205
    ensure
 
206
      t.close! if t and !t.closed?
 
207
      t2.close!
 
208
    end
 
209
  end
 
210
end if defined?(IO.console)