Skip to content

Instantly share code, notes, and snippets.

@alberto
Forked from calavera/gist:790579
Created January 23, 2011 10:40
Show Gist options
  • Save alberto/791977 to your computer and use it in GitHub Desktop.
Save alberto/791977 to your computer and use it in GitHub Desktop.
require "test_notifier/runner/rspec"
TestNotifier.default_notifier = :notify_send
class User
attr_reader :name, :age
def initialize(name, age)
@name, @age = name, age
end
end
class From
def self.[] items
From.new(items)
end
def initialize(items)
@items = items
end
def where
@items = @items.select { |item| yield item}
self
end
def select
@items.map { |item| yield item }
end
end
describe From do
it "return empty array when the list is empty" do
users = []
From[users].select{ |u| u.name }.should == []
end
context 'querying one user' do
let(:users) {[User.new('foo', 23)]}
it "return the name from an element from the list" do
From[users].select{ |u| u.name }.should == ["foo"]
end
it "return the age from an element from the list" do
From[users].select{ |u| u.age }.should == [23]
end
end
context 'querying many users' do
let(:users) {[User.new('foo', 23), User.new('bar', 34)]}
it "returns the different names for all the elements in the list" do
From[users].select{ |u| u.name }.should == ['foo', 'bar']
end
it "filters the results by the where expression" do
From[users].where{ |u| u.age == 23 }.select{ |u| u.name }.should == ['foo']
end
it "filters the results greater than an age" do
From[users].where{ |u| u.age > 23 }.select{ |u| u.name }.should == ['bar']
end
it "filters the results lower than an age" do
From[users].where{ |u| u.age < 24 }.select{ |u| u.name }.should == ['foo']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment