Created
July 26, 2018 01:49
-
-
Save danrabinowitz/e17d8c70399354a13114d97910973fa9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'erb' | |
require 'active_support' | |
require 'active_support/core_ext' | |
def newlines_to_br(s) | |
html = "".html_safe | |
s.split("\n").each_with_index do |line, idx| | |
html << "<br />".html_safe if idx > 0 | |
html << line | |
end | |
html | |
end | |
TEST_CASES = [ | |
["a", "a"], | |
["a\nb", "a<br />b"], | |
["a<div>b", "a<div>b"], | |
] | |
TEST_CASES.each do |test_case| | |
result = newlines_to_br(test_case[0]) | |
rendered_string = ERB::Util.html_escape(result) | |
expected_result = test_case[1] | |
if rendered_string != expected_result | |
puts "ERROR: expect #{rendered_string} to equal #{expected_result}" | |
else | |
puts "PASS" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment