PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/io/console/test_io_console.rb

https://github.com/ahwuyeah/ruby
Ruby | 306 lines | 286 code | 20 blank | 0 comment | 9 complexity | c4df22ebbe7c9a8e7e105d4a7ead3381 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, Unlicense, GPL-2.0
  1. begin
  2. require 'io/console'
  3. require 'test/unit'
  4. require 'pty'
  5. rescue LoadError
  6. end
  7. require_relative '../../ruby/envutil'
  8. class TestIO_Console < Test::Unit::TestCase
  9. Bug6116 = '[ruby-dev:45309]'
  10. def test_raw
  11. helper {|m, s|
  12. s.print "abc\n"
  13. assert_equal("abc\r\n", m.gets)
  14. assert_send([s, :echo?])
  15. s.raw {
  16. assert_not_send([s, :echo?], Bug6116)
  17. s.print "def\n"
  18. assert_equal("def\n", m.gets)
  19. }
  20. assert_send([s, :echo?])
  21. s.print "ghi\n"
  22. assert_equal("ghi\r\n", m.gets)
  23. }
  24. end
  25. def test_raw_minchar
  26. helper {|m, s|
  27. len = 0
  28. assert_equal([nil, 0], [s.getch(min: 0), len])
  29. main = Thread.current
  30. go = false
  31. th = Thread.start {
  32. len += 1
  33. m.print("a")
  34. m.flush
  35. sleep 0.01 until go and main.stop?
  36. len += 10
  37. m.print("1234567890")
  38. m.flush
  39. }
  40. begin
  41. assert_equal(["a", 1], [s.getch(min: 1), len])
  42. go = true
  43. assert_equal(["1", 11], [s.getch, len])
  44. ensure
  45. th.join
  46. end
  47. }
  48. end
  49. def test_raw_timeout
  50. helper {|m, s|
  51. len = 0
  52. assert_equal([nil, 0], [s.getch(min: 0, time: 0.1), len])
  53. main = Thread.current
  54. th = Thread.start {
  55. sleep 0.01 until main.stop?
  56. len += 2
  57. m.print("ab")
  58. }
  59. begin
  60. assert_equal(["a", 2], [s.getch(min: 1, time: 1), len])
  61. assert_equal(["b", 2], [s.getch(time: 1), len])
  62. ensure
  63. th.join
  64. end
  65. }
  66. end
  67. def test_cooked
  68. helper {|m, s|
  69. assert_send([s, :echo?])
  70. s.raw {
  71. s.print "abc\n"
  72. assert_equal("abc\n", m.gets)
  73. assert_not_send([s, :echo?], Bug6116)
  74. s.cooked {
  75. assert_send([s, :echo?])
  76. s.print "def\n"
  77. assert_equal("def\r\n", m.gets)
  78. }
  79. assert_not_send([s, :echo?], Bug6116)
  80. }
  81. assert_send([s, :echo?])
  82. s.print "ghi\n"
  83. assert_equal("ghi\r\n", m.gets)
  84. }
  85. end
  86. def test_echo
  87. helper {|m, s|
  88. assert_send([s, :echo?])
  89. m.print "a"
  90. assert_equal("a", m.readpartial(10))
  91. }
  92. end
  93. def test_noecho
  94. helper {|m, s|
  95. s.noecho {
  96. assert_not_send([s, :echo?])
  97. m.print "a"
  98. sleep 0.1
  99. }
  100. m.print "b"
  101. assert_equal("b", m.readpartial(10))
  102. }
  103. end
  104. def test_noecho2
  105. helper {|m, s|
  106. assert_send([s, :echo?])
  107. m.print "a\n"
  108. sleep 0.1
  109. s.print "b\n"
  110. sleep 0.1
  111. assert_equal("a\r\nb\r\n", m.readpartial(10))
  112. assert_equal("a\n", s.readpartial(10))
  113. s.noecho {
  114. assert_not_send([s, :echo?])
  115. m.print "a\n"
  116. s.print "b\n"
  117. assert_equal("b\r\n", m.readpartial(10))
  118. assert_equal("a\n", s.readpartial(10))
  119. }
  120. assert_send([s, :echo?])
  121. m.print "a\n"
  122. sleep 0.1
  123. s.print "b\n"
  124. sleep 0.1
  125. assert_equal("a\r\nb\r\n", m.readpartial(10))
  126. assert_equal("a\n", s.readpartial(10))
  127. }
  128. end
  129. def test_setecho
  130. helper {|m, s|
  131. assert_send([s, :echo?])
  132. s.echo = false
  133. m.print "a"
  134. sleep 0.1
  135. s.echo = true
  136. m.print "b"
  137. assert_equal("b", m.readpartial(10))
  138. }
  139. end
  140. def test_setecho2
  141. helper {|m, s|
  142. assert_send([s, :echo?])
  143. m.print "a\n"
  144. sleep 0.1
  145. s.print "b\n"
  146. sleep 0.1
  147. assert_equal("a\r\nb\r\n", m.readpartial(10))
  148. assert_equal("a\n", s.readpartial(10))
  149. s.echo = false
  150. assert_not_send([s, :echo?])
  151. m.print "a\n"
  152. s.print "b\n"
  153. assert_equal("b\r\n", m.readpartial(10))
  154. assert_equal("a\n", s.readpartial(10))
  155. s.echo = true
  156. assert_send([s, :echo?])
  157. m.print "a\n"
  158. sleep 0.1
  159. s.print "b\n"
  160. sleep 0.1
  161. assert_equal("a\r\nb\r\n", m.readpartial(10))
  162. assert_equal("a\n", s.readpartial(10))
  163. }
  164. end
  165. def test_iflush
  166. helper {|m, s|
  167. m.print "a"
  168. s.iflush
  169. m.print "b\n"
  170. assert_equal("b\n", s.readpartial(10))
  171. }
  172. end
  173. def test_oflush
  174. helper {|m, s|
  175. s.print "a"
  176. s.oflush # oflush may be issued after "a" is already sent.
  177. s.print "b"
  178. assert_include(["b", "ab"], m.readpartial(10))
  179. }
  180. end
  181. def test_ioflush
  182. helper {|m, s|
  183. m.print "a"
  184. s.ioflush
  185. m.print "b\n"
  186. assert_equal("b\n", s.readpartial(10))
  187. }
  188. end
  189. def test_ioflush2
  190. helper {|m, s|
  191. s.print "a"
  192. s.ioflush # ioflush may be issued after "a" is already sent.
  193. s.print "b"
  194. assert_include(["b", "ab"], m.readpartial(10))
  195. }
  196. end
  197. def test_winsize
  198. helper {|m, s|
  199. begin
  200. assert_equal([0, 0], s.winsize)
  201. rescue Errno::EINVAL # OpenSolaris 2009.06 TIOCGWINSZ causes Errno::EINVAL before TIOCSWINSZ.
  202. end
  203. }
  204. end
  205. if IO.console
  206. def test_sync
  207. assert(IO.console.sync, "console should be unbuffered")
  208. end
  209. else
  210. def test_sync
  211. r, w, pid = PTY.spawn(EnvUtil.rubybin, "-rio/console", "-e", "p IO.console.class")
  212. rescue RuntimeError
  213. skip $!
  214. else
  215. con = r.gets.chomp
  216. Process.wait(pid)
  217. assert_match("File", con)
  218. ensure
  219. r.close if r
  220. w.close if w
  221. end
  222. end
  223. private
  224. def helper
  225. m, s = PTY.open
  226. rescue RuntimeError
  227. skip $!
  228. else
  229. yield m, s
  230. ensure
  231. m.close if m
  232. s.close if s
  233. end
  234. end if defined?(PTY) and defined?(IO::console)
  235. class TestIO_Console < Test::Unit::TestCase
  236. case
  237. when Process.respond_to?(:daemon)
  238. noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"]
  239. when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty?
  240. dir, base = File.split(EnvUtil.rubybin)
  241. noctty = [File.join(dir, base.sub(/ruby/, rubyw))]
  242. end
  243. if noctty
  244. require 'tempfile'
  245. NOCTTY = noctty
  246. def test_noctty
  247. t = Tempfile.new("console")
  248. t.close
  249. t2 = Tempfile.new("console")
  250. t2.close
  251. cmd = [*NOCTTY[1..-1],
  252. '-e', 'open(ARGV[0], "w") {|f|',
  253. '-e', 'STDOUT.reopen(f)',
  254. '-e', 'STDERR.reopen(f)',
  255. '-e', 'require "io/console"',
  256. '-e', 'f.puts IO.console.inspect',
  257. '-e', 'f.flush',
  258. '-e', 'File.unlink(ARGV[1])',
  259. '-e', '}',
  260. '--', t.path, t2.path]
  261. assert_ruby_status(cmd, rubybin: NOCTTY[0])
  262. 30.times do
  263. break unless File.exist?(t2.path)
  264. sleep 0.1
  265. end
  266. t.open
  267. assert_equal("nil", t.gets(nil).chomp)
  268. ensure
  269. t.close! if t and !t.closed?
  270. t2.close!
  271. end
  272. end
  273. end if defined?(IO.console)
  274. class TestIO_Console < Test::Unit::TestCase
  275. def test_stringio_getch
  276. assert_separately %w"--disable=gems -rstringio -rio/console", %q{
  277. assert_operator(StringIO, :method_defined?, :getch)
  278. }
  279. assert_separately %w"--disable=gems -rio/console -rstringio", %q{
  280. assert_operator(StringIO, :method_defined?, :getch)
  281. }
  282. assert_separately %w"--disable=gems -rstringio", %q{
  283. assert_not_operator(StringIO, :method_defined?, :getch)
  284. }
  285. end
  286. end