File: test_invocation.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 (75 lines) | stat: -rw-r--r-- 1,545 bytes parent folder | download | duplicates (6)
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
64
65
66
67
68
69
70
71
72
73
74
75
require 'test/unit'

class TestInvocation < Test::Unit::TestCase

  TMP = "script_tmp"
  
  $interpreter = 'ruby'

  def teardown
    begin
      File.unlink TMP or `/bin/rm -f "#{TMP}"`
      File.unlink TMP + ".bak" or `/bin/rm -f "#{TMP}.bak"`
    rescue
    end
  end

  def tmp_write
    File.open(TMP, "w") do |f|
      yield f
    end
  end

  # --------------------------------------------------------

  def test_00_Basic
    assert_equal("foobar", `#$interpreter -e 'print "foobar"'`)
  end

  def testUnadornedScript
    tmp_write { |f| f.puts "print $zzz" }

    assert_equal('true', `#$interpreter -s script_tmp -zzz`)
    assert_equal('555', `#$interpreter -s script_tmp -zzz=555`)
  end

  def testScriptWithShebang
    tmp_write do |f|
      f.puts "#! /usr/local/bin/ruby -s"
      f.puts "print $zzz"
    end

    assert_equal('678', `#$interpreter script_tmp -zzz=678`)
  end

  def testScriptWithLeadingJunk
    tmp_write do |f|
      f.puts "this is a leading junk"
      f.puts "#! /usr/local/bin/ruby -s"
      f.puts "print $zzz"
      f.puts "__END__"
      f.puts "this is a trailing junk"
    end

    assert_equal('nil', `#$interpreter -x script_tmp`)
    assert_equal('555', `#$interpreter -x script_tmp -zzz=555`)
  end


  def testSciptWith_pe
    tmp_write do |f|
      for i in 1..5
        f.puts i
      end
    end

    `#$interpreter -i.bak -pe 'sub(/^[0-9]+$/){$&.to_i * 5}' script_tmp`

    File.open(TMP) do |f|
      while line = f.gets
        assert_equal(0, line.to_i % 5)
      end
    end
  end

end