Skip to content
Browse files

Organize the matchers a bit more

  • Loading branch information...
1 parent 0e68882 commit 3ab6ae0c601d1b4459efd8bb39650fee370aa5b8 Carl Lerche committed Mar 12, 2010
View
87 spec/algebra/integration/basic_spec.rb
@@ -1,93 +1,7 @@
require 'spec_helper'
-def have_rows(expected)
- simple_matcher "have rows" do |given, matcher|
- found, got, expected = [], [], expected.map { |r| r.tuple }
- given.each do |row|
- got << row.tuple
- found << expected.find { |r| row.tuple == r }
- end
-
- matcher.failure_message = "Expected to get:\n" \
- "#{expected.map {|r| " #{r.inspect}" }.join("\n")}\n" \
- "instead, got:\n" \
- "#{got.map {|r| " #{r.inspect}" }.join("\n")}"
-
- found.compact.length == expected.length && got.compact.length == expected.length
- end
-end
-
-share_examples_for 'A Relation' do
-
- before :all do
- # The two needed instance variables need to be set in a
- # before :all callback.
- # @relation is the relation being tested here.
- # @expected is an array of the elements that are expected to be in
- # the relation.
- %w[ @relation @expected ].each do |ivar|
- raise "#{ivar} needs to be defined" unless instance_variable_get(ivar)
- end
-
- # There needs to be enough items to be able to run all the tests
- raise "@expected needs to have at least 6 items" unless @expected.length >= 6
- end
-
- describe "#each" do
- it "iterates over the rows in any order" do
- @relation.should have_rows(@expected)
- end
- end
-
- describe "#where" do
- before :all do
- @expected = @expected.sort_by { |r| r[@relation[:age]] }
- @pivot = @expected[@expected.length / 2]
- end
-
- it "finds rows with an equal to predicate" do
- expected = @expected.select { |r| r[@relation[:age]] == @pivot[@relation[:age]] }
- @relation.where(@relation[:age].eq(@pivot[@relation[:age]])).should have_rows(expected)
- end
-
- it "finds rows with a not predicate" do
- expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] }
- @relation.where(@relation[:age].not(@pivot[@relation[:age]])).should have_rows(expected)
- end
-
- it "finds rows with a less than predicate" do
- expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] }
- @relation.where(@relation[:age].lt(@pivot[@relation[:age]])).should have_rows(expected)
- end
-
- it "finds rows with a less than or equal to predicate" do
- expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] }
- @relation.where(@relation[:age].lteq(@pivot[@relation[:age]])).should have_rows(expected)
- end
-
- it "finds rows with a greater than predicate" do
- expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] }
- @relation.where(@relation[:age].gt(@pivot[@relation[:age]])).should have_rows(expected)
- end
-
- it "finds rows with a greater than or equal to predicate" do
- expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] }
- @relation.where(@relation[:age].gteq(@pivot[@relation[:age]])).should have_rows(expected)
- end
-
- it "finds rows with a matches predicate"
-
- it "finds rows with an in predicate" do
- pending
- set = @expected[1..(@expected.length/2+1)]
- @relation.all(:id.in => set.map { |r| r.id }).should have_resources(set)
- end
- end
-end
-
module Arel
describe "Relation" do
-
before :all do
@engine = Testing::Engine.new
@relation = Model.build do |r|
@@ -113,6 +27,5 @@ module Arel
@engine.rows.should == [[1, 'Foo', 10]]
end
end
-
end
end
View
92 spec/shared/relation_spec.rb
@@ -0,0 +1,92 @@
+share_examples_for 'A Relation' do
+
+ before :all do
+ # The two needed instance variables need to be set in a
+ # before :all callback.
+ # @relation is the relation being tested here.
+ # @expected is an array of the elements that are expected to be in
+ # the relation.
+ %w[ @relation @expected ].each do |ivar|
+ raise "#{ivar} needs to be defined" unless instance_variable_get(ivar)
+ end
+
+ # There needs to be enough items to be able to run all the tests
+ raise "@expected needs to have at least 6 items" unless @expected.length >= 6
+ end
+
+ describe "#each" do
+ it "iterates over the rows in any order" do
+ @relation.should have_rows(@expected)
+ end
+ end
+
+ describe "#where" do
+ before :all do
+ @expected = @expected.sort_by { |r| r[@relation[:age]] }
+ @pivot = @expected[@expected.length / 2]
+ end
+
+ it "finds rows with an equal to predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] == @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].eq(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a not predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] != @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].not(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a less than predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] < @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].lt(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a less than or equal to predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] <= @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].lteq(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a greater than predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] > @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].gt(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a greater than or equal to predicate" do
+ expected = @expected.select { |r| r[@relation[:age]] >= @pivot[@relation[:age]] }
+ @relation.where(@relation[:age].gteq(@pivot[@relation[:age]])).should have_rows(expected)
+ end
+
+ it "finds rows with a matches predicate"
+
+ it "finds rows with an in predicate" do
+ pending
+ set = @expected[1..(@expected.length/2+1)]
+ @relation.all(:id.in => set.map { |r| r.id }).should have_resources(set)
+ end
+ end
+
+ describe "#order" do
+ describe "by one attribute" do
+ before :all do
+ @expected.map! { |r| r[@relation[:age]] }
+ @expected.sort!
+ end
+
+ it "can be specified as ascending order" do
+ actual = []
+ @relation.order(@relation[:age].asc).each { |r| actual << r[@relation[:age]] }
+ actual.should == @expected
+ end
+
+ it "can be specified as descending order" do
+ actual = []
+ @relation.order(@relation[:age].desc).each { |r| actual << r[@relation[:age]] }
+ actual.should == @expected.reverse
+ end
+ end
+
+ describe "by two attributes" do
+ it "works"
+ end
+ end
+end
View
4 spec/spec_helper.rb
@@ -7,12 +7,12 @@
require 'fileutils'
require 'arel'
-Dir["#{dir}/support/*.rb"].each do |file|
+Dir["#{dir}/{support,shared}/*.rb"].each do |file|
require file
end
Spec::Runner.configure do |config|
- config.include BeLikeMatcher, HashTheSameAsMatcher, DisambiguateAttributesMatcher
+ config.include Matchers
config.include AdapterGuards
config.include Check
View
3 spec/support/matchers.rb
@@ -1,3 +1,4 @@
require "support/matchers/be_like"
require "support/matchers/disambiguate_attributes"
-require "support/matchers/hash_the_same_as"
+require "support/matchers/hash_the_same_as"
+require "support/matchers/have_rows"
View
2 spec/support/matchers/be_like.rb
@@ -1,4 +1,4 @@
-module BeLikeMatcher
+module Matchers
class BeLike
def initialize(expected)
@expected = expected
View
2 spec/support/matchers/disambiguate_attributes.rb
@@ -1,4 +1,4 @@
-module DisambiguateAttributesMatcher
+module Matchers
class DisambiguateAttributes
def initialize(attributes)
@attributes = attributes
View
2 spec/support/matchers/hash_the_same_as.rb
@@ -1,4 +1,4 @@
-module HashTheSameAsMatcher
+module Matchers
class HashTheSameAs
def initialize(expected)
@expected = expected
View
18 spec/support/matchers/have_rows.rb
@@ -0,0 +1,18 @@
+module Matchers
+ def have_rows(expected)
+ simple_matcher "have rows" do |given, matcher|
+ found, got, expected = [], [], expected.map { |r| r.tuple }
+ given.each do |row|
+ got << row.tuple
+ found << expected.find { |r| row.tuple == r }
+ end
+
+ matcher.failure_message = "Expected to get:\n" \
+ "#{expected.map {|r| " #{r.inspect}" }.join("\n")}\n" \
+ "instead, got:\n" \
+ "#{got.map {|r| " #{r.inspect}" }.join("\n")}"
+
+ found.compact.length == expected.length && got.compact.length == expected.length
+ end
+ end
+end

0 comments on commit 3ab6ae0

Please sign in to comment.
Something went wrong with that request. Please try again.