2012-11-09 09:33:11 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2004-02-27 20:52:14 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'erb'
|
2017-11-21 00:01:28 -05:00
|
|
|
require 'stringio'
|
2004-02-27 20:52:14 -05:00
|
|
|
|
|
|
|
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 01:24:36 -05:00
|
|
|
assert_match(/\A\(erb\):1\b/, e.backtrace[0])
|
2004-02-27 20:52:14 -05: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 01:24:36 -05:00
|
|
|
assert_match(/\Atest filename:1\b/, e.backtrace[0])
|
2004-02-27 20:52:14 -05:00
|
|
|
end
|
|
|
|
|
2022-01-17 15:37:46 -05:00
|
|
|
# [deprecated] This will be removed later
|
|
|
|
def test_without_filename_with_safe_level
|
|
|
|
erb = EnvUtil.suppress_warning do
|
|
|
|
ERB.new("<% raise ::TestERB::MyError %>", 1)
|
|
|
|
end
|
|
|
|
e = assert_raise(MyError) {
|
|
|
|
erb.result
|
|
|
|
}
|
|
|
|
assert_match(/\A\(erb\):1\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
# [deprecated] This will be removed later
|
|
|
|
def test_with_filename_and_safe_level
|
|
|
|
erb = EnvUtil.suppress_warning do
|
|
|
|
ERB.new("<% raise ::TestERB::MyError %>", 1)
|
|
|
|
end
|
|
|
|
erb.filename = "test filename"
|
|
|
|
e = assert_raise(MyError) {
|
|
|
|
erb.result
|
|
|
|
}
|
|
|
|
assert_match(/\Atest filename:1\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
|
2014-12-12 05:31:47 -05:00
|
|
|
def test_with_filename_lineno
|
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>")
|
|
|
|
erb.filename = "test filename"
|
|
|
|
erb.lineno = 100
|
|
|
|
e = assert_raise(MyError) {
|
|
|
|
erb.result
|
|
|
|
}
|
|
|
|
assert_match(/\Atest filename:101\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_location
|
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>")
|
|
|
|
erb.location = ["test filename", 200]
|
|
|
|
e = assert_raise(MyError) {
|
|
|
|
erb.result
|
|
|
|
}
|
|
|
|
assert_match(/\Atest filename:201\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
|
2012-08-13 00:17:00 -04:00
|
|
|
def test_html_escape
|
2012-08-21 18:30:47 -04:00
|
|
|
assert_equal(" !"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
|
2012-08-13 00:17:00 -04:00
|
|
|
ERB::Util.html_escape(" !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"))
|
|
|
|
|
|
|
|
assert_equal("", ERB::Util.html_escape(""))
|
|
|
|
assert_equal("abc", ERB::Util.html_escape("abc"))
|
2012-11-09 09:33:11 -05:00
|
|
|
assert_equal("<<", ERB::Util.html_escape("<\<"))
|
2022-11-03 01:28:45 -04:00
|
|
|
assert_equal("'&"><", ERB::Util.html_escape("'&\"><"))
|
2012-08-14 03:28:43 -04:00
|
|
|
|
|
|
|
assert_equal("", ERB::Util.html_escape(nil))
|
|
|
|
assert_equal("123", ERB::Util.html_escape(123))
|
2012-08-13 00:17:00 -04:00
|
|
|
end
|
2012-11-09 09:33:11 -05:00
|
|
|
|
2022-11-03 01:28:45 -04:00
|
|
|
def test_html_escape_to_s
|
|
|
|
object = Object.new
|
|
|
|
def object.to_s
|
|
|
|
"object"
|
|
|
|
end
|
|
|
|
assert_equal("object", ERB::Util.html_escape(object))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_html_escape_extension
|
|
|
|
assert_nil(ERB::Util.method(:html_escape).source_location)
|
|
|
|
end if RUBY_ENGINE == 'ruby'
|
|
|
|
|
2012-11-09 09:33:11 -05:00
|
|
|
def test_concurrent_default_binding
|
2022-11-03 01:56:24 -04:00
|
|
|
# This test randomly fails with JRuby -- NameError: undefined local variable or method `template2'
|
|
|
|
pend if RUBY_ENGINE == 'jruby'
|
|
|
|
|
2012-11-09 09:33:11 -05:00
|
|
|
template1 = 'one <%= ERB.new(template2).result %>'
|
|
|
|
|
|
|
|
eval 'template2 = "two"', TOPLEVEL_BINDING
|
|
|
|
|
|
|
|
bug7046 = '[ruby-core:47638]'
|
|
|
|
assert_equal("one two", ERB.new(template1).result, bug7046)
|
|
|
|
end
|
2008-02-19 01:24:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestERBCore < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@erb = ERB
|
|
|
|
end
|
|
|
|
|
2017-09-26 07:30:45 -04:00
|
|
|
def test_version
|
|
|
|
assert_equal(String, @erb.version.class)
|
|
|
|
end
|
|
|
|
|
2008-04-30 08:40:52 -04:00
|
|
|
def test_core
|
2022-01-17 15:37:46 -05:00
|
|
|
# [deprecated] Fix initializer later
|
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
_test_core(nil)
|
|
|
|
_test_core(0)
|
|
|
|
_test_core(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_core(safe)
|
2008-02-19 01:24:36 -05:00
|
|
|
erb = @erb.new("hello")
|
2008-06-02 03:10:35 -04:00
|
|
|
assert_equal("hello", erb.result)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new("hello", safe, 0)
|
2008-06-02 03:10:35 -04:00
|
|
|
assert_equal("hello", erb.result)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new("hello", safe, 1)
|
2008-06-02 03:10:35 -04:00
|
|
|
assert_equal("hello", erb.result)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new("hello", safe, 2)
|
2008-06-02 03:10:35 -04:00
|
|
|
assert_equal("hello", erb.result)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
%% hi
|
|
|
|
= hello
|
|
|
|
<% 3.times do |n| %>
|
|
|
|
% n=0
|
|
|
|
* <%= n %>
|
|
|
|
<% end %>
|
|
|
|
EOS
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-02-19 01:24:36 -05:00
|
|
|
ans = <<EOS
|
|
|
|
%% hi
|
|
|
|
= hello
|
|
|
|
|
|
|
|
% n=0
|
|
|
|
* 0
|
|
|
|
|
|
|
|
% n=0
|
|
|
|
* 1
|
|
|
|
|
|
|
|
% n=0
|
|
|
|
* 2
|
|
|
|
|
|
|
|
EOS
|
|
|
|
erb = @erb.new(src)
|
|
|
|
assert_equal(ans, erb.result)
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, 0)
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans, erb.result)
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, '')
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%% hi
|
|
|
|
= hello
|
|
|
|
% n=0
|
|
|
|
* 0% n=0
|
|
|
|
* 1% n=0
|
|
|
|
* 2
|
|
|
|
EOS
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, 1)
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans.chomp, erb.result)
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, '>')
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans.chomp, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%% hi
|
|
|
|
= hello
|
|
|
|
% n=0
|
|
|
|
* 0
|
|
|
|
% n=0
|
|
|
|
* 1
|
|
|
|
% n=0
|
|
|
|
* 2
|
|
|
|
EOS
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, 2)
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans, erb.result)
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, '<>')
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
% hi
|
|
|
|
= hello
|
|
|
|
|
|
|
|
* 0
|
|
|
|
|
|
|
|
* 0
|
|
|
|
|
|
|
|
* 0
|
|
|
|
|
|
|
|
EOS
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, '%')
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
% hi
|
|
|
|
= hello
|
|
|
|
* 0* 0* 0
|
|
|
|
EOS
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, '%>')
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans.chomp, erb.result)
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
% hi
|
|
|
|
= hello
|
|
|
|
* 0
|
|
|
|
* 0
|
|
|
|
* 0
|
|
|
|
EOS
|
2022-01-17 15:37:46 -05:00
|
|
|
erb = @erb.new(src, safe, '%<>')
|
2008-02-19 01:24:36 -05:00
|
|
|
assert_equal(ans, erb.result)
|
|
|
|
end
|
|
|
|
|
2017-05-20 13:36:09 -04:00
|
|
|
def test_trim_line1_with_carriage_return
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = @erb.new("<% 3.times do %>\r\nline\r\n<% end %>\r\n", trim_mode: '>')
|
2017-05-20 13:36:09 -04:00
|
|
|
assert_equal("line\r\n" * 3, erb.result)
|
|
|
|
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = @erb.new("<% 3.times do %>\r\nline\r\n<% end %>\r\n", trim_mode: '%>')
|
2017-05-20 13:36:09 -04:00
|
|
|
assert_equal("line\r\n" * 3, erb.result)
|
|
|
|
end
|
|
|
|
|
2017-05-20 12:50:33 -04:00
|
|
|
def test_trim_line2_with_carriage_return
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = @erb.new("<% 3.times do %>\r\nline\r\n<% end %>\r\n", trim_mode: '<>')
|
2017-05-20 12:50:33 -04:00
|
|
|
assert_equal("line\r\n" * 3, erb.result)
|
2017-05-20 13:17:22 -04:00
|
|
|
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = @erb.new("<% 3.times do %>\r\nline\r\n<% end %>\r\n", trim_mode: '%<>')
|
2017-05-20 13:17:22 -04:00
|
|
|
assert_equal("line\r\n" * 3, erb.result)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_trim_line_with_carriage_return
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = @erb.new("<%- 3.times do -%>\r\nline\r\n<%- end -%>\r\n", trim_mode: '-')
|
2017-05-20 13:17:22 -04:00
|
|
|
assert_equal("line\r\n" * 3, erb.result)
|
|
|
|
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = @erb.new("<%- 3.times do -%>\r\nline\r\n<%- end -%>\r\n", trim_mode: '%-')
|
2017-05-20 13:17:22 -04:00
|
|
|
assert_equal("line\r\n" * 3, erb.result)
|
2017-05-20 12:50:33 -04:00
|
|
|
end
|
|
|
|
|
2018-11-11 19:37:15 -05:00
|
|
|
def test_invalid_trim_mode
|
2022-11-03 01:53:39 -04:00
|
|
|
pend if RUBY_ENGINE == 'truffleruby'
|
|
|
|
|
2018-11-11 19:37:15 -05:00
|
|
|
assert_warning(/#{__FILE__}:#{__LINE__ + 1}/) do
|
|
|
|
@erb.new("", trim_mode: 'abc-def')
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_warning(/Invalid ERB trim mode/) do
|
|
|
|
@erb.new("", trim_mode: 'abc-def')
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_warning(/Invalid ERB trim mode/) do
|
|
|
|
@erb.new("", trim_mode: '%<')
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_warning(/Invalid ERB trim mode/) do
|
|
|
|
@erb.new("", trim_mode: '%<>-')
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_warning(/Invalid ERB trim mode/) do
|
|
|
|
@erb.new("", trim_mode: 3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-26 07:30:45 -04:00
|
|
|
def test_run
|
|
|
|
out = StringIO.new
|
|
|
|
orig, $stdout = $stdout, out
|
|
|
|
|
|
|
|
num = 3
|
|
|
|
@erb.new('<%= num * 3 %>').run(binding)
|
|
|
|
|
|
|
|
$stdout = orig
|
|
|
|
out.rewind
|
|
|
|
assert_equal('9', out.read)
|
2017-11-21 00:01:28 -05:00
|
|
|
return unless num # to remove warning
|
2017-09-26 07:30:45 -04:00
|
|
|
end
|
|
|
|
|
2008-02-19 01:24:36 -05:00
|
|
|
class Foo; end
|
|
|
|
|
2008-04-30 08:40:52 -04:00
|
|
|
def test_def_class
|
2008-02-19 01:24:36 -05:00
|
|
|
erb = @erb.new('hello')
|
|
|
|
cls = erb.def_class
|
|
|
|
assert_equal(Object, cls.superclass)
|
2016-01-28 22:18:05 -05:00
|
|
|
assert_respond_to(cls.new, 'result')
|
2008-02-19 01:24:36 -05:00
|
|
|
cls = erb.def_class(Foo)
|
|
|
|
assert_equal(Foo, cls.superclass)
|
2016-01-28 22:18:05 -05:00
|
|
|
assert_respond_to(cls.new, 'result')
|
2008-02-19 01:24:36 -05:00
|
|
|
cls = erb.def_class(Object, 'erb')
|
|
|
|
assert_equal(Object, cls.superclass)
|
2016-01-28 22:18:05 -05:00
|
|
|
assert_respond_to(cls.new, 'erb')
|
2008-02-19 01:24:36 -05:00
|
|
|
end
|
|
|
|
|
2008-04-30 08:40:52 -04:00
|
|
|
def test_percent
|
2008-02-19 01:24:36 -05:00
|
|
|
src = <<EOS
|
|
|
|
%n = 1
|
|
|
|
<%= n%>
|
|
|
|
EOS
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal("1\n", ERB.new(src, trim_mode: '%').result(binding))
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
<%
|
|
|
|
%>
|
|
|
|
EOS
|
|
|
|
ans = "\n"
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal(ans, ERB.new(src, trim_mode: '%').result(binding))
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
src = "<%\n%>"
|
|
|
|
# ans = "\n"
|
|
|
|
ans = ""
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal(ans, ERB.new(src, trim_mode: '%').result(binding))
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
<%
|
|
|
|
n = 1
|
|
|
|
%><%= n%>
|
|
|
|
EOS
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal("1\n", ERB.new(src, trim_mode: '%').result(binding))
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
%n = 1
|
|
|
|
%% <% n = 2
|
|
|
|
n.times do |i|%>
|
|
|
|
%% %%><%%<%= i%><%
|
|
|
|
end%>
|
2009-01-03 10:36:14 -05:00
|
|
|
%%%
|
2008-02-19 01:24:36 -05:00
|
|
|
EOS
|
|
|
|
ans = <<EOS
|
2012-12-07 00:02:18 -05:00
|
|
|
%\s
|
2008-02-19 01:24:36 -05:00
|
|
|
% %%><%0
|
|
|
|
% %%><%1
|
2009-01-03 10:36:14 -05:00
|
|
|
%%
|
2008-02-19 01:24:36 -05:00
|
|
|
EOS
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal(ans, ERB.new(src, trim_mode: '%').result(binding))
|
2008-02-19 01:24:36 -05:00
|
|
|
end
|
|
|
|
|
2008-06-27 04:48:24 -04:00
|
|
|
def test_def_erb_method
|
2009-01-03 10:36:14 -05:00
|
|
|
klass = Class.new
|
|
|
|
klass.module_eval do
|
2008-02-19 01:24:36 -05:00
|
|
|
extend ERB::DefMethod
|
|
|
|
fname = File.join(File.dirname(File.expand_path(__FILE__)), 'hello.erb')
|
|
|
|
def_erb_method('hello', fname)
|
|
|
|
end
|
2016-01-28 22:18:05 -05:00
|
|
|
assert_respond_to(klass.new, 'hello')
|
2008-02-19 01:24:36 -05:00
|
|
|
|
2016-01-28 22:18:05 -05:00
|
|
|
assert_not_respond_to(klass.new, 'hello_world')
|
2008-02-19 01:24:36 -05:00
|
|
|
erb = @erb.new('hello, world')
|
2009-01-03 10:36:14 -05:00
|
|
|
klass.module_eval do
|
2008-02-19 01:24:36 -05:00
|
|
|
def_erb_method('hello_world', erb)
|
|
|
|
end
|
2016-01-28 22:18:05 -05:00
|
|
|
assert_respond_to(klass.new, 'hello_world')
|
2008-02-19 01:24:36 -05:00
|
|
|
end
|
|
|
|
|
2008-06-27 04:48:24 -04:00
|
|
|
def test_def_method_without_filename
|
2009-01-03 10:36:14 -05:00
|
|
|
klass = Class.new
|
2008-06-27 04:48:24 -04:00
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>")
|
|
|
|
erb.filename = "test filename"
|
2016-01-28 22:18:05 -05:00
|
|
|
assert_not_respond_to(klass.new, 'my_error')
|
2009-01-03 10:36:14 -05:00
|
|
|
erb.def_method(klass, 'my_error')
|
2008-06-27 04:48:24 -04:00
|
|
|
e = assert_raise(::TestERB::MyError) {
|
2009-01-03 10:36:14 -05:00
|
|
|
klass.new.my_error
|
2008-06-27 04:48:24 -04:00
|
|
|
}
|
|
|
|
assert_match(/\A\(ERB\):1\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_def_method_with_fname
|
2009-01-03 10:36:14 -05:00
|
|
|
klass = Class.new
|
2008-06-27 04:48:24 -04:00
|
|
|
erb = ERB.new("<% raise ::TestERB::MyError %>")
|
|
|
|
erb.filename = "test filename"
|
2016-01-28 22:18:05 -05:00
|
|
|
assert_not_respond_to(klass.new, 'my_error')
|
2009-01-03 10:36:14 -05:00
|
|
|
erb.def_method(klass, 'my_error', 'test fname')
|
2008-06-27 04:48:24 -04:00
|
|
|
e = assert_raise(::TestERB::MyError) {
|
2009-01-03 10:36:14 -05:00
|
|
|
klass.new.my_error
|
2008-06-27 04:48:24 -04:00
|
|
|
}
|
|
|
|
assert_match(/\Atest fname:1\b/, e.backtrace[0])
|
|
|
|
end
|
|
|
|
|
2017-09-26 07:30:45 -04:00
|
|
|
def test_def_module
|
|
|
|
klass = Class.new
|
|
|
|
klass.include ERB.new('<%= val %>').def_module('render(val)')
|
|
|
|
assert_equal('1', klass.new.render(1))
|
|
|
|
end
|
|
|
|
|
2008-04-30 08:40:52 -04:00
|
|
|
def test_escape
|
2008-02-19 01:24:36 -05: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
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal(ans, ERB.new(src, trim_mode: '%').result)
|
2008-02-19 01:24:36 -05:00
|
|
|
end
|
|
|
|
|
2008-04-30 08:40:52 -04:00
|
|
|
def test_keep_lineno
|
2008-02-19 01:24:36 -05:00
|
|
|
src = <<EOS
|
2012-12-07 00:02:18 -05:00
|
|
|
Hello,\s
|
2008-02-19 01:24:36 -05:00
|
|
|
% x = "World"
|
|
|
|
<%= x%>
|
|
|
|
% raise("lineno")
|
|
|
|
EOS
|
|
|
|
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = ERB.new(src, trim_mode: '%')
|
2016-01-28 22:18:05 -05:00
|
|
|
e = assert_raise(RuntimeError) {
|
2008-02-19 01:24:36 -05:00
|
|
|
erb.result
|
2016-01-28 22:18:05 -05:00
|
|
|
}
|
|
|
|
assert_match(/\A\(erb\):4\b/, e.backtrace[0].to_s)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
%>
|
2012-12-07 00:02:18 -05:00
|
|
|
Hello,\s
|
2008-02-19 01:24:36 -05:00
|
|
|
<% x = "World%%>
|
|
|
|
"%>
|
|
|
|
<%= x%>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
ans = <<EOS
|
2012-12-07 00:02:18 -05:00
|
|
|
%>Hello,\s
|
2008-02-19 01:24:36 -05:00
|
|
|
World%>
|
|
|
|
EOS
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal(ans, ERB.new(src, trim_mode: '>').result)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%>
|
2012-12-07 00:02:18 -05:00
|
|
|
Hello,\s
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
World%>
|
|
|
|
EOS
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal(ans, ERB.new(src, trim_mode: '<>').result)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
%>
|
2012-12-07 00:02:18 -05:00
|
|
|
Hello,\s
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
World%>
|
|
|
|
|
|
|
|
EOS
|
|
|
|
assert_equal(ans, ERB.new(src).result)
|
|
|
|
|
|
|
|
src = <<EOS
|
2012-12-07 00:02:18 -05:00
|
|
|
Hello,\s
|
2008-02-19 01:24:36 -05:00
|
|
|
<% x = "World%%>
|
|
|
|
"%>
|
|
|
|
<%= x%>
|
|
|
|
<% raise("lineno") %>
|
|
|
|
EOS
|
|
|
|
|
|
|
|
erb = ERB.new(src)
|
2016-01-28 22:18:05 -05:00
|
|
|
e = assert_raise(RuntimeError) {
|
2008-02-19 01:24:36 -05:00
|
|
|
erb.result
|
2016-01-28 22:18:05 -05:00
|
|
|
}
|
|
|
|
assert_match(/\A\(erb\):5\b/, e.backtrace[0].to_s)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = ERB.new(src, trim_mode: '>')
|
2016-01-28 22:18:05 -05:00
|
|
|
e = assert_raise(RuntimeError) {
|
2008-02-19 01:24:36 -05:00
|
|
|
erb.result
|
2016-01-28 22:18:05 -05:00
|
|
|
}
|
|
|
|
assert_match(/\A\(erb\):5\b/, e.backtrace[0].to_s)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = ERB.new(src, trim_mode: '<>')
|
2016-01-28 22:18:05 -05:00
|
|
|
e = assert_raise(RuntimeError) {
|
2008-02-19 01:24:36 -05:00
|
|
|
erb.result
|
2016-01-28 22:18:05 -05:00
|
|
|
}
|
|
|
|
assert_match(/\A\(erb\):5\b/, e.backtrace[0].to_s)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
|
|
|
src = <<EOS
|
|
|
|
% y = 'Hello'
|
|
|
|
<%- x = "World%%>
|
|
|
|
"-%>
|
2012-12-07 00:02:18 -05:00
|
|
|
<%= x %><%- x = nil -%>\s
|
2008-02-19 01:24:36 -05:00
|
|
|
<% raise("lineno") %>
|
|
|
|
EOS
|
|
|
|
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = ERB.new(src, trim_mode: '-')
|
2016-01-28 22:18:05 -05:00
|
|
|
e = assert_raise(RuntimeError) {
|
2008-02-19 01:24:36 -05:00
|
|
|
erb.result
|
2016-01-28 22:18:05 -05:00
|
|
|
}
|
|
|
|
assert_match(/\A\(erb\):5\b/, e.backtrace[0].to_s)
|
2008-02-19 01:24:36 -05:00
|
|
|
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = ERB.new(src, trim_mode: '%-')
|
2016-01-28 22:18:05 -05:00
|
|
|
e = assert_raise(RuntimeError) {
|
2008-02-19 01:24:36 -05:00
|
|
|
erb.result
|
2016-01-28 22:18:05 -05:00
|
|
|
}
|
|
|
|
assert_match(/\A\(erb\):5\b/, e.backtrace[0].to_s)
|
2008-02-19 01:24:36 -05:00
|
|
|
end
|
|
|
|
|
2008-04-30 08:40:52 -04:00
|
|
|
def test_explicit
|
2008-02-19 01:24:36 -05: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 -%>
|
2012-12-07 00:02:18 -05:00
|
|
|
KeepNewLine <%- z = nil -%>\s
|
2008-02-19 01:24:36 -05:00
|
|
|
EOS
|
|
|
|
|
|
|
|
ans = <<EOS
|
|
|
|
NotSkip NotSkip
|
|
|
|
* HELLO
|
|
|
|
* WORLD
|
2012-12-07 00:02:18 -05:00
|
|
|
NotSkip\s
|
2008-02-19 01:24:36 -05:00
|
|
|
* hello
|
|
|
|
* HELLO
|
|
|
|
* world
|
|
|
|
* WORLD
|
2012-12-07 00:02:18 -05:00
|
|
|
KeepNewLine \s
|
2008-02-19 01:24:36 -05:00
|
|
|
EOS
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal(ans, ERB.new(src, trim_mode: '-').result)
|
|
|
|
assert_equal(ans, ERB.new(src, trim_mode: '-%').result)
|
2004-02-27 20:52:14 -05:00
|
|
|
end
|
2008-04-30 08: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")))
|
2017-05-17 07:58:09 -04:00
|
|
|
|
|
|
|
assert_equal("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",
|
|
|
|
ERB::Util.url_encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"),
|
|
|
|
"should not escape any unreserved characters, as per RFC3986 Section 2.3")
|
2008-04-30 08:40:52 -04:00
|
|
|
end
|
2009-01-11 02:58:02 -05:00
|
|
|
|
|
|
|
def test_percent_after_etag
|
2018-02-22 08:28:25 -05:00
|
|
|
assert_equal("1%", @erb.new("<%= 1 %>%", trim_mode: "%").result)
|
2009-01-11 02:58:02 -05:00
|
|
|
end
|
2016-01-02 02:19:58 -05:00
|
|
|
|
|
|
|
def test_token_extension
|
|
|
|
extended_erb = Class.new(ERB)
|
|
|
|
extended_erb.module_eval do
|
|
|
|
def make_compiler(trim_mode)
|
|
|
|
compiler = Class.new(ERB::Compiler)
|
|
|
|
compiler.module_eval do
|
|
|
|
def compile_stag(stag, out, scanner)
|
|
|
|
case stag
|
|
|
|
when '<%=='
|
|
|
|
scanner.stag = stag
|
|
|
|
add_put_cmd(out, content) if content.size > 0
|
|
|
|
self.content = ''
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def compile_content(stag, out)
|
|
|
|
case stag
|
|
|
|
when '<%=='
|
|
|
|
out.push("#{@insert_cmd}(::ERB::Util.html_escape(#{content}))")
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_scanner(src)
|
|
|
|
scanner = Class.new(ERB::Compiler::SimpleScanner)
|
|
|
|
scanner.module_eval do
|
|
|
|
def stags
|
|
|
|
['<%=='] + super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
scanner.new(src, @trim_mode, @percent)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
compiler.new(trim_mode)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
src = <<~EOS
|
2016-01-28 21:13:38 -05:00
|
|
|
<% tag = '<>' \%>
|
|
|
|
<\%= tag \%>
|
|
|
|
<\%== tag \%>
|
2016-01-02 02:19:58 -05:00
|
|
|
EOS
|
|
|
|
ans = <<~EOS
|
|
|
|
|
|
|
|
<>
|
|
|
|
<>
|
|
|
|
EOS
|
|
|
|
assert_equal(ans, extended_erb.new(src).result)
|
|
|
|
end
|
2016-01-28 21:14:36 -05:00
|
|
|
|
|
|
|
def test_frozen_string_literal
|
|
|
|
bug12031 = '[ruby-core:73561] [Bug #12031]'
|
|
|
|
e = @erb.new("<%#encoding: us-ascii%>a")
|
|
|
|
e.src.sub!(/\A#(?:-\*-)?(.*)(?:-\*-)?/) {
|
|
|
|
'# -*- \1; frozen-string-literal: true -*-'
|
|
|
|
}
|
|
|
|
assert_equal("a", e.result, bug12031)
|
2016-01-29 00:16:29 -05:00
|
|
|
|
|
|
|
%w(false true).each do |flag|
|
|
|
|
erb = @erb.new("<%#frozen-string-literal: #{flag}%><%=''.frozen?%>")
|
|
|
|
assert_equal(flag, erb.result)
|
|
|
|
end
|
2016-01-28 21:14:36 -05:00
|
|
|
end
|
2017-05-25 11:38:25 -04:00
|
|
|
|
|
|
|
def test_result_with_hash
|
|
|
|
erb = @erb.new("<%= foo %>")
|
|
|
|
assert_equal("1", erb.result_with_hash(foo: "1"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_result_with_hash_does_not_use_caller_local_variables
|
|
|
|
erb = @erb.new("<%= foo %>")
|
|
|
|
foo = 1
|
|
|
|
assert_raise(NameError) { erb.result_with_hash({}) }
|
2017-05-29 06:22:47 -04:00
|
|
|
assert_equal("1", erb.result_with_hash(foo: foo))
|
2017-05-25 11:38:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_result_with_hash_does_not_modify_caller_binding
|
|
|
|
erb = @erb.new("<%= foo %>")
|
|
|
|
erb.result_with_hash(foo: "1")
|
|
|
|
assert_equal(false, binding.local_variable_defined?(:foo))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_result_with_hash_does_not_modify_toplevel_binding
|
|
|
|
erb = @erb.new("<%= foo %>")
|
|
|
|
erb.result_with_hash(foo: "1")
|
|
|
|
assert_equal(false, TOPLEVEL_BINDING.local_variable_defined?(:foo))
|
2017-12-22 03:08:31 -05:00
|
|
|
TOPLEVEL_BINDING.eval 'template2 = "two"'
|
|
|
|
erb = @erb.new("<%= template2 %>")
|
|
|
|
erb.result_with_hash(template2: "TWO")
|
|
|
|
assert_equal "two", TOPLEVEL_BINDING.local_variable_get("template2")
|
2017-05-25 11:38:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# This depends on the behavior that #local_variable_set raises TypeError by invalid key.
|
|
|
|
def test_result_with_hash_with_invalid_keys_raises_type_error
|
|
|
|
erb = @erb.new("<%= 1 %>")
|
|
|
|
assert_raise(TypeError) { erb.result_with_hash({ 1 => "1" }) }
|
|
|
|
end
|
2017-12-27 07:40:03 -05:00
|
|
|
|
|
|
|
# Bug#14243
|
|
|
|
def test_half_working_comment_backward_compatibility
|
|
|
|
assert_nothing_raised do
|
|
|
|
@erb.new("<% # comment %>\n").result
|
|
|
|
end
|
|
|
|
end
|
2018-02-22 08:28:25 -05:00
|
|
|
|
2022-01-17 15:37:46 -05:00
|
|
|
# [deprecated] These interfaces will be removed later
|
|
|
|
def test_deprecated_interface_warnings
|
|
|
|
[nil, 0, 1, 2].each do |safe|
|
|
|
|
assert_warn(/2nd argument of ERB.new is deprecated/) do
|
|
|
|
ERB.new('', safe)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[nil, '', '%', '%<>'].each do |trim|
|
|
|
|
assert_warn(/3rd argument of ERB.new is deprecated/) do
|
|
|
|
ERB.new('', nil, trim)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[nil, '_erbout', '_hamlout'].each do |eoutvar|
|
|
|
|
assert_warn(/4th argument of ERB.new is deprecated/) do
|
|
|
|
ERB.new('', nil, nil, eoutvar)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-27 23:46:48 -04:00
|
|
|
def test_prohibited_marshal_dump
|
|
|
|
erb = ERB.new("")
|
|
|
|
assert_raise(TypeError) {Marshal.dump(erb)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_prohibited_marshal_load
|
|
|
|
erb = ERB.allocate
|
|
|
|
erb.instance_variable_set(:@src, "")
|
|
|
|
erb.instance_variable_set(:@lineno, 1)
|
2019-07-04 05:12:08 -04:00
|
|
|
erb.instance_variable_set(:@_init, true)
|
2019-03-27 23:46:48 -04:00
|
|
|
erb = Marshal.load(Marshal.dump(erb))
|
|
|
|
assert_raise(ArgumentError) {erb.result}
|
|
|
|
end
|
2004-02-27 20:52:14 -05:00
|
|
|
end
|
2009-01-03 10:36:14 -05: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
|