module RSpec::Core::MemoizedHelpers::ClassMethods

Public Instance Methods

block_context(name, description = nil, &block)
Alias for: set_block
create_params(*array) click to toggle source
# File lib/rspecz/lets.rb, line 11
def create_params(*array)
  __each_if_method_not_defined(array) do |val|
    let(val) { "test-#{val}" }
  end
  let(:with_nil) { false } unless __lib_method_defined?(:with_nil)
  let(:params) {
    array.each_with_object({}) do |elem, acc|
      acc[elem] = send(elem) if send(:with_nil) || !send(elem).nil?
    end
  }
end
fwith(name = nil, *values, &block) click to toggle source
# File lib/rspecz/with.rb, line 135
def fwith(name = nil, *values, &block) _with(name, nil, true, values, block); end
fwith_invalid(name = nil, *values, &block) click to toggle source
# File lib/rspecz/with.rb, line 146
def fwith_invalid(name = nil, *values, &block) _with(name, 'invalid', true, values, block); end
Also aliased as: fwith_invalids
fwith_invalids(name = nil, *values, &block)
Alias for: fwith_invalid
fwith_missing(name = nil, *values, &block) click to toggle source
# File lib/rspecz/with.rb, line 152
def fwith_missing(name = nil, *values, &block) _with(name, 'missing',true, values, block); end
Also aliased as: fwith_missings
fwith_missings(name = nil, *values, &block)
Alias for: fwith_missing
fwith_nil(*names) click to toggle source
# File lib/rspecz/with.rb, line 158
def fwith_nil(*names) _with_nil(names, true); end
Also aliased as: fwith_nils
fwith_nils(*names)
Alias for: fwith_nil
fwith_valid(name = nil, *values, &block) click to toggle source
# File lib/rspecz/with.rb, line 140
def fwith_valid(name = nil, *values, &block) _with(name, 'valid', true, values, block); end
Also aliased as: fwith_valids
fwith_valids(name = nil, *values, &block)
Alias for: fwith_valid
invalid_context(name, value = 'invalid-value', description = nil, &block)
Alias for: set_invalid
invalid_contexts(name, *values, &block)
Alias for: set_invalids
multi_behave(*examples) click to toggle source
# File lib/rspecz/behave.rb, line 5
def multi_behave(*examples)
  examples.each do |example|
    it_behaves_like example
  end
end
nil_context(name, description = nil, &block)
Alias for: set_nil
nil_contexts(*names, &block)
Alias for: set_nils
nonexist_context(name, value = 'missing-value', description = nil, &block)
Alias for: set_missing
set(name, value, description = nil, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 23
def set(name, value, description = nil, &block)
  context description || "when #{name} is #{value}" do
    let(name) { value }
    instance_exec(name, value, &block)
  end
end
Also aliased as: value_context
set_block(name, description = nil, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 60
def set_block(name, description = nil, &block)
  continue_object = { name: name, descrioption: description, block: block, myobject: self }
  def continue_object.spec(&block)
    continue_object = self
    self[:myobject].context self[:description] || "when #{self[:name]} is different" do
      let(continue_object[:name]) { instance_eval(&continue_object[:block]) }
      instance_exec(name, &block)
    end
  end
  continue_object
end
Also aliased as: block_context
set_context(name, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 81
def set_context(name, &block)
  context "when include context(#{name})" do
    include_context name
    instance_eval(&block)
  end
end
Also aliased as: with_context
set_invalid(name, value = 'invalid-value', description = nil, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 45
def set_invalid(name, value = 'invalid-value', description = nil, &block)
  context description || "when #{name} is not valid(#{value})" do
    let(name) { value }
    instance_exec(name, value, &block)
  end
end
Also aliased as: invalid_context
set_invalids(name, *values, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 53
def set_invalids(name, *values, &block)
  values.each do |value|
    invalid_context(name, value, &block)
  end
end
Also aliased as: invalid_contexts
set_missing(name, value = 'missing-value', description = nil, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 73
def set_missing(name, value = 'missing-value', description = nil, &block)
  context description || "when #{name} does not exist(#{value})" do
    let(name) { value }
    instance_exec(name, &block)
  end
end
Also aliased as: nonexist_context
set_nil(name, description = nil, &block) click to toggle source

TODO: あとで綺麗にリファクタリングする TODO: contexts のときに配列が空だったらエラーが出るようにする

# File lib/rspecz/contexts.rb, line 8
def set_nil(name, description = nil, &block)
  context description || "when #{name} is nil" do
    let(name) { nil }
    instance_exec(name, &block)
  end
end
Also aliased as: nil_context
set_nils(*names, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 16
def set_nils(*names, &block)
  names.each do |name|
    nil_context(name, &block)
  end
end
Also aliased as: nil_contexts
set_valid(name, value, description = nil, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 38
def set_valid(name, value, description = nil, &block)
  context description || "when #{name} is valid(#{value})" do
    let(name) { value }
    instance_exec(name, value, &block)
  end
end
set_values(name, *values, &block) click to toggle source
# File lib/rspecz/contexts.rb, line 31
def set_values(name, *values, &block)
  values.each do |value|
    value_context(name, value, &block)
  end
end
Also aliased as: value_contexts
string(*names) click to toggle source
# File lib/rspecz/lets.rb, line 5
def string(*names)
  __each_if_method_not_defined(names) do |name|
    let(name) { "test-#{name}" }
  end
end
subject_freezed(&block) click to toggle source
# File lib/rspecz/subjects.rb, line 5
def subject_freezed(&block)
  if RSpec::Core::MemoizedHelpers.module_for(self).method_defined?('now')
    before(:each) { instance_eval { travel_to(now) } }
    after(:each) { instance_eval { travel_back } }
    subject(&block)
  else
    subject { raise RuntimeError.new('subject_freezed need you to define let(:now). Please define let(:now)') }
  end
end
value_context(name, value, description = nil, &block)
Alias for: set
value_contexts(name, *values, &block)
Alias for: set_values
with(name = nil, *values, &block) click to toggle source
# File lib/rspecz/with.rb, line 134
def with(name = nil, *values, &block) _with(name, nil, false, values, block); end
with_context(name, &block)
Alias for: set_context
with_invalid(name = nil, *values, &block) click to toggle source
# File lib/rspecz/with.rb, line 143
def with_invalid(name = nil, *values, &block) _with(name, 'invalid', false, values, block); end
Also aliased as: with_invalids
with_invalids(name = nil, *values, &block)
Alias for: with_invalid
with_missing(name = nil, *values, &block) click to toggle source
# File lib/rspecz/with.rb, line 149
def with_missing(name = nil, *values, &block) _with(name, 'missing',false, values, block); end
Also aliased as: with_missings
with_missings(name = nil, *values, &block)
Alias for: with_missing
with_nil(*names) click to toggle source
# File lib/rspecz/with.rb, line 155
def with_nil(*names) _with_nil(names, false); end
Also aliased as: with_nils
with_nils(*names)
Alias for: with_nil
with_valid(name = nil, *values, &block) click to toggle source
# File lib/rspecz/with.rb, line 137
def with_valid(name = nil, *values, &block) _with(name, 'valid', false, values, block); end
Also aliased as: with_valids
with_valids(name = nil, *values, &block)
Alias for: with_valid

Private Instance Methods

__each_if_method_not_defined(names) { |name| ... } click to toggle source
# File lib/rspecz/lets.rb, line 25
def __each_if_method_not_defined(names)
  names.each do |name|
    yield(name) unless __lib_method_defined?(name)
  end
end
__lib_method_defined?(name) click to toggle source
# File lib/rspecz/lets.rb, line 31
def __lib_method_defined?(name)
  RSpec::Core::MemoizedHelpers.module_for(self).method_defined?(name)
end
_with(name, hint, focused, values, block) click to toggle source
# File lib/rspecz/with.rb, line 163
def _with(name, hint, focused, values, block)
  count_up_with_count

  with_context = WithContext.new(name, values, block, self)
  with_context.hint(hint) if hint
  with_context.focused if focused
  with_context
end
_with_nil(names, focused) click to toggle source
# File lib/rspecz/with.rb, line 172
def _with_nil(names, focused)
  raise RuntimeError.new("Argument Error. You should set names.") if names.nil? || names.length == 0
  count_up_with_count

  continue_object = { names: names, focused: focused, myobject: self }

  def continue_object.desc(text)
    raise RuntimeError.new("Error. You cannot use desc with with_nil method.")
  end

  def continue_object.so(&block)
    root_context = self[:myobject].send(:root_context)
    so_count = root_context.metadata[RSpecZ::METADATA_SO_COUNT]
    root_context.metadata[RSpecZ::METADATA_SO_COUNT] = so_count == nil ? 1 : so_count + 1;

    continue_object = self
    continue_object[:names].each do |name|
      spec = lambda do
        let(name) { nil }
        instance_exec(name, &block)
      end
      if continue_object[:focused]
        self[:myobject].fcontext("When #{name} is nil", &spec)
      else
        self[:myobject].context("When #{name} is nil", &spec)
      end
    end
  end
  continue_object
end
count_up_with_count() click to toggle source
# File lib/rspecz/with.rb, line 203
def count_up_with_count
  with_count = root_context.metadata[RSpecZ::METADATA_WITH_COUNT]
  root_context.metadata[RSpecZ::METADATA_WITH_COUNT] = with_count == nil ? 1 : with_count + 1
end
root_context() click to toggle source
# File lib/rspecz/with.rb, line 208
def root_context
  current = self
  # RSpec module describe how deep context is. So, minimum module class is the root context
  current.parent_groups.min do |a,b|
    a.ancestors.length <=> b.ancestors.length
  end
end