2004-02-28 04:52:14 +03:00
|
|
|
require 'test/unit'
|
|
|
|
require 'erb'
|
|
|
|
|
|
|
|
class TestERB < Test::Unit::TestCase
|
|
|
|
class MyError < RuntimeError ; end
|
|
|
|
|
|
|
|
def test_without_filename
|
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>")
|
|
|
|
e = assert_raise(MyError) {
|
|
|
|
erb.result
|
|
|
|
}
|
2008-02-19 09:24:36 +03:00
|
|
|
assert_match(/\A\(erb\):1\b/, e.backtrace[0])
|
2004-02-28 04:52:14 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_filename
|
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>")
|
|
|
|
erb.filename = "test filename"
|
|
|
|
e = assert_raise(MyError) {
|
|
|
|
erb.result
|
|
|
|
}
|
2008-02-19 09:24:36 +03:00
|
|
|
assert_match(/\Atest filename:1\b/, e.backtrace[0])
|
2004-02-28 04:52:14 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_without_filename_with_safe_level
|
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>", 1)
|
|
|
|
e = assert_raise(MyError) {
|
|
|
|
erb.result
|
|
|
|
}
|
2008-02-19 09:24:36 +03:00
|
|
|
assert_match(/\A\(erb\):1\b/, e.backtrace[0])
|
2004-02-28 04:52:14 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_filename_and_safe_level
|
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>", 1)
|
|
|
|
erb.filename = "test filename"
|
|
|
|
e = assert_raise(MyError) {
|
|
|
|
erb.result
|
|
|
|
}
|
2008-02-19 09:24:36 +03:00
|
|
|
assert_match(/\Atest filename:1\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestERBCore < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@erb = ERB
|
|
|
|
end
|
|
|
|
|
2008-04-30 16:40:52 +04:00
|
|
|
def test_core
|
|
|
|
_test_core(nil)
|
|
|
|
_test_core(0)
|
|
|
|
_test_core(1)
|
|
|
|
_test_core(2)
|
|
|
|
_test_core(3)
|
2008-02-19 09:24:36 +03:00
|
|
|
end
|
|
|
|
|
2008-04-30 16:40:52 +04:00
|
|
|
def _test_core(safe)
|
2008-02-19 09:24:36 +03:00
|
|
|
erb = @erb.new("hello")
|
2008-06-02 11:10:35 +04:00
|
|
|
assert_equal("hello", erb.result)
|
2008-02-19 09:24:36 +03:00
|
|
|
|
|
|
|
erb = @erb.new("hello", safe, 0)
|
2008-06-02 11:10:35 +04:00
|
|
|
assert_equal("hello", erb.result)
|
2008-02-19 09:24:36 +03:00
|
|
|
|
|
|
|
erb = @erb.new("hello", safe, 1)
|
2008-06-02 11:10:35 +04:00
|
|
|
assert_equal("hello", erb.result)
|
2008-02-19 09:24:36 +03:00
|
|
|
|
|
|
|
erb = @erb.new("hello", safe, 2)
|
2008-06-02 11:10:35 +04:00
|
|
|
assert_equal("hello", erb.result)
|
2008-02-19 09:24:36 +03:00
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
%% hi
|
|
|
|
= hello
|
|
|
|
<% 3.times do |n| %>
|
|
|
|
% n=0
|
|
|
|
* <%= n %>
|
|
|
|
<% end %>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%% hi
|
|
|
|
= hello
|
|
|
|
|
|
|
|
% n=0
|
|
|
|
* 0
|
|
|
|
|
|
|
|
% n=0
|
|
|
|
* 1
|
|
|
|
|
|
|
|
% n=0
|
|
|
|
* 2
|
|
|
|
|
|
|
|
EOS
|
|
|
|
erb = @erb.new(src)
|
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
erb = @erb.new(src, safe, 0)
|
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
erb = @erb.new(src, safe, '')
|
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%% hi
|
|
|
|
= hello
|
|
|
|
% n=0
|
|
|
|
* 0% n=0
|
|
|
|
* 1% n=0
|
|
|
|
* 2
|
|
|
|
EOS
|
|
|
|
erb = @erb.new(src, safe, 1)
|
|
|
|
assert_equal(ans.chomp, erb.result)
|
|
|
|
erb = @erb.new(src, safe, '>')
|
|
|
|
assert_equal(ans.chomp, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%% hi
|
|
|
|
= hello
|
|
|
|
% n=0
|
|
|
|
* 0
|
|
|
|
% n=0
|
|
|
|
* 1
|
|
|
|
% n=0
|
|
|
|
* 2
|
|
|
|
EOS
|
|
|
|
|
|
|
|
erb = @erb.new(src, safe, 2)
|
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
erb = @erb.new(src, safe, '<>')
|
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
% hi
|
|
|
|
= hello
|
|
|
|
|
|
|
|
* 0
|
|
|
|
|
|
|
|
* 0
|
|
|
|
|
|
|
|
* 0
|
|
|
|
|
|
|
|
EOS
|
|
|
|
erb = @erb.new(src, safe, '%')
|
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
% hi
|
|
|
|
= hello
|
|
|
|
* 0* 0* 0
|
|
|
|
EOS
|
|
|
|
erb = @erb.new(src, safe, '%>')
|
|
|
|
assert_equal(ans.chomp, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
% hi
|
|
|
|
= hello
|
|
|
|
* 0
|
|
|
|
* 0
|
|
|
|
* 0
|
|
|
|
EOS
|
|
|
|
erb = @erb.new(src, safe, '%<>')
|
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
end
|
|
|
|
|
2008-04-30 16:40:52 +04:00
|
|
|
def test_safe_04
|
2008-02-19 09:24:36 +03:00
|
|
|
erb = @erb.new('<%=$SAFE%>', 4)
|
2008-06-02 11:10:35 +04:00
|
|
|
assert_equal('4', erb.result(TOPLEVEL_BINDING.taint))
|
2008-02-19 09:24:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
class Foo; end
|
|
|
|
|
2008-04-30 16:40:52 +04:00
|
|
|
def test_def_class
|
2008-02-19 09:24:36 +03:00
|
|
|
erb = @erb.new('hello')
|
|
|
|
cls = erb.def_class
|
|
|
|
assert_equal(Object, cls.superclass)
|
|
|
|
assert(cls.new.respond_to?('result'))
|
|
|
|
cls = erb.def_class(Foo)
|
|
|
|
assert_equal(Foo, cls.superclass)
|
|
|
|
assert(cls.new.respond_to?('result'))
|
|
|
|
cls = erb.def_class(Object, 'erb')
|
|
|
|
assert_equal(Object, cls.superclass)
|
|
|
|
assert(cls.new.respond_to?('erb'))
|
|
|
|
end
|
|
|
|
|
2008-04-30 16:40:52 +04:00
|
|
|
def test_percent
|
2008-02-19 09:24:36 +03:00
|
|
|
src = <<EOS
|
|
|
|
%n = 1
|
|
|
|
<%= n%>
|
|
|
|
EOS
|
|
|
|
assert_equal("1\n", ERB.new(src, nil, '%').result)
|
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
<%
|
|
|
|
%>
|
|
|
|
EOS
|
|
|
|
ans = "\n"
|
|
|
|
assert_equal(ans, ERB.new(src, nil, '%').result)
|
|
|
|
|
|
|
|
src = "<%\n%>"
|
|
|
|
# ans = "\n"
|
|
|
|
ans = ""
|
|
|
|
assert_equal(ans, ERB.new(src, nil, '%').result)
|
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
<%
|
|
|
|
n = 1
|
|
|
|
%><%= n%>
|
|
|
|
EOS
|
|
|
|
assert_equal("1\n", ERB.new(src, nil, '%').result)
|
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
%n = 1
|
|
|
|
%% <% n = 2
|
|
|
|
n.times do |i|%>
|
|
|
|
%% %%><%%<%= i%><%
|
|
|
|
end%>
|
2009-01-03 18:36:14 +03:00
|
|
|
%%%
|
2008-02-19 09:24:36 +03:00
|
|
|
EOS
|
|
|
|
ans = <<EOS
|
|
|
|
%
|
|
|
|
% %%><%0
|
|
|
|
% %%><%1
|
2009-01-03 18:36:14 +03:00
|
|
|
%%
|
2008-02-19 09:24:36 +03:00
|
|
|
EOS
|
|
|
|
assert_equal(ans, ERB.new(src, nil, '%').result)
|
|
|
|
end
|
|
|
|
|
2008-06-27 12:48:24 +04:00
|
|
|
def test_def_erb_method
|
2009-01-03 18:36:14 +03:00
|
|
|
klass = Class.new
|
|
|
|
klass.module_eval do
|
2008-02-19 09:24:36 +03:00
|
|
|
extend ERB::DefMethod
|
|
|
|
fname = File.join(File.dirname(File.expand_path(__FILE__)), 'hello.erb')
|
|
|
|
def_erb_method('hello', fname)
|
|
|
|
end
|
2009-01-03 18:36:14 +03:00
|
|
|
assert(klass.new.respond_to?('hello'))
|
2008-02-19 09:24:36 +03:00
|
|
|
|
2009-01-03 18:36:14 +03:00
|
|
|
assert(! klass.new.respond_to?('hello_world'))
|
2008-02-19 09:24:36 +03:00
|
|
|
erb = @erb.new('hello, world')
|
2009-01-03 18:36:14 +03:00
|
|
|
klass.module_eval do
|
2008-02-19 09:24:36 +03:00
|
|
|
def_erb_method('hello_world', erb)
|
|
|
|
end
|
2009-01-03 18:36:14 +03:00
|
|
|
assert(klass.new.respond_to?('hello_world'))
|
2008-02-19 09:24:36 +03:00
|
|
|
end
|
|
|
|
|
2008-06-27 12:48:24 +04:00
|
|
|
def test_def_method_without_filename
|
2009-01-03 18:36:14 +03:00
|
|
|
klass = Class.new
|
2008-06-27 12:48:24 +04:00
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>")
|
|
|
|
erb.filename = "test filename"
|
2009-01-03 18:36:14 +03:00
|
|
|
assert(! klass.new.respond_to?('my_error'))
|
|
|
|
erb.def_method(klass, 'my_error')
|
2008-06-27 12:48:24 +04:00
|
|
|
e = assert_raise(::TestERB::MyError) {
|
2009-01-03 18:36:14 +03:00
|
|
|
klass.new.my_error
|
2008-06-27 12:48:24 +04:00
|
|
|
}
|
|
|
|
assert_match(/\A\(ERB\):1\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_def_method_with_fname
|
2009-01-03 18:36:14 +03:00
|
|
|
klass = Class.new
|
2008-06-27 12:48:24 +04:00
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>")
|
|
|
|
erb.filename = "test filename"
|
2009-01-03 18:36:14 +03:00
|
|
|
assert(! klass.new.respond_to?('my_error'))
|
|
|
|
erb.def_method(klass, 'my_error', 'test fname')
|
2008-06-27 12:48:24 +04:00
|
|
|
e = assert_raise(::TestERB::MyError) {
|
2009-01-03 18:36:14 +03:00
|
|
|
klass.new.my_error
|
2008-06-27 12:48:24 +04:00
|
|
|
}
|
|
|
|
assert_match(/\Atest fname:1\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
|
2008-04-30 16:40:52 +04:00
|
|
|
def test_escape
|
2008-02-19 09:24:36 +03:00
|
|
|
src = <<EOS
|
|
|
|
1.<%% : <%="<%%"%>
|
|
|
|
2.%%> : <%="%%>"%>
|
|
|
|
3.
|
|
|
|
% x = "foo"
|
|
|
|
<%=x%>
|
|
|
|
4.
|
|
|
|
%% print "foo"
|
|
|
|
5.
|
|
|
|
%% <%="foo"%>
|
|
|
|
6.<%="
|
|
|
|
% print 'foo'
|
|
|
|
"%>
|
|
|
|
7.<%="
|
|
|
|
%% print 'foo'
|
|
|
|
"%>
|
|
|
|
EOS
|
|
|
|
ans = <<EOS
|
|
|
|
1.<% : <%%
|
|
|
|
2.%%> : %>
|
|
|
|
3.
|
|
|
|
foo
|
|
|
|
4.
|
|
|
|
% print "foo"
|
|
|
|
5.
|
|
|
|
% foo
|
|
|
|
6.
|
|
|
|
% print 'foo'
|
|
|
|
|
|
|
|
7.
|
|
|
|
%% print 'foo'
|
|
|
|
|
|
|
|
EOS
|
|
|
|
assert_equal(ans, ERB.new(src, nil, '%').result)
|
|
|
|
end
|
|
|
|
|
2008-04-30 16:40:52 +04:00
|
|
|
def test_keep_lineno
|
2008-02-19 09:24:36 +03:00
|
|
|
src = <<EOS
|
|
|
|
Hello,
|
|
|
|
% x = "World"
|
|
|
|
<%= x%>
|
|
|
|
% raise("lineno")
|
|
|
|
EOS
|
|
|
|
|
|
|
|
erb = ERB.new(src, nil, '%')
|
|
|
|
begin
|
|
|
|
erb.result
|
|
|
|
assert(false)
|
|
|
|
rescue
|
|
|
|
assert_match(/\A\(erb\):4\b/, $@[0].to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
%>
|
|
|
|
Hello,
|
|
|
|
<% x = "World%%>
|
|
|
|
"%>
|
|
|
|
<%= x%>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%>Hello,
|
|
|
|
World%>
|
|
|
|
EOS
|
|
|
|
assert_equal(ans, ERB.new(src, nil, '>').result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%>
|
|
|
|
Hello,
|
|
|
|
|
|
|
|
World%>
|
|
|
|
EOS
|
|
|
|
assert_equal(ans, ERB.new(src, nil, '<>').result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%>
|
|
|
|
Hello,
|
|
|
|
|
|
|
|
World%>
|
|
|
|
|
|
|
|
EOS
|
|
|
|
assert_equal(ans, ERB.new(src).result)
|
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
Hello,
|
|
|
|
<% x = "World%%>
|
|
|
|
"%>
|
|
|
|
<%= x%>
|
|
|
|
<% raise("lineno") %>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
erb = ERB.new(src)
|
|
|
|
begin
|
|
|
|
erb.result
|
|
|
|
assert(false)
|
|
|
|
rescue
|
|
|
|
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
erb = ERB.new(src, nil, '>')
|
|
|
|
begin
|
|
|
|
erb.result
|
|
|
|
assert(false)
|
|
|
|
rescue
|
|
|
|
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
erb = ERB.new(src, nil, '<>')
|
|
|
|
begin
|
|
|
|
erb.result
|
|
|
|
assert(false)
|
|
|
|
rescue
|
|
|
|
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
% y = 'Hello'
|
|
|
|
<%- x = "World%%>
|
|
|
|
"-%>
|
|
|
|
<%= x %><%- x = nil -%>
|
|
|
|
<% raise("lineno") %>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
erb = ERB.new(src, nil, '-')
|
|
|
|
begin
|
|
|
|
erb.result
|
|
|
|
assert(false)
|
|
|
|
rescue
|
|
|
|
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
erb = ERB.new(src, nil, '%-')
|
|
|
|
begin
|
|
|
|
erb.result
|
|
|
|
assert(false)
|
|
|
|
rescue
|
|
|
|
assert_match(/\A\(erb\):5\b/, $@[0].to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-30 16:40:52 +04:00
|
|
|
def test_explicit
|
2008-02-19 09:24:36 +03:00
|
|
|
src = <<EOS
|
|
|
|
<% x = %w(hello world) -%>
|
|
|
|
NotSkip <%- y = x -%> NotSkip
|
|
|
|
<% x.each do |w| -%>
|
|
|
|
<%- up = w.upcase -%>
|
|
|
|
* <%= up %>
|
|
|
|
<% end -%>
|
|
|
|
<%- z = nil -%> NotSkip <%- z = x %>
|
|
|
|
<%- z.each do |w| -%>
|
|
|
|
<%- down = w.downcase -%>
|
|
|
|
* <%= down %>
|
|
|
|
<%- up = w.upcase -%>
|
|
|
|
* <%= up %>
|
|
|
|
<%- end -%>
|
|
|
|
KeepNewLine <%- z = nil -%>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
NotSkip NotSkip
|
|
|
|
* HELLO
|
|
|
|
* WORLD
|
|
|
|
NotSkip
|
|
|
|
* hello
|
|
|
|
* HELLO
|
|
|
|
* world
|
|
|
|
* WORLD
|
|
|
|
KeepNewLine
|
|
|
|
EOS
|
|
|
|
assert_equal(ans, ERB.new(src, nil, '-').result)
|
|
|
|
assert_equal(ans, ERB.new(src, nil, '-%').result)
|
2004-02-28 04:52:14 +03:00
|
|
|
end
|
2008-04-30 16:40:52 +04:00
|
|
|
|
|
|
|
def test_url_encode
|
|
|
|
assert_equal("Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide",
|
|
|
|
ERB::Util.url_encode("Programming Ruby: The Pragmatic Programmer's Guide"))
|
|
|
|
|
|
|
|
assert_equal("%A5%B5%A5%F3%A5%D7%A5%EB",
|
|
|
|
ERB::Util.url_encode("\xA5\xB5\xA5\xF3\xA5\xD7\xA5\xEB".force_encoding("EUC-JP")))
|
|
|
|
end
|
2004-02-28 04:52:14 +03:00
|
|
|
end
|
2009-01-03 18:36:14 +03:00
|
|
|
|
|
|
|
class TestERBCoreWOStrScan < TestERBCore
|
|
|
|
def setup
|
|
|
|
@save_map = ERB::Compiler::Scanner.instance_variable_get('@scanner_map')
|
|
|
|
map = {[nil, false]=>ERB::Compiler::SimpleScanner}
|
|
|
|
ERB::Compiler::Scanner.instance_variable_set('@scanner_map', map)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
ERB::Compiler::Scanner.instance_variable_set('@scanner_map', @save_map)
|
|
|
|
end
|
|
|
|
end
|