Class: Bcpm::Tests::CaseBase

Inherits:
Object
  • Object
show all
Includes:
Assertions
Defined in:
lib/bcpm/tests/case_base.rb

Overview

Base class for test cases.

Each test case is its own anonymous class.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#_parse_unit_output, #fail, #should_lose, #should_lose_by, #should_match_unit_output, #should_not_throw, #should_win, #should_win_by

Constructor Details

#initialize(label, match, block) ⇒ CaseBase

Called by match.



138
139
140
141
142
# File 'lib/bcpm/tests/case_base.rb', line 138

def initialize(label, match, block)
  @label = label
  @match = match
  @block = block
end

Class Attribute Details

.environmentsObject (readonly)

All the environments used in the tests.



125
126
127
# File 'lib/bcpm/tests/case_base.rb', line 125

def environments
  @environments
end

.matchesObject (readonly)

All the matches used in the tests.



127
128
129
# File 'lib/bcpm/tests/case_base.rb', line 127

def matches
  @matches
end

.testsObject (readonly)

All test cases.



129
130
131
# File 'lib/bcpm/tests/case_base.rb', line 129

def tests
  @tests
end

Instance Attribute Details

#labelObject (readonly)

Descriptive label for the test case.



133
134
135
# File 'lib/bcpm/tests/case_base.rb', line 133

def label
  @label
end

#matchObject (readonly)

Test match used by the test case.



135
136
137
# File 'lib/bcpm/tests/case_base.rb', line 135

def match
  @match
end

Class Method Details

._env_changeObject

Called by public methods before they change the environment.



36
37
38
39
40
41
42
# File 'lib/bcpm/tests/case_base.rb', line 36

def _env_change
  if @env_used
    @environments << @env
    @env = Bcpm::Tests::Environment.new
    @env_used = false
  end
end

._post_evalObject

Called after all code is evaluated in the class context.



29
30
31
32
33
# File 'lib/bcpm/tests/case_base.rb', line 29

def _post_eval
  @environments << @env if @env_used
  @env = nil
  @options = nil
end

._setupObject

Called before any code is evaluated in the class context.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bcpm/tests/case_base.rb', line 13

def _setup
  @map = nil
  @suite_map = false
  @vs = nil
  @side = :a
  @match = nil      
  @options = {}
  @env = Bcpm::Tests::Environment.new
  @env_used = false
  
  @tests = []
  @environments = []
  @matches = []
end

.it(label, &block) ⇒ Object

Create a test match.



119
120
121
122
# File 'lib/bcpm/tests/case_base.rb', line 119

def it(label, &block)
  raise "it can only be called within match blocks!" if @match.nil?        
  @tests << self.new(label, @match, block)
end

.map(map_name) ⇒ Object

Set the map for following matches.



45
46
47
48
# File 'lib/bcpm/tests/case_base.rb', line 45

def map(map_name)
  @map = map_name.dup.to_s
  @suite_map = false
end

.match(&block) ⇒ Object

Create a test match. The block contains test cases for the match.



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bcpm/tests/case_base.rb', line 106

def match(&block)
  begin
    @env_used = true
    map = @suite_map ? @env.suite_map_path(@map) : @map
    @match = Bcpm::Tests::TestMatch.new @side, @vs, map, @env, @options
    self.class_eval(&block)
    @matches << @match
  ensure
    @match = nil
  end
end

.option(key, value) ⇒ Object

Set a simulation option.



67
68
69
70
71
72
73
74
75
# File 'lib/bcpm/tests/case_base.rb', line 67

def option(key, value)
  key = Bcpm::Match.engine_options[key.to_s] if key.kind_of? Symbol
  
  if value.nil?
    @options.delete key
  else
    @options[key] = value
  end
end

.replace_class(target, source) ⇒ Object

Replaces a class implementation file (.java) with another one (presumably from tests).



78
79
80
81
# File 'lib/bcpm/tests/case_base.rb', line 78

def replace_class(target, source)
  _env_change
  @env.file_op [:file, target, source]
end

.replace_code(target_class, target_fragment, source_class, source_fragment) ⇒ Object

Replaces all fragments labeled with target_fragment in target_class with another fragment.



84
85
86
87
# File 'lib/bcpm/tests/case_base.rb', line 84

def replace_code(target_class, target_fragment, source_class, source_fragment)
  _env_change
  @env.file_op [:fragment, [target_class, target_fragment], [source_class, source_fragment]]
end

.side(side) ⇒ Object

Set our side for the following matches.



62
63
64
# File 'lib/bcpm/tests/case_base.rb', line 62

def side(side)
  @side = side.to_s.downcase.to_sym
end

.stub_member_call(source, target) ⇒ Object

Redirects all method calls using a method name to a static method.

Assumes the target method is a member method, and passes “this” to the static method.



92
93
94
95
# File 'lib/bcpm/tests/case_base.rb', line 92

def stub_member_call(source, target)
  _env_change
  @env.patch_op [:stub_member, target, source]
end

.stub_static_call(source, target) ⇒ Object

Redirects all method calls using a method name to a static method.

Assumes the target method is a static method, and ignores the call target.



100
101
102
103
# File 'lib/bcpm/tests/case_base.rb', line 100

def stub_static_call(source, target)
  _env_change
  @env.patch_op [:stub_static, target, source]
end

.suite_map(map_name) ⇒ Object

Set the map for the following match. Use a map in the player’s test suite.



51
52
53
54
# File 'lib/bcpm/tests/case_base.rb', line 51

def suite_map(map_name)
  @map = map_name.dup.to_s
  @suite_map = true
end

.vs(player_name) ⇒ Object

Set the enemy for following matches.



57
58
59
# File 'lib/bcpm/tests/case_base.rb', line 57

def vs(player_name)
  @vs = player_name.dup.to_s
end

Instance Method Details

#check_outputObject

Verifies the match output against the test case.

Returns nil for success, or an AssertionError exception if the case failed.



152
153
154
155
156
157
158
159
# File 'lib/bcpm/tests/case_base.rb', line 152

def check_output
  begin
    self.instance_eval &@block
    return nil
  rescue Bcpm::Tests::AssertionError => e
    return e
  end
end

#descriptionObject

User-readable description of test conditions.



145
146
147
# File 'lib/bcpm/tests/case_base.rb', line 145

def description
  "#{match.description} #{label}"
end