require 'erb' require_relative '../../spec_helper' require_relative 'fixtures/classes' describe "ERB.new" do before :all do @eruby_str = <<'END' END @eruby_str2 = <<'END' %%% END end it "compiles eRuby script into ruby code when trim mode is 0 or not specified" do expected = "\n" [0, nil].each do |trim_mode| ERBSpecs.new_erb(@eruby_str, trim_mode: trim_mode).result.should == expected end end it "warns invalid trim_mode" do -> do ERBSpecs.new_erb(@eruby_str, trim_mode: '') end.should complain(/Invalid ERB trim mode/) end it "removes '\n' when trim_mode is 1 or '>'" do expected = "\n" [1, '>'].each do |trim_mode| ERBSpecs.new_erb(@eruby_str, trim_mode: trim_mode).result.should == expected end end it "removes spaces at beginning of line and '\n' when trim_mode is 2 or '<>'" do expected = "\n" [2, '<>'].each do |trim_mode| ERBSpecs.new_erb(@eruby_str, trim_mode: trim_mode).result.should == expected end end it "removes spaces around '<%- -%>' when trim_mode is '-'" do expected = "\n" input = <<'END' END ERBSpecs.new_erb(input, trim_mode: '-').result.should == expected end it "does not support '<%-= expr %> even when trim_mode is '-'" do input = <<'END'

<%= expr -%> <%-= expr -%>

END -> { ERBSpecs.new_erb(input, trim_mode: '-').result }.should raise_error(SyntaxError) end it "regards lines starting with '%' as '<% ... %>' when trim_mode is '%'" do expected = "\n%%\n" ERBSpecs.new_erb(@eruby_str2, trim_mode: "%").result.should == expected end it "regards lines starting with '%' as '<% ... %>' and remove \"\\n\" when trim_mode is '%>'" do expected = "\n%%\n" ERBSpecs.new_erb(@eruby_str2, trim_mode: '%>').result.should == expected end it "regard lines starting with '%' as '<% ... %>' and remove \"\\n\" when trim_mode is '%<>'" do expected = "\n%%\n" ERBSpecs.new_erb(@eruby_str2, trim_mode: '%<>').result.should == expected end it "regard lines starting with '%' as '<% ... %>' and spaces around '<%- -%>' when trim_mode is '%-'" do expected = "\n%%\n" input = <<'END' %%% END ERBSpecs.new_erb(input, trim_mode: '%-').result.should == expected end it "changes '_erbout' variable name in the produced source" do input = @eruby_str match_erbout = ERB.new(input, trim_mode: nil).src match_buf = ERB.new(input, trim_mode: nil, eoutvar: 'buf').src match_erbout.gsub("_erbout", "buf").should == match_buf end it "ignores '<%# ... %>'" do input = <<'END' <%# for item in list %> <%#= item %> <%# end %> END ERBSpecs.new_erb(input).result.should == "\n\n\n" ERBSpecs.new_erb(input, trim_mode: '<>').result.should == "\n" end it "forget local variables defined previous one" do ERB.new(@eruby_str).result ->{ ERB.new("<%= list %>").result }.should raise_error(NameError) end end