Module: Bcpm::Tests::Assertions

Included in:
CaseBase
Defined in:
lib/bcpm/tests/assertions.rb

Overview

Assertions for match tests.

Instance Method Summary collapse

Instance Method Details

#_parse_unit_output(line) ⇒ Object

Parses a unit’s console output (usually via System.out.print*).

If the given line looks like a unit’s console output, returns a hash with the following keys:

:team:: 'A' or 'B' (should always be 'A', unless the case enables team B's console output)
:unit_type:: e.g., 'ARCHON'
:unit_id:: the robot ID of the unit who wrote the line, parsed as an Integer
:round:: the round when the line was output, parsed as an Integer
:output:: the (first line of the) string that the unit produced

If the line doesn’t parse out, returns nil.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bcpm/tests/assertions.rb', line 87

def _parse_unit_output(line)
  line_match = /^\[([AB])\:([A-Z]+)\#(\d+)\@(\d+)\](.*)$/.match line
  return nil unless line_match
  {
    :team => line_match[1],
    :unit_type => line_match[2],
    :unit_id => line_match[3].to_i,
    :round => line_match[4].to_i,
    :output => line_match[5]
  }
end

#fail(reason = 'Test case called fail!') ⇒ Object

Always fails. Useful for obtaining the game log.



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

def fail(reason = 'Test case called fail!')
  raise Bcpm::Tests::AssertionError, reason
end

#should_loseObject

Fails unless the match was lost.



25
26
27
28
# File 'lib/bcpm/tests/assertions.rb', line 25

def should_lose
  return if match.winner == :b
  raise Bcpm::Tests::AssertionError, "Player was expected to lose, but didn't! " + match.outcome
end

#should_lose_by(reason) ⇒ Object

Fails unless the match was lost, and the Reason: line includes the argument text.



31
32
33
34
35
36
37
# File 'lib/bcpm/tests/assertions.rb', line 31

def should_lose_by(reason)
  should_lose
  
  return if match.reason.index(reason)
  raise Bcpm::Tests::AssertionError, "Player was expected to win by #{reason} and didn't. " +
                                     match.reason
end

#should_match_unit_output(pattern) ⇒ Object

Fails unless a unit’s output matches the given regular expression.

If a block is given, yields to the block for every match.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bcpm/tests/assertions.rb', line 59

def should_match_unit_output(pattern)
  matched = false
  
  match.output_lines.each do |line|
    next unless unit_output = _parse_unit_output(line)
    if match = pattern.match(unit_output[:output])
      matched = true
      if Kernel.block_given?
        yield unit_output, match
      else
        break
      end
    end
  end
      
  raise Bcpm::Tests::AssertionError, "No unit output matched #{pattern.inspect}!" unless matched
end

#should_not_throwObject

Fails if the player code threw any exception.



40
41
42
43
44
45
46
47
48
49
# File 'lib/bcpm/tests/assertions.rb', line 40

def should_not_throw
  if match.output.index(/\n(\S*)Exception(.*?)\n\S/m)
    raise Bcpm::Tests::AssertionError, "Player should not have thrown exceptions! " +
        "It threw #{$1}Exception#{$2}"
  end
  if match.chatter.index(/\n(\S*)Exception(.*?)\n\S/m)
    raise Bcpm::Tests::AssertionError, "Player should not have thrown exceptions! " +
        "It threw #{$1}Exception#{$2}"
  end
end

#should_winObject

Fails unless the match was won.



10
11
12
13
# File 'lib/bcpm/tests/assertions.rb', line 10

def should_win
  return if match.winner == :a
  raise Bcpm::Tests::AssertionError, "Player was expected to win, but didn't! " + match.outcome
end

#should_win_by(reason) ⇒ Object

Fails unless the match was won, and the Reason: line includes the argument text.



16
17
18
19
20
21
22
# File 'lib/bcpm/tests/assertions.rb', line 16

def should_win_by(reason)
  should_win
  
  return if match.reason.index(reason)
  raise Bcpm::Tests::AssertionError, "Player was expected to win by #{reason} and didn't. " +
                                     match.reason
end