Refactor tests

This commit is contained in:
Mike Cifelli 2019-01-27 12:01:23 -05:00
parent 01b1d4c7e3
commit 120f9c1e78
1 changed files with 39 additions and 39 deletions

View File

@ -8,6 +8,45 @@ require_relative '../lib/mutual_recursion'
class InventoryTest < Minitest::Test
include MutualRecursion
def one(x, y = 0)
return terminal_value(y) if x.negative?
tail_call { two(x, y + 1) }
end
def two(x, y)
tail_call { one(x - 1, y) }
end
def direct(x, y = 0)
return terminal_value(y) if x.negative?
tail_call { direct(x - 1, y + 1) }
end
def lambda_returning
terminal_value(-> { 24 })
end
def proc_returning(x, y = 0)
return terminal_value(proc { "|#{y}|" }) if x.negative?
tail_call { proc_returning(x - 1, y + 1) }
end
def bad_return
tail_call do
Class.new do
attr_reader :value, :block
def initialize
@value = 99
@block = proc { 25 }
end
end.new
end
end
def test_terminal_value
tail = terminal_value(42)
assert_equal(42, tail.invoke)
@ -53,42 +92,3 @@ class InventoryTest < Minitest::Test
assert_equal(42, tail.invoke)
end
end
def one(x, y = 0)
return terminal_value(y) if x.negative?
tail_call { two(x, y + 1) }
end
def two(x, y)
tail_call { one(x - 1, y) }
end
def direct(x, y = 0)
return terminal_value(y) if x.negative?
tail_call { direct(x - 1, y + 1) }
end
def lambda_returning
terminal_value(-> { 24 })
end
def proc_returning(x, y = 0)
return terminal_value(proc { "|#{y}|" }) if x.negative?
tail_call { proc_returning(x - 1, y + 1) }
end
def bad_return
tail_call do
Class.new do
attr_reader :value, :block
def initialize
@value = 99
@block = proc { 25 }
end
end.new
end
end