Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

switch to jeweler

  • Loading branch information...
commit 8444769f741956cf59847962c4917051b630fc6a 1 parent 73cebb9
Justin Balthrop authored
6 History.txt
... ... @@ -1,6 +0,0 @@
1   -=== 1.0.0 / 2008-11-18
2   -
3   -* 1 major enhancement
4   -
5   - * Birthday!
6   -
18 License.txt → LICENSE
... ... @@ -1,10 +1,8 @@
1   -(The MIT License)
2   -
3   -Copyright (c) 2008 Justin Balthrop, Geni.com
  1 +Copyright (c) 2008 Justin Balthrop
4 2
5 3 Permission is hereby granted, free of charge, to any person obtaining
6 4 a copy of this software and associated documentation files (the
7   -'Software'), to deal in the Software without restriction, including
  5 +"Software"), to deal in the Software without restriction, including
8 6 without limitation the rights to use, copy, modify, merge, publish,
9 7 distribute, sublicense, and/or sell copies of the Software, and to
10 8 permit persons to whom the Software is furnished to do so, subject to
@@ -13,10 +11,10 @@ the following conditions:
13 11 The above copyright notice and this permission notice shall be
14 12 included in all copies or substantial portions of the Software.
15 13
16   -THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  14 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18   -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19   -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20   -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21   -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22   -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16 +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 Manifest.txt
... ... @@ -1,7 +0,0 @@
1   -History.txt
2   -Manifest.txt
3   -README.txt
4   -Rakefile
5   -bin/model_set
6   -lib/model_set.rb
7   -test/test_model_set.rb
18 README.txt → README.rdoc
Source Rendered
@@ -7,11 +7,7 @@ the Solr search engine through a subclass, but I plan to abstract this out into
7 7 engine" class that will support SQL, Solr, Sphinx, and eventually, other query methods
8 8 (possibly raw RecordCache hashes and other search engines).
9 9
10   -== INSTALL:
11   -
12   - sudo gem install model_set
13   -
14   -== USAGE:
  10 +== Usage:
15 11
16 12 class RobotSet < ModelSet
17 13 end
@@ -32,12 +28,12 @@ set3 << Robot.find(5)
32 28 set3.ids
33 29 # => [3,4,5]
34 30
35   -== REQUIREMENTS:
  31 +== Install:
36 32
37   - * deep_clonable
38   - * ordered_set
39   - * active_record
  33 + sudo gem install ninjudd-deep_clonable -s http://gems.github.com
  34 + sudo gem install ninjudd-ordered_set -s http://gems.github.com
  35 + sudo gem install ninjudd-model_set -s http://gems.github.com
40 36
41   -== LICENSE:
  37 +== License:
42 38
43   -Copyright (c) 2008 Justin Balthrop, Geni.com; Published under The MIT License, see License.txt
  39 +Copyright (c) 2009 Justin Balthrop, Geni.com; Published under The MIT License, see LICENSE
47 Rakefile
... ... @@ -1,12 +1,43 @@
1   -# -*- ruby -*-
  1 +require 'rake'
  2 +require 'rake/testtask'
  3 +require 'rake/rdoctask'
2 4
3   -require 'rubygems'
4   -require 'hoe'
5   -require './lib/model_set.rb'
  5 +begin
  6 + require 'jeweler'
  7 + Jeweler::Tasks.new do |s|
  8 + s.name = "model_set"
  9 + s.summary = %Q{Easy manipulation of sets of ActiveRecord models}
  10 + s.email = "code@justinbalthrop.com"
  11 + s.homepage = "http://github.com/ninjudd/model_set"
  12 + s.description = "Easy manipulation of sets of ActiveRecord models"
  13 + s.authors = ["Justin Balthrop"]
  14 + end
  15 +rescue LoadError
  16 + puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
  17 +end
  18 +
  19 +Rake::TestTask.new do |t|
  20 + t.libs << 'lib'
  21 + t.pattern = 'test/**/*_test.rb'
  22 + t.verbose = false
  23 +end
  24 +
  25 +Rake::RDocTask.new do |rdoc|
  26 + rdoc.rdoc_dir = 'rdoc'
  27 + rdoc.title = 'model_set'
  28 + rdoc.options << '--line-numbers' << '--inline-source'
  29 + rdoc.rdoc_files.include('README*')
  30 + rdoc.rdoc_files.include('lib/**/*.rb')
  31 +end
6 32
7   -Hoe.new('ModelSet', ModelSet::VERSION) do |p|
8   - # p.rubyforge_name = 'ModelSetx' # if different than lowercase project name
9   - p.developer('Justin Balthrop', 'code@justinbalthrop.com')
  33 +begin
  34 + require 'rcov/rcovtask'
  35 + Rcov::RcovTask.new do |t|
  36 + t.libs << 'test'
  37 + t.test_files = FileList['test/**/*_test.rb']
  38 + t.verbose = true
  39 + end
  40 +rescue LoadError
10 41 end
11 42
12   -# vim: syntax=Ruby
  43 +task :default => :test
0  bin/model_set
No changes.
355 test/model_set_test.rb
@@ -85,206 +85,199 @@ def add_birthday!
85 85 end
86 86 end
87 87
88   - def setup
89   - CreateTables.verbose = false
90   - CreateTables.up
91   - end
  88 + context 'with a db connection' do
  89 + setup do
  90 + CreateTables.verbose = false
  91 + CreateTables.up
  92 + end
92 93
93   - def teardown
94   - CreateTables.down
95   - end
  94 + teardown do
  95 + CreateTables.down
  96 + end
96 97
97   - test "constructor" do
98   - captain = Hero.create(:name => 'Captain America', :universe => 'Marvel')
99   - spidey = Hero.create(:name => 'Spider Man', :universe => 'Marvel')
100   - batman = Hero.create(:name => 'Batman', :universe => 'D.C.' )
101   - superman = Hero.create(:name => 'Superman', :universe => 'D.C.' )
102   - ironman = Hero.create(:name => 'Iron Man', :universe => 'Marvel')
103   -
104   - set = HeroSet.with_universe('Marvel')
105   - assert_equal [captain.id, spidey.id, ironman.id], set.ids
106   - end
  98 + should "construct a model set" do
  99 + captain = Hero.create(:name => 'Captain America', :universe => 'Marvel')
  100 + spidey = Hero.create(:name => 'Spider Man', :universe => 'Marvel')
  101 + batman = Hero.create(:name => 'Batman', :universe => 'D.C.' )
  102 + superman = Hero.create(:name => 'Superman', :universe => 'D.C.' )
  103 + ironman = Hero.create(:name => 'Iron Man', :universe => 'Marvel')
  104 +
  105 + set = HeroSet.with_universe('Marvel')
  106 + assert_equal [captain.id, spidey.id, ironman.id], set.ids
  107 + end
107 108
108   - test "missing_ids" do
109   - missing_id = 5555
110   - spidey = Hero.create(:name => 'Spider Man', :universe => 'Marvel')
111   - set = HeroSet.new([spidey.id, missing_id])
112   -
113   - # Iterate through the profiles so the missing ones will be detected.
114   - set.each {}
115   - assert_equal [missing_id], set.missing_ids
116   - end
  109 + should "have missing ids" do
  110 + missing_id = 5555
  111 + spidey = Hero.create(:name => 'Spider Man', :universe => 'Marvel')
  112 + set = HeroSet.new([spidey.id, missing_id])
  113 +
  114 + # Iterate through the profiles so the missing ones will be detected.
  115 + set.each {}
  116 + assert_equal [missing_id], set.missing_ids
  117 + end
117 118
118   - test "missing_ids with add_fields" do
119   - missing_id = 5555
120   - spidey = Hero.create(:name => 'Spider Man', :universe => 'Marvel')
121   - set = HeroSet.new([spidey.id, missing_id]).add_birthday
122   -
123   - # Iterate through the profiles so the missing ones will be detected.
124   - set.each {}
125   - assert_equal [missing_id], set.missing_ids
126   - end
  119 + should "have missing ids with add_fields" do
  120 + missing_id = 5555
  121 + spidey = Hero.create(:name => 'Spider Man', :universe => 'Marvel')
  122 + set = HeroSet.new([spidey.id, missing_id]).add_birthday
  123 +
  124 + # Iterate through the profiles so the missing ones will be detected.
  125 + set.each {}
  126 + assert_equal [missing_id], set.missing_ids
  127 + end
127 128
128   - test "has_set through" do
129   - hero = Hero.create(:name => 'Mr. Invisible')
130   - invisibility = Superpower.create(:name => 'Invisibility')
131   - flying = Superpower.create(:name => 'Flying')
132   - HeroSuperpower.create(:hero_id => hero.id, :power_id => invisibility.id)
133   - HeroSuperpower.create(:hero_id => hero.id, :power_id => flying.id)
  129 + should "support has_set" do
  130 + hero = Hero.create(:name => 'Mr. Invisible')
  131 + mighty_mouse = Superpet.create(:name => 'Mighty Mouse', :owner_id => hero.id)
  132 + underdog = Superpet.create(:name => 'Underdog', :owner_id => hero.id)
  133 +
  134 + set = hero.pets
  135 + assert_equal SuperpetSet, set.class
  136 + assert_equal [mighty_mouse.id, underdog.id], set.ids
  137 + end
134 138
135   - set = hero.superpowers
136   - assert_equal SuperpowerSet, set.class
137   - assert_equal [invisibility.id, flying.id], set.ids
138   - end
  139 + should "support has_set with through" do
  140 + hero = Hero.create(:name => 'Mr. Invisible')
  141 + invisibility = Superpower.create(:name => 'Invisibility')
  142 + flying = Superpower.create(:name => 'Flying')
  143 + HeroSuperpower.create(:hero_id => hero.id, :power_id => invisibility.id)
  144 + HeroSuperpower.create(:hero_id => hero.id, :power_id => flying.id)
  145 +
  146 + set = hero.superpowers
  147 + assert_equal SuperpowerSet, set.class
  148 + assert_equal [invisibility.id, flying.id], set.ids
  149 + end
139 150
140   - test "has_set" do
141   - hero = Hero.create(:name => 'Mr. Invisible')
142   - mighty_mouse = Superpet.create(:name => 'Mighty Mouse', :owner_id => hero.id)
143   - underdog = Superpet.create(:name => 'Underdog', :owner_id => hero.id)
144   -
145   - set = hero.pets
146   - assert_equal SuperpetSet, set.class
147   - assert_equal [mighty_mouse.id, underdog.id], set.ids
148   - end
149 151
150   - test "set extensions" do
151   - hero = Hero.create(:name => 'Mr. Invisible')
152   - mighty_mouse = Superpet.create(:name => 'Mighty Mouse', :owner_id => hero.id, :species => 'mouse')
153   - sammy = Superpet.create(:name => 'Sammy Davis Jr. Jr.', :owner_id => hero.id, :species => 'dog')
154   - underdog = Superpet.create(:name => 'Underdog', :owner_id => hero.id, :species => 'dog')
155   -
156   - set = hero.pets
157   - assert_equal ['mouse', 'dog', 'dog'], set.collect {|pet| pet.species}
158   -
159   - assert_equal [sammy.id, underdog.id], set.dogs!.ids
160   - end
161   -
162   - class Robot < ActiveRecord::Base
163   - end
164   -
165   - class RobotSet < ModelSet
166   - end
  152 + should "allow set extensions" do
  153 + hero = Hero.create(:name => 'Mr. Invisible')
  154 + mighty_mouse = Superpet.create(:name => 'Mighty Mouse', :owner_id => hero.id, :species => 'mouse')
  155 + sammy = Superpet.create(:name => 'Sammy Davis Jr. Jr.', :owner_id => hero.id, :species => 'dog')
  156 + underdog = Superpet.create(:name => 'Underdog', :owner_id => hero.id, :species => 'dog')
  157 +
  158 + set = hero.pets
  159 + assert_equal ['mouse', 'dog', 'dog'], set.collect {|pet| pet.species}
  160 +
  161 + assert_equal [sammy.id, underdog.id], set.dogs!.ids
  162 + end
167 163
168   - def setup_robots
169   - @bender = Robot.create(:name => 'Bender', :classification => :smart_ass )
170   - @r2d2 = Robot.create(:name => 'R2D2', :classification => :droid )
171   - @c3po = Robot.create(:name => 'C3PO', :classification => :droid )
172   - @rosie = Robot.create(:name => 'Rosie', :classification => :domestic )
173   - @small_wonder = Robot.create(:name => 'Vicki', :classification => :child )
174   - @t1000 = Robot.create(:name => 'Terminator', :classification => :assasin )
175   - @johnny5 = Robot.create(:name => 'Johnny 5', :classification => :miltary )
176   -
177   - @bot_set = RobotSet.new([@bender,@r2d2,@c3po,@rosie,@small_wonder,@t1000,@johnny5])
  164 + class Robot < ActiveRecord::Base
  165 + end
  166 +
  167 + class RobotSet < ModelSet
  168 + end
178 169
179   - @data = Robot.create(:name => 'Data', :classification => :positronic)
180   - @number8 = Robot.create(:name => 'Boomer', :classification => :cylon )
181   - end
  170 + setup do
  171 + @bender = Robot.create(:name => 'Bender', :classification => :smart_ass )
  172 + @r2d2 = Robot.create(:name => 'R2D2', :classification => :droid )
  173 + @c3po = Robot.create(:name => 'C3PO', :classification => :droid )
  174 + @rosie = Robot.create(:name => 'Rosie', :classification => :domestic )
  175 + @small_wonder = Robot.create(:name => 'Vicki', :classification => :child )
  176 + @t1000 = Robot.create(:name => 'Terminator', :classification => :assasin )
  177 + @johnny5 = Robot.create(:name => 'Johnny 5', :classification => :miltary )
  178 +
  179 + @bot_set = RobotSet.new([@bender,@r2d2,@c3po,@rosie,@small_wonder,@t1000,@johnny5])
  180 +
  181 + @data = Robot.create(:name => 'Data', :classification => :positronic)
  182 + @number8 = Robot.create(:name => 'Boomer', :classification => :cylon )
  183 + end
182 184
183   - test "empty" do
184   - setup_robots
185   -
186   - set = RobotSet.empty
187   - assert_equal 0, set.size
188   - assert set.empty?
189   -
190   - set = RobotSet.new(@bender)
191   - assert !set.empty?
192   - end
193   -
194   - test "set with single model" do
195   - setup_robots
196   - set = RobotSet.new(@bender)
197   - assert_equal [@bender.id], set.ids
198   - end
  185 + should "be empty" do
  186 + set = RobotSet.empty
  187 + assert_equal 0, set.size
  188 + assert set.empty?
  189 +
  190 + set = RobotSet.new(@bender)
  191 + assert !set.empty?
  192 + end
199 193
200   - test "include?" do
201   - setup_robots
202   - set = RobotSet.new([@bender, @r2d2.id, @c3po.id])
203   - assert set.include?(@bender)
204   - assert set.include?(@r2d2.id)
205   - end
  194 + should "create a set with single model" do
  195 + set = RobotSet.new(@bender)
  196 + assert_equal [@bender.id], set.ids
  197 + end
206 198
207   - test "delete" do
208   - setup_robots
209   - set = RobotSet.new([@rosie, @small_wonder, @c3po])
210   -
211   - set.delete(@c3po)
212   - assert_equal [@rosie.id, @small_wonder.id], set.ids
213   -
214   - set.delete(@rosie.id)
215   - assert_equal [@small_wonder.id], set.ids
  199 + should "include models" do
  200 + set = RobotSet.new([@bender, @r2d2.id, @c3po.id])
  201 + assert set.include?(@bender)
  202 + assert set.include?(@r2d2.id)
  203 + assert set.include?(@c3po)
  204 + end
216 205
217   - set.delete(@small_wonder)
218   - assert_equal [], set.ids
219   - assert set.empty?
220   - end
  206 + should "delete models from a set" do
  207 + set = RobotSet.new([@rosie, @small_wonder, @c3po])
  208 +
  209 + set.delete(@c3po)
  210 + assert_equal [@rosie.id, @small_wonder.id], set.ids
  211 +
  212 + set.delete(@rosie.id)
  213 + assert_equal [@small_wonder.id], set.ids
  214 +
  215 + set.delete(@small_wonder)
  216 + assert_equal [], set.ids
  217 + assert set.empty?
  218 + end
221 219
222   - test "select" do
223   - setup_robots
224   - assert_equal [@r2d2, @c3po], @bot_set.select {|bot| bot.classification == :droid}.to_a
225   - assert_equal 7, @bot_set.size
  220 + should "select models from a set" do
  221 + assert_equal [@r2d2, @c3po], @bot_set.select {|bot| bot.classification == :droid}.to_a
  222 + assert_equal 7, @bot_set.size
  223 +
  224 + @bot_set.select! {|bot| bot.classification == :miltary}
  225 + assert_equal [@johnny5], @bot_set.to_a
  226 + end
226 227
227   - @bot_set.select! {|bot| bot.classification == :miltary}
228   - assert_equal [@johnny5], @bot_set.to_a
229   - end
230   -
231   - test "sort" do
232   - setup_robots
233   - assert_equal [@bender,@c3po,@johnny5,@r2d2,@rosie,@t1000,@small_wonder], @bot_set.sort {|a,b| a.name <=> b.name}.to_a
234   - assert_equal @johnny5, @bot_set.last
235   -
236   - @bot_set.sort! {|a,b| b.name <=> a.name}
237   - assert_equal [@bender,@c3po,@johnny5,@r2d2,@rosie,@t1000,@small_wonder].reverse, @bot_set.to_a
238   - end
239   -
240   - test "sort_by" do
241   - setup_robots
242   - assert_equal [@bender,@c3po,@johnny5,@r2d2,@rosie,@t1000,@small_wonder], @bot_set.sort_by {|bot| bot.name}.to_a
243   - end
244   -
245   - test "reject" do
246   - setup_robots
247   - @bot_set.reject! {|bot| bot.classification == :domestic}
248   - assert !@bot_set.include?(@rosie)
249   - end
250   -
251   - test "set operators" do
252   - setup_robots
  228 + should "sort a set" do
  229 + assert_equal [@bender,@c3po,@johnny5,@r2d2,@rosie,@t1000,@small_wonder], @bot_set.sort {|a,b| a.name <=> b.name}.to_a
  230 + assert_equal @johnny5, @bot_set.last
  231 +
  232 + @bot_set.sort! {|a,b| b.name <=> a.name}
  233 + assert_equal [@bender,@c3po,@johnny5,@r2d2,@rosie,@t1000,@small_wonder].reverse, @bot_set.to_a
  234 + end
253 235
254   - droids = RobotSet.new([@c3po, @r2d2])
255   - womanoids = RobotSet.new([@rosie, @small_wonder, @number8])
256   - humanoids = RobotSet.new([@small_wonder, @t1000, @data, @number8])
257   - metalics = RobotSet.new([@r2d2, @c3po, @johnny5])
258   - cartoons = RobotSet.new([@bender, @rosie])
259   -
260   - assert_equal ['C3PO', 'R2D2', 'Johnny 5'], (droids + metalics).collect {|bot| bot.name}
261   - assert_equal ['Bender', 'Rosie', 'C3PO', 'R2D2', 'Johnny 5'], (cartoons + droids + metalics).collect {|bot| bot.name}
262   - assert_equal 5, (cartoons + droids + metalics).size
263   - assert_equal 5, (cartoons + droids + metalics).count
  236 + should "sort a set by name" do
  237 + assert_equal [@bender,@c3po,@johnny5,@r2d2,@rosie,@t1000,@small_wonder], @bot_set.sort_by {|bot| bot.name}.to_a
  238 + end
264 239
265   - assert_equal [], (droids - metalics).collect {|bot| bot.name}
266   - assert_equal ['Johnny 5'], (metalics - droids).collect {|bot| bot.name}
267   - assert_equal ['Terminator', 'Data'], (humanoids - womanoids).collect {|bot| bot.name}
268   - assert_equal ['Bender'], (cartoons - womanoids).collect {|bot| bot.name}
269   - assert_equal 2, (humanoids - womanoids).size
270   - assert_equal 2, (humanoids - womanoids).count
271   -
272   - assert_equal ['C3PO', 'R2D2'], (droids & metalics).collect {|bot| bot.name}
273   - assert_equal ['R2D2', 'C3PO'], (metalics & droids).collect {|bot| bot.name}
274   - assert_equal ['Vicki', 'Boomer'], (humanoids & womanoids).collect {|bot| bot.name}
275   - assert_equal ['Rosie'], (cartoons & womanoids).collect {|bot| bot.name}
276   - assert_equal 2, (humanoids & womanoids).size
277   - assert_equal 2, (humanoids & womanoids).count
  240 + should "reject models from a set" do
  241 + @bot_set.reject! {|bot| bot.classification == :domestic}
  242 + assert !@bot_set.include?(@rosie)
  243 + end
278 244
279   - set = (droids + @johnny5)
280   - assert_equal ['C3PO', 'R2D2', 'Johnny 5'], set.collect {|bot| bot.name}
281   - set -= @r2d2
282   - assert_equal ['C3PO', 'Johnny 5'], set.collect {|bot| bot.name}
283   - end
  245 + should "do set arithmetic" do
  246 + droids = RobotSet.new([@c3po, @r2d2])
  247 + womanoids = RobotSet.new([@rosie, @small_wonder, @number8])
  248 + humanoids = RobotSet.new([@small_wonder, @t1000, @data, @number8])
  249 + metalics = RobotSet.new([@r2d2, @c3po, @johnny5])
  250 + cartoons = RobotSet.new([@bender, @rosie])
  251 +
  252 + assert_equal ['C3PO', 'R2D2', 'Johnny 5'], (droids + metalics).collect {|bot| bot.name}
  253 + assert_equal ['Bender', 'Rosie', 'C3PO', 'R2D2', 'Johnny 5'], (cartoons + droids + metalics).collect {|bot| bot.name}
  254 + assert_equal 5, (cartoons + droids + metalics).size
  255 + assert_equal 5, (cartoons + droids + metalics).count
  256 +
  257 + assert_equal [], (droids - metalics).collect {|bot| bot.name}
  258 + assert_equal ['Johnny 5'], (metalics - droids).collect {|bot| bot.name}
  259 + assert_equal ['Terminator', 'Data'], (humanoids - womanoids).collect {|bot| bot.name}
  260 + assert_equal ['Bender'], (cartoons - womanoids).collect {|bot| bot.name}
  261 + assert_equal 2, (humanoids - womanoids).size
  262 + assert_equal 2, (humanoids - womanoids).count
  263 +
  264 + assert_equal ['C3PO', 'R2D2'], (droids & metalics).collect {|bot| bot.name}
  265 + assert_equal ['R2D2', 'C3PO'], (metalics & droids).collect {|bot| bot.name}
  266 + assert_equal ['Vicki', 'Boomer'], (humanoids & womanoids).collect {|bot| bot.name}
  267 + assert_equal ['Rosie'], (cartoons & womanoids).collect {|bot| bot.name}
  268 + assert_equal 2, (humanoids & womanoids).size
  269 + assert_equal 2, (humanoids & womanoids).count
  270 +
  271 + set = (droids + @johnny5)
  272 + assert_equal ['C3PO', 'R2D2', 'Johnny 5'], set.collect {|bot| bot.name}
  273 + set -= @r2d2
  274 + assert_equal ['C3PO', 'Johnny 5'], set.collect {|bot| bot.name}
  275 + end
284 276
285   - test "clone" do
286   - set = RobotSet.new([1])
287   - new_set = set.clone
288   - assert new_set.object_id != set.object_id
  277 + should "clone a set" do
  278 + set = RobotSet.new([1])
  279 + new_set = set.clone
  280 + assert new_set.object_id != set.object_id
  281 + end
289 282 end
290 283 end
81 test/multi_set_test.rb
@@ -14,53 +14,52 @@ def self.down
14 14 end
15 15 end
16 16
17   - def setup
18   - CreateTables.verbose = false
19   - CreateTables.up
20   - end
21   -
22   - def teardown
23   - CreateTables.down
24   - end
25   -
26 17 class Robot < ActiveRecord::Base
27 18 end
28 19
29 20 class RobotSet < ModelSet
30 21 end
31 22
32   - def setup_robots
33   - @bender = Robot.create(:name => 'Bender', :classification => :smart_ass )
34   - @r2d2 = Robot.create(:name => 'R2D2', :classification => :droid )
35   - @c3po = Robot.create(:name => 'C3PO', :classification => :droid )
36   - @rosie = Robot.create(:name => 'Rosie', :classification => :domestic )
37   - @small_wonder = Robot.create(:name => 'Vicki', :classification => :child )
38   - @t1000 = Robot.create(:name => 'Terminator', :classification => :assasin )
39   - @johnny5 = Robot.create(:name => 'Johnny 5', :classification => :miltary )
40   - @data = Robot.create(:name => 'Data', :classification => :positronic)
41   - @number8 = Robot.create(:name => 'Boomer', :classification => :cylon )
42   - end
  23 + context 'with a db connection' do
  24 + setup do
  25 + CreateTables.verbose = false
  26 + CreateTables.up
  27 +
  28 + @bender = Robot.create(:name => 'Bender', :classification => :smart_ass )
  29 + @r2d2 = Robot.create(:name => 'R2D2', :classification => :droid )
  30 + @c3po = Robot.create(:name => 'C3PO', :classification => :droid )
  31 + @rosie = Robot.create(:name => 'Rosie', :classification => :domestic )
  32 + @small_wonder = Robot.create(:name => 'Vicki', :classification => :child )
  33 + @t1000 = Robot.create(:name => 'Terminator', :classification => :assasin )
  34 + @johnny5 = Robot.create(:name => 'Johnny 5', :classification => :miltary )
  35 + @data = Robot.create(:name => 'Data', :classification => :positronic)
  36 + @number8 = Robot.create(:name => 'Boomer', :classification => :cylon )
  37 + end
43 38
44   - test "add, subtract, intersect" do
45   - setup_robots
46   - set = MultiSet.new
47   - set += RobotSet.new([@bender, @r2d2, @rosie])
48   - set += RobotSet.new([@c3po, @r2d2, @t1000])
49   - set += RobotSet.new([@data, @number8, @johnny5])
50   -
51   - assert_equal [3,3,3], set.size
52   - assert_equal [@bender,@r2d2,@rosie,@c3po,@t1000,@data,@number8,@johnny5].collect {|r| r.id}, set.ids
53   - assert_equal [@bender,@r2d2,@rosie,@c3po,@r2d2,@t1000,@data,@number8,@johnny5], set.to_a
54   -
55   - set -= RobotSet.new([@r2d2, @rosie, @t1000, @johnny5])
56   -
57   - assert_equal [1,1,2], set.size
58   - assert_equal [@bender,@c3po,@data,@number8].collect {|r| r.id}, set.ids
59   -
60   - other_set = MultiSet.new(RobotSet.new([@data]), RobotSet.new([@bender]))
61   -
62   - set &= other_set
63   - assert_equal [1,0,1], set.size
64   - assert_equal [@bender,@data].collect {|r| r.id}, set.ids
  39 + teardown do
  40 + CreateTables.down
  41 + end
  42 +
  43 + should "add, subtract, intersect" do
  44 + set = MultiSet.new
  45 + set += RobotSet.new([@bender, @r2d2, @rosie])
  46 + set += RobotSet.new([@c3po, @r2d2, @t1000])
  47 + set += RobotSet.new([@data, @number8, @johnny5])
  48 +
  49 + assert_equal [3,3,3], set.size
  50 + assert_equal [@bender,@r2d2,@rosie,@c3po,@t1000,@data,@number8,@johnny5].collect {|r| r.id}, set.ids
  51 + assert_equal [@bender,@r2d2,@rosie,@c3po,@r2d2,@t1000,@data,@number8,@johnny5], set.to_a
  52 +
  53 + set -= RobotSet.new([@r2d2, @rosie, @t1000, @johnny5])
  54 +
  55 + assert_equal [1,1,2], set.size
  56 + assert_equal [@bender,@c3po,@data,@number8].collect {|r| r.id}, set.ids
  57 +
  58 + other_set = MultiSet.new(RobotSet.new([@data]), RobotSet.new([@bender]))
  59 +
  60 + set &= other_set
  61 + assert_equal [1,0,1], set.size
  62 + assert_equal [@bender,@data].collect {|r| r.id}, set.ids
  63 + end
65 64 end
66 65 end
24 test/test_helper.rb
... ... @@ -1,10 +1,16 @@
  1 +require 'rubygems'
1 2 require 'test/unit'
  3 +require 'shoulda'
  4 +require 'mocha'
2 5
3   -$:.unshift(File.dirname(__FILE__) + '/../../deep_clonable/lib')
4   -
5   -require File.dirname(__FILE__) + '/../lib/model_set'
  6 +$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
  7 +['deep_clonable', 'ordered_set'].each do |dir|
  8 + $LOAD_PATH.unshift File.dirname(__FILE__) + "/../../#{dir}/lib"
  9 +end
  10 +require 'model_set'
6 11
7   -require 'pp'
  12 +class Test::Unit::TestCase
  13 +end
8 14
9 15 ActiveRecord::Base.establish_connection(
10 16 :adapter => "postgresql",
@@ -13,11 +19,5 @@
13 19 :password => "",
14 20 :database => "model_set_test"
15 21 )
16   -
17   -class << Test::Unit::TestCase
18   - def test(name, &block)
19   - test_name = "test_#{name.gsub(/[\s\W]/,'_')}"
20   - raise ArgumentError, "#{test_name} is already defined" if self.instance_methods.include? test_name
21   - define_method test_name, &block
22   - end
23   -end
  22 +ActiveRecord::Migration.verbose = false
  23 +ActiveRecord::Base.connection.client_min_messages = 'panic'

0 comments on commit 8444769

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