Skip to content

Commit

Permalink
Merge pull request #979 from ksss/no-matching-pattern-error
Browse files Browse the repository at this point in the history
Add Exception for pattern-matching
  • Loading branch information
pocke authored May 6, 2022
2 parents b37a586 + a343036 commit 6208485
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
30 changes: 30 additions & 0 deletions core/errors.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,36 @@ class NameError[T] < StandardError
def receiver: () -> T?
end

class NoMatchingPatternError < StandardError
end

class NoMatchingPatternKeyError[M, K] < NoMatchingPatternError
# <!--
# rdoc-file=error.c
# - NoMatchingPatternKeyError.new(message=nil, matchee: nil, key: nil) -> no_matching_pattern_key_error
# -->
# Construct a new `NoMatchingPatternKeyError` exception with the given message,
# matchee and key.
#
def initialize: (?string message, matchee: M, key: K) -> void

# <!--
# rdoc-file=error.c
# - no_matching_pattern_key_error.matchee -> object
# -->
# Return the matchee associated with this NoMatchingPatternKeyError exception.
#
def matchee: () -> M

# <!--
# rdoc-file=error.c
# - no_matching_pattern_key_error.key -> object
# -->
# Return the key caused this NoMatchingPatternKeyError exception.
#
def key: () -> K
end

# <!-- rdoc-file=error.c -->
# Raised when memory allocation fails.
#
Expand Down
35 changes: 35 additions & 0 deletions test/stdlib/NoMatchingPatternKeyError_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative "test_helper"

class NoMatchingPatternKeyErrorSingletonTest < Test::Unit::TestCase
include TypeAssertions

testing "singleton(::NoMatchingPatternKeyError)"


def test_new
assert_send_type "[Matchee, Key] (?::string message, matchee: Integer, key: Integer) -> ::NoMatchingPatternKeyError[Integer, Integer]",
NoMatchingPatternKeyError, :new, matchee: 123, key: 234
end
end

class NoMatchingPatternKeyErrorTest < Test::Unit::TestCase
include TypeAssertions

testing "::NoMatchingPatternKeyError[Hash[Symbol, untyped], Symbol]"


def test_initialize
assert_send_type "(?::string message, matchee: Hash[Symbol, untyped], key: Symbol) -> void",
NoMatchingPatternKeyError.new(matchee: {a: 1}, key: :aa), :initialize, matchee: {a: 1}, key: :aa
end

def test_matchee
assert_send_type "() -> Hash[Symbol, untyped]",
NoMatchingPatternKeyError.new(matchee: {a: 1}, key: :aa), :matchee
end

def test_key
assert_send_type "() -> Symbol",
NoMatchingPatternKeyError.new(matchee: {a: 1}, key: :aa), :key
end
end

0 comments on commit 6208485

Please sign in to comment.