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

« back to all changes in this revision

Viewing changes to test/ruby/test_file.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
require 'tempfile'
 
3
$:.replace([File.dirname(File.expand_path(__FILE__))] | $:)
 
4
require 'ut_eof'
 
5
 
 
6
class TestFile < Test::Unit::TestCase
 
7
 
 
8
  # I don't know Ruby's spec about "unlink-before-close" exactly.
 
9
  # This test asserts current behaviour.
 
10
  def test_unlink_before_close
 
11
    filename = File.basename(__FILE__) + ".#{$$}"
 
12
    w = File.open(filename, "w")
 
13
    w << "foo"
 
14
    w.close
 
15
    r = File.open(filename, "r")
 
16
    begin
 
17
      if /(mswin|bccwin|mingw|emx)/ =~ RUBY_PLATFORM
 
18
        begin
 
19
          File.unlink(filename)
 
20
          assert(false)
 
21
        rescue Errno::EACCES
 
22
          assert(true)
 
23
        end
 
24
      else
 
25
        File.unlink(filename)
 
26
        assert(true)
 
27
      end
 
28
    ensure
 
29
      r.close
 
30
      File.unlink(filename) if File.exist?(filename)
 
31
    end
 
32
  end
 
33
 
 
34
  include TestEOF
 
35
  def open_file(content)
 
36
    f = Tempfile.new("test-eof")
 
37
    f << content
 
38
    f.rewind
 
39
    yield f
 
40
  end
 
41
  alias open_file_rw open_file
 
42
 
 
43
  include TestEOF::Seek
 
44
 
 
45
  def test_fnmatch
 
46
    # from [ruby-dev:22815] and [ruby-dev:22819]
 
47
    assert(true, File.fnmatch('\[1\]' , '[1]'))
 
48
    assert(true, File.fnmatch('*?', 'a'))
 
49
  end
 
50
 
 
51
  def test_truncate_wbuf # [ruby-dev:24191]
 
52
    f = Tempfile.new("test-truncate")
 
53
    f.print "abc"
 
54
    f.truncate(0)
 
55
    f.print "def"
 
56
    f.close
 
57
    assert_equal("\0\0\0def", File.read(f.path))
 
58
  end
 
59
 
 
60
  def test_truncate_rbuf # [ruby-dev:24197]
 
61
    f = Tempfile.new("test-truncate")
 
62
    f.puts "abc"
 
63
    f.puts "def"
 
64
    f.close
 
65
    f.open
 
66
    assert_equal("abc\n", f.gets)
 
67
    f.truncate(3)
 
68
    assert_equal(nil, f.gets)
 
69
  end
 
70
 
 
71
  def test_read_all_extended_file
 
72
    f = Tempfile.new("test-extended-file")
 
73
    assert_nil(f.getc)
 
74
    open(f.path, "w") {|g| g.print "a" }
 
75
    assert_equal("a", f.read)
 
76
  end
 
77
 
 
78
  def test_gets_extended_file
 
79
    f = Tempfile.new("test-extended-file")
 
80
    assert_nil(f.getc)
 
81
    open(f.path, "w") {|g| g.print "a" }
 
82
    assert_equal("a", f.gets("a"))
 
83
  end
 
84
 
 
85
  def test_gets_para_extended_file
 
86
    f = Tempfile.new("test-extended-file")
 
87
    assert_nil(f.getc)
 
88
    open(f.path, "w") {|g| g.print "\na" }
 
89
    assert_equal("a", f.gets(""))
 
90
  end
 
91
 
 
92
  def test_each_byte_extended_file
 
93
    f = Tempfile.new("test-extended-file")
 
94
    assert_nil(f.getc)
 
95
    open(f.path, "w") {|g| g.print "a" }
 
96
    result = []
 
97
    f.each_byte {|b| result << b }
 
98
    assert_equal([?a], result)
 
99
  end
 
100
 
 
101
  def test_getc_extended_file
 
102
    f = Tempfile.new("test-extended-file")
 
103
    assert_nil(f.getc)
 
104
    open(f.path, "w") {|g| g.print "a" }
 
105
    assert_equal(?a, f.getc)
 
106
  end
 
107
 
 
108
end