Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add higher-order types (Kinds) (#351)

authored by

Joel Drapper and committed by
GitHub
68c5d58e d0c9c095

+85 -3
+2
lib/literal.rb
··· 98 98 subtype = subtype.block.call if Types::DeferredType === subtype 99 99 supertype = supertype.block.call if Types::DeferredType === supertype 100 100 101 + return true if subtype == Types::NeverType::Instance 102 + 101 103 return true if supertype == subtype 102 104 103 105 case supertype
+4
lib/literal/types.rb
··· 273 273 ) 274 274 end 275 275 276 + def _Kind(type) 277 + KindType.new(type) 278 + end 279 + 276 280 # Matches if the value is a `Proc` and `#lambda?` returns truthy. 277 281 def _Lambda 278 282 LambdaType
+1 -1
lib/literal/types/constraint_type.rb
··· 72 72 def <=(other) 73 73 case other 74 74 when Module 75 - @object_constraints.any? { |constraint| Literal.subtype?(other, constraint) } 75 + @object_constraints.any? { |constraint| Literal.subtype?(constraint, other) } 76 76 end 77 77 end 78 78
+7
lib/literal/types/intersection_type.rb
··· 52 52 end 53 53 end 54 54 55 + def <=(other) 56 + case other 57 + when Module 58 + @types.any? { |type| Literal.subtype?(type, other) } 59 + end 60 + end 61 + 55 62 freeze 56 63 end
+18
lib/literal/types/kind_type.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Types::KindType 4 + include Literal::Type 5 + 6 + def initialize(type) 7 + @type = type 8 + freeze 9 + end 10 + 11 + attr_reader :type 12 + 13 + def ===(object) 14 + Literal.subtype?(object, @type) 15 + end 16 + 17 + freeze 18 + end
+5 -1
lib/literal/types/never_type.rb
··· 16 16 17 17 def >=(other) 18 18 case other 19 - when Literal::Types::NeverTypeClass 19 + when Literal::Types::NeverType 20 20 true 21 21 else 22 22 false 23 23 end 24 + end 25 + 26 + def <=(_other) 27 + true 24 28 end 25 29 26 30 freeze
+9 -1
lib/literal/types/union_type.rb
··· 102 102 when Literal::Types::TaggedUnionType 103 103 other.members.values.all? { |t| primitives.any? { |p| Literal.subtype?(t, p) } || types.any? { |t2| Literal.subtype?(t, t2) } } 104 104 else 105 - types.any? { |t| Literal.subtype?(other, t) } || primitives.any? { |p| Literal.subtype?(other, p) } 105 + primitives.any? { |p| Literal.subtype?(other, p) } || types.any? { |t| Literal.subtype?(other, t) } 106 + end 107 + end 108 + 109 + def <=(other) 110 + case other 111 + when Module 112 + @primitives.all? { |primitive| Literal.subtype?(primitive, other) } && 113 + @types.all? { |type| Literal.subtype?(type, other) } 106 114 end 107 115 end 108 116
+32
test/types/_kind.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "===" do 6 + assert numeric_kind = _Kind(Numeric) 7 + assert numeric_kind === Numeric 8 + assert numeric_kind === Integer 9 + assert numeric_kind === 1 10 + assert numeric_kind === _Integer(1..) 11 + assert numeric_kind === _Intersection(Integer, 1..5) 12 + assert numeric_kind === _Constraint(Integer, odd?: true) 13 + assert numeric_kind === Float 14 + assert numeric_kind === _Union(Integer, Float) 15 + assert numeric_kind === _Never 16 + assert numeric_kind === _Intersection(Integer, Numeric) 17 + assert numeric_kind === _Deferred { Integer } 18 + 19 + refute numeric_kind === String 20 + refute numeric_kind === _Union(Integer, String) 21 + refute numeric_kind === _Union(Integer, nil) 22 + refute numeric_kind === _Nilable(Integer) 23 + refute numeric_kind === _Intersection(String, _Any?) 24 + refute numeric_kind === _Deferred { String } 25 + 26 + assert _Kind(Module) === Class 27 + assert _Kind(Module) === Module 28 + refute _Kind(String) === Class 29 + 30 + assert _Kind(_Array(Integer)) === _Array(1) 31 + assert _Kind(_Array(numeric_kind.type)) === _Array(1) 32 + end
+7
test/types/_never.test.rb
··· 9 9 10 10 refute _Never === nil 11 11 end 12 + 13 + test "subtyping" do 14 + assert_subtype _Never, Integer 15 + assert_subtype _Never, _Union(Integer, String) 16 + assert_subtype _Never, _Intersection(_Any?) 17 + assert_subtype _Never, _Never 18 + end