clean_assert 1.0.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 +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +7 -0
- data/clean_assert.gemspec +26 -0
- data/lib/clean_assert/version.rb +3 -0
- data/lib/clean_assert.rb +19 -0
- data/spec/clean_assert_spec.rb +72 -0
- data/spec/regular_methods.rb +32 -0
- metadata +103 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Niclas Nilsson
|
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,39 @@
|
|
1
|
+
# clean_assert
|
2
|
+
|
3
|
+
A Ruby library to get really clean asserts.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Example:
|
8
|
+
|
9
|
+
assert / "name != nil" / "not name.empty?" / "age >= 21"
|
10
|
+
|
11
|
+
This will do three separate assertion checks and will give you an error message that includes
|
12
|
+
the broken assertion code, the class and the method name. No need for having both the actual assertion and
|
13
|
+
then a strained error message that you normally have to write to understand what broke. Also, due to it's terseness,
|
14
|
+
you can afford having several guard clauses on one line instead of letting them take over your method, which keeps
|
15
|
+
the signal-to-noise ratio sane.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
gem 'clean_assert'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install clean_assert
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
38
|
+
|
39
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'clean_assert/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "clean_assert"
|
8
|
+
spec.version = CleanAssert::VERSION
|
9
|
+
spec.authors = ["Niclas Nilsson"]
|
10
|
+
spec.email = ["niclas@niclasnilsson.se"]
|
11
|
+
spec.description = %q{A Ruby gem to get really clean asserts}
|
12
|
+
spec.summary = %q{A Ruby gem to get really clean asserts}
|
13
|
+
spec.homepage = "https://github.com/niclasnilsson/clean_assert"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "facets", "~> 2.9"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
data/lib/clean_assert.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'facets'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
alias_method :assert, :binding
|
5
|
+
end
|
6
|
+
|
7
|
+
class Binding
|
8
|
+
|
9
|
+
def / expression
|
10
|
+
if not eval expression
|
11
|
+
the_caller = /\`([^\']+)\'/.match(caller(0).first)
|
12
|
+
m = "unknown"
|
13
|
+
m = the_caller ? the_caller[1] : m
|
14
|
+
raise "Assertion '#{expression}' not satisfied in #{self.self()}##{m}"
|
15
|
+
end
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path(File.join('..', 'lib'), File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'clean_assert'
|
4
|
+
|
5
|
+
class Binary
|
6
|
+
@@binary_states = 2
|
7
|
+
|
8
|
+
def self.binary_states= value
|
9
|
+
@@binary_states = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.check_binary_states
|
13
|
+
assert / "@@binary_states == 2"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Person
|
18
|
+
attr_accessor :age
|
19
|
+
def check_invariant
|
20
|
+
assert / "@age >= 0"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "clean asserts" do
|
25
|
+
|
26
|
+
before do
|
27
|
+
@my_member = "my member"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should provide clean assertion syntax" do
|
31
|
+
name = "Niclas Nilsson"
|
32
|
+
assert / "name != nil"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should support chained assertions to keep code terse" do
|
36
|
+
name = "Niclas Nilsson"
|
37
|
+
age = 38
|
38
|
+
assert / "name != nil" / "not name.empty?" / "age >= 21"
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "that are satisfied" do
|
42
|
+
it "should work for instance variables" do
|
43
|
+
niclas = Person.new
|
44
|
+
niclas.age = 38
|
45
|
+
niclas.check_invariant
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should work for class variables" do
|
49
|
+
Binary.binary_states = 2
|
50
|
+
Binary.check_binary_states
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "that are not satisfied" do
|
56
|
+
it "should work for instance variables" do
|
57
|
+
niclas = Person.new
|
58
|
+
niclas.age = -1
|
59
|
+
lambda { niclas.check_invariant }.should raise_error(RuntimeError, /Assertion '@age >= 0' not satisfied/)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should work for class variables" do
|
63
|
+
Binary.binary_states = 3
|
64
|
+
lambda { Binary.check_binary_states }.should raise_error(RuntimeError, /Assertion '@@binary_states == 2' not satisfied in Binary#check_binary_states/)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should work for regular methods" do
|
69
|
+
load "spec/regular_methods.rb"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'clean_assert'
|
2
|
+
|
3
|
+
def method_with_valid_assert
|
4
|
+
assert / "2 == 2"
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_with_broken_assert
|
8
|
+
assert / "1 == 2"
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_with_argument value
|
12
|
+
assert / "value == 2"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Test valid assertions outside of classes and outside of rspec
|
16
|
+
method_with_valid_assert
|
17
|
+
method_with_argument 2
|
18
|
+
|
19
|
+
# Test broken assertions outside of classes and outside of rspec
|
20
|
+
|
21
|
+
begin
|
22
|
+
method_with_broken_assert
|
23
|
+
rescue RuntimeError => e
|
24
|
+
raise if not e.message =~ /Assertion '1 == 2' not satisfied in main#method_with_broken_assert/
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
method_with_argument 3
|
29
|
+
rescue RuntimeError => e
|
30
|
+
raise if not e.message =~ /Assertion 'value == 2' not satisfied in main#method_with_argument/
|
31
|
+
end
|
32
|
+
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clean_assert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Niclas Nilsson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: facets
|
16
|
+
requirement: &70159545782540 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.9'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70159545782540
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bundler
|
27
|
+
requirement: &70159545781940 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.3'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70159545781940
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70159545781500 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70159545781500
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &70159545780880 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70159545780880
|
58
|
+
description: A Ruby gem to get really clean asserts
|
59
|
+
email:
|
60
|
+
- niclas@niclasnilsson.se
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- clean_assert.gemspec
|
71
|
+
- lib/clean_assert.rb
|
72
|
+
- lib/clean_assert/version.rb
|
73
|
+
- spec/clean_assert_spec.rb
|
74
|
+
- spec/regular_methods.rb
|
75
|
+
homepage: https://github.com/niclasnilsson/clean_assert
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 1.8.17
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: A Ruby gem to get really clean asserts
|
100
|
+
test_files:
|
101
|
+
- spec/clean_assert_spec.rb
|
102
|
+
- spec/regular_methods.rb
|
103
|
+
has_rdoc:
|