1# Copyright 2014 The Chromium 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.
4import sys
5import unittest
6
7from telemetry.core import exceptions
8
9
10class BaseTestResultsUnittest(unittest.TestCase):
11
12  def CreateException(self):
13    try:
14      raise exceptions.IntentionalException
15    except Exception:
16      return sys.exc_info()
17
18  def assertEquals(self, ex, res):
19    # This helps diagnose result mismatches.
20    if ex != res and isinstance(ex, list):
21      def CleanList(l):
22        res = []
23        for x in l:
24          x = x.split('\n')
25          res.extend(x)
26        return res
27      ex = CleanList(ex)
28      res = CleanList(res)
29      max_len = max(len(ex), len(res))
30      max_width = max([len(x) for x in ex + res])
31      max_width = max(10, max_width)
32      print 'Lists differ!'
33      print '%*s | %*s' % (max_width, 'expected', max_width, 'result')
34      for i in range(max_len):
35        if i < len(ex):
36          e = ex[i]
37        else:
38          e = ''
39        if i < len(res):
40          r = res[i]
41        else:
42          r = ''
43        if e != r:
44          sep = '*'
45        else:
46          sep = '|'
47        print '%*s %s %*s' % (max_width, e, sep, max_width, r)
48      print ''
49    if ex != res and isinstance(ex, str) and isinstance(res, str):
50      print 'Strings differ!'
51      print 'exepected:\n%s' % repr(ex)
52      print 'result:\n%s\n' % repr(res)
53    super(BaseTestResultsUnittest, self).assertEquals(ex, res)
54