invariant 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Norbert Wojtowicz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Invariant
2
+
3
+ `Invariant` is a simple gem that provides `Kernel#assert` to document your assumptions in code.
4
+
5
+ > Use assertions to prevent the impossible.
6
+ >
7
+ > Whenever you find yourself thinking "but of course that could never happen,"
8
+ > add code to check it. The easiest way to do this is with assertions.
9
+ >
10
+ > http://pragmatictips.com/33
11
+
12
+
13
+ ## Installation
14
+
15
+ Add to Gemfile:
16
+
17
+ gem 'invariant'
18
+
19
+
20
+ ## Configuration
21
+
22
+ Assertions are enabled by default. Better safe than sorry. To disable assertions:
23
+
24
+ ```ruby
25
+ Invariant.disable_assertions
26
+ ```
27
+
28
+ For example, in Rails you may want to disable assertions in Production:
29
+
30
+ ```ruby
31
+ # config/initializers/invariant.rb
32
+ Invariant.disable_assertions if Rails.env.production?
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ Use `assert` to test a condition:
38
+
39
+ ```ruby
40
+ assert age > 0
41
+ ```
42
+
43
+ Provide an optional message:
44
+
45
+ ```ruby
46
+ assert errors.empty?, "Why do we still have errors?"
47
+ ```
48
+
49
+ You can also test a block of code. This is handy when you're invariant requires several lines of code.
50
+
51
+ ```ruby
52
+ assert do
53
+ one_thing = calculate_something
54
+ other_thing = calculate_something_else
55
+ one_thing > other_thing
56
+ end
57
+ ```
58
+
59
+ Blocks also support an optional message:
60
+
61
+ ```ruby
62
+ assert 'That one thing should always be greater' do
63
+ one_thing = calculate_something
64
+ other_thing = calculate_something_else
65
+ one_thing > other_thing
66
+ end
67
+ ```
68
+
69
+ ## Errors
70
+
71
+ A failed assertion raises `Invariant::AssertionError` which inherits directly from `Exception`.
72
+
73
+ An `AssertionError` should be an *exceptional* failure. It should not be something a program knows how to recover from. This is why `AssertionError` is not a `StandardError`:
74
+
75
+ ```ruby
76
+ def real_world
77
+ assert 1 == 0, 'We are in the real world'
78
+ rescue
79
+ 'We are in the Matrix'
80
+ end
81
+
82
+ real_world # => raises Invariant::AssertionError, 'We are in the real world'
83
+ ```
84
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/invariant.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'invariant'
7
+ gem.version = '0.1.0'
8
+ gem.authors = ["Norbert Wojtowicz"]
9
+ gem.email = ["wojtowicz.norbert@gmail.com"]
10
+ gem.description = 'Document your code invariants'
11
+ gem.summary = gem.description
12
+ gem.homepage = "https://github.com/pithyless/invariant"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency('rspec', "~> 2.12.0")
20
+ gem.add_development_dependency('rake')
21
+ end
data/lib/invariant.rb ADDED
@@ -0,0 +1,25 @@
1
+ module Invariant
2
+ class AssertionError < StandardError
3
+ end
4
+
5
+ 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
15
+ end
16
+
17
+ def self.disable_assertions
18
+ Kernel.class_eval do
19
+ def assert(*args, &block)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ Invariant.enable_assertions
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Invariant Assertion' do
4
+
5
+ before :each do
6
+ Invariant.enable_assertions
7
+ end
8
+
9
+ describe 'assert' do
10
+
11
+ context 'without message' do
12
+ it 'fails when condition is false' do
13
+ expect{ assert false }.to raise_error Invariant::AssertionError
14
+ end
15
+
16
+ it 'fails when condition evaluates to false' do
17
+ expect{ assert nil }.to raise_error Invariant::AssertionError
18
+ end
19
+
20
+ it 'returns nil when condition is true' do
21
+ expect{ assert true }.to_not raise_error Invariant::AssertionError
22
+ assert(true).should be_nil
23
+ end
24
+
25
+ it 'returns nil when condition evaluates to true' do
26
+ expect{ assert 'This evaluates to true' }.to_not raise_error Invariant::AssertionError
27
+ assert('This evaluates to true').should be_nil
28
+ end
29
+ end
30
+
31
+ context 'with message' do
32
+ it 'raises error with specified message when condition evaluates to false' do
33
+ expect{ assert nil, 'The error message' }.to raise_error(Invariant::AssertionError, 'The error message')
34
+ end
35
+
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)
38
+ assert('This evaluates to true').should be_nil
39
+ end
40
+ end
41
+
42
+ context 'block invariant without message' do
43
+ it 'invokes block' do
44
+ expect{ |b| assert(&b) }.to yield_with_no_args
45
+ end
46
+
47
+ it 'invokes assert with the block result' do
48
+ assert { 'Evaluates to true' }.should be_nil
49
+ end
50
+
51
+ it 'raises error without message' do
52
+ expect{ assert { nil } }.to raise_error(Invariant::AssertionError)
53
+ end
54
+ end
55
+
56
+ context 'block invariant with message' do
57
+ it 'invokes block' do
58
+ expect{ |b| assert('The error message', &b) }.to yield_with_no_args
59
+ end
60
+
61
+ it 'invokes assert with the block result and message' do
62
+ assert('The error message'){ 'Evaluates to true' }.should be_nil
63
+ end
64
+
65
+ it 'raises error with message' do
66
+ expect{ assert('The error message'){ 1 == 2 } }.to raise_error(Invariant::AssertionError, 'The error message')
67
+ end
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Invariant do
4
+
5
+ describe '::disable_assertions' do
6
+ it 'disables assertions' do
7
+ Invariant.disable_assertions
8
+ assert(false).should be_nil
9
+ end
10
+ end
11
+
12
+ describe '::disable_assertions' do
13
+ it 'enables assertions' do
14
+ Invariant.enable_assertions
15
+ expect{ assert false }.to raise_error Invariant::AssertionError
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,9 @@
1
+ require 'invariant'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ config.order = 'random'
9
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: invariant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Norbert Wojtowicz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.12.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.12.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Document your code invariants
47
+ email:
48
+ - wojtowicz.norbert@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - invariant.gemspec
60
+ - lib/invariant.rb
61
+ - spec/invariant/assert_spec.rb
62
+ - spec/invariant/invariant_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: https://github.com/pithyless/invariant
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ segments:
77
+ - 0
78
+ hash: 970666457985510709
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ segments:
86
+ - 0
87
+ hash: 970666457985510709
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.23
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Document your code invariants
94
+ test_files:
95
+ - spec/invariant/assert_spec.rb
96
+ - spec/invariant/invariant_spec.rb
97
+ - spec/spec_helper.rb