Skip to content

Commit cac6a80

Browse files
committed
added hash_pair test
1 parent 303cd56 commit cac6a80

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

test/typecheck/hash_pair/Steepfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
D = Steep::Diagnostic
2+
3+
target :test do
4+
signature "."
5+
check "."
6+
configure_code_diagnostics(D::Ruby.all_error)
7+
end

test/typecheck/hash_pair/test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class ToAry
2+
def initialize (*x) @x = x end
3+
def to_ary = @x
4+
end
5+
6+
class Pair
7+
def initialize(x, y) @x, @y = x, y end
8+
def to_ary = [@x, @y]
9+
end
10+
11+
class Test
12+
def self.takes_pair(pair)
13+
x, y = pair.to_ary
14+
x + y
15+
end
16+
end
17+
18+
is_pair = Pair.new(1, 2r) #: Pair[Integer, Rational]
19+
20+
Test.takes_pair(is_pair)
21+
22+
23+
Hash[[
24+
Pair.new(:a, 1r),
25+
Pair.new(:b, 2r)
26+
]] #: Hash[Symbol, Rational]
27+
28+
Hash[
29+
ToAry.new(
30+
Pair.new(:a, 1r),
31+
Pair.new(:b, 2r)
32+
)
33+
] #: Hash[Symbol, Rational]
34+
35+
hash = { a: 3, b: 4 }
36+
hash.to_h { |k, v| Pair.new(k.to_s, v.to_r) } #: Hash[String, Rational]
37+

test/typecheck/hash_pair/test.rbs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class ToAry[X]
2+
@x: Array[X]
3+
def initialize: (*X) -> void
4+
def to_ary: () -> Array[X]
5+
end
6+
7+
class Pair[X, Y]
8+
@x: X
9+
@y: Y
10+
11+
def initialize: (X, Y) -> void
12+
13+
def to_ary: () -> [X, Y]
14+
end
15+
16+
class Test
17+
def self.takes_pair: (Hash::_Pair[Integer, Rational]) -> Rational
18+
end

0 commit comments

Comments
 (0)