Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Create allocations_test.rb

+36
+36
test/allocations_test.rb
··· 1 + #!/usr/bin/env ruby 2 + # frozen_string_literal: true 3 + 4 + # NOTE: This is not a quickdraw test becuase it needs to be run in a single thread. 5 + 6 + require "literal" 7 + include Literal::Types 8 + 9 + def count_allocations 10 + x = GC.stat(:total_allocated_objects) 11 + yield 12 + GC.stat(:total_allocated_objects) - x 13 + end 14 + 15 + def count_warm_allocations(&) 16 + count_allocations(&) 17 + count_allocations(&) 18 + end 19 + 20 + def no_allocations(type, thing) 21 + count_warm_allocations do 22 + type === thing 23 + end => 0 24 + end 25 + 26 + no_allocations(_Any, "anything") 27 + no_allocations(_Any, false) 28 + 29 + no_allocations(_Array(String), []) 30 + no_allocations(_Array(String), ["a", "b"]) 31 + no_allocations(_Array(String), ["a", "b", 1]) 32 + 33 + no_allocations(_Boolean, true) 34 + no_allocations(_Boolean, false) 35 + no_allocations(_Boolean, nil) 36 + no_allocations(_Boolean, 1)