Class: Megatest::State::Suite

Inherits:
Object
  • Object
show all
Defined in:
lib/megatest/state.rb

Direct Known Subclasses

SharedSuite, TestSuite

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ Suite

Returns a new instance of Suite.



33
34
35
36
37
38
39
40
41
# File 'lib/megatest/state.rb', line 33

def initialize(registry)
  @registry = registry
  @tags = nil
  @setup_callbacks = []
  @teardown_callbacks = []
  @around_callbacks = []
  @current_context = nil
  @current_tags = nil
end

Instance Attribute Details

#around_callbacksObject (readonly)

Returns the value of attribute around_callbacks.



31
32
33
# File 'lib/megatest/state.rb', line 31

def around_callbacks
  @around_callbacks
end

#setup_callbacksObject (readonly)

Returns the value of attribute setup_callbacks.



31
32
33
# File 'lib/megatest/state.rb', line 31

def setup_callbacks
  @setup_callbacks
end

#teardown_callbacksObject (readonly)

Returns the value of attribute teardown_callbacks.



31
32
33
# File 'lib/megatest/state.rb', line 31

def teardown_callbacks
  @teardown_callbacks
end

Instance Method Details

#add_tags(tags) ⇒ Object



60
61
62
63
64
65
# File 'lib/megatest/state.rb', line 60

def add_tags(tags)
  return if tags.empty?

  @tags ||= {}
  @tags.merge!(tags)
end

#build_test_case(name, callable, tags, source_location) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/megatest/state.rb', line 75

def build_test_case(name, callable, tags, source_location)
  name = [*@current_context, name].join(" ")
  tags = if tags
    @current_tags ? @current_tags.merge(tags) : tags
  else
    @current_tags
  end
  if callable.is_a?(UnboundMethod)
    MethodTest.new(self, @klass, name, callable, tags, source_location)
  else
    BlockTest.new(self, @klass, name, callable, tags, source_location)
  end
end

#on_around(block) ⇒ Object

Raises:



95
96
97
98
99
# File 'lib/megatest/state.rb', line 95

def on_around(block)
  raise Error, "around blocks can't be defined in context blocks" if @current_context

  @around_callbacks << block
end

#on_setup(block) ⇒ Object

Raises:



89
90
91
92
93
# File 'lib/megatest/state.rb', line 89

def on_setup(block)
  raise Error, "setup blocks can't be defined in context blocks" if @current_context

  @setup_callbacks.unshift(block)
end

#on_teardown(block) ⇒ Object

Raises:



101
102
103
104
105
# File 'lib/megatest/state.rb', line 101

def on_teardown(block)
  raise Error, "teardown blocks can't be defined in context blocks" if @current_context

  @teardown_callbacks << block
end

#own_tagsObject



71
72
73
# File 'lib/megatest/state.rb', line 71

def own_tags
  @tags
end

#tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/megatest/state.rb', line 67

def tag?(name)
  @tags&.key?(name)
end

#with_context(context, tags) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/megatest/state.rb', line 43

def with_context(context, tags)
  previous_context = @current_context
  @current_context = [@current_context, context].compact.join(" ")

  previous_tags = @current_tags
  if tags
    @current_tags = @current_tags ? @current_tags.merge(tags) : tags
  end

  begin
    yield
  ensure
    @current_context = previous_context
    @current_tags = previous_tags
  end
end