summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Cheek <josh.cheek@gmail.com>2019-06-03 03:09:31 -0500
committerKyrylo Silin <silin@kyrylo.org>2019-06-03 11:09:31 +0300
commit6cc6e55f205c421ca6ca010a8afcafc3ed7dc299 (patch)
treedb2cff1e834bd1624b30d46858c27db67c2475ac
parent29bdd2e403c3d7e30435a6acc1f66689dd32f4a8 (diff)
downloadpry-6cc6e55f205c421ca6ca010a8afcafc3ed7dc299.tar.gz
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.
-rw-r--r--lib/pry/config/memoized_value.rb6
-rw-r--r--spec/config/memoized_value_spec.rb16
2 files changed, 18 insertions, 4 deletions
diff --git a/lib/pry/config/memoized_value.rb b/lib/pry/config/memoized_value.rb
index 670f45b9..339f6e2b 100644
--- a/lib/pry/config/memoized_value.rb
+++ b/lib/pry/config/memoized_value.rb
@@ -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
diff --git a/spec/config/memoized_value_spec.rb b/spec/config/memoized_value_spec.rb
index 22e278dc..9b8ab864 100644
--- a/spec/config/memoized_value_spec.rb
+++ b/spec/config/memoized_value_spec.rb
@@ -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