assure 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *~
2
+ *.gem
3
+ pkg
4
+ .kdev4
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2010-2013 Pluron, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ assure
2
+ ======
3
+ Provides two methods: internal_error and assure
4
+
5
+
6
+ internal_error
7
+ --------------
8
+ Raises internal error with a given message.
9
+
10
+
11
+ assure
12
+ ------
13
+ Raises internal error when expression evaluates to nil or false.
14
+ Use this to check various preconditions, for example:
15
+
16
+ def do_smth(x)
17
+ assure(x.is_a? String)
18
+ end
19
+
20
+ You can provide optional message to be printed in the exception output.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+ require 'bundler'
5
+
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test assure.'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = true
16
+ end
17
+
18
+ desc 'Generate documentation for assure.'
19
+ Rake::RDocTask.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'assure'
22
+ rdoc.options << '--line-numbers' << '--inline-source'
23
+ rdoc.rdoc_files.include('README')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
data/assure.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+
3
+ SPEC = Gem::Specification.new do |s|
4
+ s.name = "assure"
5
+ s.version = "0.1.1"
6
+ s.author = "Pluron, Inc."
7
+ s.email = "support@pluron.com"
8
+ s.homepage = "http://github.com/acunote/assure"
9
+ s.platform = Gem::Platform::RUBY
10
+ s.description = "Adds assure and internal_error methods"
11
+ s.summary = "Raises internal error when expression evaluates to nil or false."
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- test/*`.split("\n")
15
+
16
+ s.require_path = "lib"
17
+ s.has_rdoc = true
18
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + "/lib")
2
+ require 'assure'
data/lib/assure.rb ADDED
@@ -0,0 +1,44 @@
1
+ # Copyright (c) 2010-2013 Pluron, Inc.
2
+
3
+ #Use this to raise internal error with a given message.
4
+ #This works for all modes - production, development, etc.
5
+ def internal_error(message = 'internal error')
6
+ raise message
7
+ end
8
+
9
+ #Raises internal error when expression evaluates to nil or false
10
+ #Use this to check various preconditions, for example
11
+ # def do_smth(x)
12
+ # assure(x.is_a? String)
13
+ # end
14
+ #You can provide optional message to be printed in the exception output.
15
+ def assure(expression, message = nil)
16
+ return if expression
17
+ file, method, line = get_caller_location_for_assure
18
+
19
+ #Try to find expression in assert
20
+ expression = File.readlines(file)[line.to_i-1].
21
+ gsub(/^.*assure\s*\(\s*/, '').gsub(/\s*\)\s*$/, '')
22
+
23
+ #cleanup path - remove rails root and "./"
24
+ if defined? Rails
25
+ file = file.gsub(Rails.root.to_s+'/', '')
26
+ else
27
+ file = file.gsub(Dir.pwd, '')
28
+ end
29
+ file = file.gsub(/^\.\//, '')
30
+ raise internal_error("#{file}:#{line}: #{method}: Assertion \"#{expression}\" failed#{message ? "\n#{message}" : ""}")
31
+ end
32
+
33
+ def get_caller_location_for_assure(options = {:depth => 2})
34
+ caller_method = caller(options[:depth])[0]
35
+
36
+ #Sample output is:
37
+ # test/unit/assure_test.rb:9:in `test_assure
38
+ #When working dir isn't set, the RAILS_ROOT will be prepended to the path
39
+ caller_method =~ /([^:]+):([0-9]+):in `(.+)'/
40
+ file = $1
41
+ line = $2
42
+ method = $3
43
+ [file, method, line]
44
+ end
@@ -0,0 +1,33 @@
1
+ # Copyright (c) 2011 Pluron, Inc.
2
+
3
+ require 'test/unit'
4
+ require 'assure'
5
+
6
+ class AssureTest < Test::Unit::TestCase
7
+
8
+ def test_basic
9
+ assure("Test".is_a? String)
10
+
11
+ exception = assert_raise(RuntimeError) do
12
+ assure(false)
13
+ end
14
+ assert_equal 'test/assure_test.rb:12: test_basic: Assertion "false" failed', exception.message
15
+
16
+ exception = assert_raise(RuntimeError) do
17
+ assure(nil)
18
+ end
19
+ assert_equal 'test/assure_test.rb:17: test_basic: Assertion "nil" failed', exception.message
20
+
21
+ exception = assert_raise(RuntimeError) do
22
+ x = 2
23
+ assure( x == 3, "#{x} == 2" )
24
+ end
25
+ assert_equal "test/assure_test.rb:23: test_basic: Assertion \"x == 3, \"\#{x} == 2\"\" failed\n2 == 2", exception.message
26
+
27
+ exception = assert_raise(RuntimeError) do
28
+ assure(1.is_a? String)
29
+ end
30
+ assert_equal 'test/assure_test.rb:28: test_basic: Assertion "1.is_a? String" failed', exception.message
31
+ end
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pluron, Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Adds assure and internal_error methods
15
+ email: support@pluron.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gitignore
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
24
+ - assure.gemspec
25
+ - init.rb
26
+ - lib/assure.rb
27
+ - test/assure_test.rb
28
+ homepage: http://github.com/acunote/assure
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.23
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Raises internal error when expression evaluates to nil or false.
52
+ test_files:
53
+ - test/assure_test.rb
54
+ has_rdoc: true