test/either_test.rb in rumonade-0.2.2 vs test/either_test.rb in rumonade-0.3.0

- old
+ new

@@ -158,10 +158,46 @@ assert_equal "Right(42)", Right(42).to_s assert_equal "RightProjection(Left(error))", Left("error").right.to_s assert_equal "LeftProjection(Right(42))", Right(42).left.to_s end - def test_plus_for_left_and_right - assert_equal Left(["bad", "worse"]), Left("bad") + Right(:good) + Left("worse") + Right(:good) - assert_equal Right([:good, :better]), Right(:good) + Right(:better) + def test_inspect_for_left_and_right_and_their_projections + assert_equal "Left(\"error\")", Left("error").inspect + assert_equal "Right(\"success\")", Right("success").inspect + assert_equal "RightProjection(Left(\"error\"))", Left("error").right.inspect + assert_equal "LeftProjection(Right(\"success\"))", Right("success").left.inspect + end + + def test_plus_concatenates_left_and_right_using_plus_operator + assert_equal Left("badworse"), Left("bad") + Right(1) + Left("worse") + Right(2) + assert_equal Left(["bad", "worse"]), Left(["bad"]) + Right(1) + Left(["worse"]) + Right(2) + assert_equal Right(3), Right(1) + Right(2) + end + + def test_concat_concatenates_left_and_right_with_custom_concatenation_function + multiply = lambda { |a, b| a * b } + assert_equal Left(33), Left(3).concat(Left(11), :concat_left => multiply) + assert_equal Left(14), Left(3).concat(Left(11), :concat_right => multiply) + assert_equal Right(44), Right(4).concat(Right(11), :concat_right => multiply) + assert_equal Right(15), Right(4).concat(Right(11), :concat_left => multiply) + end + + def test_lift_to_a_wraps_left_and_right_values_in_array + assert_equal Left(["error"]), Left("error").lift_to_a + assert_equal Right([42]), Right(42).lift_to_a + end + + def test_plus_and_lift_to_a_work_together_to_concatenate_errors + assert_equal Left([1, 2]), Left(1).lift_to_a + Right(:a).lift_to_a + Left(2).lift_to_a + Right(:b).lift_to_a + assert_equal Right([:a, :b]), Right(:a).lift_to_a + Right(:b).lift_to_a + end + + Person = Struct.new(:name, :age, :address) + + def test_concat_maps_concatenated_right_values_through_a_block + assert_equal Right(Person.new("Joe", 23, ["123 Some St", "Boston"])), + Right(["Joe"]).concat(Right([23])).concat(Right([["123 Some St", "Boston"]])) { |n, a, addr| Person.new(n, a, addr) } + # this usage is equivalent, but since ruby can't pass a block to a binary operator, must use .right.map on result: + assert_equal Right(Person.new("Joe", 23, ["123 Some St", "Boston"])), + (Right(["Joe"]) + Right([23]) + Right([["123 Some St", "Boston"]])).right.map { |n, a, addr| Person.new(n, a, addr) } end end