File: test_system.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 (67 lines) | stat: -rw-r--r-- 1,809 bytes parent folder | download | duplicates (10)
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
require 'test/unit'
$:.replace([File.dirname(File.expand_path(__FILE__))] | $:)
require 'envutil'

class TestSystem < Test::Unit::TestCase
  def valid_syntax?(code, fname)
    code = code.sub(/\A(?:\s*\#.*$)*(\n)?/n) {
      "#$&#{"\n" if $1 && !$2}BEGIN{return true}\n"
    }
    eval(code, nil, fname, 0)
  end

  def test_system
    ruby = EnvUtil.rubybin
    assert_equal("foobar\n", `echo foobar`)
    assert_equal('foobar', `#{ruby} -e 'print "foobar"'`)

    tmp = open("script_tmp", "w")
    tmp.print "print $zzz\n";
    tmp.close

    assert_equal('true', `#{ruby} -s script_tmp -zzz`)
    assert_equal('555', `#{ruby} -s script_tmp -zzz=555`)

    tmp = open("script_tmp", "w")
    tmp.print "#! /usr/local/bin/ruby -s\n";
    tmp.print "print $zzz\n";
    tmp.close

    assert_equal('678', `#{ruby} script_tmp -zzz=678`)

    tmp = open("script_tmp", "w")
    tmp.print "this is a leading junk\n";
    tmp.print "#! /usr/local/bin/ruby -s\n";
    tmp.print "print $zzz\n";
    tmp.print "__END__\n";
    tmp.print "this is a trailing junk\n";
    tmp.close

    assert_equal('nil', `#{ruby} -x script_tmp`)
    assert_equal('555', `#{ruby} -x script_tmp -zzz=555`)

    tmp = open("script_tmp", "w")
    for i in 1..5
      tmp.print i, "\n"
    end
    tmp.close

    `#{ruby} -i.bak -pe 'sub(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
    tmp = open("script_tmp", "r")
    while tmp.gets
      assert_equal(0, $_.to_i % 5)
    end
    tmp.close

    File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
    File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
  end

  def test_syntax
    assert_nothing_raised(Exception) do
      for script in Dir[File.expand_path("../../../{lib,sample,ext}/**/*.rb", __FILE__)]
        valid_syntax? IO::read(script), script
      end
    end
  end
end