~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to test/ruby/test_system.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'test/unit'
 
2
$:.replace([File.dirname(File.expand_path(__FILE__))] | $:)
 
3
require 'envutil'
 
4
 
 
5
class TestSystem < Test::Unit::TestCase
 
6
  def valid_syntax?(code, fname)
 
7
    eval("BEGIN {return true}\n#{code}", nil, fname, 0)
 
8
  end
 
9
 
 
10
  def test_system
 
11
    ruby = EnvUtil.rubybin
 
12
    assert_equal("foobar\n", `echo foobar`)
 
13
    assert_equal('foobar', `#{ruby} -e 'print "foobar"'`)
 
14
 
 
15
    tmp = open("script_tmp", "w")
 
16
    tmp.print "print $zzz\n";
 
17
    tmp.close
 
18
 
 
19
    assert_equal('true', `#{ruby} -s script_tmp -zzz`)
 
20
    assert_equal('555', `#{ruby} -s script_tmp -zzz=555`)
 
21
 
 
22
    tmp = open("script_tmp", "w")
 
23
    tmp.print "#! /usr/local/bin/ruby -s\n";
 
24
    tmp.print "print $zzz\n";
 
25
    tmp.close
 
26
 
 
27
    assert_equal('678', `#{ruby} script_tmp -zzz=678`)
 
28
 
 
29
    tmp = open("script_tmp", "w")
 
30
    tmp.print "this is a leading junk\n";
 
31
    tmp.print "#! /usr/local/bin/ruby -s\n";
 
32
    tmp.print "print $zzz\n";
 
33
    tmp.print "__END__\n";
 
34
    tmp.print "this is a trailing junk\n";
 
35
    tmp.close
 
36
 
 
37
    assert_equal('nil', `#{ruby} -x script_tmp`)
 
38
    assert_equal('555', `#{ruby} -x script_tmp -zzz=555`)
 
39
 
 
40
    tmp = open("script_tmp", "w")
 
41
    for i in 1..5
 
42
      tmp.print i, "\n"
 
43
    end
 
44
    tmp.close
 
45
 
 
46
    `#{ruby} -i.bak -pe 'sub(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
 
47
    tmp = open("script_tmp", "r")
 
48
    while tmp.gets
 
49
      assert_equal(0, $_.to_i % 5)
 
50
    end
 
51
    tmp.close
 
52
 
 
53
    File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
 
54
    File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
 
55
  end
 
56
 
 
57
  def test_syntax
 
58
    assert_nothing_raised(Exception) do
 
59
      for script in Dir[File.expand_path("../../../{lib,sample,ext}/**/*.rb", __FILE__)]
 
60
        valid_syntax? IO::read(script), script
 
61
      end
 
62
    end
 
63
  end
 
64
end