Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Introduce `_Unit` type

+59
+12
lib/literal/types.rb
··· 441 441 ) 442 442 end 443 443 444 + # The unit type is a type that matches only the same object 445 + def _Unit(object) 446 + UnitType.new(object) 447 + end 448 + 449 + # Nilable version of `_Unit` 450 + def _Unit?(...) 451 + _Nilable( 452 + _Unit(...) 453 + ) 454 + end 455 + 444 456 def _Void 445 457 VoidType::Instance 446 458 end
+30
lib/literal/types/unit_type.rb
··· 1 + # frozen_string_literal: true 2 + 3 + # @api private 4 + class Literal::Types::UnitType 5 + include Literal::Type 6 + 7 + EQUAL_METHOD = BasicObject.instance_method(:equal?) 8 + 9 + def initialize(object) 10 + @object = object 11 + freeze 12 + end 13 + 14 + attr_reader :object 15 + 16 + def ===(value) 17 + EQUAL_METHOD.bind_call(@object, value) 18 + end 19 + 20 + def >=(other) 21 + case other 22 + when Literal::Types::UnitType 23 + EQUAL_METHOD.bind_call(@object, other.object) 24 + else 25 + false 26 + end 27 + end 28 + 29 + freeze 30 + end
+17
test/types/_unit.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "===" do 6 + object = -> { "hello" } 7 + 8 + assert _Unit(object) === object 9 + refute _Unit(object) === -> { "hello" } 10 + end 11 + 12 + test "hierarchy" do 13 + object = "a" 14 + 15 + assert_subtype _Unit(object), _Unit(object) 16 + refute_subtype _Unit(object), _Unit(+"a") 17 + end