Skip to content

Instantly share code, notes, and snippets.

@lucianspec
Forked from chrisroos/Gemfile
Created April 26, 2017 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucianspec/fb3a8a2fcc63655bab08cceb92b00662 to your computer and use it in GitHub Desktop.
Save lucianspec/fb3a8a2fcc63655bab08cceb92b00662 to your computer and use it in GitHub Desktop.
Comparison of the various ERB trim modes available

I found the ERB documentation about the various trim modes confusing.

I've written a number of tests that attempt to demonstrate the trim mode behaviour, and optional percentage sign behaviour.

I think I have a better idea of what's going on but I'm still not sure I could do a very good job of describing what's happening.

I've taken the names of the trim modes from the ERB source code (see comments around TrimScanner#initialize).

source 'https://rubygems.org'
gem 'minitest'
GEM
remote: https://rubygems.org/
specs:
minitest (5.6.1)
PLATFORMS
ruby
DEPENDENCIES
minitest
require 'minitest/autorun'
require 'erb'
class ErbTest < Minitest::Test
def test_all_trim_modes_on_single_line_ruby_code_and_expressions
erb_template = "<% if true %>
<%= hello_world_method %>
<% end %>"
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, nil)
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, '-')
assert_equal "hello world", output_with_trim_mode(erb_template, '>')
assert_equal "hello world", output_with_trim_mode(erb_template, '<>')
end
def test_all_trim_modes_on_multi_line_ruby_code_and_expressions
erb_template = "<%
if true
%>
<%=
hello_world_method
%>
<%
end
%>"
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, nil)
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, '-')
assert_equal "hello world", output_with_trim_mode(erb_template, '>')
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, '<>')
end
def test_explicit_trim_mode_on_ruby_expression
erb_template = "<% if true %>
<%= hello_world_method -%>
<% end %>
"
assert_equal "\nhello world\n", output_with_trim_mode(erb_template, '-')
end
def test_explicit_trim_mode_on_ruby_expression_and_if_ruby_statement
erb_template = "<% if true -%>
<%= hello_world_method -%>
<% end %>
"
assert_equal "hello world\n", output_with_trim_mode(erb_template, '-')
end
def test_explicit_trim_mode_on_ruby_expression_and_end_ruby_statement
erb_template = "<% if true %>
<%= hello_world_method -%>
<% end -%>
"
assert_equal "\nhello world", output_with_trim_mode(erb_template, '-')
end
def test_explicit_trim_mode_on_ruby_expression_and_if_and_end_ruby_statements
erb_template = "<% if true -%>
<%= hello_world_method -%>
<% end -%>
"
assert_equal 'hello world', output_with_trim_mode(erb_template, '-')
end
def test_combining_percentage_and_no_trim_mode
erb_template = "% if true
<%= hello_world_method %>
% end"
assert_equal "hello world\n", output_with_trim_mode(erb_template, '%')
end
def test_combining_percentage_and_trim_line_1
erb_template = "% if true
<%= hello_world_method %>
% end"
assert_equal 'hello world', output_with_trim_mode(erb_template, '%>')
end
def test_combining_percentage_and_trim_line_2
erb_template = "% if true
<%= hello_world_method %>
% end"
assert_equal 'hello world', output_with_trim_mode(erb_template, '%<>')
end
def test_combining_percentage_and_explicit_trim_line
erb_template = "% if true
<%= hello_world_method -%>
% end"
assert_equal 'hello world', output_with_trim_mode(erb_template, '%-')
end
private
def hello_world_method
'hello world'
end
def output_with_trim_mode(erb_template, trim_mode)
safe_mode = nil
ERB.new(erb_template, safe_mode, trim_mode).result(binding)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment