fail-fast 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ == 1.1.0 2009-07-19
2
+
3
+ * 1 major enhancement:
4
+ * Added assert_only_keys
5
+
1
6
  == 1.0.0 2008-11-29
2
7
 
3
8
  * 1 major enhancement:
@@ -51,7 +51,11 @@ FailFast is a collection of assertion methods intended for lightweight contract
51
51
  try { assert_keys(h, :foo, :bar) } # => {:foo=>1, :bar=>2, :baz=>nil}
52
52
  try { assert_keys(h, :baz) } # => "<Assertion Failure>"
53
53
  try { assert_keys(h, :buz) } # => "<Assertion Failure>"
54
-
54
+
55
+ # Assert that an object is hash-like and has only the given keys
56
+ h = {:foo => 1, :bar => nil}
57
+ try { assert_keys(h, :foo, :bar) } # => {:foo => 1, :bar => nil}
58
+ try { assert_keys(h, :foo) } # => "<Assertion Failure>"
55
59
 
56
60
  == REQUIREMENTS:
57
61
 
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module FailFast
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.0'
6
6
 
7
7
  class AssertionFailureError < Exception
8
8
  end
@@ -48,6 +48,18 @@ module FailFast
48
48
  hash
49
49
  end
50
50
 
51
+ # Assert that +hash+ exists, responds to #[], and has no keys other than
52
+ # +keys+. Returns +hash+.
53
+ def assert_only_keys(hash, *keys)
54
+ assert_exists(hash)
55
+ assert(hash.respond_to?(:[]))
56
+ values = hash.inject([]) { |vals, (k, v)|
57
+ assert(keys.include?(k)); vals << hash[k]
58
+ }
59
+ assert(yield(*values)) if block_given?
60
+ hash
61
+ end
62
+
51
63
  # Assert that +object+ responds to +messages+. Returns +object+.
52
64
  def assert_respond_to(object, *messages)
53
65
  messages.each do |message|
@@ -277,6 +277,77 @@ describe FailFast::Assertions, "#assert_keys" do
277
277
  end
278
278
  end
279
279
 
280
+ describe FailFast::Assertions, "#assert_only_keys" do
281
+
282
+ include FailFast::Assertions
283
+
284
+ def do_assertion(*args, &block)
285
+ assert_only_keys(*args, &block)
286
+ end
287
+
288
+ def do_success(&block)
289
+ assert_only_keys({:foo => true}, :foo, &block)
290
+ end
291
+
292
+ def do_failure(&block)
293
+ assert_only_keys({:foo, :bar}, :foo, &block)
294
+ end
295
+
296
+ it_should_behave_like "any assertion"
297
+ it_should_behave_like "an assertion taking a block"
298
+
299
+ it "should fail if an unspecified key is present" do
300
+ lambda { assert_only_keys({:bar => true}, :foo) }.
301
+ should raise_error(FailFast::AssertionFailureError)
302
+ end
303
+
304
+ it "should fail if the given hash is nil" do
305
+ lambda do
306
+ assert_only_keys(nil)
307
+ end.should raise_error(FailFast::AssertionFailureError)
308
+ end
309
+
310
+ it "should fail if given something unlike a hash" do
311
+ lambda do
312
+ assert_only_keys(true)
313
+ end.should raise_error(FailFast::AssertionFailureError)
314
+ end
315
+
316
+ it "should always fail if no keys are given" do
317
+ lambda do
318
+ assert_only_keys({:foo => true, :bar => nil})
319
+ end.should raise_error(FailFast::AssertionFailureError)
320
+ end
321
+
322
+ it "should yield values of keys which are present" do
323
+ did_yield = false
324
+ assert_only_keys({:foo => 23}, :foo, :bar) do |x, y|
325
+ did_yield = true
326
+ x.should == 23
327
+ y.should be_nil
328
+ true
329
+ end
330
+ did_yield.should be_true
331
+ end
332
+
333
+ it "should yield nothing if a key is wrong" do
334
+ begin
335
+ did_yield = false
336
+ assert_only_keys({:foo => 23, :baz => 32}, :foo, :bar) do |x, y|
337
+ did_yield = true
338
+ end
339
+ rescue FailFast::AssertionFailureError
340
+ did_yield.should be_false
341
+ end
342
+ end
343
+
344
+ it "should return the hash" do
345
+ @hash = { :buz => 42 }
346
+ assert_only_keys(@hash, :buz).should equal(@hash)
347
+ end
348
+ end
349
+
350
+
280
351
  describe FailFast::Assertions, "#assert_respond_to" do
281
352
  include FailFast::Assertions
282
353
 
metadata CHANGED
@@ -1,35 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fail-fast
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avdi Grimm
8
8
  autorequire:
9
9
  bindir: bin
10
- cert_chain:
11
- - |
12
- -----BEGIN CERTIFICATE-----
13
- MIIDKDCCAhCgAwIBAgIBADANBgkqhkiG9w0BAQUFADA6MQ0wCwYDVQQDDARhdmRp
14
- MRQwEgYKCZImiZPyLGQBGRYEYXZkaTETMBEGCgmSJomT8ixkARkWA29yZzAeFw0w
15
- ODExMjYwMzQwMTBaFw0wOTExMjYwMzQwMTBaMDoxDTALBgNVBAMMBGF2ZGkxFDAS
16
- BgoJkiaJk/IsZAEZFgRhdmRpMRMwEQYKCZImiZPyLGQBGRYDb3JnMIIBIjANBgkq
17
- hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxp+TCbzj+cyaiE1xAqxO+irHbdNF+9y4
18
- rZF3gCs0wfIcqeKDIvlks7FvH0Qj6yiAj8YHUgmuLORat2IBLBmX/G+G0Z2L6MLs
19
- bNwx4YpbTW1cwOFVENSWwNjsvY68/I/EpAeRdtiskG/J33TDxi427dyBDSHsrQ8J
20
- 4oC24EnKwsTOdHwkEaDEmZJQeo5ienR7dalAwIPCIm4tA41kbilLVpMVp0f1hn5o
21
- cVFYZgJFOdNnggVO9B/0/doQhxvC4Jdj2XeoOdIkjul1VjCeF+s+i/PhnKk/Y0++
22
- /9fBCqzfFHYay5TOtTLDlzc7WoN5NBtKChn1gG0AnD+mJ4EnjvgTIQIDAQABozkw
23
- NzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQUen5OQUuU+3P6OyVZ
24
- NsrtAIO7DQcwDQYJKoZIhvcNAQEFBQADggEBAGwXuO2cpDc9m8iJmmbSjnDhhQYL
25
- OssAG5bMy88JnrD+KufXA4BKvLTLkNtTSaNFvL74+FSIK8VwCUpEyn8rsYi3s6N8
26
- /f5+5LDg19Jm6dmkRgFGQjF8cW0PheDtlY0Ywo+wdUhD+LFH+2VanZNx2dz6IjSh
27
- A0uvcb+roaNG70ESPvoJO4PZFCkrgS2QE9TwnhRLi1DONaGmbMYWSZku4tHFEu+R
28
- B/QQd37jgRGtEcplcoMaoCOqAvlIu6QkQt4X3jVhNGqeF2Wgmb1QXEF8cH/hqX4T
29
- NNbtubu/GbyYwC2cb3Clh5gMrAYS765Q8U2aySfRySAFaQyPub+h0uVWkIc=
30
- -----END CERTIFICATE-----
10
+ cert_chain: []
31
11
 
32
- date: 2008-11-30 00:00:00 -05:00
12
+ date: 2009-07-19 00:00:00 -04:00
33
13
  default_executable:
34
14
  dependencies:
35
15
  - !ruby/object:Gem::Dependency
@@ -40,7 +20,7 @@ dependencies:
40
20
  requirements:
41
21
  - - ">="
42
22
  - !ruby/object:Gem::Version
43
- version: 1.1.0
23
+ version: 1.3.0
44
24
  version:
45
25
  - !ruby/object:Gem::Dependency
46
26
  name: hoe
@@ -109,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
89
  requirements: []
110
90
 
111
91
  rubyforge_project: fail-fast
112
- rubygems_version: 1.2.0
92
+ rubygems_version: 1.3.1
113
93
  signing_key:
114
94
  specification_version: 2
115
95
  summary: FailFast is a collection of assertion methods intended for lightweight contract checking.
data.tar.gz.sig DELETED
Binary file
metadata.gz.sig DELETED
Binary file