Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Fixes error for missing `Literal::TypeError::Context#to_h` and adds `deconstruct` to `Literal::TypeError`.

+51
+17
lib/literal/errors/type_error.rb
··· 41 41 expected.record_literal_type_errors(child) if expected.respond_to?(:record_literal_type_errors) 42 42 @children << child 43 43 end 44 + 45 + def to_h 46 + { 47 + receiver: @receiver, 48 + method: @method, 49 + label: @label, 50 + expected: @expected, 51 + actual: @actual, 52 + children: @children, 53 + } 54 + end 55 + 56 + alias to_hash to_h 44 57 end 45 58 46 59 def initialize(context:) ··· 71 84 end 72 85 end 73 86 message 87 + end 88 + 89 + def deconstruct 90 + to_h.values 74 91 end 75 92 76 93 def deconstruct_keys(keys)
+34
test/type_error.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + Age = Literal::Value(Integer, 18..) 4 + 5 + test do 6 + Age.new(17) 7 + rescue => error 8 + assert_equal error.class, Literal::TypeError 9 + assert_equal error.message, "Type mismatch\n\n" \ 10 + " _Constraint(Integer, 18..)\n" \ 11 + " Expected: 18..\n" \ 12 + " Actual (Integer): 17\n" 13 + 14 + # deconstruct_keys 15 + key_names = [:receiver, :method, :label, :actual] 16 + exp_keys = { 17 + receiver: nil, 18 + method: nil, 19 + label: nil, 20 + actual: 17, 21 + } 22 + assert_equal error.deconstruct_keys(key_names), exp_keys 23 + 24 + # deconstruct 25 + expected_value = error.deconstruct 26 + assert_equal expected_value.size, 6 27 + assert_equal expected_value[0], nil 28 + assert_equal expected_value[1], nil 29 + assert_equal expected_value[2], nil # "_Constraint(Integer, 18..)" 30 + assert_equal expected_value[3].class, Literal::Types::ConstraintType 31 + assert_equal expected_value[4], 17 32 + assert_equal expected_value[5].class, Array 33 + assert_equal expected_value[5].first.class, Literal::TypeError::Context 34 + end