tworgy-ruby 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/tworgy/dbc.rb CHANGED
@@ -1,35 +1,42 @@
1
1
  class DBC
2
+ class PreconditionException < RuntimeError; end
3
+ class AssertconditionException < RuntimeError; end
4
+ class PostconditionException < RuntimeError; end
5
+ class FailException < RuntimeError; end
6
+
2
7
  def self.require(condition, message = "")
3
8
  unless condition
4
- error('Pre', message, caller)
9
+ error(PreconditionException, message, caller)
5
10
  end
6
11
  end
7
12
 
8
13
  def self.require_not_blank(string, message = "")
9
- if string.strip.blank?
10
- error('Pre', message, caller)
14
+ if string.nil? || string.strip.blank?
15
+ error(PreconditionException, message, caller)
11
16
  end
12
17
  end
13
18
 
14
19
  def self.assert(condition, message = "")
15
20
  unless condition
16
- error('Assert', message, caller)
21
+ error(AssertconditionException, message, caller)
17
22
  end
18
23
  end
19
24
 
20
25
  def self.ensure(condition, message = "")
21
26
  unless condition
22
- error('Post', message, caller)
27
+ error(PostconditionException, message, caller)
23
28
  end
24
29
  end
25
30
 
26
31
  def self.fail(message = "")
27
- error('Fail', message, caller)
32
+ error(FailException, message, caller)
28
33
  end
29
34
 
30
35
  private
31
36
 
32
- def self.error(type, message, caller)
33
- raise RuntimeError.new("#{type}-condition failed: #{message}\nTrace was: #{caller.join("\n")}")
37
+ def self.error(klass, message, caller)
38
+ raise klass.new("#{klass.name}-condition failed: #{message}\nTrace was: #{caller.join("\n")}")
34
39
  end
35
- end
40
+
41
+ end
42
+
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
2
+
3
+ describe DBC do
4
+
5
+ describe 'should raise DBC exception for' do
6
+ it 'require with false' do
7
+ lambda { DBC.require(false) }.should raise_error(DBC::PreconditionException)
8
+ end
9
+
10
+ it 'require with nil' do
11
+ lambda { DBC.require(nil) }.should raise_error(DBC::PreconditionException)
12
+ end
13
+
14
+ it 'require_not_blank with nil' do
15
+ lambda { DBC.require_not_blank(nil) }.should raise_error(DBC::PreconditionException)
16
+ end
17
+
18
+ it 'require_not_blank with " "' do
19
+ lambda { DBC.require_not_blank(" ") }.should raise_error(DBC::PreconditionException)
20
+ end
21
+
22
+ it 'assert with false' do
23
+ lambda { DBC.assert(false) }.should raise_error(DBC::AssertconditionException)
24
+ end
25
+
26
+ it 'assert with nil' do
27
+ lambda { DBC.assert(nil) }.should raise_error(DBC::AssertconditionException)
28
+ end
29
+
30
+ it 'ensure with false' do
31
+ lambda { DBC.ensure(false) }.should raise_error(DBC::PostconditionException)
32
+ end
33
+
34
+ it 'ensure with nil' do
35
+ lambda { DBC.ensure(nil) }.should raise_error(DBC::PostconditionException)
36
+ end
37
+ end
38
+
39
+ end
40
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tworgy-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat Holroyd
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-08 00:00:00 +11:00
12
+ date: 2010-02-17 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,7 @@ files:
42
42
  - lib/tworgy/standard_extensions.rb
43
43
  - lib/tworgy/testing.rb
44
44
  - lib/tworgy/tmail.rb
45
+ - spec/lib/tworgy/dbc_spec.rb
45
46
  - spec/lib/tworgy/hash_and_array_extensions_spec.rb
46
47
  - spec/lib/tworgy/testing_spec.rb
47
48
  - spec/spec.opts
@@ -75,6 +76,7 @@ signing_key:
75
76
  specification_version: 3
76
77
  summary: Handy Ruby extensions
77
78
  test_files:
79
+ - spec/lib/tworgy/dbc_spec.rb
78
80
  - spec/lib/tworgy/hash_and_array_extensions_spec.rb
79
81
  - spec/lib/tworgy/testing_spec.rb
80
82
  - spec/spec_helper.rb