Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Reduce runtime allocations

+41 -14
+3 -1
lib/literal/types/hash_type.rb
··· 10 10 def inspect = "_Hash(#{@key_type.inspect}, #{@value_type.inspect})" 11 11 12 12 def ===(value) 13 - Hash === value && value.all? { |k, v| @key_type === k && @value_type === v } 13 + Hash === value && value.each do |k, v| 14 + return false unless @key_type === k && @value_type === v 15 + end 14 16 end 15 17 end
+3 -1
lib/literal/types/json_data_type.rb
··· 9 9 when String, Integer, Float, true, false, nil 10 10 true 11 11 when Hash 12 - value.all? { |k, v| String === k && self === v } 12 + value.each do |k, v| 13 + return false unless String === k && self === v 14 + end 13 15 when Array 14 16 value.all?(self) 15 17 else
+3 -1
lib/literal/types/map_type.rb
··· 11 11 end 12 12 13 13 def ===(other) 14 - Hash === other && @shape.all? { |k, t| t === other[k] } 14 + Hash === other && @shape.each do |k, v| 15 + return false unless v === other[k] 16 + end 15 17 end 16 18 17 19 def record_literal_type_errors(context)
+32 -11
test/allocations_test.rb
··· 17 17 count_allocations(&) 18 18 end 19 19 20 - def no_allocations(type, thing) 20 + def assert_allocations(type, thing) 21 21 count_warm_allocations do 22 22 type === thing 23 - end => 0 23 + end.tap do |n| 24 + puts "#{n} allocations for #{type.inspect} === #{thing.inspect}" 25 + end 24 26 end 25 27 26 - no_allocations(_Any, "anything") 27 - no_allocations(_Any, false) 28 + assert_allocations(_Any, "anything") => 0 29 + assert_allocations(_Any, false) => 0 30 + 31 + assert_allocations(_Array(String), []) => 0 32 + assert_allocations(_Array(String), ["a", "b"]) => 0 33 + assert_allocations(_Array(String), ["a", "b", 1]) => 0 34 + 35 + assert_allocations(_Boolean, true) => 0 36 + assert_allocations(_Boolean, false) => 0 37 + assert_allocations(_Boolean, nil) => 0 38 + assert_allocations(_Boolean, 1) => 0 39 + 40 + assert_allocations(_Callable, -> {}) => 0 41 + assert_allocations(_Callable, method(:puts)) => 0 42 + 43 + assert_allocations(_Class(Enumerable), Array) => 0 44 + 45 + assert_allocations(_Descendant(Enumerable), Array) => 0 46 + 47 + assert_allocations(_Enumerable(String), []) => 0 48 + 49 + assert_allocations(_Float(18.0..), 1.234) => 0 50 + 51 + assert_allocations(_Frozen(String), "hello") => 0 52 + 53 + assert_allocations(_Hash(String, Integer), { "a" => 1, "b" => 2 }) => 0 28 54 29 - no_allocations(_Array(String), []) 30 - no_allocations(_Array(String), ["a", "b"]) 31 - no_allocations(_Array(String), ["a", "b", 1]) 55 + assert_allocations(_JSONData, { "a" => 1, "b" => [true, false, 0, 1.23, nil, { "a" => 1 }] }) => 0 32 56 33 - no_allocations(_Boolean, true) 34 - no_allocations(_Boolean, false) 35 - no_allocations(_Boolean, nil) 36 - no_allocations(_Boolean, 1) 57 + assert_allocations(_Map(name: String, age: Integer), { name: "Joel", age: 30 }) => 0