PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/test_kernel.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 850 lines | 729 code | 88 blank | 33 comment | 39 complexity | fcf242f94e87b9f95d944e35c056b38e MD5 | raw file
  1. require 'test/unit'
  2. require 'rbconfig'
  3. require 'test/test_helper'
  4. require 'pathname'
  5. class TestKernel < Test::Unit::TestCase
  6. include TestHelper
  7. def log(msg)
  8. $stderr.puts msg if $VERBOSE
  9. end
  10. TESTAPP_DIR = File.expand_path(File.join(File.dirname(__FILE__), 'testapp'))
  11. if (WINDOWS)
  12. # the testapp.exe exists for sure, it is pre-built
  13. TESTAPP_EXISTS = true
  14. else
  15. Dir.chdir(TESTAPP_DIR) {
  16. file = File.join(TESTAPP_DIR, 'testapp')
  17. `gcc testapp.c -o testapp` unless (File.exist?(file))
  18. TESTAPP_EXISTS = File.exist?(file)
  19. }
  20. end
  21. TESTAPP_REL = File.join(File.join(File.dirname(__FILE__), 'testapp'), 'testapp')
  22. TESTAPP_REL_NONORM = File.join(File.join(File.join(File.join(File.dirname(__FILE__), 'testapp'), '..'), 'testapp'), 'testapp')
  23. TESTAPP = File.expand_path(TESTAPP_REL)
  24. TESTAPP_NONORM = File.join(File.join(File.join(TESTAPP_DIR, '..'), 'testapp'), 'testapp')
  25. # TODO: path with spaces!
  26. TESTAPPS = [] << TESTAPP_REL << TESTAPP << TESTAPP_REL_NONORM << TESTAPP_NONORM
  27. if WINDOWS
  28. TESTAPPS << TESTAPP_REL + ".exe"
  29. TESTAPPS << (TESTAPP_REL + ".exe").gsub('/', '\\')
  30. TESTAPPS << TESTAPP_REL + ".bat"
  31. TESTAPPS << (TESTAPP_REL + ".bat").gsub('/', '\\')
  32. TESTAPPS << TESTAPP + ".exe"
  33. TESTAPPS << (TESTAPP + ".exe").gsub('/', '\\')
  34. TESTAPPS << TESTAPP + ".bat"
  35. TESTAPPS << (TESTAPP + ".bat").gsub('/', '\\')
  36. end
  37. def test_stuff_seemingly_out_of_context
  38. assert !Kernel.eval("defined? some_unknown_variable")
  39. assert_equal nil, true && defined?(unknownConstant)
  40. # JRUBY-117 - to_a should be public
  41. unless RUBY_VERSION =~ /1\.9/
  42. assert_equal ["to_a"], Object.public_instance_methods.grep("to_a")
  43. end
  44. # JRUBY-117 - remove_instance_variable should be private
  45. if RUBY_VERSION =~ /1\.9/
  46. assert_equal [:remove_instance_variable], Object.private_instance_methods.grep(:remove_instance_variable)
  47. else
  48. assert_equal ["remove_instance_variable"], Object.private_instance_methods.grep("remove_instance_variable")
  49. end
  50. end
  51. def test_Array_should_return_empty_array_for_nil
  52. assert_equal [], Kernel.Array(nil)
  53. end
  54. class ToAryReturnsAnArray; def to_ary() [1] end; end
  55. class ToAryReturnsAnInteger; def to_ary() 2 end; end
  56. def test_Array_should_use_to_ary_and_make_sure_that_it_returns_either_array_or_nil
  57. assert_equal [1], Kernel.Array(ToAryReturnsAnArray.new)
  58. assert_raises(TypeError) { Kernel.Array(ToAryReturnsAnInteger.new) }
  59. end
  60. class BothToAryAndToADefined; def to_ary() [3] end; def to_a() raise "to_a() should not be called" end; end
  61. class OnlyToADefined; def to_a() [4] end; end
  62. def test_Array_should_fallback_on_to_a_if_to_ary_is_not_defined
  63. assert_equal [3], Kernel.Array(BothToAryAndToADefined.new)
  64. assert_equal [4], Kernel.Array(OnlyToADefined.new)
  65. end
  66. class NeitherToANorToAryDefined; end
  67. def test_Array_should_return_array_containing_argument_if_the_argument_has_neither_to_ary_nor_to_a
  68. assert_equal [1], Kernel.Array(1)
  69. assert_equal [:foo], Kernel.Array(:foo)
  70. obj = NeitherToANorToAryDefined.new
  71. assert_equal [obj], Kernel.Array(obj)
  72. end
  73. class ToAryReturnsNil; def to_ary() nil end; end
  74. def test_Array_should_return_array_containing_arg_if_to_ary_returns_nil
  75. obj = ToAryReturnsNil.new
  76. assert_equal [obj], Kernel.Array(obj)
  77. end
  78. def test_Integer
  79. assert_equal 10, Kernel.Integer("0xA")
  80. assert_equal 8, Kernel.Integer("010")
  81. assert_equal 2, Kernel.Integer("0b10")
  82. assert_raises(ArgumentError) { Kernel.Integer("abc") }
  83. assert_raises(ArgumentError) { Kernel.Integer("x10") }
  84. assert_raises(ArgumentError) { Kernel.Integer("xxxx10000000000000000000000000000000000000000000000000000") }
  85. end
  86. def test_Float
  87. assert_equal 1.0, Kernel.Float("1")
  88. assert_equal 10.0, Kernel.Float("1e1")
  89. assert_raises(ArgumentError) { Kernel.Float("abc") }
  90. assert_raises(ArgumentError) { Kernel.Float("x10") }
  91. assert_raises(ArgumentError) { Kernel.Float("xxxx10000000000000000000000000000000000000000000000000000") }
  92. end
  93. # String
  94. # URI
  95. # `
  96. # abort
  97. # at_exit
  98. # binding
  99. # block_given?
  100. class CheckBlockGiven; def self.go() block_given? end; end
  101. def test_iterator?
  102. assert !(Kernel.block_given?)
  103. assert(CheckBlockGiven.go { true })
  104. assert(!CheckBlockGiven.go)
  105. assert(!CheckBlockGiven.go(&Proc.new))
  106. end
  107. # callcc
  108. # caller
  109. def test_catch_throw
  110. been_where_it_shouldnt = false
  111. catch :fred do throw :fred; fail("shouldn't get here") end
  112. assert(!been_where_it_shouldnt)
  113. if RUBY_VERSION =~ /1\.9/
  114. ex = ArgumentError
  115. else
  116. ex = NameError
  117. end
  118. assert_raises (ex) do
  119. catch :fred do throw :wilma end
  120. end
  121. end
  122. def test_throw_should_bubble_up_to_the_right_catch
  123. been_at_fred2 = false
  124. catch :fred1 do
  125. catch :fred2 do
  126. catch :fred3 do
  127. catch :fred4 do
  128. throw :fred2
  129. fail("should have jumped to fred2 catch")
  130. end
  131. fail("should have jumped to fred2 catch")
  132. end
  133. end
  134. been_at_fred2 = true
  135. end
  136. assert been_at_fred2
  137. end
  138. def test_invalid_throw_after_inner_catch_should_unwind_the_stack_all_the_way_to_the_top
  139. been_at_fred1 = false
  140. been_at_fred2 = false
  141. if RUBY_VERSION =~ /1\.9/
  142. ex = ArgumentError
  143. else
  144. ex = NameError
  145. end
  146. assert_raises(ex) do
  147. catch :fred1 do
  148. catch :fred2 do
  149. catch :fred3 do
  150. throw :fred2
  151. test_fail("should have jumped to after fred2 catch")
  152. end
  153. test_fail("should have jumped to after fred2 catch")
  154. end
  155. been_at_fred2 = true
  156. throw :wilma
  157. end
  158. been_at_fred1 = true
  159. end
  160. assert been_at_fred2
  161. assert !been_at_fred1
  162. end
  163. def throw_outside_of_any_block_shoudl_raise_an_error
  164. assert_raises (NameError) { throw :wilma }
  165. end
  166. def test_catch_stack_should_be_cleaned_up
  167. if RUBY_VERSION =~ /1\.9/
  168. ex = ArgumentError
  169. else
  170. ex = NameError
  171. end
  172. assert_raises(ex) do
  173. catch :fred1 do
  174. catch :fred2 do
  175. catch :fred3 do
  176. end
  177. end
  178. end
  179. throw :fred2
  180. fail "catch stack should have been cleaned up"
  181. end
  182. end
  183. # chomp
  184. # chomp!
  185. # JRUBY-2527
  186. unless RUBY_VERSION =~ /1\.9/
  187. def test_chomp_no_block_doesnt_break
  188. $_ = "test"
  189. assert_equal("test", chomp)
  190. assert_equal("te", chomp("st"))
  191. $_ = "test"
  192. chomp!
  193. assert_equal("test", $_)
  194. chomp!("st")
  195. assert_equal("te", $_)
  196. end
  197. end
  198. # chop
  199. # chop!
  200. # eval
  201. def test_eval_should_use_local_variable_defined_in_parent_scope
  202. x = 1
  203. assert_equal 1, Kernel.eval('x')
  204. Kernel.eval("x = 2")
  205. assert_equal 2, x
  206. end
  207. def test_eval_should_not_bring_local_variables_defined_in_its_input_to_parent_scope
  208. existing_variable = 0
  209. another_variable = 1
  210. assert !(defined? new_variable)
  211. Kernel.eval("new_variable = another_variable = existing_variable")
  212. assert_equal 0, existing_variable
  213. assert_equal 0, another_variable
  214. assert !(defined? new_variable)
  215. end
  216. # exec
  217. # should not really work, since there is no fork
  218. # exit
  219. # exit!
  220. # fail
  221. # fork
  222. def test_format
  223. assert_equal "Hello, world", Kernel.format("Hello, %s", "world")
  224. assert_raises(TypeError) { Kernel.format("%01.3f", nil) }
  225. end
  226. # getc
  227. # gets
  228. # global_variables
  229. # gsub
  230. # gsub!
  231. class CheckIterator; def self.go() iterator? end; end
  232. def test_iterator?
  233. assert !(Kernel.iterator?)
  234. assert(CheckIterator.go { true })
  235. assert(!CheckIterator.go)
  236. end
  237. # lambda
  238. # load
  239. class ToStrPointToRequireTarget
  240. attr_accessor :target_required
  241. def to_str() "#{File.dirname(__FILE__)}/require_target.rb" end
  242. end
  243. def test_load_should_call_to_str_on_arg
  244. $target_required = false
  245. Kernel.load ToStrPointToRequireTarget.new
  246. assert $target_required
  247. assert_raises(TypeError) { Kernel.load Object.new }
  248. end
  249. def test_shall_load_from_load_path
  250. tmp = ENV["TEMP"] || ENV["TMP"] || ENV["TMPDIR"] || "/tmp"
  251. Dir.chdir(tmp) do
  252. load_path_save = $LOAD_PATH.dup
  253. begin
  254. File.open(File.expand_path('.') +'/file_to_be_loaded','w' ) do |f|
  255. f.puts "raise"
  256. end
  257. $LOAD_PATH.delete_if{|dir| dir=='.'}
  258. assert_raise(LoadError) {
  259. load 'file_to_be_loaded'
  260. }
  261. ensure
  262. File.delete(File.expand_path('.') +'/file_to_be_loaded')
  263. $LOAD_PATH.clear
  264. $LOAD_PATH.concat(load_path_save)
  265. end
  266. end
  267. end
  268. def test_local_variables
  269. if RbConfig::CONFIG['ruby_install_name'] == 'jruby'
  270. if RUBY_VERSION =~ /1\.9/
  271. a = lambda do
  272. assert_equal [:a, :b], local_variables
  273. b = 1
  274. assert_equal [:a, :b], local_variables
  275. end
  276. else
  277. a = lambda do
  278. assert_equal %w(a b), local_variables
  279. b = 1
  280. assert_equal %w(a b), local_variables
  281. end
  282. end
  283. a.call
  284. else
  285. # This behaves like MRI 1.9, and fails on 1.8, so, skip it
  286. end
  287. end
  288. # loop
  289. # method_missing
  290. # open
  291. # p
  292. # print
  293. def test_printf_should_raise_argument_error_when_asked_to_format_string_as_a_number
  294. assert_raises(ArgumentError) { Kernel.printf "%d", 's' }
  295. end
  296. # proc
  297. # putc
  298. # puts
  299. def test_raise
  300. assert_raises(RuntimeError) { Kernel.raise }
  301. assert_raises(StandardError) { Kernel.raise(StandardError) }
  302. assert_raises(TypeError) { Kernel.raise(TypeError.new) }
  303. begin
  304. Kernel.raise(StandardError, 'oops')
  305. fail "shouldn't get here!"
  306. rescue => e1
  307. assert e1.is_a?(StandardError)
  308. assert_equal 'oops', e1.message
  309. end
  310. begin
  311. Kernel.raise('oops')
  312. fail "shouldn't get here!"
  313. rescue => e2
  314. assert e2.is_a?(StandardError)
  315. assert_equal 'oops', e2.message
  316. end
  317. end
  318. # JRUBY-2696
  319. def test_raise_in_debug_mode
  320. require 'stringio'
  321. old_debug, $DEBUG = $DEBUG, true
  322. # by Exception Class
  323. $stderr = StringIO.new
  324. raise StandardError rescue nil
  325. tobe = "Exception `StandardError' at #{__FILE__}:#{__LINE__ - 1} - StandardError"
  326. assert_equal(tobe, $stderr.string.split("\n")[0])
  327. # by String
  328. $stderr.reopen
  329. raise "TEST_ME" rescue nil
  330. tobe = "Exception `RuntimeError' at #{__FILE__}:#{__LINE__ - 1} - TEST_ME"
  331. assert_equal(tobe, $stderr.string.split("\n")[0])
  332. # by Exception
  333. $stderr.reopen
  334. raise RuntimeError.new("TEST_ME") rescue nil
  335. tobe = "Exception `RuntimeError' at #{__FILE__}:#{__LINE__ - 1} - TEST_ME"
  336. assert_equal(tobe, $stderr.string.split("\n")[0])
  337. # by re-raise
  338. $stderr.reopen
  339. raise "TEST_ME" rescue raise rescue nil
  340. tobe = "Exception `RuntimeError' at #{__FILE__}:#{__LINE__ - 1} - TEST_ME"
  341. traces = $stderr.string.split("\n")
  342. assert_equal(tobe, traces[0])
  343. assert_equal(tobe, traces[1])
  344. ensure
  345. $DEBUG = old_debug
  346. $stderr = STDERR
  347. end
  348. # rand
  349. # readline
  350. # readlines
  351. # scan
  352. # select
  353. # set_trace_func
  354. def test_sleep
  355. assert_raises(ArgumentError) { sleep(-10) }
  356. # FIXME: below is true for MRI, but not for JRuby
  357. # assert_raises(TypeError) { sleep "foo" }
  358. assert_equal 0, sleep(0)
  359. t1 = Time.now
  360. sleep(0.1)
  361. t2 = Time.now
  362. # this should cover systems with 10 msec clock resolution
  363. assert t2 >= t1 + 0.08
  364. end
  365. def test_sprintf
  366. assert_equal 'Hello', Kernel.sprintf('Hello')
  367. end
  368. def test_sprintf_with_object
  369. obj = Object.new
  370. def obj.to_int() 4 end
  371. assert_equal '4', Kernel.sprintf('%d', obj)
  372. end
  373. # JRUBY-4802
  374. def test_sprintf_float
  375. assert_equal "0.00000", Kernel.sprintf("%.5f", 0.000004)
  376. assert_equal "0.00001", Kernel.sprintf("%.5f", 0.000005)
  377. assert_equal "0.00001", Kernel.sprintf("%.5f", 0.000006)
  378. end
  379. def test_srand
  380. Kernel.srand
  381. Kernel.srand(0)
  382. if RUBY_VERSION =~ /1\.9/
  383. assert_raises(TypeError) { Kernel.srand(:foo) }
  384. else
  385. Kernel.srand(:foo)
  386. end
  387. assert_raises(TypeError) { Kernel.srand([:foo]) }
  388. end
  389. # sub
  390. # sub!
  391. # syscall
  392. def test_system
  393. assert !system('non_existant_command')
  394. # TODO: test for other OSes (Windows, for example, wouldn't know what to do with /dev/null)
  395. if RbConfig::CONFIG['target_os'] == 'linux'
  396. assert_equal true, system('echo > /dev/null')
  397. end
  398. end
  399. def test_system_empty
  400. assert !system('')
  401. end
  402. unless RUBY_VERSION =~ /1\.9/ # FIXME figure out why this doesn't pass in 1.9 mode
  403. def test_system_existing
  404. quiet do
  405. if (WINDOWS)
  406. res = system('cd')
  407. else
  408. res = system('pwd')
  409. end
  410. assert_equal true, res
  411. end
  412. end
  413. end
  414. unless RUBY_VERSION =~ /1\.9/ # FIXME figure out why this doesn't pass in 1.9 mode
  415. def test_system_existing_with_leading_space
  416. quiet do
  417. if (WINDOWS)
  418. res = system(' cd')
  419. else
  420. res = system(' pwd')
  421. end
  422. assert_equal true, res
  423. end
  424. end
  425. end
  426. unless RUBY_VERSION =~ /1\.9/ # FIXME figure out why this doesn't pass in 1.9 mode
  427. def test_system_existing_with_trailing_space
  428. quiet do
  429. if (WINDOWS)
  430. res = system('cd ')
  431. else
  432. res = system('pwd ')
  433. end
  434. assert_equal true, res
  435. end
  436. end
  437. end
  438. def test_system_non_existing
  439. res = system('program-that-doesnt-exist-for-sure')
  440. assert !res
  441. end
  442. unless RUBY_VERSION =~ /1\.9/ # FIXME figure out why this doesn't pass in 1.9 mode
  443. def test_system_existing_with_arg
  444. if (WINDOWS)
  445. res = system('cd .')
  446. else
  447. res = system('pwd . 2>&1 > /dev/null')
  448. end
  449. assert_equal true, res
  450. end
  451. end
  452. def test_system_non_existing_with_arg
  453. res = system('program-that-doesnt-exist-for-sure .')
  454. assert !res
  455. end
  456. def test_system_non_existing_with_args
  457. res = system('program-that-doesnt-exist-for-sure','arg1','arg2')
  458. assert !res
  459. end
  460. def test_exec_empty
  461. assert_raise(Errno::ENOENT) {
  462. exec("")
  463. }
  464. end
  465. def test_exec_non_existing
  466. assert_raise(Errno::ENOENT) {
  467. exec("program-that-doesnt-exist-for-sure")
  468. }
  469. end
  470. def test_exec_non_existing_with_args
  471. assert_raise(Errno::ENOENT) {
  472. exec("program-that-doesnt-exist-for-sure", "arg1", "arg2")
  473. }
  474. end
  475. # JRUBY-4834
  476. def test_backquote_with_changed_path
  477. orig_env = ENV['PATH']
  478. # Append a directory where testapp resides to the PATH
  479. paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR)
  480. paths.unshift TESTAPP_DIR
  481. ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR)
  482. res = `testapp`.chomp
  483. assert_equal("NO_ARGS", res)
  484. ensure
  485. ENV['PATH'] = orig_env
  486. end
  487. # JRUBY-4127
  488. def test_backquote_with_quotes
  489. if (WINDOWS)
  490. result = `"#{TESTAPP_NONORM}" #{Dir.pwd}`.strip
  491. else
  492. result = `"pwd" .`.strip
  493. end
  494. expected = Dir.pwd
  495. assert_equal(expected, result)
  496. end
  497. def test_backquote1
  498. if (WINDOWS)
  499. result = `cmd /c cd`.strip.gsub('\\', '/').downcase
  500. expected = Dir.pwd.downcase
  501. else
  502. result = `sh -c pwd`.strip
  503. expected = Dir.pwd
  504. end
  505. assert_equal(expected, result)
  506. end
  507. def test_backquote1_1
  508. if (WINDOWS)
  509. result = `cmd.exe /c cd`.strip.gsub('\\', '/').downcase
  510. expected = Dir.pwd.downcase
  511. else
  512. result = `pwd`.strip
  513. expected = Dir.pwd
  514. end
  515. assert_equal(expected, result)
  516. end
  517. def test_backquote2
  518. TESTAPPS.each { |app|
  519. if (app =~ /\/.*\.bat/ && Pathname.new(app).relative?)
  520. # MRI can't launch relative BAT files with / in their paths
  521. log "-- skipping #{app}"
  522. next
  523. end
  524. log "testing #{app}"
  525. result = `#{app}`.strip
  526. assert_equal('NO_ARGS', result, "Can't properly launch '#{app}'")
  527. }
  528. end
  529. def test_backquote2_1
  530. TESTAPPS.each { |app|
  531. if (app =~ /\/.*\.bat/ && Pathname.new(app).relative?)
  532. # MRI can't launch relative BAT files with / in their paths
  533. log "-- skipping #{app}"
  534. next
  535. end
  536. log "testing #{app}"
  537. result = `#{app} one`.strip
  538. assert_equal('one', result, "Can't properly launch '#{app}'")
  539. }
  540. end
  541. def test_backquote3
  542. TESTAPPS.each { |app|
  543. if (app =~ /\// && Pathname.new(app).relative? && WINDOWS)
  544. log "-- skipping #{app}"
  545. next
  546. end
  547. if (TESTAPP_DIR =~ /\s/) # spaces in paths, quote!
  548. app = '"' + app + '"'
  549. end
  550. log "testing #{app}"
  551. result = `#{app} 2>&1`.strip
  552. assert_equal("NO_ARGS", result, "Can't properly launch '#{app}'")
  553. }
  554. end
  555. def test_backquote3_1
  556. TESTAPPS.each { |app|
  557. if (app =~ /\// && Pathname.new(app).relative? && WINDOWS)
  558. log "-- skipping #{app}"
  559. next
  560. end
  561. if (TESTAPP_DIR =~ /\s/) # spaces in paths, quote!
  562. app = '"' + app + '"'
  563. end
  564. log "testing #{app}"
  565. result = `#{app} one 2>&1`.strip
  566. assert_equal("one", result, "Can't properly launch '#{app}'")
  567. }
  568. end
  569. def test_backquote3_2
  570. TESTAPPS.each { |app|
  571. if (app =~ /\// && Pathname.new(app).relative? && WINDOWS)
  572. log "-- skipping #{app}"
  573. next
  574. end
  575. if (TESTAPP_DIR =~ /\s/) # spaces in paths, quote!
  576. app = '"' + app + '"'
  577. end
  578. log "testing #{app}"
  579. result = `#{app} one two three 2>&1`.strip.split(/[\r\n]+/)
  580. assert_equal(%w[one two three], result, "Can't properly launch '#{app}'")
  581. # TODO: \r\n vs \n
  582. # result = `#{app} one two three 2>&1`.strip
  583. # assert_equal("one\ntwo\nthree", result, "Can't properly launch '#{app}'")
  584. }
  585. end
  586. def test_backquote4
  587. TESTAPPS.each { |app|
  588. if (app =~ /\/.*\.bat/ && Pathname.new(app).relative?)
  589. # MRI can't launch relative BAT files with / in their paths
  590. log "-- skipping #{app}"
  591. next
  592. end
  593. log "testing #{app}"
  594. result = `#{app} "" two`.split(/[\r\n]+/)
  595. assert_equal(["", "two"], result, "Can't properly launch '#{app}'")
  596. result = `#{app} ""`.chomp
  597. assert_equal('', result, "Can't properly launch '#{app}'")
  598. result = `#{app} " "`.chomp
  599. assert_equal(' ', result, "Can't properly launch '#{app}'")
  600. result = `#{app} a""`.chomp
  601. assert_equal('a', result, "Can't properly launch '#{app}'")
  602. result = `#{app} a" "`.chomp
  603. assert_equal('a ', result, "Can't properly launch '#{app}'")
  604. result = `#{app} ""b`.chomp
  605. assert_equal('b', result, "Can't properly launch '#{app}'")
  606. result = `#{app} " "b`.chomp
  607. assert_equal(' b', result, "Can't properly launch '#{app}'")
  608. result = `#{app} a""b`.chomp
  609. assert_equal('ab', result, "Can't properly launch '#{app}'")
  610. result = `#{app} a" "b`.chomp
  611. assert_equal('a b', result, "Can't properly launch '#{app}'")
  612. result = `#{app} a" "b" "c`.chomp
  613. assert_equal('a b c', result, "Can't properly launch '#{app}'")
  614. result = `#{app} a" "b""c`.chomp
  615. assert_equal('a bc', result, "Can't properly launch '#{app}'")
  616. }
  617. end
  618. def test_backquote4_1
  619. TESTAPPS.each { |app|
  620. if (app =~ /\// && Pathname.new(app).relative? && WINDOWS)
  621. log "-- skipping #{app}"
  622. next
  623. end
  624. log "testing #{app}"
  625. if (TESTAPP_DIR =~ /\s/) # spaces in paths, quote!
  626. app = '"' + app + '"'
  627. end
  628. result = `#{app} " " 2>&1`.chomp
  629. assert_equal(' ', result, "Can't properly launch '#{app}'")
  630. result = `#{app} a"" 2>&1`.strip
  631. assert_equal('a', result, "Can't properly launch '#{app}'")
  632. result = `#{app} ""b 2>&1`.strip
  633. assert_equal('b', result, "Can't properly launch '#{app}'")
  634. result = `#{app} a""b 2>&1`.strip
  635. assert_equal('ab', result, "Can't properly launch '#{app}'")
  636. result = `#{app} a" "b 2>&1`.strip
  637. assert_equal('a b', result, "Can't properly launch '#{app}'")
  638. }
  639. end
  640. def test_backquote_with_executable_in_cwd
  641. Dir.chdir(TESTAPP_DIR) do
  642. result = `./testapp one`
  643. assert_equal 0, $?.exitstatus
  644. assert_equal "one", result.rstrip
  645. end
  646. end
  647. if (WINDOWS)
  648. def test_backquote_with_executable_in_cwd_2
  649. Dir.chdir(TESTAPP_DIR) do
  650. result = `testapp one`
  651. assert_equal 0, $?.exitstatus
  652. assert_equal "one", result.rstrip
  653. end
  654. end
  655. end
  656. # JRUBY-4518
  657. if (WINDOWS)
  658. def test_backquote_with_CRLF
  659. assert_no_match(/\r/, `cd`)
  660. assert_equal 0, $?.exitstatus
  661. end
  662. end
  663. unless RUBY_VERSION =~ /1\.9/ # FIXME figure out why this doesn't pass in 1.9 mode
  664. def test_system_with_executable_in_cwd
  665. Dir.chdir(TESTAPP_DIR) do
  666. result = nil
  667. quiet do
  668. result = system("./testapp one")
  669. end
  670. assert_equal 0, $?.exitstatus
  671. assert result
  672. end
  673. end
  674. end
  675. if (WINDOWS)
  676. def test_system_with_executable_in_cwd_2
  677. Dir.chdir(TESTAPP_DIR) do
  678. result = nil
  679. quiet do
  680. result = system("testapp one")
  681. end
  682. assert_equal 0, $?.exitstatus
  683. assert result
  684. end
  685. end
  686. end
  687. unless RUBY_VERSION =~ /1\.9/
  688. def test_test
  689. assert "Test file existence", test(?f, "README")
  690. assert "Test file non-existence", !test(?f, "READMEaewertsert45t4w5tgrsfdgrf")
  691. # Make sure that absolute paths work for testing
  692. assert "Test should handle absolute paths", test(?f, File.expand_path("README"))
  693. end
  694. end
  695. # JRUBY-4348
  696. def test_exec_rubyopt
  697. old = ENV['RUBYOPT']
  698. ENV['RUBYOPT'] = "-v"
  699. result = `bin/jruby -e "a=1"`
  700. assert_equal 0, $?.exitstatus
  701. assert_match /ruby/i, result
  702. ensure
  703. ENV['RUBYOPT'] = old
  704. end
  705. # JRUBY-5431
  706. def test_exit_bang
  707. `bin/jruby -e "exit!"`
  708. assert_equal 1, $?.exitstatus
  709. `bin/jruby -e "exit!(true)"`
  710. assert_equal 0, $?.exitstatus
  711. `bin/jruby -e "exit!(false)"`
  712. assert_equal 1, $?.exitstatus
  713. end
  714. # test
  715. # trace_var
  716. # trap
  717. # untrace_var
  718. # warn
  719. end