Skip to content

Commit

Permalink
add tests for memoizing kwargs and blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Aug 17, 2023
1 parent 3ca58de commit 37ac529
Showing 1 changed file with 119 additions and 53 deletions.
172 changes: 119 additions & 53 deletions spec/typed_params/memoize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,118 @@

RSpec.describe TypedParams::Memoize do
describe '.memoize' do
it 'memoizes' do
a = Class.new {
include TypedParams::Memoize

memoize
def without_args = SecureRandom.hex
}.new

expect(a).to receive(:without_args).twice.and_call_original
expect(a).to receive(:unmemoized_without_args).once

a.without_args
a.without_args
end

it 'memoizes args' do
a = Class.new {
include TypedParams::Memoize

memoize
def with_args(n) = SecureRandom.hex(n)
}.new

expect(a).to receive(:with_args).twice.and_call_original
expect(a).to receive(:unmemoized_with_args).once

a.with_args(64)
a.with_args(64)
end

it 'memoizes kwargs' do
a = Class.new {
include TypedParams::Memoize

memoize
def with_kwargs(n:) = SecureRandom.hex(n)
}.new

expect(a).to receive(:with_kwargs).twice.and_call_original
expect(a).to receive(:unmemoized_with_kwargs).once

a.with_kwargs(n: 64)
a.with_kwargs(n: 64)
end

it 'memoizes block' do
a = Class.new {
include TypedParams::Memoize

memoize
def with_block(&b) = SecureRandom.hex(b.call)
}.new

expect(a).to receive(:with_block).twice.and_call_original
expect(a).to receive(:unmemoized_with_block).once

a.with_block &b = -> { 64 }
a.with_block &b
end

it 'should raise on repeat instance method memoize' do
expect {
Class.new {
include TypedParams::Memoize

memoize
memoize
def call = nil
}
}.to raise_error RuntimeError
end

it 'should raise on repeat instance method def' do
expect {
Class.new {
include TypedParams::Memoize

memoize
def call = nil

memoize
def call = nil
}
}.to raise_error RuntimeError
end

it 'should raise on repeat class method memoize' do
expect {
Class.new {
include TypedParams::Memoize

memoize
memoize
def self.call = nil
}
}.to raise_error RuntimeError
end

it 'should raise on repeat class method def' do
expect {
Class.new {
include TypedParams::Memoize

memoize
def self.call = nil

memoize
def self.call = nil
}
}.to raise_error RuntimeError
end

context 'with public instance method' do
subject {
Class.new {
Expand All @@ -13,7 +125,13 @@
def without_args = SecureRandom.hex

memoize
def with_args(i) = SecureRandom.hex(i)
def with_args(n) = SecureRandom.hex(n)

memoize
def with_kwargs(n:) = SecureRandom.hex(n)

memoize
def with_block(&b) = SecureRandom.hex(b.call)
}
}

Expand Down Expand Up @@ -341,57 +459,5 @@ def self.with_args(...) = priv_with_args(...)
expect(x).to_not eq y
end
end

it 'should raise on repeat instance method memoize' do
expect {
Class.new {
include TypedParams::Memoize

memoize
memoize
def call = nil
}
}.to raise_error RuntimeError
end

it 'should raise on repeat instance method def' do
expect {
Class.new {
include TypedParams::Memoize

memoize
def call = nil

memoize
def call = nil
}
}.to raise_error RuntimeError
end

it 'should raise on repeat class method memoize' do
expect {
Class.new {
include TypedParams::Memoize

memoize
memoize
def self.call = nil
}
}.to raise_error RuntimeError
end

it 'should raise on repeat class method def' do
expect {
Class.new {
include TypedParams::Memoize

memoize
def self.call = nil

memoize
def self.call = nil
}
}.to raise_error RuntimeError
end
end
end

0 comments on commit 37ac529

Please sign in to comment.