Skip to content

Commit

Permalink
Fix missing params for enumerator for Readers::PlainText (#59)
Browse files Browse the repository at this point in the history
Also, ensure we have tests for both yield and enumerator paths where appropriate: `Container#each`, `Enumerable#each`.

Gotta thank the rubocop `Lint/ToEnumArguments` rule for this one!
  • Loading branch information
gonzedge committed Feb 20, 2024
1 parent 5563185 commit b1b9278
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/rambling/trie/readers/plain_text.rb
Expand Up @@ -10,7 +10,7 @@ class PlainText < Reader
# @yield [String] Each line read from the file.
# @return [self]
def each_word filepath
return enum_for :each_word unless block_given?
return enum_for :each_word, filepath unless block_given?

::File.foreach(filepath) { |line| yield line.chomp! }

Expand Down
6 changes: 6 additions & 0 deletions spec/lib/rambling/trie/container_spec.rb
Expand Up @@ -355,6 +355,12 @@
describe '#each' do
before { add_words container, %w(yes no why) }

it 'yields every word previously added' do
yielded = []
container.each { |word| yielded << word }
expect(yielded).to eq %w(yes no why)
end

it 'returns an enumerator when no block is given' do
expect(container.each).to be_instance_of Enumerator
end
Expand Down
8 changes: 6 additions & 2 deletions spec/lib/rambling/trie/enumerable_spec.rb
Expand Up @@ -15,16 +15,20 @@ module Trie
expect(node.each).to be_an Enumerator
end

it 'iterates through all words contained in the trie' do
expect(node.each.to_a).to match_array words
end

it 'has the same word count as the trie' do
expect(node.count).to eq words.count
end

it 'includes every word contained in the trie' do
it 'yields every word contained in the trie' do
node.each { |word| expect(words).to include word }
end

it 'returns the enumerable when a block is given' do
expect(node.each { |_| }).to eq node
expect(node.each { |word| print word }).to eq node
end
end

Expand Down
11 changes: 8 additions & 3 deletions spec/lib/rambling/trie/readers/plain_text_spec.rb
Expand Up @@ -15,12 +15,17 @@
expect(yielded).to eq words
end

it 'returns the enumerable when a block is given' do
yielded = []
expect(reader.each_word(filepath) { |word| yielded << word }).to eq reader
end

it 'returns an enumerator when no block is given' do
expect(reader.each_word filepath).to be_an Enumerator
expect(reader.each_word(filepath)).to be_an Enumerator
end

it 'returns the enumerable when a block is given' do
expect(reader.each_word(filepath) { |_| }).to eq reader
it 'iterates through all words in the given file' do
expect(reader.each_word(filepath).to_a).to eq words
end
end
end

0 comments on commit b1b9278

Please sign in to comment.