Class: Loupe::Expectation

Inherits:
Object
  • Object
show all
Defined in:
lib/loupe/expectation.rb

Overview

Expectation

This class is responsible for instantiating a target so that expectations can be invoked on it.

Example:

expectation = Expectation.new(target, test)
expectation.to_be_empty

The goal of ths class is so that expectations can be written in such a way that they read in plain English. E.g.: expect(something).to_be_truthy

See Also:

  • Test#expect

Defined Under Namespace

Classes: ExpectationFailed

Instance Method Summary collapse

Constructor Details

#initialize(target, test) ⇒ Loupe::Expectation

Parameters:



69
70
71
72
73
# File 'lib/loupe/expectation.rb', line 69

def initialize(target, test)
  @target = target
  @color = test.color
  @test = test
end

Instance Method Details

#to_be(predicate) ⇒ Loupe::Expectation

expect(target).to_be(predicate)

Expects target to return true for a given predicate. In this expectation, predicate is a method name and the expectation will pass if invoking the method on target returns true.

Example:

Verify if the result of a calculation is an odd number. The `target` is the calculation result
and the `predicate` is the method `odd?`.

calculation_result = a_complex_math_operation
expect(calculation_result).to_be(:odd?)

Parameters:

  • predicate (String, Symbol)

Returns:



281
282
283
284
285
286
# File 'lib/loupe/expectation.rb', line 281

def to_be(predicate)
  assert(
    @target.public_send(predicate),
    "Expected #{@color.p(@target.inspect, :red)} to be #{@color.p(predicate, :green)}."
  )
end

#to_be_a_kind_of(klass) ⇒ Loupe::Expectation

expect(target).to_be_a_kind_of(klass)

Expects target to be a kind of klass. Which means that if target.is_a?(klass) returns true, the expectation will pass.

Parameters:

  • klass (Class)

Returns:



246
247
248
249
250
251
# File 'lib/loupe/expectation.rb', line 246

def to_be_a_kind_of(klass)
  failure_message = "Expected #{@color.p(@target.inspect, :red)} to be a kind of " \
                    "#{@color.p(klass, :green)}, not #{@color.p(@target.inspect.class, :red)}."

  assert(@target.is_a?(klass), failure_message)
end

#to_be_an_existing_path ⇒ Loupe::Expectation

expect(target).to_be_an_existing_path

Expects target to be an existing path in the file system. That means, invoking File.exist?(target) must return true for this expectation to pass.

Returns:



384
385
386
# File 'lib/loupe/expectation.rb', line 384

def to_be_an_existing_path
  assert(File.exist?(@target), "Expected path '#{@color.p(@target.inspect, :red)}' to exist.")
end

#to_be_an_instance_of(klass) ⇒ Loupe::Expectation

expect(target).to_be_an_instance_of(klass)

Expects target to be an instance of klass. For example, "ruby is awesome" is an instance of String and will pass the expectation.

Parameters:

  • klass (Class)

Returns:



218
219
220
221
222
223
# File 'lib/loupe/expectation.rb', line 218

def to_be_an_instance_of(klass)
  failure_message = "Expected #{@color.p(@target.inspect, :red)} to be an instance " \
                    "of #{@color.p(klass, :green)}, not #{@color.p(@target.inspect.class, :red)}."

  assert(@target.instance_of?(klass), failure_message)
end

#to_be_empty ⇒ Loupe::Expectation

expect(target).to_be_empty

Expects target to be empty. Which means invoking empty? must return true.

Returns:



126
127
128
# File 'lib/loupe/expectation.rb', line 126

def to_be_empty
  assert(@target.empty?, "Expected #{@color.p(@target.inspect, :red)} to be empty.")
end

#to_be_equal_to(value) ⇒ Loupe::Expectation

expect(target).to_be_equal_to(value)

Expects target to be equal to value. Compares if the objects are equal in terms of values but not if the objects are the exact same instance. For comparing identity, use #to_be_the_same_as.

Parameters:

  • value (#==)

Returns:



100
101
102
103
104
105
# File 'lib/loupe/expectation.rb', line 100

def to_be_equal_to(value)
  assert(
    @target == value,
    "Expected #{@color.p(@target.inspect, :red)} to be equal to #{@color.p(value.inspect, :green)}."
  )
end

#to_be_falsey ⇒ Loupe::Expectation

expect(target).to_be_falsey

Expects target to be a falsey value (nil or false).

Returns:



89
90
91
# File 'lib/loupe/expectation.rb', line 89

def to_be_falsey
  assert(!@target, "Expected #{@color.p(@target.inspect, :red)} to be #{@color.p('falsey', :green)}.")
end

#to_be_in_delta_of(value, delta = 0.001) ⇒ Loupe::Expectation

expect(target).to_be_in_delta_of(value, delta = 0.001)

Expects target to be within delta of value. This means that the absolute difference between target and value cannot be bigger than delta.

Example:

expect(5.0).to_be_in_delta_of(5.1, 0.2)

Parameters:

  • value (Numeric)
  • delta (Numeric) (defaults to: 0.001)

Returns:



409
410
411
412
413
414
415
416
# File 'lib/loupe/expectation.rb', line 409

def to_be_in_delta_of(value, delta = 0.001)
  difference = (@target - value).abs

  failure_message = "Expected |#{@target} - #{value}| " \
                    "(#{@color.p(difference, :red)}) to be <= #{@color.p(delta, :green)}."

  assert(delta >= difference, failure_message)
end

#to_be_in_epsilon_of(value, epsilon = 0.001) ⇒ Loupe::Expectation

expect(target).to_be_in_epsilon_of(value, epsilon = 0.001)

Expects target to be within epsilon times the smallest absolute value between value and target. This expectation simply invokes #to_be_in_delta_of, where the delta is epsilon * [@target.abs, value.abs].min.

Example:

The minimum absolute value between 5.0 and 5.1 is equal to 5.0.
The delta is equal to 5.0 * 0.1, which is 0.5.
The absolute difference 5.1 - 5.0 is 0.1.
0.1 is smaller than 0.5, therefore the expectation passes.

expect(5.0).to_be_in_epsilon_of(5.1, 0.1)

Parameters:

  • value (Numeric)
  • epsilon (Numeric) (defaults to: 0.001)

Returns:



455
456
457
# File 'lib/loupe/expectation.rb', line 455

def to_be_in_epsilon_of(value, epsilon = 0.001)
  to_be_in_delta_of(value, [@target.abs, value.abs].min * epsilon)
end

#to_be_nil ⇒ Loupe::Expectation

expect(target).to_be_nil

Expects target to be nil.

Returns:



198
199
200
# File 'lib/loupe/expectation.rb', line 198

def to_be_nil
  assert(@target.nil?, "Expected #{@color.p(@target.inspect, :red)} to be nil.")
end

#to_be_the_same_as(object) ⇒ Loupe::Expectation

expect(target).to_be_the_same_as(object)

Expects target to be the same as object. This means, target and object must be the exact same object, with the same object_id.

Parameters:

  • object (BasicObject)

Returns:



354
355
356
357
358
359
# File 'lib/loupe/expectation.rb', line 354

def to_be_the_same_as(object)
  failure_message = "Expected #{@color.p(@target.inspect, :red)} (#{@color.p(@target.inspect.object_id, :red)}) " \
                    "to be the same as #{@color.p(object, :green)} (#{@color.p(object.object_id, :green)})."

  assert(@target.equal?(object), failure_message)
end

#to_be_truthy ⇒ Loupe::Expectation

expect(target).to_be_truthy

Expects target to be a truthy value (not nil or false).

Returns:



80
81
82
# File 'lib/loupe/expectation.rb', line 80

def to_be_truthy
  assert(@target, "Expected #{@color.p(@target.inspect, :red)} to be #{@color.p('truthy', :green)}.")
end

#to_include(object) ⇒ Loupe::Expectation

expect(target).to_include(object)

Expects target to include object. In this expectation, target is typically a collection or another object that responds to include?. The expectation passes if target includes object.

Parameters:

  • object (BasicObject)

Returns:



172
173
174
175
176
177
# File 'lib/loupe/expectation.rb', line 172

def to_include(object)
  assert(
    @target.include?(object),
    "Expected #{@color.p(@target.inspect, :red)} to include #{@color.p(object, :green)}."
  )
end

#to_match(object) ⇒ Loupe::Expectation

expect(target).to_match(object)

Expects target to match object. In this expectation, target is a matcher (string or a regex) that must match object. The matcher needs to respond to =~ and needs to return true when it is invoked with object.

Example:

expect(/ruby .*/).to_match("ruby is awesome")
expect("awesome").to_match("ruby is awesome")

Parameters:

  • object (BasicObject)

Returns:



321
322
323
324
325
326
# File 'lib/loupe/expectation.rb', line 321

def to_match(object)
  to_respond_to(:=~)

  @target = Regexp.new(Regexp.escape(@target)) if @target.is_a?(String)
  assert(@target =~ object, "Expected #{@color.p(@target.inspect, :red)} to match #{@color.p(object, :green)}.")
end

#to_not_be(predicate) ⇒ Loupe::Expectation

expect(target).to_not_be(predicate)

Expects target to return false for a given predicate. In this expectation, predicate is a method name and the expectation will pass if invoking the method on target returns false.

Example:

Verify if the result of a calculation is not zero. The `target` is the calculation result
and the `predicate` is the method `zero?`.

calculation_result = a_complex_math_operation
expect(calculation_result).to_not_be(:zero?)

Parameters:

  • predicate (String, Symbol)

Returns:



302
303
304
305
306
307
# File 'lib/loupe/expectation.rb', line 302

def to_not_be(predicate)
  assert(
    !@target.public_send(predicate),
    "Expected #{@color.p(@target.inspect, :red)} to not be #{@color.p(predicate, :green)}."
  )
end

#to_not_be_a_kind_of(klass) ⇒ Loupe::Expectation

expect(target).to_not_be_a_kind_of(klass)

Expects target to not be a kind of klass. Which means that if target.is_a?(klass) returns false, the expectation will pass.

Parameters:

  • klass (Class)

Returns:



260
261
262
263
264
265
# File 'lib/loupe/expectation.rb', line 260

def to_not_be_a_kind_of(klass)
  failure_message = "Expected #{@color.p(@target.inspect, :red)} to not be a kind of " \
                    "#{@color.p(klass, :green)}, not #{@color.p(@target.inspect.class, :red)}."

  assert(!@target.is_a?(klass), failure_message)
end

#to_not_be_an_existing_path ⇒ Loupe::Expectation

expect(target).to_not_be_an_existing_path

Expects target to not be an existing path in the file system. That means, invoking File.exist?(target) must return false for this expectation to pass.

Returns:



394
395
396
# File 'lib/loupe/expectation.rb', line 394

def to_not_be_an_existing_path
  assert(!File.exist?(@target), "Expected path '#{@color.p(@target.inspect, :red)}' to not exist.")
end

#to_not_be_an_instance_of(klass) ⇒ Loupe::Expectation

expect(target).to_not_be_an_instance_of(klass)

Expects target to not be an instance of klass. For example, "ruby is awesome" is not an instance of Integer and will not pass the expectation.

Parameters:

  • klass (Class)

Returns:



232
233
234
235
236
237
# File 'lib/loupe/expectation.rb', line 232

def to_not_be_an_instance_of(klass)
  failure_message = "Expected #{@color.p(@target.inspect, :red)} to not be an instance of " \
                    "#{@color.p(klass, :green)}, not #{@color.p(@target.inspect.class, :red)}."

  assert(!@target.instance_of?(klass), failure_message)
end

#to_not_be_empty ⇒ Loupe::Expectation

expect(target).to_not_be_empty

Expects target to not be empty. Which means invoking empty? must return false.

Returns:



135
136
137
# File 'lib/loupe/expectation.rb', line 135

def to_not_be_empty
  assert(!@target.empty?, "Expected #{@color.p(@target.inspect, :red)} to not be empty.")
end

#to_not_be_equal_to(value) ⇒ Loupe::Expectation

expect(target).to_not_be_equal_to(value)

Expects target to not be equal to value. Compares if the objects are different in terms of values, but not if the objects are the different instances. For comparing identity, use #to_not_be_the_same_as.

Parameters:

  • value (#!=)

Returns:



114
115
116
117
118
119
# File 'lib/loupe/expectation.rb', line 114

def to_not_be_equal_to(value)
  assert(
    @target != value,
    "Expected #{@color.p(@target.inspect, :red)} to not be equal to #{@color.p(value.inspect, :green)}."
  )
end

#to_not_be_in_delta_of(value, delta = 0.001) ⇒ Loupe::Expectation

expect(target).to_not_be_in_delta_of(value, delta = 0.001)

Expects target to not be within delta of value. This means that the absolute difference between target and value must be bigger than delta.

Example:

expect(5.0).to_not_be_in_delta_of(5.1, 0.01)

Parameters:

  • value (Numeric)
  • delta (Numeric) (defaults to: 0.001)

Returns:



429
430
431
432
433
434
435
436
# File 'lib/loupe/expectation.rb', line 429

def to_not_be_in_delta_of(value, delta = 0.001)
  difference = (@target - value).abs

  failure_message = "Expected |#{@target} - #{value}| " \
                    "(#{@color.p(difference, :red)}) to not be <= #{@color.p(delta, :green)}."

  assert(delta <= difference, failure_message)
end

#to_not_be_in_epsilon_of(value, epsilon = 0.001) ⇒ Loupe::Expectation

expect(target).to_not_be_in_epsilon_of(value, epsilon = 0.001)

Expects target to not be within epsilon times the smallest absolute value between value and target. This expectation simply invokes #to_not_be_in_delta_of, where the delta is epsilon * [@target.abs, value.abs].min.

Example:

The minimum absolute value between 5.0 and 5.1 is equal to 5.0.
The delta is equal to 5.0 * 0.01, which is 0.05.
The absolute difference 5.1 - 5.0 is 0.1.
0.1 is not smaller than 0.05, therefore the expectation passes.

expect(5.0).to_not_be_in_epsilon_of(5.1, 0.01)

Parameters:

  • value (Numeric)
  • epsilon (Numeric) (defaults to: 0.001)

Returns:



476
477
478
# File 'lib/loupe/expectation.rb', line 476

def to_not_be_in_epsilon_of(value, epsilon = 0.001)
  to_not_be_in_delta_of(value, [@target.abs, value.abs].min * epsilon)
end

#to_not_be_nil ⇒ Loupe::Expectation

expect(target).to_not_be_nil

Expects target not to be nil.

Returns:



207
208
209
# File 'lib/loupe/expectation.rb', line 207

def to_not_be_nil
  assert(!@target.nil?, "Expected #{@color.p(@target.inspect, :red)} to not be nil.")
end

#to_not_be_the_same_as(object) ⇒ Loupe::Expectation

expect(target).to_not_be_the_same_as(object)

Expects target to not be the same as object. This means, target and object are not the exact same object with the same object_id.

Note: this is an identity comparison. If the values of both objects are the same, but the object IDs are different, this expectation will pass.

Parameters:

  • object (BasicObject)

Returns:



371
372
373
374
375
376
# File 'lib/loupe/expectation.rb', line 371

def to_not_be_the_same_as(object)
  failure_message = "Expected #{@color.p(@target.inspect, :red)} (#{@color.p(@target.inspect.object_id, :red)}) " \
                    "to not be the same as #{@color.p(object, :green)} (#{@color.p(object.object_id, :green)})."

  assert(!@target.equal?(object), failure_message)
end

#to_not_include(object) ⇒ Loupe::Expectation

expect(target).to_not_include(object)

Expects target to not include object. In this expectation, target is typically a collection or another object that responds to include?. The expectation passes if target does not include object.

Parameters:

  • object (BasicObject)

Returns:



186
187
188
189
190
191
# File 'lib/loupe/expectation.rb', line 186

def to_not_include(object)
  assert(
    !@target.include?(object),
    "Expected #{@color.p(@target.inspect, :red)} to not include #{@color.p(object, :green)}."
  )
end

#to_not_match(object) ⇒ Loupe::Expectation

expect(target).to_not_match(object)

Expects target to not match object. In this expectation, target is a matcher (string or a regex) that must not match object. The matcher needs to respond to =~ and needs to return false when it is invoked with object.

Example:

expect(/python .*/).to_not_match("ruby is awesome")
expect("terrible").to_not_match("ruby is awesome")

Parameters:

  • object (BasicObject)

Returns:



340
341
342
343
344
345
# File 'lib/loupe/expectation.rb', line 340

def to_not_match(object)
  to_respond_to(:=~)

  @target = Regexp.new(Regexp.escape(@target)) if @target.is_a?(String)
  assert(@target !~ object, "Expected #{@color.p(@target.inspect, :red)} to not match #{@color.p(object, :green)}.")
end

#to_not_respond_to(method) ⇒ Loupe::Expectation

expect(target).to_not_respond_to(method)

Expects target to not respond to method. This expectation passes if method does not exist in target.

Parameters:

  • method (String, Symbol)

Returns:



158
159
160
161
162
163
# File 'lib/loupe/expectation.rb', line 158

def to_not_respond_to(method)
  assert(
    !@target.respond_to?(method.to_sym),
    "Expected #{@color.p(@target.inspect, :red)} to not respond to #{@color.p(method, :green)}."
  )
end

#to_not_satisfy_operator(operator, other) ⇒ Loupe::Expectation

expect(target).to_not_satisfy_operator(operator, other)

Expects target to return false when the operator is applied on other. This expectation is used to verify operations between two objects.

Example:

expect(5.0).to_not_satisfy_operator(:<, 4.9)

Parameters:

  • operator (Symbol)
  • other (BasicObject)

Returns:



513
514
515
516
517
518
519
520
# File 'lib/loupe/expectation.rb', line 513

def to_not_satisfy_operator(operator, other)
  return to_not_be(operator) unless other

  failure_message = "Expected #{@color.p(@target.inspect, :red)} to not be #{operator}" \
                    " #{@color.p(other, :green)}."

  assert(!@target.public_send(operator, other), failure_message)
end

#to_respond_to(method) ⇒ Loupe::Expectation

expect(target).to_respond_to(method)

Expects target to respond to method. This expectation passes if method exists in target.

Parameters:

  • method (String, Symbol)

Returns:



145
146
147
148
149
150
# File 'lib/loupe/expectation.rb', line 145

def to_respond_to(method)
  assert(
    @target.respond_to?(method.to_sym),
    "Expected #{@color.p(@target.inspect, :red)} to respond to #{@color.p(method, :green)}."
  )
end

#to_satisfy_operator(operator, other) ⇒ Loupe::Expectation

expect(target).to_satisfy_operator(operator, other)

Expects target to return true when the operator is applied on other. This expectation is used to verify operations between two objects.

Example:

expect(5.0).to_satisfy_operator(:>, 4.9)

Parameters:

  • operator (Symbol)
  • other (BasicObject)

Returns:



492
493
494
495
496
497
498
499
# File 'lib/loupe/expectation.rb', line 492

def to_satisfy_operator(operator, other)
  return to_be(operator) unless other

  failure_message = "Expected #{@color.p(@target.inspect, :red)} to be #{operator}" \
                    " #{@color.p(other, :green)}."

  assert(@target.public_send(operator, other), failure_message)
end