Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

at main 95 lines 2.5 kB view raw
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 6require "literal" 7require "set" 8 9include Literal::Types 10 11def count_allocations 12 x = GC.stat(:total_allocated_objects) 13 yield 14 GC.stat(:total_allocated_objects) - x 15end 16 17def count_warm_allocations(&) 18 count_allocations(&) 19 count_allocations(&) 20 count_allocations(&) 21end 22 23def assert_allocations(type, thing) 24 count_warm_allocations do 25 type === thing 26 end.tap do |n| 27 puts "#{n} allocations for #{type.inspect} === #{thing.inspect}" 28 end 29end 30 31assert_allocations(_Any, "anything") => 0 32assert_allocations(_Any, false) => 0 33 34assert_allocations(_Array(String), []) => 0 35assert_allocations(_Array(String), ["a", "b"]) => 0 36assert_allocations(_Array(String), ["a", "b", 1]) => 0 37 38assert_allocations(_Boolean, true) => 0 39assert_allocations(_Boolean, false) => 0 40assert_allocations(_Boolean, nil) => 0 41assert_allocations(_Boolean, 1) => 0 42 43assert_allocations(_Callable, -> {}) => 0 44assert_allocations(_Callable, method(:puts)) => 0 45 46assert_allocations(_Class(Enumerable), Array) => 0 47 48assert_allocations(_Descendant(Enumerable), Array) => 0 49 50assert_allocations(_Enumerable(String), []) => 0 51 52assert_allocations(_Float(18.0..), 1.234) => 0 53 54assert_allocations(_Frozen(String), "hello") => 0 55 56assert_allocations(_Hash(String, Integer), { "a" => 1, "b" => 2 }) => 0 57 58assert_allocations(_Integer(18..), 18) => 0 59 60assert_allocations(_Interface(:to_s), "Hello") => 0 61 62assert_allocations(_Intersection(Enumerable, Array), []) => 0 63 64assert_allocations(_JSONData, { "a" => 1, "b" => [true, false, 0, 1.23, nil, { "a" => 1 }] }) => 0 65 66assert_allocations(_Lambda, proc {}) => 0 67 68assert_allocations(_Map(name: String, age: Integer), { name: "Joel", age: 30 }) => 0 69 70assert_allocations(_Never, false) => 0 71 72assert_allocations(_Nilable(String), nil) => 0 73 74assert_allocations(_Not(Integer), 18) => 0 75 76assert_allocations(_Procable, proc {}) => 0 77 78assert_allocations(_Range(Integer), 0..10) => 0 79 80assert_allocations(_Set(String), Set["a", "b", "c"]) => 0 81 82assert_allocations(_String(length: 5..10), "Hello") => 0 83 84assert_allocations(_Symbol(size: 5..10), :symbol) => 0 85 86assert_allocations(_Truthy, true) => 0 87assert_allocations(_Truthy, false) => 0 88 89assert_allocations(_Tuple(String, Integer), ["a", 1]) => 0 90 91assert_allocations(_Union(String, Integer), 42) => 0 92 93assert_allocations(_Union(String, "Hello", "World"), "World") => 0 94 95assert_allocations(_Void, nil) => 0