Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Extract more type tests into their own files

+248 -211
-152
test/types.test.rb
··· 42 42 assert _Deferred { Numeric } >= _Deferred { Integer } 43 43 end 44 44 45 - test "_Any" do 46 - Fixtures::Objects.each do |object| 47 - assert _Any === object 48 - end 49 - 50 - refute _Any === nil 51 - 52 - assert _Any >= _Any 53 - assert _Any >= String 54 - assert _Any >= "Hello" 55 - assert _Any >= 1 56 - 57 - refute _Any >= nil 58 - refute _Any >= Object 59 - refute _Any >= _Nilable(_Any) 60 - refute _Any >= _Nilable(String) 61 - end 62 - 63 45 test "_Array" do 64 46 assert _Array(String) === [] 65 47 assert _Array(String) === ["a", "b", "c"] ··· 78 60 refute _Boolean >= nil 79 61 assert _Boolean >= _Boolean 80 62 end 81 - 82 - 83 63 84 64 test "_Constraint with object constraints" do 85 65 age_constraint = _Constraint(Integer, 18..) ··· 147 127 refute _Constraint(String, size: 4) >= _Constraint(String, size: 1) 148 128 end 149 129 150 - test "_Date" do 151 - assert _Date(_Interface(:to_date)) === Date.today 152 - assert _Date(Date.today) === Date.today 153 - assert _Date((Date.today)..) === Date.today + 1 154 - 155 - refute _Date(_Interface(:non_existing_method)) === Date.today 156 - refute _Date(Date.today) === "2025-01-13" 157 - refute _Date(Date.today) === nil 158 - refute _Date((Date.today)..) === Date.today - 1 159 - 160 - assert _Date(year: 2025) === Date.new(2025, 1, 13) 161 - refute _Date(year: 2025) === Date.new(2024, 1, 13) 162 - 163 - assert _Date(DateTime, _Interface(:to_datetime)) === DateTime.now 164 - assert _Date(DateTime, year: 2025) === DateTime.new(2025, 1, 13) 165 - refute _Date(DateTime, year: 2025) === Date.new(2025, 1, 13) 166 - end 167 - 168 130 test "_Descendant" do 169 131 assert _Descendant(Enumerable) === Array 170 132 assert _Descendant(Enumerable) === Set ··· 177 139 refute _Descendant(Enumerable) >= _Descendant(String) 178 140 end 179 141 180 - test "_Enumerable" do 181 - assert _Enumerable(String) === ["a", "b", "c"] 182 - assert _Enumerable(Integer) === Set[1, 2, 3] 183 - 184 - refute _Enumerable(String) === [1, "a", :symbol] 185 - 186 - assert _Enumerable(Enumerable) >= _Enumerable(Array) 187 - assert _Enumerable(Enumerable) >= _Enumerable(Set) 188 - 189 - refute _Enumerable(Enumerable) >= _Enumerable(String) 190 - end 191 - 192 142 test "_Falsy" do 193 143 falsy_objects = Set[false, nil] 194 144 truthy_objects = Fixtures::Objects - falsy_objects ··· 218 168 refute _Float(18.0..) === 42 219 169 refute _Float(18.0..) === "string" 220 170 refute _Float(18.0..) === nil 221 - end 222 - 223 - test "_Frozen" do 224 - assert _Frozen(Array) === [].freeze 225 - assert _Frozen(String) === "immutable" 226 - 227 - refute _Frozen(Array) === [] 228 - refute _Frozen(String) === +"mutable" 229 - refute _Frozen(Array) === nil 230 - assert _Frozen(Enumerable) >= _Constraint(Array, frozen?: true) 231 171 end 232 172 233 173 test "_Hash" do ··· 575 515 refute _String(size: 5) === "string" 576 516 end 577 517 578 - test "_Symbol" do 579 - assert _Symbol(_Interface(:to_sym)) === :symbol 580 - assert _Symbol(size: 6) === :symbol 581 - 582 - refute _Symbol(_Interface(:non_existing_method)) === :symbol 583 - refute _Symbol(size: 5) === :symbol 584 - end 585 - 586 - test "_Time" do 587 - current_time = Time.now 588 - 589 - assert _Time (_Interface(:to_time)) === current_time 590 - assert _Time(current_time) === current_time 591 - assert _Time(current_time..) === current_time + 60 592 - 593 - refute _Time(_Interface(:non_existing_method)) === current_time 594 - refute _Time(current_time..) === current_time - 60 595 - 596 - unless RUBY_ENGINE == "jruby" 597 - assert _Time(zone: "UTC") === Time.new(2025, 1, 13, 20, 0, 0, "UTC") 598 - end 599 - 600 - fifteen_mins_ago = proc { |t| ((Time.now - (60 * 15))..(Time.now)) === t } 601 - assert _Time(fifteen_mins_ago) === Time.now - (60 * 10) 602 - refute _Time(fifteen_mins_ago) === Time.now - (60 * 20) 603 - occurs_on_monday = proc(&:monday?) 604 - assert _Time(occurs_on_monday) === Time.new(2025, 1, 13) 605 - refute _Time(occurs_on_monday) === Time.new(2025, 1, 14) 606 - 607 - refute _Time(Time.new(2025, 1, 13, 20, 0, 0, "UTC")) === "2025-01-13 20:00:00 UTC" 608 - refute _Time(Time.new(2025, 1, 13, 20, 0, 0, "UTC")) === nil 609 - end 610 - 611 518 test "_Truthy" do 612 519 truthy_objects = Fixtures::Objects - Set[false, nil] 613 520 falsy_objects = Set[false, nil] ··· 668 575 Expected: Integer 669 576 Actual (NilClass): nil 670 577 ERROR 671 - end 672 - 673 - test "_Union matching" do 674 - type = _Union(String, Integer) 675 - 676 - assert type === "string" 677 - assert type === 42 678 - 679 - refute type === :symbol 680 - refute type === [] 681 - refute type === nil 682 - 683 - assert _Union(String, Integer) >= _Union(String, Integer) 684 - refute _Union(String, Integer) >= _Union(String, Float) 685 - assert _Union(String, Integer) >= String 686 - refute _Union(String, Integer) >= Numeric 687 - assert _Union(String, Numeric) >= Float 688 - 689 - expect_type_error(expected: type, actual: :symbol, message: <<~ERROR) 690 - Type mismatch 691 - 692 - Expected: _Union([String, Integer]) 693 - Actual (Symbol): :symbol 694 - ERROR 695 - 696 - expect_type_error(expected: _Union(_Array(String), _Array(Integer)), actual: [nil], message: <<~ERROR) 697 - Type mismatch 698 - 699 - _Array(String) 700 - [0] 701 - Expected: String 702 - Actual (NilClass): nil 703 - _Array(Integer) 704 - [0] 705 - Expected: Integer 706 - Actual (NilClass): nil 707 - ERROR 708 - end 709 - 710 - test "_Union flattens types" do 711 - type = _Union( 712 - _Union(String, Integer), 713 - _Union(Symbol, Float), 714 - ) 715 - 716 - assert_equal type.inspect, "_Union([String, Integer, Symbol, Float])" 717 - end 718 - 719 - test "_Union with primitives" do 720 - position = _Union(:top, :right, :bottom, :left, Integer) 721 - 722 - assert position === :top 723 - assert position === :right 724 - assert position === :bottom 725 - assert position === :left 726 - assert position === 42 727 - 728 - refute position === :center 729 - refute position === "top" 730 578 end 731 579 732 580 test "_Void" do
+23
test/types/_any.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "===" do 6 + Fixtures::Objects.each do |object| 7 + assert _Any === object 8 + end 9 + 10 + refute _Any === nil 11 + end 12 + 13 + test "hierarchy" do 14 + assert_subtype _Any, _Any 15 + assert_subtype String, _Any 16 + assert_subtype "Hello", _Any 17 + assert_subtype 1, _Any 18 + 19 + refute_subtype nil, _Any 20 + refute_subtype Object, _Any 21 + refute_subtype _Nilable(_Any), _Any 22 + refute_subtype _Nilable(String), _Any 23 + end
+21
test/types/_date.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "===" do 6 + assert _Date(_Interface(:to_date)) === Date.today 7 + assert _Date(Date.today) === Date.today 8 + assert _Date((Date.today)..) === Date.today + 1 9 + 10 + refute _Date(_Interface(:non_existing_method)) === Date.today 11 + refute _Date(Date.today) === "2025-01-13" 12 + refute _Date(Date.today) === nil 13 + refute _Date((Date.today)..) === Date.today - 1 14 + 15 + assert _Date(year: 2025) === Date.new(2025, 1, 13) 16 + refute _Date(year: 2025) === Date.new(2024, 1, 13) 17 + 18 + assert _Date(DateTime, _Interface(:to_datetime)) === DateTime.now 19 + assert _Date(DateTime, year: 2025) === DateTime.new(2025, 1, 13) 20 + refute _Date(DateTime, year: 2025) === Date.new(2025, 1, 13) 21 + end
+17
test/types/_enumerable.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "===" do 6 + assert _Enumerable(String) === ["a", "b", "c"] 7 + assert _Enumerable(Integer) === Set[1, 2, 3] 8 + 9 + refute _Enumerable(String) === [1, "a", :symbol] 10 + end 11 + 12 + test "hierarchy" do 13 + assert_subtype _Enumerable(Array), _Enumerable(Enumerable) 14 + assert_subtype _Enumerable(Set), _Enumerable(Enumerable) 15 + 16 + refute_subtype _Enumerable(String), _Enumerable(Enumerable) 17 + end
+16
test/types/_frozen.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "===" do 6 + assert _Frozen(Array) === [].freeze 7 + assert _Frozen(String) === "immutable" 8 + 9 + refute _Frozen(Array) === [] 10 + refute _Frozen(String) === +"mutable" 11 + refute _Frozen(Array) === nil 12 + end 13 + 14 + test "hierarchy" do 15 + assert_subtype _Constraint(Array, frozen?: true), _Frozen(Enumerable) 16 + end
+7 -1
test/types/_predicate.test.rb
··· 2 2 3 3 include Literal::Types 4 4 5 - test "success" do 5 + test "===" do 6 6 predicate = _Predicate("starts with 'H'") { |it| it.start_with? "H" } 7 7 8 8 assert predicate === "Hello" 9 9 refute predicate === "World" 10 10 end 11 + 12 + test "predicates are subtypes of themselves" do 13 + predicate = _Predicate("even") { |it| it.even? } 14 + 15 + assert_subtype predicate, predicate 16 + end
+11
test/types/_symbol.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "===" do 6 + assert _Symbol(_Interface(:to_sym)) === :symbol 7 + assert _Symbol(size: 6) === :symbol 8 + 9 + refute _Symbol(_Interface(:non_existing_method)) === :symbol 10 + refute _Symbol(size: 5) === :symbol 11 + end
+28
test/types/_time.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + test "===" do 6 + current_time = Time.now 7 + 8 + assert _Time (_Interface(:to_time)) === current_time 9 + assert _Time(current_time) === current_time 10 + assert _Time(current_time..) === current_time + 60 11 + 12 + refute _Time(_Interface(:non_existing_method)) === current_time 13 + refute _Time(current_time..) === current_time - 60 14 + 15 + unless RUBY_ENGINE == "jruby" 16 + assert _Time(zone: "UTC") === Time.new(2025, 1, 13, 20, 0, 0, "UTC") 17 + end 18 + 19 + fifteen_mins_ago = proc { |t| ((Time.now - (60 * 15))..(Time.now)) === t } 20 + assert _Time(fifteen_mins_ago) === Time.now - (60 * 10) 21 + refute _Time(fifteen_mins_ago) === Time.now - (60 * 20) 22 + occurs_on_monday = proc(&:monday?) 23 + assert _Time(occurs_on_monday) === Time.new(2025, 1, 13) 24 + refute _Time(occurs_on_monday) === Time.new(2025, 1, 14) 25 + 26 + refute _Time(Time.new(2025, 1, 13, 20, 0, 0, "UTC")) === "2025-01-13 20:00:00 UTC" 27 + refute _Time(Time.new(2025, 1, 13, 20, 0, 0, "UTC")) === nil 28 + end
+125
test/types/_union.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + include Literal::Types 4 + 5 + def expect_type_error(expected:, actual:, message:) 6 + error = assert_raises(Literal::TypeError) do 7 + Literal.check(expected:, actual:) 8 + end 9 + 10 + assert_equal error.message, message 11 + end 12 + 13 + test "#enumerability" do 14 + union = _Union(:a, :b, :c, 1, 2, 3, String, Integer) 15 + 16 + assert_equal union.to_a, [:a, :b, :c, 1, 2, 3, String, Integer] 17 + end 18 + 19 + test "#deconstruct" do 20 + union = _Union(:a, :b, :c, 1, 2, 3, String, Integer) 21 + 22 + assert_equal union.deconstruct, [:a, :b, :c, 1, 2, 3, String, Integer] 23 + end 24 + 25 + test "#[]" do 26 + union = _Union(:a, :b, :c, 1, 2, 3, String, Integer) 27 + 28 + assert_equal union[:a], :a 29 + assert_equal union[:b], :b 30 + assert_equal union[:c], :c 31 + assert_equal union[1], 1 32 + assert_equal union[2], 2 33 + assert_equal union[3], 3 34 + assert_equal union[String], String 35 + assert_equal union[Integer], Integer 36 + 37 + assert_equal union[:d], nil 38 + end 39 + 40 + test "#fetch" do 41 + union = _Union(:a, :b, :c, 1, 2, 3, String, Integer) 42 + 43 + assert_equal union.fetch(:a), :a 44 + assert_equal union.fetch(:b), :b 45 + assert_equal union.fetch(:c), :c 46 + assert_equal union.fetch(1), 1 47 + assert_equal union.fetch(2), 2 48 + assert_equal union.fetch(3), 3 49 + assert_equal union.fetch(String), String 50 + assert_equal union.fetch(Integer), Integer 51 + 52 + assert_raises(KeyError) { union.fetch(:d) } 53 + end 54 + 55 + test ">=" do 56 + assert _Union(:a, :b, :c) >= _Union(:a, :b, :c) 57 + assert _Union(:a, :b, :c) >= _Union(:a, :b) 58 + refute _Union(:a, :b, :c) >= _Union(:a, :b, :c, :d) 59 + 60 + assert _Union(:a, :b, :c) >= :a 61 + 62 + assert _Union(Integer, String) >= 1 63 + assert _Union(Integer, String) >= "1" 64 + assert _Union(Array, Hash) >= {} 65 + assert _Union(Array, Hash) >= [] 66 + end 67 + 68 + test "_Union with primitives" do 69 + position = _Union(:top, :right, :bottom, :left, Integer) 70 + 71 + assert position === :top 72 + assert position === :right 73 + assert position === :bottom 74 + assert position === :left 75 + assert position === 42 76 + 77 + refute position === :center 78 + refute position === "top" 79 + end 80 + 81 + test "other unions are flattened" do 82 + type = _Union( 83 + _Union(String, Integer), 84 + _Union(Symbol, Float), 85 + ) 86 + 87 + assert_equal type.inspect, "_Union([String, Integer, Symbol, Float])" 88 + end 89 + 90 + test "_Union matching" do 91 + type = _Union(String, Integer) 92 + 93 + assert type === "string" 94 + assert type === 42 95 + 96 + refute type === :symbol 97 + refute type === [] 98 + refute type === nil 99 + 100 + assert _Union(String, Integer) >= _Union(String, Integer) 101 + refute _Union(String, Integer) >= _Union(String, Float) 102 + assert _Union(String, Integer) >= String 103 + refute _Union(String, Integer) >= Numeric 104 + assert _Union(String, Numeric) >= Float 105 + 106 + expect_type_error(expected: type, actual: :symbol, message: <<~ERROR) 107 + Type mismatch 108 + 109 + Expected: _Union([String, Integer]) 110 + Actual (Symbol): :symbol 111 + ERROR 112 + 113 + expect_type_error(expected: _Union(_Array(String), _Array(Integer)), actual: [nil], message: <<~ERROR) 114 + Type mismatch 115 + 116 + _Array(String) 117 + [0] 118 + Expected: String 119 + Actual (NilClass): nil 120 + _Array(Integer) 121 + [0] 122 + Expected: Integer 123 + Actual (NilClass): nil 124 + ERROR 125 + end
-58
test/union.test.rb
··· 1 - # frozen_string_literal: true 2 - 3 - include Literal::Types 4 - 5 - test "#enumerability" do 6 - union = _Union(:a, :b, :c, 1, 2, 3, String, Integer) 7 - 8 - assert_equal union.to_a, [:a, :b, :c, 1, 2, 3, String, Integer] 9 - end 10 - 11 - test "#deconstruct" do 12 - union = _Union(:a, :b, :c, 1, 2, 3, String, Integer) 13 - 14 - assert_equal union.deconstruct, [:a, :b, :c, 1, 2, 3, String, Integer] 15 - end 16 - 17 - test "#[]" do 18 - union = _Union(:a, :b, :c, 1, 2, 3, String, Integer) 19 - 20 - assert_equal union[:a], :a 21 - assert_equal union[:b], :b 22 - assert_equal union[:c], :c 23 - assert_equal union[1], 1 24 - assert_equal union[2], 2 25 - assert_equal union[3], 3 26 - assert_equal union[String], String 27 - assert_equal union[Integer], Integer 28 - 29 - assert_equal union[:d], nil 30 - end 31 - 32 - test "#fetch" do 33 - union = _Union(:a, :b, :c, 1, 2, 3, String, Integer) 34 - 35 - assert_equal union.fetch(:a), :a 36 - assert_equal union.fetch(:b), :b 37 - assert_equal union.fetch(:c), :c 38 - assert_equal union.fetch(1), 1 39 - assert_equal union.fetch(2), 2 40 - assert_equal union.fetch(3), 3 41 - assert_equal union.fetch(String), String 42 - assert_equal union.fetch(Integer), Integer 43 - 44 - assert_raises(KeyError) { union.fetch(:d) } 45 - end 46 - 47 - test ">=" do 48 - assert _Union(:a, :b, :c) >= _Union(:a, :b, :c) 49 - assert _Union(:a, :b, :c) >= _Union(:a, :b) 50 - refute _Union(:a, :b, :c) >= _Union(:a, :b, :c, :d) 51 - 52 - assert _Union(:a, :b, :c) >= :a 53 - 54 - assert _Union(Integer, String) >= 1 55 - assert _Union(Integer, String) >= "1" 56 - assert _Union(Array, Hash) >= {} 57 - assert _Union(Array, Hash) >= [] 58 - end