Module: NRSER::RSpex::ExampleGroup

Defined in:
lib/nrser/rspex.rb

Overview

Instance methods to extend example groups with.

Instance Method Summary collapse

Instance Method Details

#context_where(description = nil, **bindings, &body) ⇒ Object

Define a ‘context` block with `let` bindings and evaluate the `body` block in it.

Parameters:

  • **bindings (Hash<Symbol, Object>)

    Map of symbol names to value to bind using ‘let`.

  • &body (#call)

    Body block to evaluate in the context.

Returns:

  • Whatever ‘context` returns.



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/nrser/rspex.rb', line 523

def context_where description = nil, **bindings, &body
  
  if description.nil?
    description = bindings.map { |name, value|
      "#{ name }: #{ NRSER::RSpex.short_s value }"
    }.join( ", " )
  end
  
  context "△ #{ description }", type: :where do
    bindings.each { |name, value|
      let( name ) { unwrap value, context: self }
    }
    
    instance_exec &body
  end
end

#describe_attribute(symbol, **metadata, &block) ⇒ Object Also known as: describe_attr



496
497
498
499
500
501
502
503
504
505
# File 'lib/nrser/rspex.rb', line 496

def describe_attribute symbol, **, &block
  describe(
    "#{ NRSER::RSpex::PREFIXES[:attribute] } ##{ symbol }",
    type: :attribute,
    **
  ) do
    subject { super().public_send symbol }
    instance_exec &block
  end
end

#describe_called_with(*args, &body) ⇒ Object Also known as: called_with, when_called_with

Create a new RSpec.describe section where the subject is set by calling the parent subject with ‘args` and evaluate `block` in it.

Examples:

describe "hi sayer" do
  subject{ ->( name ) { "Hi #{ name }!" } }

  describe_called_with 'Mom' do
    it { is_expected.to eq 'Hi Mom!' }
  end
end

Parameters:

  • *args (Array)

    Arguments to call ‘subject` with to produce the new subject.

  • &block (#call)

    Block to execute in the context of the example group after refining the subject.



314
315
316
317
318
319
# File 'lib/nrser/rspex.rb', line 314

def describe_called_with *args, &body
  describe_x_type  "called with", List(*args),
    type: :invocation,
    subject_block: -> { super().call *args },
    &body
end

#describe_class(klass, bind_subject: true, **metadata, &block) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/nrser/rspex.rb', line 446

def describe_class klass, bind_subject: true, **, &block
  description = "#{ NRSER::RSpex::PREFIXES[:class] } #{ klass.name }"
  
  describe(
    description,
    type: :class,
    class: klass,
    **
  ) do
    if bind_subject
      subject { klass }
    end
    
    instance_exec &block
  end
end

#describe_file(path, **metadata, &body) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/nrser/rspex.rb', line 421

def describe_file path, **, &body
  title = path
  
  describe(
    "#{ NRSER::RSpex::PREFIXES[:file] } #{ title }",
    type: :file,
    file: path,
    **
  ) do
    instance_exec &body
  end
end

#describe_group(title, **metadata, &block) ⇒ Object



469
470
471
472
473
474
475
476
477
# File 'lib/nrser/rspex.rb', line 469

def describe_group title, **, &block
  describe(
    "#{ NRSER::RSpex::PREFIXES[:group] } #{ title }",
    type: :group,
    **
  ) do
    instance_exec &block
  end
end

#describe_instance(*constructor_args, &body) ⇒ return_type

TODO:

Document describe_instance method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



283
284
285
286
287
288
289
290
291
292
# File 'lib/nrser/rspex.rb', line 283

def describe_instance *constructor_args, &body
  describe_x_type ".new(", Args(*constructor_args), ")",
    type: :instance,
    metadata: {
      constructor_args: constructor_args,
    },
    # subject_block: -> { super().new *described_args },
    subject_block: -> { super().new *described_constructor_args },
    &body
end

#describe_message(symbol, *args, &body) ⇒ Object



330
331
332
333
334
335
336
337
338
# File 'lib/nrser/rspex.rb', line 330

def describe_message symbol, *args, &body
  description = \
    "message #{ [symbol, *args].map( &NRSER::RSpex.method( :short_s ) ).join( ', ' ) }"
  
  describe description, type: :message do
    subject { NRSER::Message.new symbol, *args }
    instance_exec &body
  end
end

#describe_method(name, **metadata, &block) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/nrser/rspex.rb', line 480

def describe_method name, **, &block
  describe(
    "#{ NRSER::RSpex::PREFIXES[:method] } #{ name }",
    type: :method,
    method_name: name,
    **
  ) do
    if name.is_a? Symbol
      subject { super().method name }
    end
    
    instance_exec &block
  end
end

#describe_module(mod, **metadata, &block) ⇒ Object



435
436
437
438
439
440
441
442
443
# File 'lib/nrser/rspex.rb', line 435

def describe_module mod, **, &block
  describe(
    "#{ NRSER::RSpex::PREFIXES[:module] } #{ mod.name }",
    type: :module,
    **
  ) do
    instance_exec &block
  end
end

#describe_return_value(*args, &body) ⇒ Object



377
378
379
380
381
382
383
384
# File 'lib/nrser/rspex.rb', line 377

def describe_return_value *args, &body
  msg = NRSER::Message.from *args
  
  describe "return value from #{ msg }" do
    subject { msg.send_to super() }
    instance_exec &body
  end # "return value from #{ msg }"
end

#describe_section(title, **metadata, &block) ⇒ Object Also known as: describe_topic

Describe a “section”. Just like RSpec.describe except it:

  1. Expects a string title.

  2. Prepends a little section squiggle ‘§` to the title so sections are easier to pick out visually.

  3. Adds ‘type: :section` metadata.

Parameters:

Returns:

  • Whatever RSpec.describe returns.



407
408
409
410
411
412
413
414
415
# File 'lib/nrser/rspex.rb', line 407

def describe_section title, **, &block
  describe(
    "#{ NRSER::RSpex::PREFIXES[:section] } #{ title }",
    type: :section,
    **
  ) do
    instance_exec &block
  end
end

#describe_sent_to(receiver, publicly: true, &block) ⇒ Object Also known as: sent_to, when_sent_to

For use when ‘subject` is a Message. Create a new context for the `receiver` where the subject is the result of sending that message to the receiver.

Parameters:

  • receiver (Object)

    Object that will receive the message to create the new subject.

  • publicly: (Boolean) (defaults to: true)

    Send message publicly via Object#public_send (default) or privately

    via Object.send.

Returns:

  • Whatever the ‘context` call returns.



355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/nrser/rspex.rb', line 355

def describe_sent_to receiver, publicly: true, &block
  mode = if publicly
    "publicly"
  else
    "privately"
  end
  
  describe "sent to #{ receiver } (#{ mode })" do
    subject { super().send_to unwrap( receiver, context: self ) }
    instance_exec &block
  end
end

#describe_x_type(*description_parts, type:, metadata: {}, subject_block: nil, &body) ⇒ return_type

TODO:

Document describe_x method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/nrser/rspex.rb', line 259

def describe_x_type *description_parts,
                    type:,
                    metadata: {},
                    subject_block: nil,
                    &body
  
  description = NRSER::RSpex.format *description_parts, type: type
  
  describe description, **, type: type do
    subject( &subject_block ) if subject_block
    instance_exec &body
  end # description, 
  
end

#described_classObject



464
465
466
# File 'lib/nrser/rspex.rb', line 464

def described_class
  [:class] || super()
end