diff --git a/stdlib/erb/0/erb.rbs b/stdlib/erb/0/erb.rbs index 5611aab64..a4ea15f88 100644 --- a/stdlib/erb/0/erb.rbs +++ b/stdlib/erb/0/erb.rbs @@ -446,4 +446,111 @@ class ERB # print MyClass.new('foo', 123).render() # def def_class: (?Class, ?String) -> Class + + # + # A utility module for conversion routines, often handy in HTML generation. + # + module Util + # + # A utility method for escaping HTML tag characters in *s*. + # + # require "erb" + # include ERB::Util + # + # puts html_escape("is a > 0 & a < 10?") + # + # *Generates* + # + # is a > 0 & a < 10? + # + def self?.html_escape: (String str) -> String + + # + # + alias h html_escape + + # + # + alias self.h self.html_escape + + # + # A utility method for encoding the String *s* as a URL. + # + # require "erb" + # include ERB::Util + # + # puts url_encode("Programming Ruby: The Pragmatic Programmer's Guide") + # + # *Generates* + # + # Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide + # + def self?.url_encode: (String) -> String + + # + # + alias u url_encode + + # + # + alias self.u self.url_encode + end + + # + # Utility module to define eRuby script as instance method. + # + # ### Example + # + # example.rhtml: + # <% for item in @items %> + # <%= item %> + # <% end %> + # + # example.rb: + # require 'erb' + # class MyClass + # extend ERB::DefMethod + # def_erb_method('render()', 'example.rhtml') + # def initialize(items) + # @items = items + # end + # end + # print MyClass.new([10,20,30]).render() + # + # result: + # + # 10 + # + # 20 + # + # 30 + # + module DefMethod + # + # define *methodname* as instance method of current module, using ERB object or + # eRuby file + # + def self.def_erb_method: (String methodname, (String | ERB) erb_or_fname) -> untyped + end end diff --git a/test/stdlib/ERB_test.rb b/test/stdlib/ERB_test.rb index 2a2c9ab7b..80d3847d5 100644 --- a/test/stdlib/ERB_test.rb +++ b/test/stdlib/ERB_test.rb @@ -91,3 +91,60 @@ def template '<%= ERB.version %>' end end + +class ERBUtilSingletonTest < Test::Unit::TestCase + include TypeAssertions + + library "erb" + testing "singleton(::ERB::Util)" + + def test_html_escape + assert_send_type "(String str) -> String", + ERB::Util, :html_escape, "abc" + assert_send_type "(String str) -> String", + ERB::Util, :h, "abc" + end + + def test_url_encode + assert_send_type "(String str) -> String", + ERB::Util, :url_encode, "abc" + assert_send_type "(String str) -> String", + ERB::Util, :u, "abc" + end +end + +class ERBUtilTest < Test::Unit::TestCase + include TypeAssertions + class Mock + include ERB::Util + end + + library "erb" + testing "::ERB::Util" + + def test_html_escape + assert_send_type "(String str) -> String", + Mock.new, :html_escape, "abc" + assert_send_type "(String str) -> String", + Mock.new, :h, "abc" + end + + def test_url_encode + assert_send_type "(String str) -> String", + Mock.new, :url_encode, "abc" + assert_send_type "(String str) -> String", + Mock.new, :u, "abc" + end +end + +class ERBDefMethodSingletonTest < Test::Unit::TestCase + include TypeAssertions + + library "erb" + testing "singleton(::ERB::DefMethod)" + + def test_def_erb_method + assert_send_type "(String, String) -> untyped", + ERB::DefMethod, :def_erb_method, "render()", __FILE__ + end +end