Skip to content

Commit

Permalink
Add type for Enumerator::Chain
Browse files Browse the repository at this point in the history
  • Loading branch information
ParadoxV5 committed Jan 8, 2025
1 parent 4a3ac04 commit cfb8ed6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
24 changes: 22 additions & 2 deletions core/enumerator.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,22 @@ end
# This type of objects can be created by Enumerable#chain and Enumerator#+.
#
class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
include Enumerable[Elem]
# * `Rubocop: `self` type is not allowed in this context
# * {Enumerator::Chain#each} without block doesn't return `self`, unlike {Enumerator#each}.
#include Enumerator::_Each[Enum, self] # Workaround: def each:

# <!--
# rdoc-file=enumerator.c
# - Enumerator::Chain.new(*enums) -> enum
# -->
# Generates a new enumerator object that iterates over the elements of given
# enumerable objects in sequence.
#
# e = Enumerator::Chain.new(1..3, [4, 5])
# e.to_a #=> [1, 2, 3, 4, 5]
# e.size #=> 5
#
def initialize: (*_Each[Elem] enums) -> void

# <!--
# rdoc-file=enumerator.c
Expand All @@ -626,5 +641,10 @@ class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
#
# If no block is given, returns an enumerator.
#
def each: () { (Elem) -> void } -> void
def each: () { (Enum) -> void } -> self
| () -> Enumerator[E, self]

# wrong argument type chain (expected enumerator) (TypeError)
def with_index: (?Integer) ?{ (?) -> untyped } -> bot
def each_with_index: () ?{ (?) -> untyped } -> bot
end
19 changes: 19 additions & 0 deletions test/stdlib/Enumerator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,22 @@ def test_to_proc
end.next
end
end

class EnumeratorChainTest < Test::Unit::TestCase
include TestHelper

testing "::Enumerator::Chain[::Integer]"

def test_class_new
assert_send_type "(*_Each[Integer] enums) -> Enumerator::Chain[Integer]",
Enumerator::Chain, :new, 1..3, [4, 5]
end

def test_each
enum = Enumerator::Chain.new 1..3, [4, 5]
assert_send_type "() { (Integer) -> nil } -> Enumerator::Chain[Integer]",
enum, :each do end
assert_send_type "() -> Enumerator[Integer, Enumerator::Chain[Integer]]",
enum, :each
end
end

0 comments on commit cfb8ed6

Please sign in to comment.