Module: RSpecMagic::Stable::ContextWhen::Exports

Defined in:
lib/rspec_magic/stable/context_when.rb

Instance Method Summary collapse

Instance Method Details

#_context_when_formatter(h) ⇒ String

Default formatter for #context_when. Defined your custom one at the describe level if needed.

Parameters:

  • h (Hash)

Returns:

  • (String)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rspec_magic/stable/context_when.rb', line 50

def _context_when_formatter(h)
  # Extract labels for Proc arguments, if any.
  labels = {}
  h.each do |k, v|
    if v.is_a? Proc
      begin
        labels[k] = h.fetch(lk = "#{k}_label".to_sym)
        h.delete(lk)
      rescue KeyError
        raise ArgumentError, "`#{k}` is a `Proc`, `#{k}_label` must be given"
      end
    end
  end

  pcs = h.map do |k, v|
    [
      k.is_a?(Symbol) ? "#{k}:" : "#{k.inspect} =>",
      v.is_a?(Proc) ? labels[k] : v.inspect,
    ].join(" ")
  end

  "when { " + pcs.join(", ") + " }"
end

#context_when(h, &block) ⇒ void

Create a context.

Parameters:

  • h (Hash)


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rspec_magic/stable/context_when.rb', line 76

def context_when(h, &block)
  context _context_when_formatter(h) do
    h.each do |k, v|
      if v.is_a? Proc
        let(k, &v)
      else
        # Generic scalar value.
        let(k) { v }
      end
    end
    class_eval(&block)
  end
end

#xcontext_when(h, &block) ⇒ void

Create a temporarily excluded context.



91
92
93
# File 'lib/rspec_magic/stable/context_when.rb', line 91

def xcontext_when(h, &block)
  xcontext _context_when_formatter(h) { class_eval(&block) }
end