xref: /aosp_15_r20/external/cronet/build/android/pylib/instrumentation/instrumentation_parser_test.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env vpython3
2# Copyright 2015 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6
7"""Unit tests for instrumentation.InstrumentationParser."""
8
9
10import unittest
11
12from pylib.instrumentation import instrumentation_parser
13
14
15class InstrumentationParserTest(unittest.TestCase):
16
17  def testInstrumentationParser_nothing(self):
18    parser = instrumentation_parser.InstrumentationParser([''])
19    statuses = list(parser.IterStatus())
20    code, bundle = parser.GetResult()
21    self.assertEqual(None, code)
22    self.assertEqual({}, bundle)
23    self.assertEqual([], statuses)
24
25  def testInstrumentationParser_noMatchingStarts(self):
26    raw_output = [
27      '',
28      'this.is.a.test.package.TestClass:.',
29      'Test result for =.',
30      'Time: 1.234',
31      '',
32      'OK (1 test)',
33    ]
34
35    parser = instrumentation_parser.InstrumentationParser(raw_output)
36    statuses = list(parser.IterStatus())
37    code, bundle = parser.GetResult()
38    self.assertEqual(None, code)
39    self.assertEqual({}, bundle)
40    self.assertEqual([], statuses)
41
42  def testInstrumentationParser_resultAndCode(self):
43    raw_output = [
44      'INSTRUMENTATION_RESULT: shortMsg=foo bar',
45      'INSTRUMENTATION_RESULT: longMsg=a foo',
46      'walked into',
47      'a bar',
48      'INSTRUMENTATION_CODE: -1',
49    ]
50
51    parser = instrumentation_parser.InstrumentationParser(raw_output)
52    statuses = list(parser.IterStatus())
53    code, bundle = parser.GetResult()
54    self.assertEqual(-1, code)
55    self.assertEqual(
56        {'shortMsg': 'foo bar', 'longMsg': 'a foo\nwalked into\na bar'}, bundle)
57    self.assertEqual([], statuses)
58
59  def testInstrumentationParser_oneStatus(self):
60    raw_output = [
61      'INSTRUMENTATION_STATUS: foo=1',
62      'INSTRUMENTATION_STATUS: bar=hello',
63      'INSTRUMENTATION_STATUS: world=false',
64      'INSTRUMENTATION_STATUS: class=this.is.a.test.package.TestClass',
65      'INSTRUMENTATION_STATUS: test=testMethod',
66      'INSTRUMENTATION_STATUS_CODE: 0',
67    ]
68
69    parser = instrumentation_parser.InstrumentationParser(raw_output)
70    statuses = list(parser.IterStatus())
71
72    expected = [
73      (0, {
74        'foo': '1',
75        'bar': 'hello',
76        'world': 'false',
77        'class': 'this.is.a.test.package.TestClass',
78        'test': 'testMethod',
79      })
80    ]
81    self.assertEqual(expected, statuses)
82
83  def testInstrumentationParser_multiStatus(self):
84    raw_output = [
85      'INSTRUMENTATION_STATUS: class=foo',
86      'INSTRUMENTATION_STATUS: test=bar',
87      'INSTRUMENTATION_STATUS_CODE: 1',
88      'INSTRUMENTATION_STATUS: test_skipped=true',
89      'INSTRUMENTATION_STATUS_CODE: 0',
90      'INSTRUMENTATION_STATUS: class=hello',
91      'INSTRUMENTATION_STATUS: test=world',
92      'INSTRUMENTATION_STATUS: stack=',
93      'foo/bar.py (27)',
94      'hello/world.py (42)',
95      'test/file.py (1)',
96      'INSTRUMENTATION_STATUS_CODE: -1',
97    ]
98
99    parser = instrumentation_parser.InstrumentationParser(raw_output)
100    statuses = list(parser.IterStatus())
101
102    expected = [
103      (1, {'class': 'foo', 'test': 'bar',}),
104      (0, {'test_skipped': 'true'}),
105      (-1, {
106        'class': 'hello',
107        'test': 'world',
108        'stack': '\nfoo/bar.py (27)\nhello/world.py (42)\ntest/file.py (1)',
109      }),
110    ]
111    self.assertEqual(expected, statuses)
112
113  def testInstrumentationParser_statusResultAndCode(self):
114    raw_output = [
115      'INSTRUMENTATION_STATUS: class=foo',
116      'INSTRUMENTATION_STATUS: test=bar',
117      'INSTRUMENTATION_STATUS_CODE: 1',
118      'INSTRUMENTATION_RESULT: result=hello',
119      'world',
120      '',
121      '',
122      'INSTRUMENTATION_CODE: 0',
123    ]
124
125    parser = instrumentation_parser.InstrumentationParser(raw_output)
126    statuses = list(parser.IterStatus())
127    code, bundle = parser.GetResult()
128
129    self.assertEqual(0, code)
130    self.assertEqual({'result': 'hello\nworld\n\n'}, bundle)
131    self.assertEqual([(1, {'class': 'foo', 'test': 'bar'})], statuses)
132
133
134if __name__ == '__main__':
135  unittest.main(verbosity=2)
136