Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion data/adjs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[
null,
"a",
"a_cappella",
"a_couple_of",
Expand Down
7 changes: 6 additions & 1 deletion lib/random_word.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ def next_unused_idx(used)

def excluded?(word)
exclude_list = Array(@random_word_exclude_list)
exclude_list.any? {|r| r === word} || word.length < min_length || (!(max_length.nil?) && word.length > max_length)
(
word.nil? ||
exclude_list.any? {|r| r === word} ||
word.length < min_length ||
(!(max_length.nil?) && word.length > max_length)
)
end

class OutOfWords < Exception; end
Expand Down
19 changes: 10 additions & 9 deletions spec/random_word_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require File.expand_path("spec_helper", File.dirname(__FILE__))

describe RandomWord, "enumerator" do
describe RandomWord, "enumerator" do
subject {RandomWord.enumerator(["aaa", "bbb", "ccc"])}

it "can get you the next word in its list" do
expect(subject.next).to be_one_of(["aaa", "bbb", "ccc"])
end

it "raises error when it runs out of words" do
it "raises error when it runs out of words" do
3.times{subject.next}

expect{subject.next}.to raise_error(StopIteration)
Expand All @@ -20,28 +20,29 @@
already_received << new_word
end
end

end

describe RandomWord do
describe RandomWord do
after(:all) do
RandomWord.instance_eval{ @nouns, @adjs = nil, nil } # reset rspec effects
end

it "can return a random noun enumerator" do
it "can return a random noun enumerator" do
expect(RandomWord.nouns).to respond_to(:next)
end

it "can return a random adj enumerator" do
it "can return a random adj enumerator" do
expect(RandomWord.adjs).to respond_to(:next)
end

it "can return a random phrase enumerator" do
it "can return a random phrase enumerator" do
expect(RandomWord.phrases.next).to be_a(String)
end
end

describe RandomWord, "#exclude" do
let(:word_list) { ["aaa","ccc","c", "cab", "abc", "ace", "dad"] }
let(:word_list) { [nil, "aaa","ccc","c", "cab", "abc", "ace", "dad"] }

[
{:name => "normal words", :exclude => "ccc", :expected => Set.new(["aaa","c", "cab", "abc", "ace", "dad"])},
Expand All @@ -54,7 +55,7 @@
received_words = []
loop do
received_words << subject.next
end rescue StopIteration
end

expect(Set.new(received_words)).to eq(rec[:expected])
end
Expand Down Expand Up @@ -198,4 +199,4 @@
include_examples 'allows constraints on word length', :adjs
include_examples 'changing constraints in subsequent calls', :adjs
end
end
end