Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

100% code coverage #62

Merged
merged 2 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 85 additions & 12 deletions spec/memo_wise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ def public_memowise_method
memo_wise :public_memowise_method
public :public_memowise_method

def unmemoized_method
"unmemoized"
end
def unmemoized_method; end
end
end

Expand Down Expand Up @@ -276,19 +274,45 @@ def unmemoized_method
expect(instance2.no_args_counter).to eq(0)
end

it "keeps private methods private" do
expect(instance.private_methods.include?(:private_memowise_method)).
to eq(true)
context "with private methods" do
it "keeps private methods private" do
expect(instance.private_methods.include?(:private_memowise_method)).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be clearer as

expect(instance.private_methods).to include(: private_memowise_method)

(And same comment below.) Thoughts?

to eq(true)
end

it "memoizes private methods" do
expect(Array.new(4) do
instance.send(:private_memowise_method)
end).to all eq("private_memowise_method")
expect(instance.private_memowise_method_counter).to eq(1)
end
end

it "keeps public methods public" do
expect(instance.public_methods.include?(:public_memowise_method)).
to eq(true)
context "with public methods" do
it "keeps public methods public" do
expect(instance.public_methods.include?(:public_memowise_method)).
to eq(true)
end

it "memoizes public methods" do
expect(Array.new(4) { instance.public_memowise_method }).
to all eq("public_memowise_method")
expect(instance.public_memowise_method_counter).to eq(1)
end
end

it "keeps protected methods protected" do
expect(instance.protected_methods.include?(:protected_memowise_method)).
to eq(true)
context "with protected methods" do
it "keeps protected methods protected" do
expect(instance.protected_methods.include?(:protected_memowise_method)).
to eq(true)
end

it "memoizes protected methods" do
expect(Array.new(4) do
instance.send(:protected_memowise_method)
end).to all eq("protected_memowise_method")
expect(instance.protected_memowise_method_counter).to eq(1)
end
end

context "when the name of the method to memoize is not a symbol" do
Expand Down Expand Up @@ -396,6 +420,24 @@ def no_args
expect(instance.with_keyword_args_counter).to eq(3)
end

it "resets memoization for methods with positional and keyword args" do
instance.with_positional_and_keyword_args(1, b: 2)
instance.with_positional_and_keyword_args(2, b: 3)
instance.reset_memo_wise(:with_positional_and_keyword_args, 1, b: 2)

expect(Array.new(4) do
instance.with_positional_and_keyword_args(1, b: 2)
end).to all eq("with_positional_and_keyword_args: a=1, b=2")

expect(Array.new(4) do
instance.with_positional_and_keyword_args(2, b: 3)
end).to all eq("with_positional_and_keyword_args: a=2, b=3")

# This should be executed twice for each set of arguments passed,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a little confusing—it's really once for each set of arguments passed, which is twice total. Could you rephrase this?

# and a third time for the set of arguments that was reset.
expect(instance.with_positional_and_keyword_args_counter).to eq(3)
end

it "resets memoization for methods with special characters in the name" do
instance.special_chars?
instance.reset_memo_wise(:special_chars?)
Expand Down Expand Up @@ -608,6 +650,37 @@ def no_args
end
end

context "with positional and keyword args" do
let(:expected_counter) { overriding ? 2 : 0 }

before(:each) do
if overriding
instance.with_positional_and_keyword_args(1, b: 2)
instance.with_positional_and_keyword_args(2, b: 3)
end
end

it "presets memoization" do
instance.preset_memo_wise(
:with_positional_and_keyword_args, 1, b: 2
) { "first" }
instance.preset_memo_wise(
:with_positional_and_keyword_args, 2, b: 3
) { "second" }

expect(Array.new(4) do
instance.with_positional_and_keyword_args(1, b: 2)
end).to all eq("first")

expect(Array.new(4) do
instance.with_positional_and_keyword_args(2, b: 3)
end).to all eq("second")

expect(instance.with_positional_and_keyword_args_counter).
to eq(expected_counter)
end
end

context "with special chars" do
before(:each) { instance.special_chars? if overriding }

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# SimpleCov.refuse_coverage_drop is only implemented for line coverage, so for
# branch coverage we must use `minimum_coverage`
SimpleCov.minimum_coverage branch: 90
SimpleCov.minimum_coverage branch: 100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


SimpleCov.refuse_coverage_drop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have strong feelings, but it might be more explicit if we changed this to also be 100. Happy either way though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to leave as is for now!

I have a PR up on SimpleCov to hopefully get branch coverage working for :refuse_coverage_drop, so hoping to just use refuse_coverage_drop for both eventually.

end
Expand Down