Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Improve error handling in Literal::Enum

+36 -2
+7 -2
lib/literal/enum.rb
··· 55 55 56 56 types = @indexes_definitions.fetch(key) 57 57 type = types.first 58 - Literal.check(value, type) { |c| raise NotImplementedError } 58 + Literal.check(value, type) 59 59 60 60 @indexes.fetch(key)[value] 61 61 end ··· 125 125 126 126 index.each do |key, values| 127 127 unless type === key 128 - raise Literal::TypeError.expected(key, to_be_a: type) 128 + raise Literal::TypeError.new( 129 + context: Literal::TypeError::Context.new( 130 + expected: type, 131 + actual: key, 132 + ) 133 + ) 129 134 end 130 135 131 136 if unique && values.size > 1
+9
mise.toml
··· 1 1 [tools] 2 2 ruby = "latest" 3 + 4 + [tasks.test] 5 + run = "bundle exec qt" 6 + 7 + [tasks.lint] 8 + run = "bundle exec rubocop" 9 + 10 + [tasks.format] 11 + run = "bundle exec rubocop -A"
+20
test/enum.test.rb
··· 179 179 assert_equal [Color::Red], Color.where(hex: "#FF0000") 180 180 end 181 181 182 + test "#where raises Literal::TypeError when index value has the wrong type" do 183 + assert_raises(Literal::TypeError) do 184 + Color.where(hex: 123) 185 + end 186 + end 187 + 182 188 test "#find_by" do 183 189 assert_equal Color::Red, Color.find_by(hex: "#FF0000") 190 + end 191 + 192 + test "index definitions raise Literal::TypeError when index key type is wrong" do 193 + klass = Class.new(Literal::Enum(Integer)) do 194 + index :bad, String do |it| 195 + it.value 196 + end 197 + 198 + A = new(1) 199 + end 200 + 201 + assert_raises(Literal::TypeError) do 202 + klass.__after_defined__ 203 + end 184 204 end 185 205 186 206 test "#find_by raises when used with a non-unique index" do