covenant 0.0.1 → 0.6.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.
@@ -1,3 +1,3 @@
1
1
  module Covenant
2
- VERSION = "0.0.1"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/covenant.rb CHANGED
@@ -1,7 +1,3 @@
1
- require 'covenant/assertions/is_a'
2
- require 'covenant/assertions/is'
3
- require 'covenant/assertions/query'
4
-
5
1
  module Covenant
6
2
  # Adds the Covenant DSL to base.
7
3
  #
@@ -34,6 +30,8 @@ module Covenant
34
30
  end
35
31
  end
36
32
 
33
+ alias_method :asserting, :assert
34
+
37
35
  # Ensures that the condition on target evaluates to a false value.
38
36
  #
39
37
  # @api public
@@ -49,16 +47,48 @@ module Covenant
49
47
  Covenant::Denial.new(target, message)
50
48
  end
51
49
  end
50
+
51
+ alias_method :denying, :deny
52
52
  end
53
53
 
54
54
  class AssertionFailed < Exception; end
55
55
 
56
- class Statement
57
- include Assertions
58
-
56
+ class Statement < BasicObject
59
57
  def initialize(target, message)
60
- @target = target
61
- @message = message
58
+ @target = target
59
+ @message = message
60
+ end
61
+
62
+ def ==(other)
63
+ test(target == other, ErrorMessage.new(target, :==, [other]))
64
+ end
65
+
66
+ def !=(other)
67
+ test(target != other, ErrorMessage.new(target, :!=, [other]))
68
+ end
69
+
70
+ def method_missing(name, *args)
71
+ if target.respond_to?(name)
72
+ return test(target.send(name, *args),
73
+ ErrorMessage.new(target, name, args))
74
+ end
75
+
76
+ query = "#{name}?"
77
+
78
+ if target.respond_to?(query)
79
+ return test(target.send(query, *args),
80
+ ErrorMessage.new(target, query, args))
81
+ end
82
+
83
+ no_is = name.to_s.sub(/^is_/, '')
84
+ is_query = "#{no_is}?"
85
+
86
+ if target.respond_to?(is_query)
87
+ return test(target.send(is_query, *args),
88
+ ErrorMessage.new(target, is_query, args))
89
+ end
90
+
91
+ super
62
92
  end
63
93
 
64
94
  protected
@@ -69,30 +99,74 @@ module Covenant
69
99
  msg = self.message || message
70
100
 
71
101
  if msg
72
- raise AssertionFailed, msg
102
+ ::Kernel.raise AssertionFailed, msg
73
103
  else
74
- raise AssertionFailed
104
+ ::Kernel.raise AssertionFailed
105
+ end
106
+ end
107
+
108
+ class NullErrorMessage
109
+ def for_assertion; end
110
+ def for_denial; end
111
+ end
112
+
113
+ class ErrorMessage
114
+ attr_reader :target, :message, :args
115
+
116
+ def initialize(target, message, args)
117
+ @target = target
118
+ @message = message
119
+ @args = args
120
+ end
121
+
122
+ def for_assertion
123
+ error_message('should be true')
124
+ end
125
+
126
+ def for_denial
127
+ error_message('should be false')
128
+ end
129
+
130
+ private
131
+
132
+ def error_message(expected)
133
+ argl = args.map(&:inspect).join(", ")
134
+
135
+ "#{target.inspect}.#{message} #{argl} #{expected}"
75
136
  end
76
137
  end
77
138
  end
78
139
 
79
140
  class Assertion < Statement
80
- def test(condition, message = nil, _ = nil)
141
+ def test(condition, error_message = NullErrorMessage.new)
81
142
  if condition
82
143
  target
83
144
  else
84
- raise_error message
145
+ raise_error error_message.for_assertion
85
146
  end
86
147
  end
87
148
  end
88
149
 
89
150
  class Denial < Statement
90
- def test(condition, _ = nil, message = nil)
151
+ def test(condition, error_message = NullErrorMessage.new)
91
152
  if ! condition
92
153
  target
93
154
  else
94
- raise_error message
155
+ raise_error error_message.for_denial
95
156
  end
96
157
  end
97
158
  end
98
159
  end
160
+
161
+ =begin
162
+
163
+ require 'benchmark'
164
+
165
+ Covenant.abide self
166
+
167
+ Benchmark.bm do |x|
168
+ x.report { 1_000_000.times { assert('aaa').start_with 'a' } }
169
+ x.report { 1_000_000.times { 'aaa'.start_with? 'a' } }
170
+ end
171
+
172
+ =end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: covenant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-29 00:00:00.000000000 Z
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &76572980 !ruby/object:Gem::Requirement
16
+ requirement: &86482920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '2.11'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *76572980
24
+ version_requirements: *86482920
25
25
  description:
26
26
  email:
27
27
  - gems@mojotech.com
@@ -30,9 +30,6 @@ extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
32
  - lib/covenant.rb
33
- - lib/covenant/assertions/is.rb
34
- - lib/covenant/assertions/is_a.rb
35
- - lib/covenant/assertions/query.rb
36
33
  - lib/covenant/version.rb
37
34
  homepage: https://github.com/mojotech/covenant
38
35
  licenses: []
@@ -1,21 +0,0 @@
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
@@ -1,18 +0,0 @@
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
@@ -1,20 +0,0 @@
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