Runtime assertions for Ruby
literal.fun
ruby
1# frozen_string_literal: true
2
3include Literal::Types
4
5test "===" do
6 type = _Intersection(
7 _Interface(:size),
8 _Interface(:each),
9 )
10
11 assert type === []
12 assert type === Set.new
13
14 refute type === "string"
15end
16
17test "hierarchy" do
18 assert_subtype _Intersection(String), _Intersection(String)
19 assert_subtype _Constraint(String, size: 1..5), _Intersection(String)
20
21 refute_subtype _Constraint(Integer, size: 1..2), _Intersection(String)
22end
23
24test "error message" do
25 error = assert_raises(Literal::TypeError) do
26 Literal.check(
27 "string",
28 _Intersection(
29 _Interface(:size),
30 _Interface(:each),
31 )
32 )
33 end
34
35 assert_equal error.message, <<~MSG
36 Type mismatch
37
38 _Intersection(_Interface(:size), _Interface(:each))
39 Expected: _Interface(:each)
40 Actual (String): "string"
41 MSG
42end