Skip to content

Commit

Permalink
Add ERB::Util and ERB::DefMethod signature
Browse files Browse the repository at this point in the history
  • Loading branch information
ksss committed Jul 28, 2022
1 parent 0f5fdaa commit 4468007
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
107 changes: 107 additions & 0 deletions stdlib/erb/0/erb.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,111 @@ class ERB
# print MyClass.new('foo', 123).render()
#
def def_class: (?Class, ?String) -> Class

# <!-- rdoc-file=lib/erb.rb -->
# A utility module for conversion routines, often handy in HTML generation.
#
module Util
# <!--
# rdoc-file=lib/erb.rb
# - html_escape(s)
# -->
# 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 &gt; 0 &amp; a &lt; 10?
#
def self?.html_escape: (String str) -> String

# <!--
# rdoc-file=lib/erb.rb
# - h(s)
# -->
#
alias h html_escape

# <!--
# rdoc-file=lib/erb.rb
# - h(s)
# -->
#
alias self.h self.html_escape

# <!--
# rdoc-file=lib/erb.rb
# - url_encode(s)
# -->
# 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

# <!--
# rdoc-file=lib/erb.rb
# - u(s)
# -->
#
alias u url_encode

# <!--
# rdoc-file=lib/erb.rb
# - u(s)
# -->
#
alias self.u self.url_encode
end

# <!-- rdoc-file=lib/erb.rb -->
# Utility module to define eRuby script as instance method.
#
# ### Example
#
# example.rhtml:
# <% for item in @items %>
# <b><%= item %></b>
# <% 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:
#
# <b>10</b>
#
# <b>20</b>
#
# <b>30</b>
#
module DefMethod
# <!--
# rdoc-file=lib/erb.rb
# - def_erb_method(methodname, erb_or_fname)
# -->
# 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
57 changes: 57 additions & 0 deletions test/stdlib/ERB_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4468007

Please sign in to comment.