File: test_source_layout.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 (190 lines) | stat: -rw-r--r-- 3,369 bytes parent folder | download | duplicates (6)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'test/unit'

class TestSourceLayout < Test::Unit::TestCase

  # ------------------------------------------------------------

  def test_semicolon_separated_expressions
    assert_equal(2, eval("1; 2"))
    assert_equal('1; 2', "1; 2")
    assert_equal(99, eval("def fred() 99; end; fred"))
  end

  

  # ------------------------------------------------------------

  def lcbb_test(*args)
    return args.length
  end

  def test_line_continuation_by_backslash
    assert_equal(3, 1 \
                    + \
                    2)
    assert_equal("hello", "h\
e\
l\
l\
o")

    assert_equal("h e l l o", "h\
 e\
 l\
 l\
 o")

    assert_equal("h\ne\nl\nl\no", "h
e
l
l
o")

    a = lcbb_test \
"arg?"
    assert_equal(1, a)          # would be '0' if arg not passed

  end

  # ------------------------------------------------------------

  def test_line_continuation_in_mid_expression
    assert_equal(3, 1 +
                 2)

    assert_equal(3, 1 + # spurious comment
                 2)

    assert_equal(3, (1 +
                     2))

    assert_equal(2, lcbb_test(1,
                              2))

    assert_equal(2, lcbb_test(
                              1,
                              2
                              ))
  end

  # ------------------------------------------------------------

  def test_comments
    assert_equal 4, 1 + # 2
                    3

    # check that comment isn't continued by '\'
    assert_equal 4, 1 + # 2\
                    3
  end

  # ------------------------------------------------------------

  SAMPLE1 = <<'SAMPLE1'
a\
=begin
skip this
=end
= 99
puts(a + 1)
SAMPLE1

  SAMPLE2 = <<'SAMPLE2'
a\
=begin label
=begin
skip this
=end
= 99
puts(a + 1)
SAMPLE2

  SAMPLE3 = <<'SAMPLE3'
 =begin
skip this
=end
SAMPLE3

  def rubyRun(str)
    result = nil
    code = nil

    File.open("_prog.rb", "w") { |f| f.puts str }

    begin
      result = nil
      IO.popen("#$interpreter _prog.rb 2>_err_tmp") do |p|
	result = p.read
      end
      code   = $?
    ensure
      File.delete("_prog.rb")
      File.delete("_err_tmp")
    end
    [ code, result ]
  end

  def test_equals_begin
    assert_equal([0, "100\n"], rubyRun(SAMPLE1))
    assert_equal([0, "100\n"], rubyRun(SAMPLE2))
    assert(rubyRun(SAMPLE3)[0] != 0)
  end

  # ------------------------------------------------------------

  SAMPLE4 = <<-SAMPLE4
  END   { puts 1 }
  BEGIN { puts 2 }
  END   { puts 3 }
  BEGIN { puts 4 }
  SAMPLE4

  SAMPLE5 = <<-SAMPLE5
  END   { puts 5 }
  require "_prog1.rb"
  BEGIN { puts 8 }
  SAMPLE5

  
  def test_BEGIN_END
    assert_equal([0, "2\n4\n3\n1\n"], rubyRun(SAMPLE4))

    # confirm that require brings things in at runtime
    File.open("_prog1.rb", "w") do |f|
      f.puts "BEGIN { puts 6 }"
      f.puts "END { puts 7 }"
    end
    begin
      assert_equal([0, "8\n6\n7\n5\n"], rubyRun(SAMPLE5))
    ensure
      File.delete("_prog1.rb")
    end
  end

  # ------------------------------------------------------------

  SAMPLE6 = <<-SAMPLE6
puts DATA.gets
__END__
wombat
SAMPLE6

  SAMPLE7 = <<-SAMPLE7
puts DATA.gets
 __END__
wombat
SAMPLE7

  SAMPLE8 = <<-SAMPLE8
puts DATA.gets
__END__ of data
wombat
SAMPLE8

  def test___END__
    assert_equal([ 0, "wombat\n"], rubyRun(SAMPLE6))
    assert_equal(256, rubyRun(SAMPLE7)[0])
    assert_equal(256, rubyRun(SAMPLE8)[0])
  end

end