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

« back to all changes in this revision

Viewing changes to test/readline/test_readline.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
begin
 
2
  require "readline"
 
3
rescue LoadError
 
4
end
 
5
 
 
6
if defined?(Readline) && !/EditLine/n.match(Readline::VERSION)
 
7
 
 
8
require "test/unit"
 
9
require "tempfile"
 
10
 
 
11
class TestReadline < Test::Unit::TestCase
 
12
  def test_readline
 
13
    stdin = Tempfile.new("test_readline_stdin")
 
14
    stdout = Tempfile.new("test_readline_stdout")
 
15
    begin
 
16
      stdin.write("hello\n")
 
17
      stdin.close
 
18
      stdout.close
 
19
      line = replace_stdio(stdin.path, stdout.path) {
 
20
        Readline.readline("> ", true)
 
21
      }
 
22
      assert_equal("hello", line)
 
23
      assert_equal(true, line.tainted?)
 
24
      stdout.open
 
25
      assert_equal("> ", stdout.read(2))
 
26
      assert_equal(1, Readline::HISTORY.length)
 
27
      assert_equal("hello", Readline::HISTORY[0])
 
28
      assert_raises(SecurityError) do
 
29
        Thread.start {
 
30
          $SAFE = 1
 
31
          replace_stdio(stdin.path, stdout.path) do
 
32
            Readline.readline("> ".taint)
 
33
          end
 
34
        }.join
 
35
      end
 
36
      assert_raises(SecurityError) do
 
37
        Thread.start {
 
38
          $SAFE = 4
 
39
          replace_stdio(stdin.path, stdout.path) { Readline.readline("> ") }
 
40
        }.join
 
41
      end
 
42
    ensure
 
43
      stdin.close(true)
 
44
      stdout.close(true)
 
45
    end
 
46
  end
 
47
 
 
48
  def test_completion_append_character
 
49
    begin
 
50
      Readline.completion_append_character = "x"
 
51
      assert_equal("x", Readline.completion_append_character)
 
52
      Readline.completion_append_character = "xyz"
 
53
      assert_equal("x", Readline.completion_append_character)
 
54
      Readline.completion_append_character = nil
 
55
      assert_equal(nil, Readline.completion_append_character)
 
56
      Readline.completion_append_character = ""
 
57
      assert_equal(nil, Readline.completion_append_character)
 
58
    rescue NotImplementedError
 
59
    end
 
60
  end
 
61
 
 
62
  private
 
63
 
 
64
  def replace_stdio(stdin_path, stdout_path)
 
65
    open(stdin_path, "r"){|stdin|
 
66
      open(stdout_path, "w"){|stdout|
 
67
        orig_stdin = STDIN.dup
 
68
        orig_stdout = STDOUT.dup
 
69
        STDIN.reopen(stdin)
 
70
        STDOUT.reopen(stdout)
 
71
        begin
 
72
          yield
 
73
        ensure
 
74
          STDIN.reopen(orig_stdin)
 
75
          STDOUT.reopen(orig_stdout)
 
76
          orig_stdin.close
 
77
          orig_stdout.close
 
78
        end
 
79
      }
 
80
    }
 
81
  end
 
82
end
 
83
 
 
84
end