invariant 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ ## 0.2.0
2
+
3
+ Bugfixes:
4
+
5
+ - Invariant::AssertionError actually inherits from Exception as documented
6
+
7
+ Features:
8
+
9
+ - add support for custom failure_handler callable (#1, @obrok)
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = 'invariant'
7
- gem.version = '0.1.0'
7
+ gem.version = '0.2.0'
8
8
  gem.authors = ["Norbert Wojtowicz"]
9
9
  gem.email = ["wojtowicz.norbert@gmail.com"]
10
10
  gem.description = 'Document your code invariants'
@@ -1,23 +1,32 @@
1
- module Invariant
2
- class AssertionError < StandardError
1
+ require "singleton"
2
+
3
+ class Invariant
4
+ include Singleton
5
+
6
+ class AssertionError < Exception
3
7
  end
4
8
 
9
+ attr_accessor :failure_handler
10
+
5
11
  def self.enable_assertions
6
- Kernel.class_eval do
7
- def assert(first=nil, second=nil)
8
- if block_given?
9
- assert(yield, first)
10
- else
11
- raise AssertionError, second unless first
12
- end
13
- end
14
- end
12
+ failure_handler { |msg| raise AssertionError.new(msg) }
15
13
  end
16
14
 
17
15
  def self.disable_assertions
18
- Kernel.class_eval do
19
- def assert(*args, &block)
20
- end
16
+ failure_handler { }
17
+ end
18
+
19
+ def self.failure_handler(&block)
20
+ instance.failure_handler = block
21
+ end
22
+ end
23
+
24
+ Kernel.class_eval do
25
+ def assert(first=nil, second=nil)
26
+ if block_given?
27
+ assert(yield, first)
28
+ else
29
+ Invariant.instance.failure_handler.call(second) unless first
21
30
  end
22
31
  end
23
32
  end
@@ -18,12 +18,12 @@ describe 'Invariant Assertion' do
18
18
  end
19
19
 
20
20
  it 'returns nil when condition is true' do
21
- expect{ assert true }.to_not raise_error Invariant::AssertionError
21
+ expect{ assert true }.to_not raise_error
22
22
  assert(true).should be_nil
23
23
  end
24
24
 
25
25
  it 'returns nil when condition evaluates to true' do
26
- expect{ assert 'This evaluates to true' }.to_not raise_error Invariant::AssertionError
26
+ expect{ assert 'This evaluates to true' }.to_not raise_error
27
27
  assert('This evaluates to true').should be_nil
28
28
  end
29
29
  end
@@ -34,7 +34,7 @@ describe 'Invariant Assertion' do
34
34
  end
35
35
 
36
36
  it 'returns nil without error when condition evaluates to true' do
37
- expect{ assert 'This evaluates to true', 'The error message' }.to_not raise_error(Invariant::AssertionError)
37
+ expect{ assert 'This evaluates to true', 'The error message' }.to_not raise_error
38
38
  assert('This evaluates to true').should be_nil
39
39
  end
40
40
  end
@@ -16,4 +16,15 @@ describe Invariant do
16
16
  end
17
17
  end
18
18
 
19
+ describe '::failure_handler' do
20
+ it 'calls the block with assertion failures' do
21
+ block = Proc.new {}
22
+ block.should_receive(:call).with("some message")
23
+
24
+ Invariant.failure_handler(&block)
25
+
26
+ expect { assert(false, "some message") }.not_to raise_error
27
+ end
28
+ end
29
+
19
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: invariant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-10 00:00:00.000000000 Z
12
+ date: 2014-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -52,6 +52,7 @@ extra_rdoc_files: []
52
52
  files:
53
53
  - .gitignore
54
54
  - .rspec
55
+ - CHANGELOG.md
55
56
  - Gemfile
56
57
  - LICENSE.txt
57
58
  - README.md
@@ -73,18 +74,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
74
  - - ! '>='
74
75
  - !ruby/object:Gem::Version
75
76
  version: '0'
76
- segments:
77
- - 0
78
- hash: 970666457985510709
79
77
  required_rubygems_version: !ruby/object:Gem::Requirement
80
78
  none: false
81
79
  requirements:
82
80
  - - ! '>='
83
81
  - !ruby/object:Gem::Version
84
82
  version: '0'
85
- segments:
86
- - 0
87
- hash: 970666457985510709
88
83
  requirements: []
89
84
  rubyforge_project:
90
85
  rubygems_version: 1.8.23
@@ -95,3 +90,4 @@ test_files:
95
90
  - spec/invariant/assert_spec.rb
96
91
  - spec/invariant/invariant_spec.rb
97
92
  - spec/spec_helper.rb
93
+ has_rdoc: