Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Simplify the subtype method (#307)

Remove the `of` keyword argument and make it positional.

authored by

Joel Drapper and committed by
GitHub
8757090b 7220c436

+45 -46
+2 -2
config/quickdraw.rb
··· 42 42 43 43 class Quickdraw::Test 44 44 def assert_subtype(subtype, supertype) 45 - assert Literal.subtype?(subtype, of: supertype) do 45 + assert Literal.subtype?(subtype, supertype) do 46 46 "Expected #{subtype.inspect} to be a subtype of #{supertype.inspect}." 47 47 end 48 48 end 49 49 50 50 def refute_subtype(subtype, supertype) 51 - refute Literal.subtype?(subtype, of: supertype) do 51 + refute Literal.subtype?(subtype, supertype) do 52 52 "Expected #{subtype.inspect} not to be a subtype of #{supertype.inspect}." 53 53 end 54 54 end
+7 -8
lib/literal.rb
··· 21 21 type = Literal::Types._Constraint(*args, **kwargs) 22 22 value_class.define_method(:type) { type } 23 23 24 - if subtype?(type, of: Integer) 24 + if subtype?(type, Integer) 25 25 value_class.alias_method :to_i, :value 26 - elsif subtype?(type, of: String) 26 + elsif subtype?(type, String) 27 27 value_class.alias_method :to_s, :value 28 28 value_class.alias_method :to_str, :value 29 - elsif subtype?(type, of: Array) 29 + elsif subtype?(type, Array) 30 30 value_class.alias_method :to_a, :value 31 31 value_class.alias_method :to_ary, :value 32 - elsif subtype?(type, of: Hash) 32 + elsif subtype?(type, Hash) 33 33 value_class.alias_method :to_h, :value 34 - elsif subtype?(type, of: Float) 34 + elsif subtype?(type, Float) 35 35 value_class.alias_method :to_f, :value 36 - elsif subtype?(type, of: Set) 36 + elsif subtype?(type, Set) 37 37 value_class.alias_method :to_set, :value 38 38 end 39 39 ··· 88 88 end 89 89 end 90 90 91 - def self.subtype?(type, of:) 92 - supertype = of 91 + def self.subtype?(type, supertype) 93 92 subtype = type 94 93 95 94 subtype = subtype.block.call if Types::DeferredType === subtype
+4 -4
lib/literal/array.rb
··· 17 17 alias_method :[], :new 18 18 19 19 def ===(value) 20 - Literal::Array === value && Literal.subtype?(value.__type__, of: @type) 20 + Literal::Array === value && Literal.subtype?(value.__type__, @type) 21 21 end 22 22 23 23 def >=(other) 24 24 case other 25 25 when Literal::Array::Generic 26 - Literal.subtype?(other.type, of: @type) 26 + Literal.subtype?(other.type, @type) 27 27 else 28 28 false 29 29 end ··· 346 346 my_type = @__type__ 347 347 transform_type = Literal::Transforms.dig(my_type, block) 348 348 349 - if transform_type && Literal.subtype?(transform_type, of: my_type) 349 + if transform_type && Literal.subtype?(transform_type, my_type) 350 350 Literal::Array.allocate.__initialize_without_check__( 351 351 @__value__.map(&block), 352 352 type:, ··· 392 392 end 393 393 394 394 def narrow(type) 395 - unless Literal.subtype?(type, of: @__type__) 395 + unless Literal.subtype?(type, @__type__) 396 396 raise ArgumentError.new("Cannot narrow #{@__type__} to #{type}") 397 397 end 398 398
+1 -1
lib/literal/transforms.rb
··· 140 140 to_s: String, 141 141 year: Integer, 142 142 }, 143 - }.transform_values! { |it| it.transform_keys(&:to_proc) }.freeze 143 + }.transform_values! { |it| it.transform_keys(&:to_proc).freeze }.freeze
+2 -2
lib/literal/tuple.rb
··· 19 19 20 20 i, len = 0, types.size 21 21 while i < len 22 - return false unless Literal.subtype?(other_types[i], of: types[i]) 22 + return false unless Literal.subtype?(other_types[i], types[i]) 23 23 i += 1 24 24 end 25 25 ··· 36 36 37 37 i, len = 0, types.size 38 38 while i < len 39 - return false unless Literal.subtype?(other_types[i], of: types[i]) 39 + return false unless Literal.subtype?(other_types[i], types[i]) 40 40 i += 1 41 41 end 42 42
+2 -2
lib/literal/types/class_type.rb
··· 22 22 def >=(other) 23 23 case other 24 24 when Literal::Types::ClassType 25 - Literal.subtype?(other.type, of: @type) 25 + Literal.subtype?(other.type, @type) 26 26 when Literal::Types::DescendantType 27 - (Class === other.type) && Literal.subtype?(other.type, of: @type) 27 + (Class === other.type) && Literal.subtype?(other.type, @type) 28 28 else 29 29 false 30 30 end
+4 -4
lib/literal/types/constraint_type.rb
··· 46 46 when Literal::Types::ConstraintType 47 47 other_object_constraints = other.object_constraints 48 48 return false unless @object_constraints.all? do |constraint| 49 - other_object_constraints.any? { |c| Literal.subtype?(c, of: constraint) } 49 + other_object_constraints.any? { |c| Literal.subtype?(c, constraint) } 50 50 end 51 51 52 52 other_property_constraints = other.property_constraints 53 53 return false unless @property_constraints.all? do |k, v| 54 - Literal.subtype?(other_property_constraints[k], of: v) 54 + Literal.subtype?(other_property_constraints[k], v) 55 55 end 56 56 57 57 true 58 58 when Literal::Types::IntersectionType 59 59 other_object_constraints = other.types 60 60 return false unless @object_constraints.all? do |constraint| 61 - other_object_constraints.any? { |c| Literal.subtype?(c, of: constraint) } 61 + other_object_constraints.any? { |c| Literal.subtype?(c, constraint) } 62 62 end 63 63 64 64 true 65 65 when Literal::Types::FrozenType 66 - @object_constraints.all? { |constraint| Literal.subtype?(other.type, of: constraint) } 66 + @object_constraints.all? { |constraint| Literal.subtype?(other.type, constraint) } 67 67 else 68 68 false 69 69 end
+1 -1
lib/literal/types/deferred_type.rb
··· 18 18 end 19 19 20 20 def >=(other) 21 - Literal.subtype?(other, of: @block.call) 21 + Literal.subtype?(other, @block.call) 22 22 end 23 23 end
+1 -1
lib/literal/types/descendant_type.rb
··· 21 21 def >=(other) 22 22 case other 23 23 when Literal::Types::DescendantType, Literal::Types::ClassType 24 - Literal.subtype?(other.type, of: @type) 24 + Literal.subtype?(other.type, @type) 25 25 else 26 26 false 27 27 end
+1 -1
lib/literal/types/enumerable_type.rb
··· 22 22 def >=(other) 23 23 case other 24 24 when Literal::Types::EnumerableType 25 - Literal.subtype?(other.type, of: @type) 25 + Literal.subtype?(other.type, @type) 26 26 else 27 27 false 28 28 end
+2 -2
lib/literal/types/frozen_type.rb
··· 31 31 @type >= other.type 32 32 when Literal::Types::ConstraintType 33 33 type_match = false 34 - frozen_match = Literal.subtype?(other.property_constraints[:frozen?], of: true) 34 + frozen_match = Literal.subtype?(other.property_constraints[:frozen?], true) 35 35 36 36 other.object_constraints.each do |constraint| 37 37 frozen_match ||= ALWAYS_FROZEN.include?(constraint) 38 - type_match ||= Literal.subtype?(constraint, of: @type) 38 + type_match ||= Literal.subtype?(constraint, @type) 39 39 return true if frozen_match && type_match 40 40 end 41 41
+2 -2
lib/literal/types/hash_type.rb
··· 30 30 case other 31 31 when Literal::Types::HashType 32 32 ( 33 - Literal.subtype?(other.key_type, of: @key_type) 33 + Literal.subtype?(other.key_type, @key_type) 34 34 ) && ( 35 - Literal.subtype?(other.value_type, of: @value_type) 35 + Literal.subtype?(other.value_type, @value_type) 36 36 ) 37 37 else 38 38 false
+2 -2
lib/literal/types/interface_type.rb
··· 34 34 when Module 35 35 @methods.map { |m| METHOD_TYPE_MAPPINGS[m] }.all? { |types| types&.include?(other) } 36 36 when Literal::Types::IntersectionType 37 - other.types.any? { |type| Literal.subtype?(type, of: self) } 37 + other.types.any? { |type| Literal.subtype?(type, self) } 38 38 when Literal::Types::ConstraintType 39 - other.object_constraints.any? { |type| Literal.subtype?(type, of: self) } 39 + other.object_constraints.any? { |type| Literal.subtype?(type, self) } 40 40 else 41 41 false 42 42 end
+3 -3
lib/literal/types/intersection_type.rb
··· 36 36 when Literal::Types::IntersectionType 37 37 @types.all? do |type| 38 38 other.types.any? do |other_type| 39 - Literal.subtype?(other_type, of: type) 39 + Literal.subtype?(other_type, type) 40 40 end 41 41 end 42 42 when Literal::Types::ConstraintType 43 43 @types.all? do |type| 44 44 other.object_constraints.any? do |object_constraint| 45 - Literal.subtype?(object_constraint, of: type) 45 + Literal.subtype?(object_constraint, type) 46 46 end 47 47 end 48 48 when Literal::Types::FrozenType 49 - @types.all? { |type| Literal.subtype?(other.type, of: type) } 49 + @types.all? { |type| Literal.subtype?(other.type, type) } 50 50 else 51 51 false 52 52 end
+1 -1
lib/literal/types/map_type.rb
··· 45 45 other_shape = other.shape 46 46 47 47 @shape.all? do |k, v| 48 - Literal.subtype?(other_shape[k], of: v) 48 + Literal.subtype?(other_shape[k], v) 49 49 end 50 50 else 51 51 false
+2 -2
lib/literal/types/nilable_type.rb
··· 26 26 def >=(other) 27 27 case other 28 28 when Literal::Types::NilableType 29 - Literal.subtype?(other.type, of: @type) 29 + Literal.subtype?(other.type, @type) 30 30 when nil 31 31 true 32 32 else 33 - Literal.subtype?(other, of: @type) 33 + Literal.subtype?(other, @type) 34 34 end 35 35 end 36 36
+3 -3
lib/literal/types/not_type.rb
··· 22 22 def >=(other) 23 23 case other 24 24 when Literal::Types::NotType 25 - Literal.subtype?(other.type, of: @type) 25 + Literal.subtype?(other.type, @type) 26 26 when Literal::Types::ConstraintType 27 - other.object_constraints.any? { |constraint| Literal.subtype?(constraint, of: self) } 27 + other.object_constraints.any? { |constraint| Literal.subtype?(constraint, self) } 28 28 when Literal::Types::IntersectionType 29 - other.types.any? { |type| Literal.subtype?(type, of: self) } 29 + other.types.any? { |type| Literal.subtype?(type, self) } 30 30 else 31 31 false 32 32 end
+1 -1
lib/literal/types/range_type.rb
··· 28 28 def >=(other) 29 29 case other 30 30 when Literal::Types::RangeType 31 - Literal.subtype?(other.type, of: @type) 31 + Literal.subtype?(other.type, @type) 32 32 else 33 33 false 34 34 end
+1 -1
lib/literal/types/set_type.rb
··· 38 38 def >=(other) 39 39 case other 40 40 when Literal::Types::SetType 41 - Literal.subtype?(other.type, of: @type) 41 + Literal.subtype?(other.type, @type) 42 42 else 43 43 false 44 44 end
+3 -3
lib/literal/types/union_type.rb
··· 76 76 case other 77 77 when Literal::Types::UnionType 78 78 types_have_at_least_one_subtype = other.types.all? do |other_type| 79 - primitives.any? { |p| Literal.subtype?(other_type, of: p) } || types.any? { |t| Literal.subtype?(other_type, of: t) } 79 + primitives.any? { |p| Literal.subtype?(other_type, p) } || types.any? { |t| Literal.subtype?(other_type, t) } 80 80 end 81 81 82 82 primitives_have_at_least_one_subtype = other.primitives.all? do |other_primitive| 83 - primitives.any? { |p| Literal.subtype?(other_primitive, of: p) } || types.any? { |t| Literal.subtype?(other_primitive, of: t) } 83 + primitives.any? { |p| Literal.subtype?(other_primitive, p) } || types.any? { |t| Literal.subtype?(other_primitive, t) } 84 84 end 85 85 86 86 types_have_at_least_one_subtype && primitives_have_at_least_one_subtype 87 87 else 88 - types.any? { |t| Literal.subtype?(other, of: t) } || primitives.any? { |p| Literal.subtype?(other, of: p) } 88 + types.any? { |t| Literal.subtype?(other, t) } || primitives.any? { |p| Literal.subtype?(other, p) } 89 89 end 90 90 end 91 91