diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-20 20:18:52 +0000 |
commit | 1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch) | |
tree | a3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/library/erb | |
parent | 75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff) |
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory.
[Misc #13792] [ruby-core:82287]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/library/erb')
-rw-r--r-- | spec/ruby/library/erb/def_class_spec.rb | 29 | ||||
-rw-r--r-- | spec/ruby/library/erb/def_method_spec.rb | 26 | ||||
-rw-r--r-- | spec/ruby/library/erb/def_module_spec.rb | 27 | ||||
-rw-r--r-- | spec/ruby/library/erb/defmethod/def_erb_method_spec.rb | 63 | ||||
-rw-r--r-- | spec/ruby/library/erb/filename_spec.rb | 40 | ||||
-rw-r--r-- | spec/ruby/library/erb/new_spec.rb | 132 | ||||
-rw-r--r-- | spec/ruby/library/erb/result_spec.rb | 86 | ||||
-rw-r--r-- | spec/ruby/library/erb/run_spec.rb | 97 | ||||
-rw-r--r-- | spec/ruby/library/erb/src_spec.rb | 33 | ||||
-rw-r--r-- | spec/ruby/library/erb/util/h_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/library/erb/util/html_escape_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/library/erb/util/shared/html_escape.rb | 42 | ||||
-rw-r--r-- | spec/ruby/library/erb/util/shared/url_encode.rb | 50 | ||||
-rw-r--r-- | spec/ruby/library/erb/util/u_spec.rb | 8 | ||||
-rw-r--r-- | spec/ruby/library/erb/util/url_encode_spec.rb | 7 |
15 files changed, 655 insertions, 0 deletions
diff --git a/spec/ruby/library/erb/def_class_spec.rb b/spec/ruby/library/erb/def_class_spec.rb new file mode 100644 index 0000000000..ae2dcbd1e4 --- /dev/null +++ b/spec/ruby/library/erb/def_class_spec.rb @@ -0,0 +1,29 @@ +require 'erb' +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ERB#def_class" do + + it "return an unnamed class which has instance method to render eRuby script" do + input = <<'END' +@arg1=<%=@arg1.inspect%> +@arg2=<%=@arg2.inspect%> +END + expected = <<'END' +@arg1="foo" +@arg2=123 +END + class MyClass1ForErb_ + def initialize(arg1, arg2) + @arg1 = arg1; @arg2 = arg2 + end + end + filename = 'example.rhtml' + #erb = ERB.new(File.read(filename)) + erb = ERB.new(input) + erb.filename = filename + MyClass1ForErb = erb.def_class(MyClass1ForErb_, 'render()') + MyClass1ForErb.method_defined?(:render).should == true + MyClass1ForErb.new('foo', 123).render().should == expected + end + +end diff --git a/spec/ruby/library/erb/def_method_spec.rb b/spec/ruby/library/erb/def_method_spec.rb new file mode 100644 index 0000000000..e4ddedea4c --- /dev/null +++ b/spec/ruby/library/erb/def_method_spec.rb @@ -0,0 +1,26 @@ +require 'erb' +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ERB#def_method" do + + it "define module's instance method to render eRuby file" do + input = <<'END' +arg1=<%= arg1.inspect %> +arg2=<%= arg2.inspect %> +END + expected = <<'END' +arg1="foo" +arg2=123 +END + # + filename = 'example.rhtml' # 'arg1' and 'arg2' are used in example.rhtml + #erb = ERB.new(File.read(filename)) + erb = ERB.new(input) + class MyClass0ForErb + end + erb.def_method(MyClass0ForErb, 'render(arg1, arg2)', filename) + MyClass0ForErb.method_defined?(:render) + MyClass0ForErb.new.render('foo', 123).should == expected + end + +end diff --git a/spec/ruby/library/erb/def_module_spec.rb b/spec/ruby/library/erb/def_module_spec.rb new file mode 100644 index 0000000000..ed52fdfc15 --- /dev/null +++ b/spec/ruby/library/erb/def_module_spec.rb @@ -0,0 +1,27 @@ +require 'erb' +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ERB#def_module" do + + it "return unnamed module which has instance method to render eRuby" do + input = <<'END' +arg1=<%= arg1.inspect %> +arg2=<%= arg2.inspect %> +END + expected = <<'END' +arg1="foo" +arg2=123 +END + filename = 'example.rhtml' + #erb = ERB.new(File.read(filename)) + erb = ERB.new(input) + erb.filename = filename + MyModule2ForErb = erb.def_module('render(arg1, arg2)') + MyModule2ForErb.method_defined?(':render') + class MyClass2ForErb + include MyModule2ForErb + end + MyClass2ForErb.new.render('foo', 123).should == expected + end + +end diff --git a/spec/ruby/library/erb/defmethod/def_erb_method_spec.rb b/spec/ruby/library/erb/defmethod/def_erb_method_spec.rb new file mode 100644 index 0000000000..e1eca2fbef --- /dev/null +++ b/spec/ruby/library/erb/defmethod/def_erb_method_spec.rb @@ -0,0 +1,63 @@ +require 'erb' +require File.expand_path('../../../../spec_helper', __FILE__) + +describe "ERB::DefMethod.def_erb_method" do + + + input = <<'END' +<% for item in @items %> +<b><%= item %></b> +<% end %> +END + + + it "define method to render eRuby file as an instance method of current module" do + expected = <<'END' + +<b>10</b> + +<b>20</b> + +<b>30</b> + +END + # + begin + file = tmp('_example.rhtml') + File.open(file, 'w') {|f| f.write(input) } + klass = Class.new do + extend ERB::DefMethod + def_erb_method('render()', file) + def initialize(items) + @items = items + end + end + klass.new([10,20,30]).render().should == expected + ensure + rm_r file + end + + end + + + it "define method to render eRuby object as an instance method of current module" do + expected = <<'END' +<b>10</b> +<b>20</b> +<b>30</b> +END + # + MY_INPUT4_FOR_ERB = input + class MyClass4ForErb + extend ERB::DefMethod + erb = ERB.new(MY_INPUT4_FOR_ERB, nil, '<>') + def_erb_method('render()', erb) + def initialize(items) + @items = items + end + end + MyClass4ForErb.new([10,20,30]).render().should == expected + end + + +end diff --git a/spec/ruby/library/erb/filename_spec.rb b/spec/ruby/library/erb/filename_spec.rb new file mode 100644 index 0000000000..4615f5d808 --- /dev/null +++ b/spec/ruby/library/erb/filename_spec.rb @@ -0,0 +1,40 @@ +require 'erb' +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ERB#filename" do + it "raises an exception if there are errors processing content" do + filename = 'foobar.rhtml' + erb = ERB.new('<% if true %>') # will raise SyntaxError + erb.filename = filename + lambda { + begin + erb.result(binding) + rescue Exception => e + @ex = e + raise e + end + }.should raise_error(SyntaxError) + expected = filename + + @ex.message =~ /^(.*?):(\d+): / + $1.should == expected + $2.to_i.should == 1 + end + + it "uses '(erb)' as filename when filename is not set" do + erb = ERB.new('<% if true %>') # will raise SyntaxError + lambda { + begin + erb.result(binding) + rescue Exception => e + @ex = e + raise e + end + }.should raise_error(SyntaxError) + expected = '(erb)' + + @ex.message =~ /^(.*?):(\d+): / + $1.should == expected + $2.to_i.should == 1 + end +end diff --git a/spec/ruby/library/erb/new_spec.rb b/spec/ruby/library/erb/new_spec.rb new file mode 100644 index 0000000000..917fd470b7 --- /dev/null +++ b/spec/ruby/library/erb/new_spec.rb @@ -0,0 +1,132 @@ +require 'erb' +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ERB.new" do + before :all do + @eruby_str = <<'END' +<ul> +<% list = [1,2,3] %> +<% for item in list %> +<% if item %> +<li><%= item %></li> +<% end %> +<% end %> +</ul> +END + + @eruby_str2 = <<'END' +<ul> +% list = [1,2,3] +%for item in list +% if item + <li><%= item %> + <% end %> +<% end %> +</ul> +%%% +END + + end + + it "compiles eRuby script into ruby code when trim mode is 0 or not specified" do + expected = "<ul>\n\n\n\n<li>1</li>\n\n\n\n<li>2</li>\n\n\n\n<li>3</li>\n\n\n</ul>\n" + [0, '', nil].each do |trim_mode| + ERB.new(@eruby_str, nil, trim_mode).result.should == expected + end + end + + it "removes '\n' when trim_mode is 1 or '>'" do + expected = "<ul>\n<li>1</li>\n<li>2</li>\n<li>3</li>\n</ul>\n" + [1, '>'].each do |trim_mode| + ERB.new(@eruby_str, nil, trim_mode).result.should == expected + end + end + + it "removes spaces at beginning of line and '\n' when trim_mode is 2 or '<>'" do + expected = "<ul>\n<li>1</li>\n<li>2</li>\n<li>3</li>\n</ul>\n" + [2, '<>'].each do |trim_mode| + ERB.new(@eruby_str, nil, trim_mode).result.should == expected + end + end + + it "removes spaces around '<%- -%>' when trim_mode is '-'" do + expected = "<ul>\n <li>1 <li>2 <li>3</ul>\n" + input = <<'END' +<ul> +<%- for item in [1,2,3] -%> + <%- if item -%> + <li><%= item -%> + <%- end -%> +<%- end -%> +</ul> +END + + ERB.new(input, nil, '-').result.should == expected + end + + + it "not support '<%-= expr %> even when trim_mode is '-'" do + + input = <<'END' +<p> + <%= expr -%> + <%-= expr -%> +</p> +END + + lambda { ERB.new(input, nil, '-').result }.should raise_error + end + + it "regards lines starting with '%' as '<% ... %>' when trim_mode is '%'" do + expected = "<ul>\n <li>1\n \n <li>2\n \n <li>3\n \n\n</ul>\n%%\n" + ERB.new(@eruby_str2, nil, "%").result.should == expected + end + it "regards lines starting with '%' as '<% ... %>' and remove \"\\n\" when trim_mode is '%>'" do + expected = "<ul>\n <li>1 <li>2 <li>3 </ul>\n%%\n" + ERB.new(@eruby_str2, nil, '%>').result.should == expected + end + + + it "regard lines starting with '%' as '<% ... %>' and remove \"\\n\" when trim_mode is '%<>'" do + expected = "<ul>\n <li>1\n \n <li>2\n \n <li>3\n \n</ul>\n%%\n" + ERB.new(@eruby_str2, nil, '%<>').result.should == expected + end + + + it "regard lines starting with '%' as '<% ... %>' and spaces around '<%- -%>' when trim_mode is '%-'" do + expected = "<ul>\n<li>1</li>\n<li>2</li>\n</ul>\n%%\n" + input = <<'END' +<ul> +%list = [1,2] +%for item in list +<li><%= item %></li> +<% end %></ul> +%%% +END + + ERB.new(input, nil, '%-').result.should == expected + end + + it "changes '_erbout' variable name in the produced source" do + input = @eruby_str + match_erbout = ERB.new(input, nil, nil).src + match_buf = ERB.new(input, nil, nil, 'buf').src + match_erbout.gsub("_erbout", "buf").should == match_buf + end + + + it "ignores '<%# ... %>'" do + input = <<'END' +<%# for item in list %> +<b><%#= item %></b> +<%# end %> +END + ERB.new(input).result.should == "\n<b></b>\n\n" + ERB.new(input, nil, '<>').result.should == "<b></b>\n" + end + + it "forget local variables defined previous one" do + ERB.new(@eruby_str).result + lambda{ ERB.new("<%= list %>").result }.should raise_error(NameError) + end +end diff --git a/spec/ruby/library/erb/result_spec.rb b/spec/ruby/library/erb/result_spec.rb new file mode 100644 index 0000000000..d79584b221 --- /dev/null +++ b/spec/ruby/library/erb/result_spec.rb @@ -0,0 +1,86 @@ +require 'erb' +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ERB#result" do + + + it "return the result of compiled ruby code" do + input = <<'END' +<ul> +<% for item in list %> + <li><%= item %> +<% end %> +</ul> +END + expected = <<'END' +<ul> + + <li>AAA + + <li>BBB + + <li>CCC + +</ul> +END + erb = ERB.new(input) + list = %w[AAA BBB CCC] + actual = erb.result(binding) + actual.should == expected + end + + + it "share local variables" do + input = "<% var = 456 %>" + expected = 456 + var = 123 + ERB.new(input).result(binding) + var.should == expected + end + + + it "is not able to h() or u() unless including ERB::Util" do + input = "<%=h '<>' %>" + lambda { + ERB.new(input).result() + }.should raise_error(NameError) + end + + + it "is able to h() or u() if ERB::Util is included" do + myerb1 = Class.new do + include ERB::Util + def main + input = "<%=h '<>' %>" + return ERB.new(input).result(binding) + end + end + expected = '<>' + actual = myerb1.new.main() + actual.should == expected + end + + + it "use TOPLEVEL_BINDING if binding is not passed" do + myerb2 = Class.new do + include ERB::Util + def main1 + #input = "<%= binding.to_s %>" + input = "<%= _xxx_var_ %>" + return ERB.new(input).result() + end + def main2 + input = "<%=h '<>' %>" + return ERB.new(input).result() + end + end + + eval '_xxx_var_ = 123', TOPLEVEL_BINDING + expected = '123' + myerb2.new.main1().should == expected + + lambda { + myerb2.new.main2() + }.should raise_error(NameError) + end +end diff --git a/spec/ruby/library/erb/run_spec.rb b/spec/ruby/library/erb/run_spec.rb new file mode 100644 index 0000000000..6ad3808a16 --- /dev/null +++ b/spec/ruby/library/erb/run_spec.rb @@ -0,0 +1,97 @@ +require 'erb' +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ERB#run" do + # TODO: what is this? why does it not use + # lambda { ... }.should output + def _steal_stdout + orig = $stdout + s = '' + def s.write(arg); self << arg.to_s; end + $stdout = s + begin + yield + ensure + $stdout = orig + end + return s + end + + it "print the result of compiled ruby code" do + input = <<END +<ul> +<% for item in list %> + <li><%= item %> +<% end %> +</ul> +END + expected = <<END +<ul> + + <li>AAA + + <li>BBB + + <li>CCC + +</ul> +END + erb = ERB.new(input) + list = %w[AAA BBB CCC] + actual = _steal_stdout { erb.run(binding) } + actual.should == expected + end + + it "share local variables" do + input = "<% var = 456 %>" + expected = 456 + var = 123 + _steal_stdout { ERB.new(input).run(binding) } + var.should == expected + end + + it "is not able to h() or u() unless including ERB::Util" do + input = "<%=h '<>' %>" + lambda { + _steal_stdout { ERB.new(input).run() } + }.should raise_error(NameError) + end + + it "is able to h() or u() if ERB::Util is included" do + myerb1 = Class.new do + include ERB::Util + def main + input = "<%=h '<>' %>" + ERB.new(input).run(binding) + end + end + expected = '<>' + actual = _steal_stdout { myerb1.new.main() } + actual.should == expected + end + + it "use TOPLEVEL_BINDING if binding is not passed" do + myerb2 = Class.new do + include ERB::Util + def main1 + #input = "<%= binding.to_s %>" + input = "<%= _xxx_var_ %>" + return ERB.new(input).run() + end + def main2 + input = "<%=h '<>' %>" + return ERB.new(input).run() + end + end + + eval '_xxx_var_ = 123', TOPLEVEL_BINDING + expected = '123' + actual = _steal_stdout { myerb2.new.main1() } + actual.should == expected + + lambda { + _steal_stdout { myerb2.new.main2() } + }.should raise_error(NameError) + end +end + diff --git a/spec/ruby/library/erb/src_spec.rb b/spec/ruby/library/erb/src_spec.rb new file mode 100644 index 0000000000..cf5b67dcbf --- /dev/null +++ b/spec/ruby/library/erb/src_spec.rb @@ -0,0 +1,33 @@ +require 'erb' +require File.expand_path('../../../spec_helper', __FILE__) + +describe "ERB#src" do + + it "returns the compiled ruby code evaluated to a String" do + # note that what concrete code is emitted is not guaranteed. + + input = <<'END' +<ul> +<% for item in list %> + <li><%= item %> +<% end %> +</ul> +END + + expected = <<'END' +<ul> + + <li>AAA + + <li>BBB + + <li>CCC + +</ul> +END + + list = %w[AAA BBB CCC] + eval(ERB.new(input).src).should == expected + end + +end diff --git a/spec/ruby/library/erb/util/h_spec.rb b/spec/ruby/library/erb/util/h_spec.rb new file mode 100644 index 0000000000..ba36574433 --- /dev/null +++ b/spec/ruby/library/erb/util/h_spec.rb @@ -0,0 +1,7 @@ +require 'erb' +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/html_escape', __FILE__) + +describe "ERB::Util.h" do + it_behaves_like :erb_util_html_escape, :h +end diff --git a/spec/ruby/library/erb/util/html_escape_spec.rb b/spec/ruby/library/erb/util/html_escape_spec.rb new file mode 100644 index 0000000000..9bc9359f2c --- /dev/null +++ b/spec/ruby/library/erb/util/html_escape_spec.rb @@ -0,0 +1,8 @@ +require 'erb' +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/html_escape', __FILE__) + +describe "ERB::Util.html_escape" do + it_behaves_like :erb_util_html_escape, :html_escape +end + diff --git a/spec/ruby/library/erb/util/shared/html_escape.rb b/spec/ruby/library/erb/util/shared/html_escape.rb new file mode 100644 index 0000000000..71b378755e --- /dev/null +++ b/spec/ruby/library/erb/util/shared/html_escape.rb @@ -0,0 +1,42 @@ +describe :erb_util_html_escape, shared: true do + it "escape (& < > \" ') to (& < > " ')" do + input = '& < > " \'' + expected = '& < > " '' + ERB::Util.__send__(@method, input).should == expected + end + + it "not escape characters except (& < > \" ')" do + input = (0x20..0x7E).to_a.collect {|ch| ch.chr}.join('') + expected = input. + gsub(/&/,'&'). + gsub(/</,'<'). + gsub(/>/,'>'). + gsub(/'/,'''). + gsub(/"/,'"') + ERB::Util.__send__(@method, input).should == expected + end + + it "return empty string when argument is nil" do + input = nil + expected = '' + ERB::Util.__send__(@method, input).should == expected + end + + it "returns string when argument is number" do + input = 123 + expected = '123' + ERB::Util.__send__(@method, input).should == expected + input = 3.14159 + expected = '3.14159' + ERB::Util.__send__(@method, input).should == expected + end + + it "returns string when argument is boolean" do + input = true + expected = 'true' + ERB::Util.__send__(@method, input).should == expected + input = false + expected = 'false' + ERB::Util.__send__(@method, input).should == expected + end +end diff --git a/spec/ruby/library/erb/util/shared/url_encode.rb b/spec/ruby/library/erb/util/shared/url_encode.rb new file mode 100644 index 0000000000..5ac6215523 --- /dev/null +++ b/spec/ruby/library/erb/util/shared/url_encode.rb @@ -0,0 +1,50 @@ +describe :erb_util_url_encode, shared: true do + it "encode characters" do + #input = (0x20..0x7E).to_a.collect{|ch| ch.chr}.join + input = " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}" + expected = "%20%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D" + ERB::Util.__send__(@method, input).should == expected + end + + ruby_version_is ""..."2.5" do + it "escapes tilde" do + ERB::Util.__send__(@method, "~").should == "%7E" + end + end + + ruby_version_is "2.5" do + it "does not escape tilde" do + ERB::Util.__send__(@method, "~").should == "~" + end + end + + it "encode unicode string" do + input = "http://ja.wikipedia.org/wiki/\343\203\255\343\203\240\343\202\271\343\202\253\343\203\273\343\203\221\343\203\255\343\203\273\343\202\246\343\203\253\343\203\273\343\203\251\343\203\224\343\203\245\343\202\277" + expected = 'http%3A%2F%2Fja.wikipedia.org%2Fwiki%2F%E3%83%AD%E3%83%A0%E3%82%B9%E3%82%AB%E3%83%BB%E3%83%91%E3%83%AD%E3%83%BB%E3%82%A6%E3%83%AB%E3%83%BB%E3%83%A9%E3%83%94%E3%83%A5%E3%82%BF' + ERB::Util.__send__(@method, input).should == expected + end + + it "returns empty string when argument is nil" do + input = nil + expected = '' + ERB::Util.__send__(@method, input).should == expected + end + + it "returns string when argument is number" do + input = 123 + expected = '123' + ERB::Util.__send__(@method, input).should == expected + input = 3.14159 + expected = '3.14159' + ERB::Util.__send__(@method, input).should == expected + end + + it "returns string when argument is boolean" do + input = true + expected = 'true' + ERB::Util.__send__(@method, input).should == expected + input = false + expected = 'false' + ERB::Util.__send__(@method, input).should == expected + end +end diff --git a/spec/ruby/library/erb/util/u_spec.rb b/spec/ruby/library/erb/util/u_spec.rb new file mode 100644 index 0000000000..9200244c8e --- /dev/null +++ b/spec/ruby/library/erb/util/u_spec.rb @@ -0,0 +1,8 @@ +require 'erb' +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/url_encode', __FILE__) + +describe "ERB::Util.u" do + it_behaves_like :erb_util_url_encode, :u +end + diff --git a/spec/ruby/library/erb/util/url_encode_spec.rb b/spec/ruby/library/erb/util/url_encode_spec.rb new file mode 100644 index 0000000000..303a2e3cd7 --- /dev/null +++ b/spec/ruby/library/erb/util/url_encode_spec.rb @@ -0,0 +1,7 @@ +require 'erb' +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../shared/url_encode', __FILE__) + +describe "ERB::Util.url_encode" do + it_behaves_like :erb_util_url_encode, :url_encode +end |