Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Raise Literal::ArgumentError in tag_for, type_of, and resolve when no match found

+6 -6
+3 -3
lib/literal/types/tagged_union_type.rb
··· 40 40 41 41 def tag_for(value) 42 42 @members.each { |tag, type| return tag if type === value } 43 - nil 43 + raise Literal::ArgumentError.new("No tag found for #{value.inspect} in #{inspect}.") 44 44 end 45 45 46 46 def type_of(value) 47 47 @members.each_value { |type| return type if type === value } 48 - nil 48 + raise Literal::ArgumentError.new("No type found for #{value.inspect} in #{inspect}.") 49 49 end 50 50 51 51 def resolve(value) 52 52 @members.each { |tag, type| return tag, type if type === value } 53 - nil 53 + raise Literal::ArgumentError.new("No match found for #{value.inspect} in #{inspect}.") 54 54 end 55 55 56 56 def >=(other)
+3 -3
test/types/_tagged_union.test.rb
··· 21 21 22 22 assert_equal type.tag_for("Alice"), :name 23 23 assert_equal type.tag_for(42), :age 24 - assert_equal type.tag_for(:other), nil 24 + assert_raises(Literal::ArgumentError) { type.tag_for(:other) } 25 25 end 26 26 27 27 test "#type_of" do ··· 29 29 30 30 assert_equal type.type_of("Alice"), String 31 31 assert_equal type.type_of(42), Integer 32 - assert_equal type.type_of(:other), nil 32 + assert_raises(Literal::ArgumentError) { type.type_of(:other) } 33 33 end 34 34 35 35 test "#resolve" do ··· 37 37 38 38 assert_equal type.resolve("Alice"), [:name, String] 39 39 assert_equal type.resolve(42), [:age, Integer] 40 - assert_equal type.resolve(:other), nil 40 + assert_raises(Literal::ArgumentError) { type.resolve(:other) } 41 41 end 42 42 43 43 test "#inspect" do