Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

No more allocations

+61 -6
+5 -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.each do |k, v| 13 + return false unless Hash === value 14 + 15 + value.each do |k, v| 14 16 return false unless @key_type === k && @value_type === v 15 17 end 18 + 19 + true 16 20 end 17 21 end
+7 -1
lib/literal/types/set_type.rb
··· 9 9 def inspect = "_Set(#{@type.inspect})" 10 10 11 11 def ===(value) 12 - Set === value && value.all?(@type) 12 + return false unless Set === value 13 + 14 + value.each do |v| 15 + return false unless @type === v 16 + end 17 + 18 + true 13 19 end 14 20 end
+10 -1
lib/literal/types/tuple_type.rb
··· 11 11 def inspect = "_Tuple(#{@types.map(&:inspect).join(', ')})" 12 12 13 13 def ===(value) 14 - Array === value && value.size == @types.size && @types.each_with_index.all? { |t, i| t === value[i] } 14 + return false unless Array === value 15 + return false unless value.size == @types.size 16 + 17 + i, len = 0, @types.size 18 + while i < len 19 + return false unless @types[i] === value[i] 20 + i += 1 21 + end 22 + 23 + true 15 24 end 16 25 end
+7 -2
lib/literal/types/union_type.rb
··· 6 6 def initialize(*types) 7 7 raise Literal::ArgumentError.new("_Union type must have at least one type.") if types.size < 1 8 8 9 - @types = Set[] 9 + @types = [] 10 10 load_types(types) 11 + @types.uniq! 11 12 @types.freeze 12 13 end 13 14 14 15 def inspect = "_Union(#{@types.inspect})" 15 16 16 17 def ===(value) 17 - @types.any? { |type| type === value } 18 + i, len = 0, @types.size 19 + while i < len 20 + return true if @types[i] === value 21 + i += 1 22 + end 18 23 end 19 24 20 25 def each(&)
+31
test/allocations_test.rb
··· 54 54 55 55 assert_allocations(_Integer(18..), 18) => 0 56 56 57 + assert_allocations(_Interface(:to_s), "Hello") => 0 58 + 59 + assert_allocations(_Intersection(Enumerable, Array), []) => 0 60 + 57 61 assert_allocations(_JSONData, { "a" => 1, "b" => [true, false, 0, 1.23, nil, { "a" => 1 }] }) => 0 58 62 63 + assert_allocations(_Lambda, proc {}) => 0 64 + 59 65 assert_allocations(_Map(name: String, age: Integer), { name: "Joel", age: 30 }) => 0 66 + 67 + assert_allocations(_Never, false) => 0 68 + 69 + assert_allocations(_Nilable(String), nil) => 0 70 + 71 + assert_allocations(_Not(Integer), 18) => 0 72 + 73 + assert_allocations(_Procable, proc {}) => 0 74 + 75 + assert_allocations(_Range(Integer), 0..10) => 0 76 + 77 + assert_allocations(_Set(String), Set["a", "b", "c"]) => 0 78 + 79 + assert_allocations(_String(length: 5..10), "Hello") => 0 80 + 81 + assert_allocations(_Symbol(size: 5..10), :symbol) => 0 82 + 83 + assert_allocations(_Truthy, true) => 0 84 + assert_allocations(_Truthy, false) => 0 85 + 86 + assert_allocations(_Tuple(String, Integer), ["a", 1]) => 0 87 + 88 + assert_allocations(_Union(String, Integer), 42) => 0 89 + 90 + assert_allocations(_Void, nil) => 0
+1 -1
test/types.test.rb
··· 299 299 _Union(Symbol, Float), 300 300 ) 301 301 302 - expect(type.inspect) == "_Union(#<Set: {String, Integer, Symbol, Float}>)" 302 + expect(type.inspect) == "_Union([String, Integer, Symbol, Float])" 303 303 end 304 304 305 305 test "_Void" do