-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sorbet-runtime] Add T.must_because type assertion (#6395)
* [sorbet-runtime] Add T.must_because type assertion * Add RBI, infer test data, and docs * Add untyped special casing * Fix auto-correct for T.must_because * Reuse T_must instrinsic * Fix infer test * Add missing semicolon * Make ternary into a one liner * Ran the tests and formatter locally * Try to fix infer test * [perf] Remove explicit &reason_blk arg
- Loading branch information
1 parent
1c7f45e
commit 2cb8794
Showing
8 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
require_relative '../test_helper' | ||
|
||
module Opus::Types::Test | ||
class MustBecauseTest < Critic::Unit::UnitTest | ||
EXAMPLE_REASON = 'some_must_because_reason' | ||
|
||
it 'allows non-nil' do | ||
assert_equal(:a, T.must_because(:a) {EXAMPLE_REASON}) | ||
assert_equal(0, T.must_because(0) {EXAMPLE_REASON}) | ||
assert_equal("", T.must_because("") {EXAMPLE_REASON}) | ||
assert_equal(false, T.must_because(false) {EXAMPLE_REASON}) | ||
end | ||
|
||
it 'disallows nil' do | ||
e = assert_raises(TypeError) do | ||
T.must_because(nil) {EXAMPLE_REASON} | ||
end | ||
|
||
assert_equal("Unexpected `nil` because #{EXAMPLE_REASON}", e.message) | ||
end | ||
|
||
it 'does not calculate the reason unless nil is passed' do | ||
T.must_because(:a) do | ||
raise('reason block should not have been called') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# typed: strict | ||
|
||
def test_must_because # error: does not have a `sig` | ||
x = T.cast(nil, T.nilable(String)) # error: `T.cast` is useless | ||
T.assert_type!(T.must_because(x) {'reason'}, String) | ||
|
||
T.must_because(x) {'reason'} | ||
T.must_because() | ||
# ^^ error: Not enough arguments | ||
# ^ error: requires a block parameter | ||
|
||
T.must_because(x) # error: requires a block parameter | ||
T.must_because(x, 0) | ||
# ^ error: Expected: `1`, got: `2` | ||
# ^ error: requires a block parameter | ||
T.must_because(x) {0} # error: Expected `String` | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters