Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Add `_Predicate` type (#290)

authored by

Joel Drapper and committed by
GitHub
d8840618 e92339ae

+37 -25
+5
lib/literal/types.rb
··· 21 21 autoload :NeverType, "literal/types/never_type" 22 22 autoload :NilableType, "literal/types/nilable_type" 23 23 autoload :NotType, "literal/types/not_type" 24 + autoload :PredicateType, "literal/types/predicate_type" 24 25 autoload :RangeType, "literal/types/range_type" 25 26 autoload :SetType, "literal/types/set_type" 26 27 autoload :TruthyType, "literal/types/truthy_type" ··· 337 338 else 338 339 NotType.new(type) 339 340 end 341 + end 342 + 343 + def _Predicate(message, &block) 344 + PredicateType.new(message:, block:) 340 345 end 341 346 342 347 # Matches if the value is a `Proc` or responds to `#to_proc`.
+22
lib/literal/types/predicate_type.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Types::PredicateType 4 + include Literal::Type 5 + 6 + def initialize(message:, block:) 7 + @message = message 8 + @block = block 9 + 10 + freeze 11 + end 12 + 13 + def inspect 14 + %(_Predicate("#{@message}")) 15 + end 16 + 17 + def ===(other) 18 + @block === other 19 + end 20 + 21 + freeze 22 + end
-25
test/types.test.rb
··· 771 771 assert _Void >= nil 772 772 assert _Void >= Object 773 773 end 774 - 775 - test "all methods are callable" do 776 - # also ensures that args sent to `?` methods match the non-`?` methods 777 - args = { 778 - _Array: [String], 779 - _Class: [Enumerable], 780 - _Constraint: [Integer, 1..2], 781 - _Descendant: [Enumerable], 782 - _Enumerable: [String], 783 - _Frozen: [Array], 784 - _Hash: [Symbol, Integer], 785 - _Interface: [:each, :map, :select], 786 - _Intersection: [1, 2], 787 - _Nilable: [String], 788 - _Not: [Integer], 789 - _Range: [Integer], 790 - _Set: [String], 791 - _Tuple: [String, Integer], 792 - _Union: [String, Integer], 793 - } 794 - Literal::Types.instance_methods(false).each do |method| 795 - args_for_method = args[method] || args[method.name.chomp("?").to_sym] 796 - assert public_send(method, *args_for_method) 797 - end 798 - end
+10
test/types/predicate_type.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "success" do 6 + predicate = _Predicate("starts with 'H'") { |it| it.start_with? "H" } 7 + 8 + assert predicate === "Hello" 9 + refute predicate === "World" 10 + end