File: test_argf.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (63 lines) | stat: -rw-r--r-- 1,645 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'test/unit'
require 'test/test_helper'
require 'rbconfig'

# Since we haven't found a good way to instantiate multiple ARGF instances for testing,
# the approach here is to fork to another process, run few things, and be done with it.
# Given JRuby startup time, ARGF is not a terribly valuable feature, so this level of testing is enough.

SCRIPT = <<END_OF_SCRIPT

def test_equal(expected, actual)
  raise "Expected: \#{expected.inspect}, got: \#{actual.inspect}" unless expected == actual
end

def test_to_io
  raise "Could not coerce ARGF to IO" unless ARGF.to_io.is_a? IO
end

test_to_io

# should not raise anything
test_equal "1:1\\n", ARGF.gets

test_to_io

ARGF.each_with_index do |line, index|
  case index
  when 0 then test_equal "1:2", line
  when 1 then test_equal "2:1\\n", line
  when 2
    test_equal "2:2\\n", line
    break
  else raise 'Should never get here'
  end
end

test_equal nil, ARGF.gets

END_OF_SCRIPT

class TestArgf < Test::Unit::TestCase
  include TestHelper

  def test_argf_sanity
    begin
      File.open('__argf_script.rb', 'w') { |f| f.write SCRIPT }
      File.open('__argf_input_1', 'w') { |f| f.write "1:1\n1:2" }
      File.open('__argf_input_2', 'w') { |f| f.write "2:1\n2:2\n" }

      assert jruby("__argf_script.rb", "__argf_input_1", "__argf_input_2"),
             "Smoke test script for ARGF failed"
    ensure
      File.unlink '__argf_script.rb' rescue nil
      File.unlink '__argf_input_1' rescue nil
      File.unlink '__argf_input_2' rescue nil
      File.unlink '__argf_input_3' rescue nil
    end
  end

  def test_argf_cloning
    assert_equal "ARGF", ARGF.clone.to_s
  end
end