covenant 0.0.1

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.
@@ -0,0 +1,21 @@
1
+ module Covenant
2
+ module Assertions
3
+ module Is
4
+ def method_missing(name, *args)
5
+ strip = name.to_s.sub(/^is_/, '')
6
+ query = "#{strip}?"
7
+ argl = args.map(&:inspect).join(", ")
8
+
9
+ if target.respond_to?(query)
10
+ test target.send(query, *args),
11
+ "#{target.inspect} must be #{strip} #{argl}",
12
+ "#{target.inspect} must not be #{strip} #{argl}"
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ Covenant::Assertions.send :include, self
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ module Covenant
2
+ module Assertions
3
+ module IsA
4
+ def is_a(type)
5
+ type.is_a?(Class) or
6
+ raise ArgumentError, "#{type.inspect} must be a Class"
7
+
8
+ test target.is_a?(type),
9
+ "#{target.inspect} must be a #{type.name}",
10
+ "#{target.inspect} must not be a #{type.name}"
11
+ end
12
+
13
+ alias_method :is_an, :is_a
14
+
15
+ Covenant::Assertions.send :include, self
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module Covenant
2
+ module Assertions
3
+ module Query
4
+ def method_missing(name, *args)
5
+ query = "#{name}?"
6
+ argl = args.map(&:inspect).join(", ")
7
+
8
+ if target.respond_to?(query)
9
+ test target.send(query, *args),
10
+ "#{target.inspect} must #{name} #{argl}",
11
+ "#{target.inspect} must not #{name} #{argl}"
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ Covenant::Assertions.send :include, self
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Covenant
2
+ VERSION = "0.0.1"
3
+ end
data/lib/covenant.rb ADDED
@@ -0,0 +1,98 @@
1
+ require 'covenant/assertions/is_a'
2
+ require 'covenant/assertions/is'
3
+ require 'covenant/assertions/query'
4
+
5
+ module Covenant
6
+ # Adds the Covenant DSL to base.
7
+ #
8
+ # @api public
9
+ #
10
+ # @param base where to add the Covenant DSL
11
+ def self.abide(base = Object)
12
+ case base
13
+ when Class
14
+ base.send :include, Covenant::DSL
15
+ else
16
+ base.extend Covenant::DSL
17
+ end
18
+ end
19
+
20
+ module DSL
21
+ # Ensures that the condition on target evaluates to a true value.
22
+ #
23
+ # @api public
24
+ #
25
+ # @param target the target on which to test the condition
26
+ # @param [#to_s] message the message that will be set if the test fails
27
+ #
28
+ # @return the wrapper object you can use to test your assertions
29
+ def assert(target = self, message = nil)
30
+ if block_given?
31
+ Covenant::Assertion.new(target, message).test(yield target)
32
+ else
33
+ Covenant::Assertion.new(target, message)
34
+ end
35
+ end
36
+
37
+ # Ensures that the condition on target evaluates to a false value.
38
+ #
39
+ # @api public
40
+ #
41
+ # @param target the target on which to test the condition
42
+ # @param message the message that will be set if the test fails
43
+ #
44
+ # @return the wrapper object you can use to test your assertions
45
+ def deny(target = self, message = nil)
46
+ if block_given?
47
+ Covenant::Denial.new(target, message).test(yield target)
48
+ else
49
+ Covenant::Denial.new(target, message)
50
+ end
51
+ end
52
+ end
53
+
54
+ class AssertionFailed < Exception; end
55
+
56
+ class Statement
57
+ include Assertions
58
+
59
+ def initialize(target, message)
60
+ @target = target
61
+ @message = message
62
+ end
63
+
64
+ protected
65
+
66
+ attr_reader :target, :message
67
+
68
+ def raise_error(message)
69
+ msg = self.message || message
70
+
71
+ if msg
72
+ raise AssertionFailed, msg
73
+ else
74
+ raise AssertionFailed
75
+ end
76
+ end
77
+ end
78
+
79
+ class Assertion < Statement
80
+ def test(condition, message = nil, _ = nil)
81
+ if condition
82
+ target
83
+ else
84
+ raise_error message
85
+ end
86
+ end
87
+ end
88
+
89
+ class Denial < Statement
90
+ def test(condition, _ = nil, message = nil)
91
+ if ! condition
92
+ target
93
+ else
94
+ raise_error message
95
+ end
96
+ end
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: covenant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Leal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &76572980 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.11'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *76572980
25
+ description:
26
+ email:
27
+ - gems@mojotech.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/covenant.rb
33
+ - lib/covenant/assertions/is.rb
34
+ - lib/covenant/assertions/is_a.rb
35
+ - lib/covenant/assertions/query.rb
36
+ - lib/covenant/version.rb
37
+ homepage: https://github.com/mojotech/covenant
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project: ! '[none]'
57
+ rubygems_version: 1.8.11
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Assertion library for Ruby
61
+ test_files: []
62
+ has_rdoc: