spectre-core 1.8.4 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ require_relative '../spectre'
2
+
1
3
  require 'ostruct'
2
4
  require_relative 'logger'
3
5
 
@@ -6,19 +8,19 @@ module Spectre
6
8
  module Assertion
7
9
  class ::Object
8
10
  def should_be(val)
9
- raise AssertionFailure.new("The value '#{self.to_s.trim}' should be '#{val.to_s.trim}'", val, self) unless self.to_s == val.to_s
11
+ raise AssertionFailure.new("The value '#{self.to_s.trim}' should be '#{val.to_s.trim}'", val, self) unless self.to_s == val.to_s
10
12
  end
11
13
 
12
14
  def should_be_empty
13
- raise AssertionFailure.new("The value '#{self.to_s.trim}' should be empty", nil, self) unless self == nil
15
+ raise AssertionFailure.new("The value '#{self.to_s.trim}' should be empty", nil, self) unless self.nil?
14
16
  end
15
17
 
16
18
  def should_not_be(val)
17
- raise AssertionFailure.new("The value '#{self.to_s.trim}' should not be '#{val.to_s.trim}'", val, self) unless self.to_s != val.to_s
19
+ raise AssertionFailure.new("The value '#{self.to_s.trim}' should not be '#{val.to_s.trim}'", val, self) unless self.to_s != val.to_s
18
20
  end
19
21
 
20
22
  def should_not_exist
21
- raise AssertionFailure.new("The value '#{self.to_s.trim}' should not exist, but it does", val, self) unless self.to_s != nil
23
+ raise AssertionFailure.new("The value '#{self.to_s.trim}' should not exist, but it does", val, self) unless self.to_s != nil
22
24
  end
23
25
 
24
26
  def should_not_be_empty
@@ -34,7 +36,6 @@ module Spectre
34
36
  end
35
37
  end
36
38
 
37
-
38
39
  class ::NilClass
39
40
  def should_be(val)
40
41
  raise AssertionFailure.new("There is nothing, but the value should be '#{val.to_s.trim}'", val, nil) unless val == nil
@@ -55,7 +56,6 @@ module Spectre
55
56
  end
56
57
  end
57
58
 
58
-
59
59
  class ::Hash
60
60
  def should_contain(other)
61
61
  raise AssertionFailure.new(other, self) unless self.merge(other) == self
@@ -64,8 +64,25 @@ module Spectre
64
64
  def should_not_contain(other)
65
65
  raise AssertionFailure.new(other, self) unless self.merge(other) != self
66
66
  end
67
+
68
+ def should_be_empty
69
+ raise AssertionFailure.new("The object should be empty", nil, self) unless self.empty?
70
+ end
71
+
72
+ def should_not_be_empty
73
+ raise AssertionFailure.new("The object should not be empty", nil, self) if self.empty?
74
+ end
67
75
  end
68
76
 
77
+ class ::OpenStruct
78
+ def should_be_empty
79
+ raise AssertionFailure.new("The object should be empty", nil, self) unless self.to_h.empty?
80
+ end
81
+
82
+ def should_not_be_empty
83
+ raise AssertionFailure.new("The object should not be empty", nil, self) if self.to_h.empty?
84
+ end
85
+ end
69
86
 
70
87
  class ::Array
71
88
  def should_contain(val)
@@ -76,7 +93,7 @@ module Spectre
76
93
  val = OpenStruct.new(val)
77
94
  end
78
95
 
79
- raise AssertionFailure.new("The list [#{list.join(', ').trim}] should contain '#{val.trim}'", val, list) unless list.include? val
96
+ raise AssertionFailure.new("The list [#{list.join(', ').trim}] should contain '#{val.to_s.trim}'", val, list) unless list.include? val
80
97
  end
81
98
 
82
99
  def should_not_contain(val)
@@ -87,19 +104,18 @@ module Spectre
87
104
  val = OpenStruct.new(val)
88
105
  end
89
106
 
90
- raise AssertionFailure.new("The list [#{list.join(', ').trim}] should not contain '#{val.trim}'", val, list) if list.include? val
107
+ raise AssertionFailure.new("The list [#{list.join(', ').trim}] should not contain '#{val.to_s.trim}'", val, list) if list.include? val
91
108
  end
92
109
 
93
110
  def should_be_empty
94
- raise AssertionFailure.new('empty list', self) unless self.length == 0
111
+ raise AssertionFailure.new('empty list', self) unless self.empty?
95
112
  end
96
113
 
97
114
  def should_not_be_empty
98
- raise AssertionFailure.new('no empty list', self) unless self.length > 0
115
+ raise AssertionFailure.new('no empty list', self) if self.empty?
99
116
  end
100
117
  end
101
118
 
102
-
103
119
  class ::String
104
120
  def should_be(val)
105
121
  raise AssertionFailure.new("The text '#{self.trim}' should be '#{val.to_s.trim}'", val, self) unless self == val
@@ -110,7 +126,7 @@ module Spectre
110
126
  end
111
127
 
112
128
  def should_not_be(val)
113
- raise AssertionFailure.new("The text '#{self.trim}' should not be '#{val.to_s.trim}'", val, self) unless self != val
129
+ raise AssertionFailure.new("The text '#{self.trim}' should not be '#{val.to_s.trim}'", val, self) unless self != val
114
130
  end
115
131
 
116
132
  def should_not_be_empty
@@ -118,8 +134,10 @@ module Spectre
118
134
  end
119
135
 
120
136
  def should_contain(value)
137
+ raise AssertionFailure.new("`value' must not be nil") if value.nil?
138
+
121
139
  predicate = proc { |x| self.include? x.to_s }
122
- evaluation = SingleEvaluation.new value
140
+ evaluation = SingleEvaluation.new(value)
123
141
  success = evaluation.call(predicate)
124
142
 
125
143
  return if success
@@ -143,7 +161,6 @@ module Spectre
143
161
  alias :& :and
144
162
  end
145
163
 
146
-
147
164
  class Evaluation
148
165
  def initialize value, other
149
166
  @value = value
@@ -162,10 +179,9 @@ module Spectre
162
179
  alias :& :and
163
180
  end
164
181
 
165
-
166
182
  class SingleEvaluation < Evaluation
167
183
  def initialize value
168
- super value, nil
184
+ super(value, nil)
169
185
  end
170
186
 
171
187
  def call predicate
@@ -177,10 +193,9 @@ module Spectre
177
193
  end
178
194
  end
179
195
 
180
-
181
196
  class OrEvaluation < Evaluation
182
197
  def initialize value, other
183
- super value, other
198
+ super(value, other)
184
199
  end
185
200
 
186
201
  def call predicate
@@ -192,10 +207,9 @@ module Spectre
192
207
  end
193
208
  end
194
209
 
195
-
196
210
  class AndEvaluation < Evaluation
197
211
  def initialize value, other
198
- super value, other
212
+ super(value, other)
199
213
  end
200
214
 
201
215
  def call predicate
@@ -207,7 +221,6 @@ module Spectre
207
221
  end
208
222
  end
209
223
 
210
-
211
224
  class AssertionFailure < ExpectationFailure
212
225
  attr_reader :expected, :actual
213
226
 
@@ -222,27 +235,16 @@ module Spectre
222
235
  class << self
223
236
  @@success = nil
224
237
 
225
- def eval_assertion predicate, val
226
- if val.is_a? Proc
227
- val.call(predicate)
228
- else
229
- predicate.call(val)
230
- end
231
- end
232
-
233
238
  def expect desc
234
239
  begin
235
240
  Logger.log_process("expect #{desc}")
236
241
  yield
237
242
  Logger.log_status(desc, Logger::Status::OK)
238
-
239
243
  rescue Interrupt => e
240
244
  raise e
241
-
242
245
  rescue AssertionFailure => e
243
246
  Logger.log_status(desc, Logger::Status::FAILED)
244
247
  raise AssertionFailure.new(e.message, e.expected, e.actual, desc), cause: nil
245
-
246
248
  rescue Exception => e
247
249
  Logger.log_status(desc, Logger::Status::ERROR)
248
250
  raise AssertionFailure.new("An unexpected error occured during expectation: #{e.message}", nil, nil, desc), cause: e
@@ -250,15 +252,21 @@ module Spectre
250
252
  end
251
253
 
252
254
  def observe desc = nil
255
+ prefix = "observing"
256
+ prefix += " '#{desc}'" if desc
257
+
253
258
  begin
254
- Logger.log_info("observing #{desc}") if desc
259
+ Logger.log_info(prefix) if desc
255
260
  yield
256
261
  @@success = true
257
-
262
+ @@logger.info("#{prefix} finished with success")
258
263
  rescue Interrupt => e
259
264
  raise e
260
-
261
265
  rescue Exception => e
266
+ error_message = "#{prefix} finished with failure: #{e.message}"
267
+ error_message += "\n" + e.backtrace.join("\n") if @@debug
268
+
269
+ @@logger.info(error_message)
262
270
  @@success = false
263
271
  end
264
272
  end
@@ -272,6 +280,11 @@ module Spectre
272
280
  end
273
281
  end
274
282
 
283
+ Spectre.register do |config|
284
+ @@logger = ::Logger.new(config['log_file'], progname: 'spectre/assertion')
285
+ @@debug = config['debug']
286
+ end
287
+
275
288
  Spectre.delegate :expect, :observe, :success?, :fail_with, to: self
276
289
  end
277
290
  end
data/lib/spectre/bag.rb CHANGED
@@ -1,19 +1,21 @@
1
- require 'ostruct'
2
-
3
- module Spectre
4
- module Bag
5
- class << self
6
- @@bag
7
-
8
- def bag
9
- @@bag
10
- end
11
- end
12
-
13
- Spectre.register do |config|
14
- @@bag = OpenStruct.new
15
- end
16
-
17
- Spectre.delegate :bag, to: Bag
18
- end
19
- end
1
+ require_relative '../spectre'
2
+
3
+ require 'ostruct'
4
+
5
+ module Spectre
6
+ module Bag
7
+ class << self
8
+ @@bag
9
+
10
+ def bag
11
+ @@bag
12
+ end
13
+ end
14
+
15
+ Spectre.register do |config|
16
+ @@bag = OpenStruct.new
17
+ end
18
+
19
+ Spectre.delegate :bag, to: self
20
+ end
21
+ end