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_callback = nil
  @teardown_callback = nil
  @around_callback = nil
  @current_context = nil
  @current_tags = nil
end

Instance Attribute Details

#around_callbackObject (readonly)

Returns the value of attribute around_callback.



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

def around_callback
  @around_callback
end

#setup_callbackObject (readonly)

Returns the value of attribute setup_callback.



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

def setup_callback
  @setup_callback
end

#teardown_callbackObject (readonly)

Returns the value of attribute teardown_callback.



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

def teardown_callback
  @teardown_callback
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:



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

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

  @around_callback = block
end

#on_setup(block) ⇒ Object

Raises:



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

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

  @setup_callback = block
end

#on_teardown(block) ⇒ Object

Raises:



103
104
105
106
107
108
# File 'lib/megatest/state.rb', line 103

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

  @teardown_callback = 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