2009-10-07 16:31:20 -04:00
|
|
|
require 'abstract_unit'
|
2011-01-28 18:00:52 -05:00
|
|
|
begin
|
|
|
|
require 'psych'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
require 'yaml'
|
2009-10-07 16:31:20 -04:00
|
|
|
|
2010-02-01 01:12:01 -05:00
|
|
|
class SafeBufferTest < ActiveSupport::TestCase
|
2009-10-07 16:31:20 -04:00
|
|
|
def setup
|
2010-02-01 01:12:01 -05:00
|
|
|
@buffer = ActiveSupport::SafeBuffer.new
|
2009-10-07 16:31:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Should look like a string" do
|
|
|
|
assert @buffer.is_a?(String)
|
|
|
|
assert_equal "", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should escape a raw string which is passed to them" do
|
|
|
|
@buffer << "<script>"
|
|
|
|
assert_equal "<script>", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should NOT escape a safe value passed to it" do
|
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
|
|
|
@buffer << "<script>".html_safe
|
2009-10-07 16:31:20 -04:00
|
|
|
assert_equal "<script>", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should not mess with an innocuous string" do
|
|
|
|
@buffer << "Hello"
|
|
|
|
assert_equal "Hello", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should not mess with a previously escape test" do
|
2009-10-10 21:29:31 -04:00
|
|
|
@buffer << ERB::Util.html_escape("<script>")
|
2009-10-07 16:31:20 -04:00
|
|
|
assert_equal "<script>", @buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should be considered safe" do
|
|
|
|
assert @buffer.html_safe?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Should return a safe buffer when calling to_s" do
|
|
|
|
new_buffer = @buffer.to_s
|
2010-02-01 01:12:01 -05:00
|
|
|
assert_equal ActiveSupport::SafeBuffer, new_buffer.class
|
2009-10-07 16:31:20 -04:00
|
|
|
end
|
2011-01-28 18:00:52 -05:00
|
|
|
|
|
|
|
def test_to_yaml
|
|
|
|
str = 'hello!'
|
|
|
|
buf = ActiveSupport::SafeBuffer.new str
|
|
|
|
yaml = buf.to_yaml
|
|
|
|
|
|
|
|
assert_match(/^--- #{str}/, yaml)
|
|
|
|
assert_equal 'hello!', YAML.load(yaml)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_nested
|
|
|
|
str = 'hello!'
|
|
|
|
data = { 'str' => ActiveSupport::SafeBuffer.new(str) }
|
|
|
|
yaml = YAML.dump data
|
|
|
|
assert_equal({'str' => str}, YAML.load(yaml))
|
|
|
|
end
|
2009-10-07 16:31:20 -04:00
|
|
|
end
|