MemoizedValue memoizes nil results (#2053)

I moved the existing `subject` into the test because it didn't make sense for the second test I added.
This commit is contained in:
Josh Cheek 2019-06-03 03:09:31 -05:00 committed by Kyrylo Silin
parent 29bdd2e403
commit 6cc6e55f20
2 changed files with 18 additions and 4 deletions

View File

@ -19,11 +19,15 @@ class Pry
class MemoizedValue
def initialize(&block)
@block = block
@called = false
@call = nil
end
def call
@call ||= @block.call
return @call if @called
@called = true
@call = @block.call
end
end
end

View File

@ -2,10 +2,20 @@
RSpec.describe Pry::Config::MemoizedValue do
describe "#call" do
subject { described_class.new { rand } }
it "memoizes the result of call" do
expect(subject.call).to eq(subject.call)
instance = described_class.new { rand }
expect(instance.call).to eq(instance.call)
end
it "doesn't conflate falsiness with unmemoizedness" do
count = 0
instance = described_class.new do
count += 1
nil
end
expect(instance.call).to eq nil
expect(instance.call).to eq nil
expect(count).to eq 1
end
end
end