2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2007-12-16 18:53:45 -05:00
|
|
|
|
|
|
|
class ErbUtilTest < Test::Unit::TestCase
|
|
|
|
include ERB::Util
|
|
|
|
|
2008-04-08 00:52:01 -04:00
|
|
|
ERB::Util::HTML_ESCAPE.each do |given, expected|
|
|
|
|
define_method "test_html_escape_#{expected.gsub /\W/, ''}" do
|
|
|
|
assert_equal expected, html_escape(given)
|
|
|
|
end
|
2007-12-16 18:53:45 -05:00
|
|
|
|
2008-04-08 00:52:01 -04:00
|
|
|
unless given == '"'
|
|
|
|
define_method "test_json_escape_#{expected.gsub /\W/, ''}" do
|
|
|
|
assert_equal ERB::Util::JSON_ESCAPE[given], json_escape(given)
|
|
|
|
end
|
|
|
|
end
|
2007-12-16 18:53:45 -05:00
|
|
|
end
|
|
|
|
|
2009-10-07 16:31:20 -04:00
|
|
|
def test_html_escape_is_html_safe
|
|
|
|
escaped = h("<p>")
|
|
|
|
assert_equal "<p>", escaped
|
|
|
|
assert escaped.html_safe?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_html_escape_passes_html_escpe_unmodified
|
For performance reasons, you can no longer call html_safe! on Strings. Instead, all Strings are always not html_safe?. Instead, you can get a SafeBuffer from a String by calling #html_safe, which will SafeBuffer.new(self).
* Additionally, instead of doing concat("</form>".html_safe), you can do
safe_concat("</form>"), which will skip both the flag set, and the flag
check.
* For the first pass, I converted virtually all #html_safe!s to #html_safe,
and the tests pass. A further optimization would be to try to use
#safe_concat as much as possible, reducing the performance impact if
we know up front that a String is safe.
2010-01-31 22:17:42 -05:00
|
|
|
escaped = h("<p>".html_safe)
|
2009-10-07 16:31:20 -04:00
|
|
|
assert_equal "<p>", escaped
|
|
|
|
assert escaped.html_safe?
|
|
|
|
end
|
|
|
|
|
2007-12-16 18:53:45 -05:00
|
|
|
def test_rest_in_ascii
|
2009-06-08 16:33:18 -04:00
|
|
|
(0..127).to_a.map {|int| int.chr }.each do |chr|
|
2007-12-16 18:53:45 -05:00
|
|
|
next if %w(& " < >).include?(chr)
|
|
|
|
assert_equal chr, html_escape(chr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|