Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Flatten union types

+34 -8
+18 -7
lib/literal/types.test.rb
··· 284 284 refute _Tuple(String, Integer) === nil 285 285 end 286 286 287 - test "_Union" do 288 - type = _Union(String, Integer) 287 + describe "_Union" do 288 + test "_Union" do 289 + type = _Union(String, Integer) 290 + 291 + assert type === "string" 292 + assert type === 42 293 + 294 + refute type === :symbol 295 + refute type === [] 296 + refute type === nil 297 + end 289 298 290 - assert type === "string" 291 - assert type === 42 299 + test "flattens types" do 300 + type = _Union( 301 + _Union(String, Integer), 302 + _Union(Symbol, Float), 303 + ) 292 304 293 - refute type === :symbol 294 - refute type === [] 295 - refute type === nil 305 + expect(type.inspect) == "_Union(#<Set: {String, Integer, Symbol, Float}>)" 306 + end 296 307 end 297 308 298 309 test "_Void" do
+16 -1
lib/literal/types/union_type.rb
··· 5 5 6 6 def initialize(*types) 7 7 raise Literal::ArgumentError.new("_Union type must have at least one type.") if types.size < 1 8 - @types = types.to_set.freeze 8 + 9 + @types = Set[] 10 + load_types(types) 11 + @types.freeze 9 12 end 10 13 11 14 def inspect = "_Union(#{@types.inspect})" ··· 20 23 21 24 def deconstruct 22 25 @types.to_a 26 + end 27 + 28 + protected 29 + 30 + attr_reader :types 31 + 32 + private 33 + 34 + def load_types(types) 35 + types.each do |type| 36 + (Literal::Types::UnionType === type) ? load_types(type.types) : @types << type 37 + end 23 38 end 24 39 end