~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to test/ruby/test_system.rb

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-01-24 11:42:29 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20080124114229-jw2f87rdxlq6gp11
Tags: 1.9.0.0-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Robustify check for target_os, fixing build failure on lpia.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
require 'test/unit'
2
2
require 'envutil'
 
3
require 'tmpdir'
3
4
 
4
5
class TestSystem < Test::Unit::TestCase
5
6
  def valid_syntax?(code, fname)
 
7
    code.force_encoding("ascii-8bit")
6
8
    code = code.sub(/\A(?:\s*\#.*$)*(\n)?/n) {
7
9
      "#$&#{"\n" if $1 && !$2}BEGIN{return true}\n"
8
10
    }
14
16
    assert_equal("foobar\n", `echo foobar`)
15
17
    assert_equal('foobar', `#{ruby} -e 'print "foobar"'`)
16
18
 
17
 
    tmp = open("script_tmp", "w")
18
 
    tmp.print "print $zzz\n";
19
 
    tmp.close
20
 
 
21
 
    assert_equal('true', `#{ruby} -s script_tmp -zzz`)
22
 
    assert_equal('555', `#{ruby} -s script_tmp -zzz=555`)
23
 
 
24
 
    tmp = open("script_tmp", "w")
25
 
    tmp.print "#! /usr/local/bin/ruby -s\n";
26
 
    tmp.print "print $zzz\n";
27
 
    tmp.close
28
 
 
29
 
    assert_equal('678', `#{ruby} script_tmp -zzz=678`)
30
 
 
31
 
    tmp = open("script_tmp", "w")
32
 
    tmp.print "this is a leading junk\n";
33
 
    tmp.print "#! /usr/local/bin/ruby -s\n";
34
 
    tmp.print "print $zzz\n";
35
 
    tmp.print "__END__\n";
36
 
    tmp.print "this is a trailing junk\n";
37
 
    tmp.close
38
 
 
39
 
    assert_equal('', `#{ruby} -x script_tmp`)
40
 
    assert_equal('555', `#{ruby} -x script_tmp -zzz=555`)
41
 
 
42
 
    tmp = open("script_tmp", "w")
43
 
    for i in 1..5
44
 
      tmp.print i, "\n"
45
 
    end
46
 
    tmp.close
47
 
 
48
 
    `#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
49
 
    tmp = open("script_tmp", "r")
50
 
    while tmp.gets
51
 
      assert_equal(0, $_.to_i % 5)
52
 
    end
53
 
    tmp.close
54
 
 
55
 
    File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
56
 
    File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
 
19
    Dir.mktmpdir("ruby_script_tmp") {|tmpdir|
 
20
      tmpfilename = "#{tmpdir}/ruby_script_tmp.#{$$}"
 
21
 
 
22
      tmp = open(tmpfilename, "w")
 
23
      tmp.print "print $zzz\n";
 
24
      tmp.close
 
25
 
 
26
      assert_equal('true', `#{ruby} -s #{tmpfilename} -zzz`)
 
27
      assert_equal('555', `#{ruby} -s #{tmpfilename} -zzz=555`)
 
28
 
 
29
      tmp = open(tmpfilename, "w")
 
30
      tmp.print "#! /usr/local/bin/ruby -s\n";
 
31
      tmp.print "print $zzz\n";
 
32
      tmp.close
 
33
 
 
34
      assert_equal('678', `#{ruby} #{tmpfilename} -zzz=678`)
 
35
 
 
36
      tmp = open(tmpfilename, "w")
 
37
      tmp.print "this is a leading junk\n";
 
38
      tmp.print "#! /usr/local/bin/ruby -s\n";
 
39
      tmp.print "print $zzz\n";
 
40
      tmp.print "__END__\n";
 
41
      tmp.print "this is a trailing junk\n";
 
42
      tmp.close
 
43
 
 
44
      assert_equal('', `#{ruby} -x #{tmpfilename}`)
 
45
      assert_equal('555', `#{ruby} -x #{tmpfilename} -zzz=555`)
 
46
 
 
47
      tmp = open(tmpfilename, "w")
 
48
      for i in 1..5
 
49
        tmp.print i, "\n"
 
50
      end
 
51
      tmp.close
 
52
 
 
53
      `#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' #{tmpfilename}`
 
54
      tmp = open(tmpfilename, "r")
 
55
      while tmp.gets
 
56
        assert_equal(0, $_.to_i % 5)
 
57
      end
 
58
      tmp.close
 
59
 
 
60
      File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
 
61
      File.unlink "#{tmpfilename}.bak" or `/bin/rm -f "#{tmpfilename}.bak"`
 
62
    }
57
63
  end
58
64
 
59
65
  def test_syntax
64
70
    end
65
71
  end
66
72
 
67
 
  def test_empty_evstr # [ruby-dev:25113]
68
 
    assert_equal("", eval('"#{}"', nil, __FILE__, __LINE__))
 
73
  def test_empty_evstr
 
74
    assert_equal("", eval('"#{}"', nil, __FILE__, __LINE__), "[ruby-dev:25113]")
69
75
  end
70
76
end