solid_assert 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
14
+
15
+ group :test do
16
+ gem 'ZenTest'
17
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jorge Manrubia
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
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.
@@ -0,0 +1,24 @@
1
+ # solid_assert
2
+
3
+ solid_assert is a simple implementation of an `assert` utility in Ruby.
4
+
5
+ It let you code in your assumptions when coding. Sometimes, when you are coding, you have this thinking of "I it reaches here, then this variable has to be this value", or "This line should never be executed", or "At this point, this list should contain an entry for all the keys in this hash". These are the kind of scenarios when assertions comes handy.
6
+
7
+ The practice of using assertions makes your code more solid, since it is the computer who systematically verifies your code integrity.
8
+
9
+ # Installation
10
+
11
+ In your `Gemfile`
12
+
13
+ gem "solid_assert"
14
+
15
+ # Usage
16
+
17
+
18
+ ## References
19
+
20
+ - [Programming with assertions](http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html). A great article on assertions. It is about the Java language, but the concepts it explains apply to any programming language.
21
+ - [Writing Solid Code](http://www.amazon.com/Writing-Solid-Code-Microsoft-Programming/dp/1556155514).
22
+
23
+
24
+
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "solid_assert"
18
+ gem.homepage = "http://github.com/jorgemanrubia/solid_assert"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Assert utility for ruby}
21
+ gem.description = %Q{Assert utility for ruby. It let you code your assumptions and code invariants, so they are checked automatically. It can be deactivated, so you it doesn't affect to your program performance (for example, in production)'}
22
+ gem.email = "jorge.manrubia@gmail.com"
23
+ gem.authors = ["Jorge Manrubia"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "solid_assert #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.5.0
@@ -0,0 +1,4 @@
1
+ Dir[File.dirname(__FILE__) + '/solid_assert/**/*.rb'].each do |file|
2
+ require file
3
+ end
4
+
@@ -0,0 +1,37 @@
1
+ module SolidAssert
2
+ module Assert
3
+
4
+ # Expresses a condition and fails if the condition is not satisfied
5
+ #
6
+ # Usage
7
+ # assert false
8
+ # assert not_nil_object
9
+ # assert !list.empty?, "The list should not be empty at this point"
10
+ #
11
+ # @param condition The condition to assert
12
+ # @param message The optional message when the condition is not satisfied
13
+ # @raise {AssertionFailedError} when the condition is not satisfied
14
+ def assert(condition, message=nil)
15
+ raise SolidAssert::AssertionFailedError.new(message) if !condition
16
+ end
17
+
18
+ # Let you {#assert} a block of code. It comes handy when your assertion requires more than one lines of code
19
+ # Usage:
20
+ # invariant do
21
+ # some_number = 1
22
+ # other_number = 2
23
+ # some_number == other_number
24
+ # end
25
+ #
26
+ # invariant "Both numbers should be equal" do
27
+ # some_number = 1
28
+ # other_number = 2
29
+ # some_number == other_number
30
+ # end
31
+ # @param message The optional message when the block doesn't evaluates to a satisfied condition
32
+ # @yield The block that will be evaluated for deciding whether the condition is satisfied
33
+ def invariant(message=nil)
34
+ assert yield, message
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,10 @@
1
+ module SolidAssert
2
+
3
+ class AssertionFailedError < StandardError
4
+ def initialize(message=nil)
5
+ super
6
+ end
7
+ end
8
+
9
+ end
10
+
@@ -0,0 +1,14 @@
1
+ module SolidAssert
2
+
3
+ # Null assert implementation. Used when the assertions are disabled
4
+ module NullAssert
5
+
6
+ # Empty implemention of {Assert#assert}
7
+ def assert(condition, message=nil)
8
+ end
9
+
10
+ # Empty implemention of {Assert#invariant}
11
+ def invariant(message=nil)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ module SolidAssert
2
+
3
+ # Turns the assertions on
4
+ def self.enable_assertions
5
+ Object.class_eval do
6
+ include Assert
7
+ end
8
+ end
9
+
10
+ # Turns the assertions off
11
+ def self.disable_assertions
12
+ Object.class_eval do
13
+ include NullAssert
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+
20
+
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{solid_assert}
8
+ s.version = "0.5.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [%q{Jorge Manrubia}]
12
+ s.date = %q{2011-09-18}
13
+ s.description = %q{Assert utility for ruby. It let you code your assumptions and code invariants, so they are checked automatically. It can be deactivated, so you it doesn't affect to your program performance (for example, in production)'}
14
+ s.email = %q{jorge.manrubia@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/solid_assert.rb",
28
+ "lib/solid_assert/assert.rb",
29
+ "lib/solid_assert/assertion_failed_error.rb",
30
+ "lib/solid_assert/null_assert.rb",
31
+ "lib/solid_assert/solid_assert.rb",
32
+ "solid_assert.gemspec",
33
+ "spec/solid_assert/assert_spec.rb",
34
+ "spec/solid_assert/null_assert_spec.rb",
35
+ "spec/solid_assert/solid_assert_spec.rb",
36
+ "spec/spec_helper.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/jorgemanrubia/solid_assert}
39
+ s.licenses = [%q{MIT}]
40
+ s.require_paths = [%q{lib}]
41
+ s.rubygems_version = %q{1.8.6}
42
+ s.summary = %q{Assert utility for ruby}
43
+
44
+ if s.respond_to? :specification_version then
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
51
+ s.add_development_dependency(%q<rcov>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ describe SolidAssert::Assert do
4
+ before(:all) do
5
+ Object.class_eval do
6
+ include SolidAssert::Assert
7
+ end
8
+ end
9
+
10
+ describe "#assert" do
11
+ describe "without assertion message" do
12
+ it "should fail when condition is false" do
13
+ lambda { assert false }.should raise_error SolidAssert::AssertionFailedError
14
+ end
15
+
16
+ it "should fail when condition evaluates to false" do
17
+ lambda { assert nil }.should raise_error SolidAssert::AssertionFailedError
18
+ end
19
+
20
+ it "should not fail when condition is true" do
21
+ lambda { assert true }.should_not raise_error SolidAssert::AssertionFailedError
22
+ end
23
+
24
+ it "should not fail when condition evaluates to true" do
25
+ lambda { assert "This evaluates to true" }.should_not raise_error SolidAssert::AssertionFailedError
26
+ end
27
+ end
28
+
29
+ describe "with assertion message" do
30
+ it "should raise error with specified message when condition evaluates to false" do
31
+ lambda { assert nil, "The error message" }.should raise_error(SolidAssert::AssertionFailedError, "The error message")
32
+ end
33
+
34
+ it "should not raise any error when condition doesn't evaluate to false'" do
35
+ lambda { assert 'This evaluates to true', "The error message" }.should_not raise_error(SolidAssert::AssertionFailedError)
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "#invariants" do
41
+ it "should invoke assert with the provided block" do
42
+ self.should_receive(:assert).with('This is the invariant block', nil)
43
+ invariant { "This is the invariant block" }
44
+ end
45
+
46
+ it "should invoke assert with the provided block and the provided error message" do
47
+ self.should_receive(:assert).with('This is the invariant block', 'the error message')
48
+ invariant "the error message" do
49
+ "This is the invariant block"
50
+ end
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe SolidAssert::NullAssert do
4
+ before(:all) do
5
+ Object.class_eval do
6
+ include SolidAssert::NullAssert
7
+ end
8
+ end
9
+
10
+ describe "#assert" do
11
+ it "should do nothing without message" do
12
+ assert false
13
+ end
14
+
15
+ it "should do nothing with message" do
16
+ assert false, "Some message"
17
+ end
18
+ end
19
+
20
+ describe "#invariants" do
21
+ it "should do nothing without message" do
22
+ invariant do
23
+ false
24
+ end
25
+ end
26
+
27
+ it "should do nothing with message" do
28
+ invariant "Some message" do
29
+ false
30
+ end
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe SolidAssert::Assert do
4
+ describe ".enable_assertions" do
5
+ it "should make the Object class to include the Assertions module" do
6
+ Object.should_receive(:include).with(SolidAssert::Assert)
7
+ SolidAssert.enable_assertions
8
+ end
9
+
10
+ it "should turn on the assertions" do
11
+ SolidAssert.enable_assertions
12
+ lambda{assert false}.should raise_error(SolidAssert::AssertionFailedError)
13
+ end
14
+ end
15
+
16
+ describe ".disable_assertions" do
17
+ it "should make the Object class to include the EmptyAssertions module" do
18
+ Object.should_receive(:include).with(SolidAssert::NullAssert)
19
+ SolidAssert.disable_assertions
20
+ end
21
+
22
+ it "should enable blank assertions, when these have been enabled previously" do
23
+ SolidAssert.disable_assertions
24
+ lambda{assert false}.should_not raise_error(SolidAssert::AssertionFailedError)
25
+ end
26
+
27
+ it "should disabled assertions, if these have been enabled previously" do
28
+ SolidAssert.enable_assertions
29
+ SolidAssert.disable_assertions
30
+ lambda{assert false}.should_not raise_error(SolidAssert::AssertionFailedError)
31
+ end
32
+
33
+ end
34
+ end
35
+
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'solid_assert'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solid_assert
3
+ version: !ruby/object:Gem::Version
4
+ hash: 11
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
+ platform: ruby
12
+ authors:
13
+ - Jorge Manrubia
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-18 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 2
30
+ - 3
31
+ - 0
32
+ version: 2.3.0
33
+ version_requirements: *id001
34
+ name: rspec
35
+ prerelease: false
36
+ - !ruby/object:Gem::Dependency
37
+ type: :development
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
49
+ version_requirements: *id002
50
+ name: bundler
51
+ prerelease: false
52
+ - !ruby/object:Gem::Dependency
53
+ type: :development
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 7
60
+ segments:
61
+ - 1
62
+ - 6
63
+ - 4
64
+ version: 1.6.4
65
+ version_requirements: *id003
66
+ name: jeweler
67
+ prerelease: false
68
+ - !ruby/object:Gem::Dependency
69
+ type: :development
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ version_requirements: *id004
80
+ name: rcov
81
+ prerelease: false
82
+ description: Assert utility for ruby. It let you code your assumptions and code invariants, so they are checked automatically. It can be deactivated, so you it doesn't affect to your program performance (for example, in production)'
83
+ email: jorge.manrubia@gmail.com
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files:
89
+ - LICENSE.txt
90
+ - README.md
91
+ files:
92
+ - .document
93
+ - .rspec
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - VERSION
99
+ - lib/solid_assert.rb
100
+ - lib/solid_assert/assert.rb
101
+ - lib/solid_assert/assertion_failed_error.rb
102
+ - lib/solid_assert/null_assert.rb
103
+ - lib/solid_assert/solid_assert.rb
104
+ - solid_assert.gemspec
105
+ - spec/solid_assert/assert_spec.rb
106
+ - spec/solid_assert/null_assert_spec.rb
107
+ - spec/solid_assert/solid_assert_spec.rb
108
+ - spec/spec_helper.rb
109
+ homepage: http://github.com/jorgemanrubia/solid_assert
110
+ licenses:
111
+ - MIT
112
+ post_install_message:
113
+ rdoc_options: []
114
+
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ requirements: []
136
+
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.6
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: Assert utility for ruby
142
+ test_files: []
143
+