class MotionSpec::Context

Attributes

block[R]
name[R]

Public Class Methods

new(name, before = nil, after = nil, &block) click to toggle source
# File lib/motion-spec/context.rb, line 10
def initialize(name, before = nil, after = nil, &block)
  @name = name
  @before = before ? before.dup : []
  @after = after ? after.dup : []
  @block = block
  @specifications = []
  @current_specification_index = 0

  MotionSpec.add_context(self)

  instance_eval(&block)
end

Public Instance Methods

after(&block) click to toggle source
# File lib/motion-spec/context.rb, line 55
def after(&block)
  @after << block
end
before(&block) click to toggle source
# File lib/motion-spec/context.rb, line 51
def before(&block)
  @before << block
end
behaves_like(name, &block)
Alias for: it_behaves_like
change?(*args, &block) click to toggle source
# File lib/motion-spec/context.rb, line 126
def change?(*args, &block)
  block.change?(*args)
end
context(*args, &block)
Alias for: describe
current_specification() click to toggle source
# File lib/motion-spec/context.rb, line 36
def current_specification
  @specifications[@current_specification_index]
end
describe(*args, &block) click to toggle source
# File lib/motion-spec/context.rb, line 85
def describe(*args, &block)
  context = MotionSpec::Context.new("#{@name} #{args.join(' ')}", @before, @after, &block)

  # FIXME: fix RM-879 and RM-806
  build_ios_parent_context(context) unless Platform.android?

  context
end
Also aliased as: context
include_examples(name) click to toggle source
# File lib/motion-spec/context.rb, line 67
def include_examples(name)
  instance_eval(&Shared[name])
end
it(description = '', &block) click to toggle source
# File lib/motion-spec/context.rb, line 71
def it(description = '', &block)
  return unless description =~ RestrictName

  block ||= proc { should.flunk 'not implemented' }

  Counter[:specifications] += 1

  @after << proc { verify_mocks_were_called }

  @specifications << Specification.new(
    self, description, block, @before, @after
  )
end
it_behaves_like(name, &block) click to toggle source
# File lib/motion-spec/context.rb, line 59
def it_behaves_like(name, &block)
  describe("behaves like #{name}") do
    include_examples(name)
    instance_eval(&block) if block_given?
  end
end
Also aliased as: behaves_like
main_activity() click to toggle source

Android-only.

# File lib/motion-spec/context.rb, line 131
def main_activity
  MotionSpec.main_activity
end
raise?(*args, &block) click to toggle source
# File lib/motion-spec/context.rb, line 118
def raise?(*args, &block)
  block.raise?(*args)
end
resume() click to toggle source
# File lib/motion-spec/context.rb, line 114
def resume
  current_specification.resume
end
run() click to toggle source
# File lib/motion-spec/context.rb, line 23
def run
  # TODO: return unless name =~ RestrictContext

  if Platform.android?
    @specifications.each(&:run)
  else
    spec = current_specification
    return spec.performSelector('run', withObject: nil, afterDelay: 0) if spec
  end

  MotionSpec.context_did_finish(self)
end
specification_did_finish(_spec) click to toggle source
# File lib/motion-spec/context.rb, line 40
def specification_did_finish(_spec)
  return if Platform.android?

  if (@current_specification_index + 1) < @specifications.size
    @current_specification_index += 1
    return run
  end

  MotionSpec.context_did_finish(self)
end
throw?(*args, &block) click to toggle source
# File lib/motion-spec/context.rb, line 122
def throw?(*args, &block)
  block.throw?(*args)
end
wait(seconds = nil, &block) click to toggle source
# File lib/motion-spec/context.rb, line 95
def wait(seconds = nil, &block)
  return current_specification.schedule_block(seconds, &block) if seconds

  current_specification.postpone_block(&block)
end
wait_for_change(object_to_observe, key_path, timeout = 1, &block) click to toggle source
# File lib/motion-spec/context.rb, line 105
def wait_for_change(object_to_observe, key_path, timeout = 1, &block)
  current_specification.postpone_block_until_change(
    object_to_observe,
    key_path,
    timeout,
    &block
  )
end
wait_max(timeout, &block) click to toggle source
# File lib/motion-spec/context.rb, line 101
def wait_max(timeout, &block)
  current_specification.postpone_block(timeout, &block)
end

Private Instance Methods

build_ios_parent_context(context) click to toggle source
# File lib/motion-spec/context.rb, line 137
def build_ios_parent_context(context)
  parent_context = self

  # object.methods(false) returns duplicate method names where one ends in
  # a ':' (e.g. ['foo:', 'foo']). This was causing a low-level Ruby error:
  # Assertion failed: (b != NULL), function rb_vm_block_method_imp, file vm.cpp, line 3386.
  # To fix the issue we removed the 'foo:' version of the method names.
  methods = parent_context
    .methods(false)
    .map { |name| name.to_s.chomp(':') }
    .uniq

  context_eigenclass = (class << context; self; end)
  context_eigenclass.send(:define_method, :parent_context) { parent_context }

  methods.each do |method_name|
    next if context.respond_to?(method_name)

    context_eigenclass.send(:define_method, method_name) do |*args|
      parent_context.send(method_name, *args)
    end
  end
end
mock_failures() click to toggle source
# File lib/motion-spec/context.rb, line 161
def mock_failures
  MotionSpec::Mocks.failures
end
verify_mocks_were_called() click to toggle source
# File lib/motion-spec/context.rb, line 165
def verify_mocks_were_called
  return unless mock_failures && !mock_failures.empty?

  fails = mock_failures.map do |object, method|
    "#{object.inspect} expected #{method}"
  end

  should.flunk "Unmet expectations: #{fails.join(', ')}"
end