Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Rename Result#tap to Result#also

+36 -12
+2 -2
lib/literal/failure.rb
··· 73 73 ) 74 74 end 75 75 76 - def tap 76 + def also 77 77 raise Literal::ArgumentError unless block_given? 78 78 self 79 79 end 80 80 81 - def then 81 + def and_then 82 82 raise Literal::ArgumentError unless block_given? 83 83 self 84 84 end
+2 -2
lib/literal/success.rb
··· 74 74 ) 75 75 end 76 76 77 - def tap 77 + def also 78 78 raise Literal::ArgumentError unless block_given? 79 79 yield(@value) 80 80 self 81 81 end 82 82 83 - def then 83 + def and_then 84 84 raise Literal::ArgumentError unless block_given? 85 85 result = yield(@value) 86 86
+32 -8
test/result.test.rb
··· 31 31 assert_equal "Expected block to throw a success or failure result", error.message 32 32 end 33 33 34 - test "then adopts returned success type" do 34 + test "and_then adopts returned success type" do 35 35 result = Literal::Result(Integer, Symbol) { |type| type.success(42) } 36 - .then { |value| Literal::Result(String, RuntimeError) { |type| type.success(value.to_s) } } 36 + .and_then { |value| Literal::Result(String, RuntimeError) { |type| type.success(value.to_s) } } 37 37 38 38 assert result.success? 39 39 assert_equal "42", result.value! 40 40 assert Literal::Result(String, _Union(Symbol, RuntimeError)) === result 41 41 end 42 42 43 - test "then unions failure types" do 43 + test "and_then unions failure types" do 44 44 result = Literal::Result(Integer, Symbol) { |type| type.success(42) } 45 - .then { Literal::Result(String, RuntimeError) { |type| type.failure(RuntimeError.new("boom")) } } 45 + .and_then { Literal::Result(String, RuntimeError) { |type| type.failure(RuntimeError.new("boom")) } } 46 46 47 47 assert result.failure? 48 48 assert RuntimeError === result.error! 49 49 assert Literal::Result(String, _Union(Symbol, RuntimeError)) === result 50 50 end 51 51 52 - test "failure then does not yield" do 52 + test "failure and_then does not yield" do 53 53 original = Literal::Result(Integer, Symbol) { |type| type.failure(:nope) } 54 54 yielded = false 55 55 56 - result = original.then do 56 + result = original.and_then do 57 57 yielded = true 58 58 Literal::Result(String, RuntimeError) { |type| type.success("ok") } 59 59 end ··· 62 62 assert result.equal?(original) 63 63 end 64 64 65 - test "then block must return result" do 65 + test "also yields success value and returns self" do 66 + original = Literal::Result(Integer, Symbol) { |type| type.success(42) } 67 + yielded = nil 68 + 69 + result = original.also do |value| 70 + yielded = value 71 + end 72 + 73 + assert_equal 42, yielded 74 + assert result.equal?(original) 75 + end 76 + 77 + test "failure also does not yield" do 78 + original = Literal::Result(Integer, Symbol) { |type| type.failure(:nope) } 79 + yielded = false 80 + 81 + result = original.also do 82 + yielded = true 83 + end 84 + 85 + refute yielded 86 + assert result.equal?(original) 87 + end 88 + 89 + test "and_then block must return result" do 66 90 result = Literal::Result(Integer, Symbol) { |type| type.success(42) } 67 91 68 92 error = assert_raises(Literal::ArgumentError) do 69 - result.then(&:to_s) 93 + result.and_then(&:to_s) 70 94 end 71 95 72 96 assert_equal "Expected block to return a Literal::Result, got String", error.message