forked from hamstergem/hamster
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement List#group_by.
- Loading branch information
Simon Harris
committed
Jan 17, 2010
1 parent
f7353d0
commit f84db96
Showing
5 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') | ||
|
||
require 'hamster/list' | ||
|
||
describe Hamster::List do | ||
|
||
describe "#group_by" do | ||
|
||
describe "on a really big list" do | ||
|
||
before do | ||
@list = Hamster.interval(0, STACK_OVERFLOW_DEPTH) | ||
end | ||
|
||
it "doesn't run out of stack" do | ||
lambda { @list.group_by }.should_not raise_error | ||
end | ||
|
||
end | ||
|
||
describe "with a block" do | ||
|
||
[ | ||
[[], []], | ||
[[1], [true => Hamster.list(1)]], | ||
[[1, 2, 3, 4], [true => Hamster.list(3, 1), false => Hamster.list(4, 2)]], | ||
].each do |values, expected| | ||
|
||
describe "on #{values.inspect}" do | ||
|
||
before do | ||
original = Hamster.list(*values) | ||
@result = original.group_by(&:odd?) | ||
end | ||
|
||
it "returns #{expected.inspect}" do | ||
@result.should == Hamster.hash(*expected) | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
|
||
describe "without a block" do | ||
|
||
[ | ||
[[], []], | ||
[[1], [1 => Hamster.list(1)]], | ||
[[1, 2, 3, 4], [1 => Hamster.list(1), 2 => Hamster.list(2), 3 => Hamster.list(3), 4 => Hamster.list(4)]], | ||
].each do |values, expected| | ||
|
||
describe "on #{values.inspect}" do | ||
|
||
before do | ||
original = Hamster.list(*values) | ||
@result = original.group_by | ||
end | ||
|
||
it "returns #{expected.inspect}" do | ||
@result.should == Hamster.hash(*expected) | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') | ||
|
||
require 'hamster/set' | ||
|
||
describe Hamster::Set do | ||
|
||
describe "#one?" do | ||
|
||
describe "when empty" do | ||
|
||
before do | ||
@set = Hamster.set | ||
end | ||
|
||
it "with a block returns false" do | ||
pending do | ||
@set.one? {}.should == false | ||
end | ||
end | ||
|
||
it "with no block returns false" do | ||
pending do | ||
@set.one?.should == false | ||
end | ||
end | ||
|
||
end | ||
|
||
describe "when not empty" do | ||
|
||
describe "with a block" do | ||
|
||
before do | ||
@set = Hamster.set("A", "B", "C") | ||
end | ||
|
||
it "returns false if the block returns true more than once" do | ||
pending do | ||
@set.one? { |item| true }.should == false | ||
end | ||
end | ||
|
||
it "returns fale if the block never returns true" do | ||
pending do | ||
@set.one? { |item| false }.should == false | ||
end | ||
end | ||
|
||
it "returns true if the block only returns true once" do | ||
pending do | ||
@set.one? { |item| item == "A" }.should == true | ||
end | ||
end | ||
|
||
end | ||
|
||
describe "with no block" do | ||
|
||
it "returns false if more than one value is truthy" do | ||
pending do | ||
Hamster.set(nil, true, "A").one?.should == false | ||
end | ||
end | ||
|
||
it "returns true if only one value is truthy" do | ||
pending do | ||
Hamster.set(nil, true, false).one?.should == true | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |