1# Copyright 2018 the V8 project authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import re
6
7from . import base
8
9
10def _is_failure_output(output):
11  return (
12    output.exit_code != 0 or
13    'FAILED!' in output.stdout
14  )
15
16
17class ExceptionOutProc(base.OutProc):
18  """Output processor for tests with expected exception."""
19  def __init__(
20      self, expected_outcomes, expected_exception=None, negative=False):
21    super(ExceptionOutProc, self).__init__(expected_outcomes)
22    self._expected_exception = expected_exception
23    self._negative = negative
24
25  @property
26  def negative(self):
27    return self._negative
28
29  def _is_failure_output(self, output):
30    if self._expected_exception != self._parse_exception(output.stdout):
31      return True
32    return _is_failure_output(output)
33
34  def _parse_exception(self, string):
35    # somefile:somelinenumber: someerror[: sometext]
36    # somefile might include an optional drive letter on windows e.g. "e:".
37    match = re.search(
38        '^(?:\w:)?[^:]*:[0-9]+: ([^: ]+?)($|: )', string, re.MULTILINE)
39    if match:
40      return match.group(1).strip()
41    else:
42      return None
43
44
45class NoExceptionOutProc(base.OutProc):
46  """Output processor optimized for tests without expected exception."""
47  def __init__(self, expected_outcomes):
48    super(NoExceptionOutProc, self).__init__(expected_outcomes)
49
50  def _is_failure_output(self, output):
51    return _is_failure_output(output)
52
53
54class PassNoExceptionOutProc(base.PassOutProc):
55  """
56  Output processor optimized for tests expected to PASS without expected
57  exception.
58  """
59  def _is_failure_output(self, output):
60    return _is_failure_output(output)
61
62
63PASS_NO_EXCEPTION = PassNoExceptionOutProc()
64