erbse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.txt +828 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +51 -0
  5. data/benchmark/Makefile +6 -0
  6. data/benchmark/bench.rb +313 -0
  7. data/benchmark/bench_context.yaml +141 -0
  8. data/benchmark/templates/_footer.html +4 -0
  9. data/benchmark/templates/_header.html +52 -0
  10. data/benchmark/templates/bench_erb.rhtml +29 -0
  11. data/benchmark/templates/bench_erubis.rhtml +29 -0
  12. data/benchmark/templates/bench_eruby.rhtml +29 -0
  13. data/bin/erubis +10 -0
  14. data/erbse.gemspec +24 -0
  15. data/lib/erbse.rb +52 -0
  16. data/lib/erbse/context.rb +83 -0
  17. data/lib/erbse/converter.rb +357 -0
  18. data/lib/erbse/engine.rb +114 -0
  19. data/lib/erbse/engine/ec.rb +117 -0
  20. data/lib/erbse/engine/ecpp.rb +113 -0
  21. data/lib/erbse/engine/ejava.rb +110 -0
  22. data/lib/erbse/engine/ejavascript.rb +119 -0
  23. data/lib/erbse/engine/enhanced.rb +126 -0
  24. data/lib/erbse/engine/eperl.rb +95 -0
  25. data/lib/erbse/engine/ephp.rb +99 -0
  26. data/lib/erbse/engine/eruby.rb +119 -0
  27. data/lib/erbse/engine/escheme.rb +114 -0
  28. data/lib/erbse/engine/optimized.rb +127 -0
  29. data/lib/erbse/enhancer.rb +723 -0
  30. data/lib/erbse/error.rb +23 -0
  31. data/lib/erbse/evaluator.rb +88 -0
  32. data/lib/erbse/generator.rb +85 -0
  33. data/lib/erbse/helper.rb +47 -0
  34. data/lib/erbse/helpers/rails_form_helper.rb +197 -0
  35. data/lib/erbse/helpers/rails_helper.rb +353 -0
  36. data/lib/erbse/main.rb +516 -0
  37. data/lib/erbse/preprocessing.rb +58 -0
  38. data/lib/erbse/tiny.rb +144 -0
  39. data/lib/erbse/util.rb +22 -0
  40. data/lib/erbse/version.rb +3 -0
  41. data/test/assert-text-equal.rb +44 -0
  42. data/test/data/users-guide/Example.ejava +55 -0
  43. data/test/data/users-guide/array_example.result +9 -0
  44. data/test/data/users-guide/arraybuffer_example.result +9 -0
  45. data/test/data/users-guide/bipattern-example.rhtml +4 -0
  46. data/test/data/users-guide/bipattern_example.result +6 -0
  47. data/test/data/users-guide/bufvar-example.rb +10 -0
  48. data/test/data/users-guide/bufvar-example.result +17 -0
  49. data/test/data/users-guide/context.rb +6 -0
  50. data/test/data/users-guide/context.yaml +8 -0
  51. data/test/data/users-guide/def_method.rb +14 -0
  52. data/test/data/users-guide/def_method.result +3 -0
  53. data/test/data/users-guide/escape_example.result +9 -0
  54. data/test/data/users-guide/example.ec +27 -0
  55. data/test/data/users-guide/example.ecpp +30 -0
  56. data/test/data/users-guide/example.ejs +20 -0
  57. data/test/data/users-guide/example.eperl +18 -0
  58. data/test/data/users-guide/example.ephp +18 -0
  59. data/test/data/users-guide/example.eruby +6 -0
  60. data/test/data/users-guide/example.escheme +28 -0
  61. data/test/data/users-guide/example1.eruby +6 -0
  62. data/test/data/users-guide/example1.rb +17 -0
  63. data/test/data/users-guide/example1.result +16 -0
  64. data/test/data/users-guide/example10.rb +4 -0
  65. data/test/data/users-guide/example10.result +17 -0
  66. data/test/data/users-guide/example10.xhtml +14 -0
  67. data/test/data/users-guide/example10_x.result +17 -0
  68. data/test/data/users-guide/example11.php +20 -0
  69. data/test/data/users-guide/example11.result +23 -0
  70. data/test/data/users-guide/example11.rhtml +21 -0
  71. data/test/data/users-guide/example11_C.result +10 -0
  72. data/test/data/users-guide/example11_N.result +16 -0
  73. data/test/data/users-guide/example11_U.result +16 -0
  74. data/test/data/users-guide/example11_php.result +15 -0
  75. data/test/data/users-guide/example1_x.result +9 -0
  76. data/test/data/users-guide/example2.eruby +7 -0
  77. data/test/data/users-guide/example2.rb +10 -0
  78. data/test/data/users-guide/example2.result +27 -0
  79. data/test/data/users-guide/example2_trim.result +10 -0
  80. data/test/data/users-guide/example2_x.result +10 -0
  81. data/test/data/users-guide/example3.eruby +6 -0
  82. data/test/data/users-guide/example3.rb +10 -0
  83. data/test/data/users-guide/example31.result +22 -0
  84. data/test/data/users-guide/example32.result +4 -0
  85. data/test/data/users-guide/example3_e.result +8 -0
  86. data/test/data/users-guide/example4.eruby +3 -0
  87. data/test/data/users-guide/example4.rb +11 -0
  88. data/test/data/users-guide/example4.result +10 -0
  89. data/test/data/users-guide/example4_x.result +5 -0
  90. data/test/data/users-guide/example5.eruby +6 -0
  91. data/test/data/users-guide/example5.rb +16 -0
  92. data/test/data/users-guide/example5.result +7 -0
  93. data/test/data/users-guide/example6.rb +12 -0
  94. data/test/data/users-guide/example6.result +7 -0
  95. data/test/data/users-guide/example7.eruby +8 -0
  96. data/test/data/users-guide/example71.result +13 -0
  97. data/test/data/users-guide/example72.result +13 -0
  98. data/test/data/users-guide/example8.eruby +6 -0
  99. data/test/data/users-guide/example8_ruby.result +7 -0
  100. data/test/data/users-guide/example8_yaml.result +7 -0
  101. data/test/data/users-guide/example9.eruby +3 -0
  102. data/test/data/users-guide/example9.rb +8 -0
  103. data/test/data/users-guide/example9.result +9 -0
  104. data/test/data/users-guide/example91.result +5 -0
  105. data/test/data/users-guide/example92.result +4 -0
  106. data/test/data/users-guide/example_c.result +32 -0
  107. data/test/data/users-guide/example_java.result +56 -0
  108. data/test/data/users-guide/example_js.result +22 -0
  109. data/test/data/users-guide/example_perl.result +20 -0
  110. data/test/data/users-guide/example_php.result +19 -0
  111. data/test/data/users-guide/example_scheme.result +30 -0
  112. data/test/data/users-guide/example_scheme_display.result +29 -0
  113. data/test/data/users-guide/fasteruby-example.rb +8 -0
  114. data/test/data/users-guide/fasteruby-example.result +18 -0
  115. data/test/data/users-guide/fasteruby.rb +11 -0
  116. data/test/data/users-guide/fasteruby.result +38 -0
  117. data/test/data/users-guide/fasteruby.rhtml +15 -0
  118. data/test/data/users-guide/headerfooter-example.eruby +9 -0
  119. data/test/data/users-guide/headerfooter-example2.rb +8 -0
  120. data/test/data/users-guide/headerfooter-example2.rhtml +10 -0
  121. data/test/data/users-guide/headerfooter_example.result +11 -0
  122. data/test/data/users-guide/headerfooter_example2.result +13 -0
  123. data/test/data/users-guide/interpolation_example.result +9 -0
  124. data/test/data/users-guide/main_program1.rb +8 -0
  125. data/test/data/users-guide/main_program1.result +6 -0
  126. data/test/data/users-guide/main_program2.rb +8 -0
  127. data/test/data/users-guide/main_program2.result +6 -0
  128. data/test/data/users-guide/nocode-example.eruby +14 -0
  129. data/test/data/users-guide/nocode-php.result +20 -0
  130. data/test/data/users-guide/nocode_example.result +15 -0
  131. data/test/data/users-guide/normal-eruby-test.eruby +9 -0
  132. data/test/data/users-guide/normal_eruby_test.result +11 -0
  133. data/test/data/users-guide/notext-example.eruby +14 -0
  134. data/test/data/users-guide/notext-example.php +19 -0
  135. data/test/data/users-guide/notext-php.result +20 -0
  136. data/test/data/users-guide/notext_example.result +16 -0
  137. data/test/data/users-guide/percentline-example.rhtml +6 -0
  138. data/test/data/users-guide/percentline_example.result +9 -0
  139. data/test/data/users-guide/prefixedline-example.rb +9 -0
  140. data/test/data/users-guide/prefixedline-example.rhtml +6 -0
  141. data/test/data/users-guide/prefixedline_example.result +9 -0
  142. data/test/data/users-guide/printenable_example.result +4 -0
  143. data/test/data/users-guide/printenabled-example.eruby +3 -0
  144. data/test/data/users-guide/printenabled-example.rb +8 -0
  145. data/test/data/users-guide/printstatement_example.result +8 -0
  146. data/test/data/users-guide/simplify_example.result +9 -0
  147. data/test/data/users-guide/stderr.log +3 -0
  148. data/test/data/users-guide/stdout_exmple.result +9 -0
  149. data/test/data/users-guide/stringbuffer_example.result +9 -0
  150. data/test/data/users-guide/tail_260.result +4 -0
  151. data/test/data/users-guide/tailnewline.rhtml +3 -0
  152. data/test/data/users-guide/template1.rhtml +4 -0
  153. data/test/data/users-guide/template2.rhtml +4 -0
  154. data/test/test-engines.rb +425 -0
  155. data/test/test-enhancers.rb +646 -0
  156. data/test/test-erubis.rb +887 -0
  157. data/test/test-index-cgi.rb +191 -0
  158. data/test/test-main.rb +752 -0
  159. data/test/test-users-guide.rb +73 -0
  160. data/test/test.rb +45 -0
  161. data/test/testutil.rb +111 -0
  162. metadata +233 -0
@@ -0,0 +1,191 @@
1
+ ##
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ##
5
+
6
+ require "#{File.dirname(__FILE__)}/test.rb"
7
+
8
+ require 'stringio'
9
+
10
+ load "#{File.dirname(__FILE__)}/../public_html/index.cgi"
11
+
12
+
13
+ def spec(desc)
14
+ yield
15
+ end
16
+
17
+
18
+ def dummy_env(request_method='GET', request_uri='/', opts={})
19
+ if request_method.is_a?(Hash)
20
+ opts = request_method
21
+ request_method = 'GET'
22
+ request_uri = '/'
23
+ elsif request_uri.is_a?(Hash)
24
+ opts = request_uri
25
+ request_uri = '/'
26
+ end
27
+ env = {
28
+ 'REQUEST_METHOD' => request_method.to_s,
29
+ 'REQUEST_URI' => request_uri.to_s,
30
+ 'DOCUMENT_ROOT' => Dir.pwd,
31
+ }
32
+ opts.each {|k, v| env[k.to_s.upcase] = v }
33
+ env.update(opts)
34
+ return env
35
+ end
36
+
37
+
38
+ def dummy_template(filename, content)
39
+ begin
40
+ File.open(filename, 'wb') {|f| f.write(content) }
41
+ return yield
42
+ ensure
43
+ [filename, filename + '.cache'].each do |fname|
44
+ File.unlink(fname) if File.exist?(fname)
45
+ end
46
+ end
47
+ end
48
+
49
+
50
+ class ErubisHandlerTest < Test::Unit::TestCase
51
+
52
+ def test_initialize
53
+
54
+ spec "sets @encoding and @layout" do
55
+ encoding_bkup = $ENCODING
56
+ layout_bkup = $LAYOUT
57
+ begin
58
+ $ENCODING = 'cp932'
59
+ $LAYOUT = 'site.rhtml'
60
+ obj = ErubisHandler.new
61
+ assert_equal 'cp932', obj.encoding
62
+ assert_equal 'site.rhtml', obj.layout
63
+ ensure
64
+ $ENCODING = encoding_bkup
65
+ $LAYOUT = layout_bkup
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+ def test_handle
72
+
73
+ spec "renders requested template file." do
74
+ base = "_test_handle"
75
+ env = dummy_env('GET', "/#{base}.html")
76
+ handler = ErubisHandler.new
77
+ input = <<'END'
78
+ <h1><%= '<b>SOS</b>' %></h1>
79
+ <ul>
80
+ <% for item in %w[Haruhi Mikuru Yuki] %>
81
+ <li><%= item %></li>
82
+ <% end %>
83
+ </ul>
84
+ END
85
+ expected = <<'END'
86
+ <h1><b>SOS</b></h1>
87
+ <ul>
88
+ <li>Haruhi</li>
89
+ <li>Mikuru</li>
90
+ <li>Yuki</li>
91
+ </ul>
92
+ END
93
+ out = dummy_template("#{base}.rhtml", input) do
94
+ handler.handle(env)
95
+ end
96
+ assert_equal expected, out
97
+ end
98
+
99
+ spec "raises 404 error when requested file not found." do
100
+ req_url = "/_test_handle.html"
101
+ env = dummy_env('GET', req_url)
102
+ handler = ErubisHandler.new
103
+ ex = assert_raise HttpError do
104
+ handler.handle(env)
105
+ end
106
+ assert_equal 404, ex.status
107
+ assert_equal "#{req_url}: not found.", ex.message
108
+ end
109
+
110
+ end
111
+
112
+ end
113
+
114
+
115
+ class ErubisApplicationTest < Test::Unit::TestCase
116
+
117
+ def test_handle_request
118
+
119
+ spec "handles request by handler object and returns response data." do
120
+ app = ErubisApplication.new()
121
+ def app.get_handler
122
+ return Class.new {
123
+ def handle(env); "<p>Hello SOS</p>"; end
124
+ def encoding; "euc_jp"; end
125
+ }.new
126
+ end
127
+ expected = [
128
+ 200,
129
+ [["Content-Type", "text/html;charset=euc_jp"]],
130
+ ["<p>Hello SOS</p>"],
131
+ ]
132
+ env = dummy_env('GET', '/')
133
+ ret = app.call(env)
134
+ assert_equal expected, ret
135
+ end
136
+
137
+ end
138
+
139
+ def test_handle_http_error
140
+
141
+ spec "renders error page." do
142
+ req_path = '/HaruhiSuzumiya.html'
143
+ app = ErubisApplication.new()
144
+ env = dummy_env('GET', req_path)
145
+ expected = [
146
+ 404,
147
+ [["Content-Type", "text/html"]],
148
+ ["<h2>404 Not Found</h2>\n<p>#{req_path}: not found.</p>\n"],
149
+ ]
150
+ ret = app.call(env)
151
+ assert_equal expected, ret
152
+ end
153
+
154
+ end
155
+
156
+ def test_run
157
+
158
+ spec "prints to $stdout" do
159
+ input = "<p>Hello SOS</p>"
160
+ app = ErubisApplication.new
161
+ base = "SOS"
162
+ env = dummy_env("GET", "/#{base}.html")
163
+ sio = StringIO.new
164
+ output = dummy_template("#{base}.rhtml", input) do
165
+ app.run(env, sio)
166
+ sio.string
167
+ end
168
+ expected = ""
169
+ expected << "Content-Type: text/html\r\n"
170
+ expected << "\r\n"
171
+ expected << "<p>Hello SOS</p>"
172
+ assert_equal expected, output
173
+ end
174
+
175
+ spec "prints 'Status:' header if status code is not 200." do
176
+ req_path = "/SOS.html"
177
+ env = dummy_env("GET", req_path)
178
+ app = ErubisApplication.new
179
+ sio = StringIO.new
180
+ app.run(env, sio)
181
+ expected = "Status: 404 Not Found\r\n"
182
+ expected << "Content-Type: text/html\r\n"
183
+ expected << "\r\n"
184
+ expected << "<h2>404 Not Found</h2>\n"
185
+ expected << "<p>#{req_path}: not found.</p>\n"
186
+ assert_equal expected, sio.string
187
+ end
188
+
189
+ end
190
+
191
+ end
@@ -0,0 +1,752 @@
1
+ ##
2
+ ## $Rev$
3
+ ## $Release: 2.7.0 $
4
+ ## $Date$
5
+ ##
6
+
7
+ require "#{File.dirname(__FILE__)}/test.rb"
8
+
9
+ require 'tempfile'
10
+ require 'fileutils'
11
+ require 'erubis/main'
12
+
13
+
14
+ $script = File.dirname(TESTDIR) + '/bin/erubis'
15
+ #if test(?f, 'bin/erubis')
16
+ # $script = 'bin/erubis'
17
+ #elsif test(?f, '../bin/erubis')
18
+ # $script = '../bin/erubis'
19
+ #end
20
+
21
+
22
+ class StringWriter < String
23
+ def write(arg)
24
+ self << arg
25
+ end
26
+ def flush(*args)
27
+ # pass
28
+ end
29
+ def puts(arg)
30
+ case arg
31
+ when Array
32
+ arg.each do |item|
33
+ self << item << "\n"
34
+ end
35
+ else
36
+ self << arg.to_s
37
+ self << "\n" unless arg =~ /\n$/
38
+ end
39
+ end
40
+ end
41
+
42
+ class Erubis::Main
43
+ public :usage
44
+ public :show_properties
45
+ public :show_enhancers
46
+ end
47
+
48
+
49
+ class MainTest < Test::Unit::TestCase
50
+
51
+ INPUT = <<'END'
52
+ list:
53
+ <% list = ['<aaa>', 'b&b', '"ccc"']
54
+ for item in list %>
55
+ - <%= item %>
56
+ <% end %>
57
+ user: <%= defined?(user) ? user : "(none)" %>
58
+ END
59
+ INPUT2 = INPUT.gsub(/\blist([^:])/, '@list\1').gsub(/\buser([^:])/, '@user\1')
60
+
61
+ # SRC = <<'END'
62
+ #_buf = ''; _buf << "list:\n"
63
+ # list = ['<aaa>', 'b&b', '"ccc"']
64
+ # for item in list
65
+ #_buf << " - "; _buf << ( item ).to_s; _buf << "\n"
66
+ # end
67
+ #_buf << "user: "; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << "\n"
68
+ #_buf
69
+ #END
70
+ SRC = <<'END'
71
+ _buf = ''; _buf << 'list:
72
+ '; list = ['<aaa>', 'b&b', '"ccc"']
73
+ for item in list
74
+ _buf << ' - '; _buf << ( item ).to_s; _buf << '
75
+ '; end
76
+ _buf << 'user: '; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << '
77
+ ';
78
+ _buf.to_s
79
+ END
80
+ # SRC2 = SRC.gsub(/\blist /, '@list ').gsub(/\buser /, '@user ')
81
+
82
+ OUTPUT = <<'END'
83
+ list:
84
+ - <aaa>
85
+ - b&b
86
+ - "ccc"
87
+ user: (none)
88
+ END
89
+
90
+ ESCAPED_OUTPUT = <<'END'
91
+ list:
92
+ - &lt;aaa&gt;
93
+ - b&amp;b
94
+ - &quot;ccc&quot;
95
+ user: (none)
96
+ END
97
+
98
+
99
+ PI_INPUT = <<'END'
100
+ <ul>
101
+ <?rb @list = ['<aaa>', 'b&b', '"ccc"']
102
+ for item in @list ?>
103
+ <li>@{item}@ / @!{item}@
104
+ <%= item %> / <%== item %></li>
105
+ <?rb end ?>
106
+ <ul>
107
+ END
108
+
109
+ PI_SRC = <<'END'
110
+ _buf = ''; _buf << '<ul>
111
+ '; @list = ['<aaa>', 'b&b', '"ccc"']
112
+ for item in @list
113
+ _buf << ' <li>'; _buf << Erubis::XmlHelper.escape_xml(item); _buf << ' / '; _buf << (item).to_s; _buf << '
114
+ '; _buf << ( item ).to_s; _buf << ' / '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</li>
115
+ '; end
116
+ _buf << '<ul>
117
+ ';
118
+ _buf.to_s
119
+ END
120
+
121
+ PI_ESCAPED_SRC = <<'END'
122
+ _buf = ''; _buf << '<ul>
123
+ '; @list = ['<aaa>', 'b&b', '"ccc"']
124
+ for item in @list
125
+ _buf << ' <li>'; _buf << (item).to_s; _buf << ' / '; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '
126
+ '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << ' / '; _buf << ( item ).to_s; _buf << '</li>
127
+ '; end
128
+ _buf << '<ul>
129
+ ';
130
+ _buf.to_s
131
+ END
132
+
133
+ PI_OUTPUT = <<'END'
134
+ <ul>
135
+ <li>&lt;aaa&gt; / <aaa>
136
+ <aaa> / &lt;aaa&gt;</li>
137
+ <li>b&amp;b / b&b
138
+ b&b / b&amp;b</li>
139
+ <li>&quot;ccc&quot; / "ccc"
140
+ "ccc" / &quot;ccc&quot;</li>
141
+ <ul>
142
+ END
143
+
144
+ PI_ESCAPED_OUTPUT = <<'END'
145
+ <ul>
146
+ <li><aaa> / &lt;aaa&gt;
147
+ &lt;aaa&gt; / <aaa></li>
148
+ <li>b&b / b&amp;b
149
+ b&amp;b / b&b</li>
150
+ <li>"ccc" / &quot;ccc&quot;
151
+ &quot;ccc&quot; / "ccc"</li>
152
+ <ul>
153
+ END
154
+
155
+ def _test()
156
+ if @filename.nil?
157
+ method = (caller[0] =~ /in `(.*)'/) && $1 #'
158
+ method =~ /block in (.*)/ and method = $1 # for Ruby 1.9
159
+ @filename = "tmp.#{method}"
160
+ end
161
+ File.open(@filename, 'w') {|f| f.write(@input) } if @filename
162
+ begin
163
+ argv = @options.is_a?(Array) ? @options.dup : @options.split
164
+ argv << @filename if @filename
165
+ $stdout = output = StringWriter.new
166
+ Erubis::Main.new.execute(argv)
167
+ ensure
168
+ $stdout = STDOUT
169
+ File.unlink(@filename) if @filename && test(?f, @filename)
170
+ end
171
+ assert_text_equal(@expected, output)
172
+ end
173
+
174
+ def _error_test(errclass, errmsg)
175
+ ex = assert_raise(errclass) { _test() }
176
+ assert_equal(errmsg, ex.message)
177
+ end
178
+
179
+
180
+ def test_help # -h
181
+ @options = '-h'
182
+ m = Erubis::Main.new
183
+ @expected = m.usage() + "\n" + m.show_properties() + m.show_enhancers()
184
+ @filename = false
185
+ _test()
186
+ end
187
+
188
+ def test_version # -v
189
+ @options = '-v'
190
+ @expected = (("$Release: 2.7.0 $" =~ /[.\d]+/) && $&) + "\n"
191
+ @filename = false
192
+ _test()
193
+ end
194
+
195
+
196
+ def test_basic1
197
+ @input = INPUT
198
+ @expected = OUTPUT
199
+ @options = ''
200
+ _test()
201
+ end
202
+
203
+
204
+ def test_source1 # -x
205
+ @input = INPUT
206
+ @expected = SRC
207
+ @options = '-x'
208
+ _test()
209
+ end
210
+
211
+
212
+ def _with_dummy_file
213
+ bindir = File.join(File.dirname(File.dirname(__FILE__)), 'bin')
214
+ env_path = ENV['PATH']
215
+ env__ = ENV['_']
216
+ begin
217
+ ENV['PATH'] = bindir + File::PATH_SEPARATOR + ENV['PATH']
218
+ ENV['_'] = 'erubis'
219
+ Tempfile.open(self.name.gsub(/[^\w]/,'_')) do |f|
220
+ f.write(INPUT)
221
+ f.flush
222
+ yield(f.path)
223
+ end
224
+ ensure
225
+ ENV['PATH'] = env_path
226
+ ENV['_'] = env__ if env__
227
+ end
228
+ end
229
+
230
+
231
+ def test_syntax1 # -z (syntax ok)
232
+ @input = INPUT
233
+ @expected = "Syntax OK\n"
234
+ @options = '-z'
235
+ _test()
236
+ #
237
+ _with_dummy_file do |filepath|
238
+ actual = `erubis #{@options} #{filepath}`
239
+ assert_equal @expected, actual
240
+ end
241
+ end
242
+
243
+
244
+ def test_syntax2 # -z (syntax error)
245
+ inputs = []
246
+ inputs << <<'END'
247
+ <ul>
248
+ <% for item in list %>
249
+ <li><%= item[:name]] %></li>
250
+ <% end %>
251
+ </ul>
252
+ END
253
+ inputs << <<'END'
254
+ <ul>
255
+ <% for item in list %>
256
+ <li><%= item[:name] %></li>
257
+ <% edn %>
258
+ </ul>
259
+ END
260
+ basename = 'tmp.test_syntax2_%d.rhtml'
261
+ filenames = [ basename % 0, basename % 1 ]
262
+ errmsgs = []
263
+ if ruby19?
264
+ errmsgs << <<'END'
265
+ 3: syntax error, unexpected ']', expecting ')'
266
+ _buf << ' <li>'; _buf << ( item[:name]] ).to_s; _buf << '</li>
267
+ ^
268
+ -:4: syntax error, unexpected keyword_end, expecting ')'
269
+ '; end
270
+ ^
271
+ -:7: syntax error, unexpected $end, expecting ')'
272
+ END
273
+ errmsgs << <<'END'
274
+ 7: syntax error, unexpected $end, expecting keyword_end
275
+ END
276
+ elsif rubinius?
277
+ errmsgs << <<'END'
278
+ 3: expecting ')'
279
+ _buf << ' <li>'; _buf << ( item[:name]] ).to_s; _buf << '</li>
280
+ ^
281
+ END
282
+ errmsgs << <<'END'
283
+ 7: missing 'end' for 'for' started on line 2
284
+ _buf.to_s
285
+ ^
286
+ END
287
+ else
288
+ errmsgs << <<'END'
289
+ 3: syntax error, unexpected ']', expecting ')'
290
+ _buf << ' <li>'; _buf << ( item[:name]] ).to_s; _buf << '</li>
291
+ ^
292
+ -:4: syntax error, unexpected kEND, expecting ')'
293
+ '; end
294
+ ^
295
+ -:7: syntax error, unexpected $end, expecting ')'
296
+ END
297
+ errmsgs << <<'END'
298
+ 7: syntax error, unexpected $end, expecting kEND
299
+ END
300
+ end
301
+ #
302
+ max = inputs.length
303
+ (0...max).each do |i|
304
+ @input = inputs[i]
305
+ @expected = "tmp.test_syntax2:#{errmsgs[i]}"
306
+ @options = '-z'
307
+ if rubinius?
308
+ @expected.sub! /unexpected kEND/, 'unexpected keyword_end'
309
+ @expected.sub! /expecting kEND/, 'expecting keyword_end'
310
+ end
311
+ _test()
312
+ end
313
+ #
314
+ begin
315
+ (0...max).each do |i|
316
+ File.open(filenames[i], 'w') {|f| f.write(inputs[i]) }
317
+ end
318
+ @input = '<ok/>'
319
+ @expected = ''
320
+ @options = '-z'
321
+ (0...max).each do |i|
322
+ @expected << "#{filenames[i]}:#{errmsgs[i]}"
323
+ @options << " #{filenames[i]}"
324
+ end
325
+ if rubinius?
326
+ @expected.sub! /unexpected kEND/, 'unexpected keyword_end'
327
+ @expected.sub! /expecting kEND/, 'expecting keyword_end'
328
+ end
329
+ _test()
330
+ ensure
331
+ (0...max).each do |i|
332
+ File.unlink(filenames[i]) if test(?f, filenames[i])
333
+ end
334
+ end
335
+ end
336
+
337
+
338
+ def test_pattern1 # -p
339
+ @input = INPUT.gsub(/<%/, '<!--%').gsub(/%>/, '%-->')
340
+ @expected = OUTPUT
341
+ #@options = "-p '<!--% %-->'"
342
+ @options = ["-p", "<!--% %-->"]
343
+ _test()
344
+ end
345
+
346
+
347
+ # def test_class1 # -C
348
+ # @input = INPUT
349
+ # @expected = OUTPUT.gsub(/<aaa>/, '&lt;aaa&gt;').gsub(/b&b/, 'b&amp;b').gsub(/"ccc"/, '&quot;ccc&quot;')
350
+ # @options = "-C XmlEruby"
351
+ # _test()
352
+ # end
353
+
354
+
355
+ def test_notrim1 # --trim=false
356
+ @input = INPUT
357
+ @expected = <<'END'
358
+ list:
359
+
360
+ - <aaa>
361
+
362
+ - b&b
363
+
364
+ - "ccc"
365
+
366
+ user: (none)
367
+ END
368
+ @options = "--trim=false" # -T
369
+ _test()
370
+ end
371
+
372
+
373
+ def test_notrim2 # --trim=false
374
+ @input = INPUT
375
+ # @expected = <<'END'
376
+ #_buf = ''; _buf << "list:\n"
377
+ # list = ['<aaa>', 'b&b', '"ccc"']
378
+ # for item in list ; _buf << "\n"
379
+ #_buf << " - "; _buf << ( item ).to_s; _buf << "\n"
380
+ # end ; _buf << "\n"
381
+ #_buf << "user: "; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << "\n"
382
+ #_buf
383
+ #END
384
+ @expected = <<'END'
385
+ _buf = ''; _buf << 'list:
386
+ '; list = ['<aaa>', 'b&b', '"ccc"']
387
+ for item in list ; _buf << '
388
+ '; _buf << ' - '; _buf << ( item ).to_s; _buf << '
389
+ '; end ; _buf << '
390
+ '; _buf << 'user: '; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << '
391
+ ';
392
+ _buf.to_s
393
+ END
394
+ @options = "-x --trim=false" # -xT
395
+ _test()
396
+ end
397
+
398
+
399
+ #--
400
+ #def test_context1
401
+ # @input = INPUT
402
+ # @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
403
+ # @options = '--user=Hello'
404
+ # _test()
405
+ #end
406
+ #++
407
+
408
+
409
+ def test_datafile1 # -f data.yaml
410
+ datafile = "test.context1.yaml"
411
+ @input = INPUT2
412
+ @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
413
+ @options = "-f #{datafile}"
414
+ #
415
+ str = <<-END
416
+ user: Hello
417
+ password: world
418
+ END
419
+ File.open(datafile, 'w') {|f| f.write(str) }
420
+ begin
421
+ _test()
422
+ ensure
423
+ File.unlink(datafile) if test(?f, datafile)
424
+ end
425
+ end
426
+
427
+
428
+ def test_datafile2 # -f data.rb
429
+ datafile = "test.context1.rb"
430
+ @input = INPUT2
431
+ @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
432
+ @options = "-f #{datafile}"
433
+ #
434
+ str = <<-END
435
+ @user = 'Hello'
436
+ @password = 'world'
437
+ END
438
+ File.open(datafile, 'w') {|f| f.write(str) }
439
+ begin
440
+ _test()
441
+ ensure
442
+ File.unlink(datafile) if test(?f, datafile)
443
+ end
444
+ end
445
+
446
+
447
+ def test_untabify1 # -t (obsolete)
448
+ yamlfile = "test.context2.yaml"
449
+ @input = INPUT2
450
+ @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
451
+ @options = "-tf #{yamlfile}"
452
+ #
453
+ yaml = <<-END
454
+ user: Hello
455
+ password: world
456
+ END
457
+ File.open(yamlfile, 'w') {|f| f.write(yaml) }
458
+ begin
459
+ _test()
460
+ ensure
461
+ File.unlink(yamlfile) if test(?f, yamlfile)
462
+ end
463
+ end
464
+
465
+
466
+ def test_untabify2 # -T
467
+ yamlfile = "test.context2.yaml"
468
+ @input = INPUT2
469
+ @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
470
+ @options = "-Tf #{yamlfile}"
471
+ #
472
+ yaml = <<-END
473
+ user: Hello
474
+ items:
475
+ - aaa
476
+ - bbb
477
+ - ccc
478
+ END
479
+ File.open(yamlfile, 'w') {|f| f.write(yaml) }
480
+ assert_raise(ArgumentError) do
481
+ _test()
482
+ end
483
+ File.open(yamlfile, 'w') {|f| f.write(yaml.gsub(/\t/, ' '*8)) }
484
+ _test()
485
+ ensure
486
+ File.unlink(yamlfile) if test(?f, yamlfile)
487
+ end
488
+
489
+
490
+ def test_symbolify1 # -S
491
+ yamlfile = "test.context3.yaml"
492
+ @input = <<END
493
+ <% for h in @list %>
494
+ <tr>
495
+ <td><%= h[:name] %></td><td><%= h[:mail] %></td>
496
+ </tr>
497
+ <% end %>
498
+ END
499
+ @expected = <<END
500
+ <tr>
501
+ <td>foo</td><td>foo@mail.com</td>
502
+ </tr>
503
+ <tr>
504
+ <td>bar</td><td>bar@mail.org</td>
505
+ </tr>
506
+ END
507
+ @options = "-f #{yamlfile} -S"
508
+ #
509
+ yaml = <<-END
510
+ list:
511
+ - name: foo
512
+ mail: foo@mail.com
513
+ - name: bar
514
+ mail: bar@mail.org
515
+ END
516
+ File.open(yamlfile, 'w') { |f| f.write(yaml) }
517
+ begin
518
+ _test()
519
+ ensure
520
+ File.unlink(yamlfile) if test(?f, yamlfile)
521
+ end
522
+ end
523
+
524
+
525
+ def test_result1 # -B
526
+ yamlfile = "test.context4.yaml"
527
+ #
528
+ @input = <<'END'
529
+ user = <%= user %>
530
+ <% for item in list %>
531
+ - <%= item %>
532
+ <% end %>
533
+ END
534
+ @expected = <<'END'
535
+ user = World
536
+ - aaa
537
+ - bbb
538
+ - ccc
539
+ END
540
+ @options = "-f #{yamlfile} -B "
541
+ #
542
+ yaml = <<-END
543
+ user: World
544
+ list:
545
+ - aaa
546
+ - bbb
547
+ - ccc
548
+ END
549
+ File.open(yamlfile, 'w') {|f| f.write(yaml) }
550
+ begin
551
+ _test()
552
+ ensure
553
+ File.unlink(yamlfile) if test(?f, yamlfile)
554
+ end
555
+ end
556
+
557
+
558
+ def test_context1 # -c
559
+ @input = <<'END'
560
+ user = <%= @user %>
561
+ <% for item in @list %>
562
+ - <%= item %>
563
+ <% end %>
564
+ END
565
+ @expected = <<'END'
566
+ user = World
567
+ - aaa
568
+ - bbb
569
+ - ccc
570
+ END
571
+ #
572
+ @options = ['-c', '{user: World, list: [aaa, bbb, ccc]}']
573
+ _test()
574
+ @options = ['-c', '@user="World"; @list=%w[aaa bbb ccc]']
575
+ _test()
576
+ end
577
+
578
+
579
+ def test_include1 # -I
580
+ dir = 'foo'
581
+ lib = 'bar'
582
+ Dir.mkdir dir unless test(?d, dir)
583
+ filename = "#{dir}/#{lib}.rb"
584
+ File.open(filename, 'w') do |f|
585
+ f.write <<-'END'
586
+ def escape(str)
587
+ return "<#{str.upcase}>"
588
+ end
589
+ END
590
+ end
591
+ #
592
+ @input = "<% require '#{lib}' %>\n" + INPUT.gsub(/<%= item %>/, '<%= escape(item) %>')
593
+ @expected = OUTPUT.gsub(/<aaa>/, '<<AAA>>').gsub(/b\&b/, '<B&B>').gsub(/"ccc"/, '<"CCC">')
594
+ @options = "-I #{dir}"
595
+ #
596
+ begin
597
+ _test()
598
+ ensure
599
+ File.unlink filename if test(?f, filename)
600
+ FileUtils.rm_r dir if test(?d, dir)
601
+ end
602
+ end
603
+
604
+
605
+ def test_require1 # -r
606
+ dir = 'foo'
607
+ lib = 'bar'
608
+ Dir.mkdir dir unless test(?d, dir)
609
+ filename = "#{dir}/#{lib}.rb"
610
+ File.open(filename, 'w') do |f|
611
+ f.write <<-'END'
612
+ def escape(str)
613
+ return "<#{str.upcase}>"
614
+ end
615
+ END
616
+ end
617
+ #
618
+ @input = INPUT.gsub(/<%= item %>/, '<%= escape(item) %>')
619
+ @expected = OUTPUT.gsub(/<aaa>/, '<<AAA>>').gsub(/b\&b/, '<B&B>').gsub(/"ccc"/, '<"CCC">')
620
+ @options = "-I #{dir} -r #{lib}"
621
+ #
622
+ begin
623
+ _test()
624
+ ensure
625
+ File.unlink filename if test(?f, filename)
626
+ FileUtils.rm_r dir if test(?d, dir)
627
+ end
628
+ end
629
+
630
+
631
+ def test_enhancers1 # -E
632
+ @input = <<END
633
+ <% list = %w[<aaa> b&b "ccc"] %>
634
+ % for item in list
635
+ - <%= item %> : <%== item %>
636
+ - [= item =] : [== item =]
637
+ % end
638
+ END
639
+ @expected = <<END
640
+ - &lt;aaa&gt; : <aaa>
641
+ - &lt;aaa&gt; : <aaa>
642
+ - b&amp;b : b&b
643
+ - b&amp;b : b&b
644
+ - &quot;ccc&quot; : "ccc"
645
+ - &quot;ccc&quot; : "ccc"
646
+ END
647
+ @options = "-E Escape,PercentLine,HeaderFooter,BiPattern"
648
+ _test()
649
+ end
650
+
651
+
652
+ def test_bodyonly1 # -b
653
+ @input = INPUT
654
+ @expected = SRC.sub(/\A_buf = '';/,'').sub(/\n_buf.to_s\n\z/,'')
655
+ @options = '-b -x'
656
+ _test()
657
+ end
658
+
659
+
660
+ def test_escape1 # -e
661
+ @input = INPUT
662
+ @expected = SRC.gsub(/<< \((.*?)\).to_s;/, '<< Erubis::XmlHelper.escape_xml(\1);')
663
+ @options = '-ex'
664
+ _test()
665
+ end
666
+
667
+
668
+ def test_invalid_option # -1 (invalid option)
669
+ @input = INPUT
670
+ @options = '-1'
671
+ _error_test(Erubis::CommandOptionError, "-1: unknown option.")
672
+ end
673
+
674
+
675
+ def test_invalid_enhancer # -E hoge
676
+ @options = '-E hoge'
677
+ errmsg = "hoge: no such Enhancer (try '-h' to show all enhancers)."
678
+ _error_test(Erubis::CommandOptionError, errmsg)
679
+ end
680
+
681
+
682
+ def test_invalid_lang # -l hoge
683
+ @options = '-l hoge'
684
+ errmsg = "-l hoge: invalid language name (class Erubis::Ehoge not found)."
685
+ _error_test(Erubis::CommandOptionError, errmsg)
686
+ end
687
+
688
+
689
+ def test_missing_argument # -E
690
+ @filename = false
691
+ @options = '-E'
692
+ _error_test(Erubis::CommandOptionError, "-E: enhancers required.")
693
+ @options = '-l'
694
+ _error_test(Erubis::CommandOptionError, "-l: lang required.")
695
+ end
696
+
697
+
698
+ def test_pi1 # --pi -x
699
+ @input = PI_INPUT
700
+ @expected = PI_SRC
701
+ @options = '-x --pi'
702
+ _test()
703
+ end
704
+
705
+ def test_pi2 # --pi -x --escape=false
706
+ @input = PI_INPUT
707
+ @expected = PI_ESCAPED_SRC
708
+ @options = '-x --pi --escape=false'
709
+ _test()
710
+ end
711
+
712
+ def test_pi3 # --pi
713
+ @input = PI_INPUT
714
+ @expected = PI_OUTPUT
715
+ @options = '--pi'
716
+ _test()
717
+ end
718
+
719
+ def test_pi4 # --pi --escape=false
720
+ @input = PI_INPUT
721
+ @expected = PI_ESCAPED_OUTPUT
722
+ @options = '--pi --escape=false'
723
+ _test()
724
+ end
725
+
726
+ def test_pi5 # --pi=ruby -x
727
+ @input = PI_INPUT.gsub(/<\?rb/, '<?ruby')
728
+ @expected = PI_SRC
729
+ @options = '--pi=ruby -x'
730
+ _test()
731
+ end
732
+
733
+ def test_pi6 # --pi -xl java
734
+ @input = <<'END'
735
+ <?java for (int i = 0; i < arr.length; i++) { ?>
736
+ - @{arr[i]}@ / @!{arr[i]}@
737
+ <?java } ?>
738
+ END
739
+ @expected = <<'END'
740
+ StringBuffer _buf = new StringBuffer(); for (int i = 0; i < arr.length; i++) {
741
+ _buf.append(" - "); _buf.append(escape(arr[i])); _buf.append(" / "); _buf.append(arr[i]); _buf.append("\n");
742
+ }
743
+ return _buf.toString();
744
+ END
745
+ @options = '--pi -xl java'
746
+ _test()
747
+ end
748
+
749
+
750
+ self.post_definition()
751
+
752
+ end