Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Require explicit throw in Result#try blocks

Prevent implicit success wrapping from block return values so Result control flow is always explicit via success/failure throws.

+9 -20
+1 -2
lib/literal/result/generic.rb
··· 34 34 Literal.check(caught.result, self) 35 35 caught.result 36 36 else 37 - Literal.check(caught, @success_type) 38 - success(caught) 37 + raise Literal::ArgumentError.new("Expected block to throw a success or failure result") 39 38 end 40 39 end 41 40
+8 -18
test/result.test.rb
··· 11 11 assert_equal 42, result.value! 12 12 end 13 13 14 - test "result block wraps returned success values" do 15 - result = Literal::Result(Integer, Symbol) do 16 - 42 17 - end 18 - 19 - assert result.success? 20 - assert_equal 42, result.value! 21 - assert_equal Integer, result.success_type 22 - assert_equal Symbol, result.failure_type 23 - end 24 - 25 - test "result block checks returned values against success type" do 26 - error = assert_raises(Literal::TypeError) do 14 + test "result block requires throwing a result" do 15 + error = assert_raises(Literal::ArgumentError) do 27 16 Literal::Result(Integer, Symbol) do 28 - "42" 17 + 42 29 18 end 30 19 end 31 20 32 - assert_equal Integer, error.to_h[:expected] 33 - assert_equal "42", error.to_h[:actual] 21 + assert_equal "Expected block to throw a success or failure result", error.message 34 22 end 35 23 36 - test "result block must return a matching result" do 37 - assert_raises(Literal::TypeError) do 24 + test "result block must throw, not return, a result" do 25 + error = assert_raises(Literal::ArgumentError) do 38 26 Literal::Result(Integer, Symbol) do 39 27 Literal::Result(String, Symbol) { |type| type.success("42") } 40 28 end 41 29 end 30 + 31 + assert_equal "Expected block to throw a success or failure result", error.message 42 32 end 43 33 44 34 test "then adopts returned success type" do