Class: MotionSpec::Context

Direct Known Subclasses

Bacon::Context

Instance Attribute Summary collapse

Attributes included from MotionSpec::ContextHelper::MemoizedHelpers

#__memoized

Instance Method Summary collapse

Methods included from MotionSpec::ContextHelper::MemoizedHelpers

#is_expected, #let, #let!, #reset_memoized, #subject, #subject!

Methods included from MotionSpec::ContextHelper::Expectation

#expect

Methods included from MotionSpec::ContextHelper::Should

#should

Methods included from MotionSpec::ContextHelper::Matchers

#be, #be_false, #be_nil, #be_true, #be_within, #change, #end_with, #eq, #eql, #have, #include, #match, #match_array, #method_missing, #raise_error, #respond_to, #satisfy, #start_with

Constructor Details

#initialize(name, before = nil, after = nil, &block) ⇒ Context

Returns a new instance of Context.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/motion-spec/context.rb', line 11

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MotionSpec::ContextHelper::Matchers

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



9
10
11
# File 'lib/motion-spec/context.rb', line 9

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/motion-spec/context.rb', line 9

def name
  @name
end

Instance Method Details

#after(&block) ⇒ Object



56
57
58
# File 'lib/motion-spec/context.rb', line 56

def after(&block)
  @after << block
end

#before(&block) ⇒ Object



52
53
54
# File 'lib/motion-spec/context.rb', line 52

def before(&block)
  @before << block
end

#change?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/motion-spec/context.rb', line 125

def change?(*args, &block)
  block.change?(*args)
end

#current_specificationObject



37
38
39
# File 'lib/motion-spec/context.rb', line 37

def current_specification
  @specifications[@current_specification_index]
end

#describe(*args, &block) ⇒ Object Also known as: context



84
85
86
87
88
89
90
91
# File 'lib/motion-spec/context.rb', line 84

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

#include_examples(name) ⇒ Object



68
69
70
# File 'lib/motion-spec/context.rb', line 68

def include_examples(name)
  instance_eval(&Shared[name])
end

#it(description = '', &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/motion-spec/context.rb', line 72

def it(description = '', &block)
  return unless description =~ RestrictName

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

  Counter[:specifications] += 1

  @specifications << Specification.new(
    self, description, block, @before, @after
  )
end

#it_behaves_like(name, &block) ⇒ Object Also known as: behaves_like



60
61
62
63
64
65
# File 'lib/motion-spec/context.rb', line 60

def it_behaves_like(name, &block)
  describe("behaves like #{name}") do
    include_examples(name)
    instance_eval(&block) if block_given?
  end
end

#main_activityObject

Android-only.



130
131
132
# File 'lib/motion-spec/context.rb', line 130

def main_activity
  MotionSpec.main_activity
end

#raise?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/motion-spec/context.rb', line 117

def raise?(*args, &block)
  block.raise?(*args)
end

#resumeObject



113
114
115
# File 'lib/motion-spec/context.rb', line 113

def resume
  current_specification.resume
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/motion-spec/context.rb', line 24

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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/motion-spec/context.rb', line 41

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) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/motion-spec/context.rb', line 121

def throw?(*args, &block)
  block.throw?(*args)
end

#wait(seconds = nil, &block) ⇒ Object



94
95
96
97
98
# File 'lib/motion-spec/context.rb', line 94

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) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/motion-spec/context.rb', line 104

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) ⇒ Object



100
101
102
# File 'lib/motion-spec/context.rb', line 100

def wait_max(timeout, &block)
  current_specification.postpone_block(timeout, &block)
end