| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Unit tests for classes in annotator.py.""" | 6 """Unit tests for classes in annotator.py.""" |
| 7 | 7 |
| 8 import cStringIO | 8 import cStringIO |
| 9 import json | 9 import json |
| 10 import types | 10 import types |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 class TestAnnotationStreams(unittest.TestCase): | 24 class TestAnnotationStreams(unittest.TestCase): |
| 25 def setUp(self): | 25 def setUp(self): |
| 26 self.buf = cStringIO.StringIO() | 26 self.buf = cStringIO.StringIO() |
| 27 | 27 |
| 28 def _getLines(self): | 28 def _getLines(self): |
| 29 """Return list of non-empty lines in output.""" | 29 """Return list of non-empty lines in output.""" |
| 30 return [line for line in self.buf.getvalue().rstrip().split('\n') if line] | 30 return [line for line in self.buf.getvalue().rstrip().split('\n') if line] |
| 31 | 31 |
| 32 def testBasicUsage(self): | 32 def testBasicUsage(self): |
| 33 stream = annotator.StructuredAnnotationStream(stream=self.buf) | 33 stream = annotator.StructuredAnnotationStream( |
| 34 stream=self.buf, time_fn=lambda: 123456) |
| 34 with stream.step('one') as _: | 35 with stream.step('one') as _: |
| 35 pass | 36 pass |
| 36 with stream.step('two') as _: | 37 with stream.step('two') as _: |
| 37 pass | 38 pass |
| 38 | 39 |
| 39 result = [ | 40 result = [ |
| 40 '@@@SEED_STEP one@@@', | 41 '@@@SEED_STEP one@@@', |
| 41 '@@@STEP_CURSOR one@@@', | 42 '@@@STEP_CURSOR one@@@', |
| 43 '@@@CURRENT_TIMESTAMP@123456@@@', |
| 42 '@@@STEP_STARTED@@@', | 44 '@@@STEP_STARTED@@@', |
| 43 '@@@STEP_CURSOR one@@@', | 45 '@@@STEP_CURSOR one@@@', |
| 46 '@@@CURRENT_TIMESTAMP@123456@@@', |
| 44 '@@@STEP_CLOSED@@@', | 47 '@@@STEP_CLOSED@@@', |
| 45 '@@@SEED_STEP two@@@', | 48 '@@@SEED_STEP two@@@', |
| 46 '@@@STEP_CURSOR two@@@', | 49 '@@@STEP_CURSOR two@@@', |
| 50 '@@@CURRENT_TIMESTAMP@123456@@@', |
| 47 '@@@STEP_STARTED@@@', | 51 '@@@STEP_STARTED@@@', |
| 48 '@@@STEP_CURSOR two@@@', | 52 '@@@STEP_CURSOR two@@@', |
| 53 '@@@CURRENT_TIMESTAMP@123456@@@', |
| 49 '@@@STEP_CLOSED@@@', | 54 '@@@STEP_CLOSED@@@', |
| 50 ] | 55 ] |
| 51 | 56 |
| 52 self.assertEquals(result, self._getLines()) | 57 self.assertEquals(result, self._getLines()) |
| 53 | 58 |
| 54 def testStepAnnotations(self): | 59 def testStepAnnotations(self): |
| 55 stream = annotator.StructuredAnnotationStream(stream=self.buf) | 60 stream = annotator.StructuredAnnotationStream( |
| 61 stream=self.buf, time_fn=lambda: 123456) |
| 56 with stream.step('one') as s: | 62 with stream.step('one') as s: |
| 57 s.step_warnings() | 63 s.step_warnings() |
| 58 s.step_failure() | 64 s.step_failure() |
| 59 s.step_exception() | 65 s.step_exception() |
| 60 s.step_clear() | 66 s.step_clear() |
| 61 s.step_summary_clear() | 67 s.step_summary_clear() |
| 62 s.step_text('hello') | 68 s.step_text('hello') |
| 63 s.step_summary_text('hello!') | 69 s.step_summary_text('hello!') |
| 64 s.step_log_line('mylog', 'test') | 70 s.step_log_line('mylog', 'test') |
| 65 s.step_log_end('mylog') | 71 s.step_log_end('mylog') |
| 66 s.step_log_line('myperflog', 'perf data') | 72 s.step_log_line('myperflog', 'perf data') |
| 67 s.step_log_end_perf('myperflog', 'dashboardname') | 73 s.step_log_end_perf('myperflog', 'dashboardname') |
| 68 s.step_link('cool_link', 'https://cool.example.com/beano_gnarly') | 74 s.step_link('cool_link', 'https://cool.example.com/beano_gnarly') |
| 69 s.write_log_lines('full_log', ['line one', 'line two']) | 75 s.write_log_lines('full_log', ['line one', 'line two']) |
| 70 s.write_log_lines('full_perf_log', ['perf line one', 'perf line two'], | 76 s.write_log_lines('full_perf_log', ['perf line one', 'perf line two'], |
| 71 perf='full_perf') | 77 perf='full_perf') |
| 72 | 78 |
| 73 result = [ | 79 result = [ |
| 74 '@@@SEED_STEP one@@@', | 80 '@@@SEED_STEP one@@@', |
| 75 '@@@STEP_CURSOR one@@@', | 81 '@@@STEP_CURSOR one@@@', |
| 82 '@@@CURRENT_TIMESTAMP@123456@@@', |
| 76 '@@@STEP_STARTED@@@', | 83 '@@@STEP_STARTED@@@', |
| 77 '@@@STEP_WARNINGS@@@', | 84 '@@@STEP_WARNINGS@@@', |
| 78 '@@@STEP_FAILURE@@@', | 85 '@@@STEP_FAILURE@@@', |
| 79 '@@@STEP_EXCEPTION@@@', | 86 '@@@STEP_EXCEPTION@@@', |
| 80 '@@@STEP_CLEAR@@@', | 87 '@@@STEP_CLEAR@@@', |
| 81 '@@@STEP_SUMMARY_CLEAR@@@', | 88 '@@@STEP_SUMMARY_CLEAR@@@', |
| 82 '@@@STEP_TEXT@hello@@@', | 89 '@@@STEP_TEXT@hello@@@', |
| 83 '@@@STEP_SUMMARY_TEXT@hello!@@@', | 90 '@@@STEP_SUMMARY_TEXT@hello!@@@', |
| 84 '@@@STEP_LOG_LINE@mylog@test@@@', | 91 '@@@STEP_LOG_LINE@mylog@test@@@', |
| 85 '@@@STEP_LOG_END@mylog@@@', | 92 '@@@STEP_LOG_END@mylog@@@', |
| 86 '@@@STEP_LOG_LINE@myperflog@perf data@@@', | 93 '@@@STEP_LOG_LINE@myperflog@perf data@@@', |
| 87 '@@@STEP_LOG_END_PERF@myperflog@dashboardname@@@', | 94 '@@@STEP_LOG_END_PERF@myperflog@dashboardname@@@', |
| 88 '@@@STEP_LINK@cool_link@https://cool.example.com/beano_gnarly@@@', | 95 '@@@STEP_LINK@cool_link@https://cool.example.com/beano_gnarly@@@', |
| 89 '@@@STEP_LOG_LINE@full_log@line one@@@', | 96 '@@@STEP_LOG_LINE@full_log@line one@@@', |
| 90 '@@@STEP_LOG_LINE@full_log@line two@@@', | 97 '@@@STEP_LOG_LINE@full_log@line two@@@', |
| 91 '@@@STEP_LOG_END@full_log@@@', | 98 '@@@STEP_LOG_END@full_log@@@', |
| 92 '@@@STEP_LOG_LINE@full_perf_log@perf line one@@@', | 99 '@@@STEP_LOG_LINE@full_perf_log@perf line one@@@', |
| 93 '@@@STEP_LOG_LINE@full_perf_log@perf line two@@@', | 100 '@@@STEP_LOG_LINE@full_perf_log@perf line two@@@', |
| 94 '@@@STEP_LOG_END_PERF@full_perf_log@full_perf@@@', | 101 '@@@STEP_LOG_END_PERF@full_perf_log@full_perf@@@', |
| 95 '@@@STEP_CURSOR one@@@', | 102 '@@@STEP_CURSOR one@@@', |
| 103 '@@@CURRENT_TIMESTAMP@123456@@@', |
| 96 '@@@STEP_CLOSED@@@', | 104 '@@@STEP_CLOSED@@@', |
| 97 ] | 105 ] |
| 98 | 106 |
| 99 self.assertEquals(result, self._getLines()) | 107 self.assertEquals(result, self._getLines()) |
| 100 | 108 |
| 101 def testStepAnnotationsWrongParams(self): | 109 def testStepAnnotationsWrongParams(self): |
| 102 stream = annotator.StructuredAnnotationStream(stream=self.buf) | 110 stream = annotator.StructuredAnnotationStream(stream=self.buf) |
| 103 with stream.step('one') as s: | 111 with stream.step('one') as s: |
| 104 with self.assertRaisesRegexp(TypeError, r'1 argument \(2 given\)'): | 112 with self.assertRaisesRegexp(TypeError, r'1 argument \(2 given\)'): |
| 105 s.step_warnings('bar') | 113 s.step_warnings('bar') |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 def testDupLogs(self): | 147 def testDupLogs(self): |
| 140 stream = annotator.StructuredAnnotationStream(stream=self.buf) | 148 stream = annotator.StructuredAnnotationStream(stream=self.buf) |
| 141 | 149 |
| 142 with stream.step('one') as s: | 150 with stream.step('one') as s: |
| 143 lines = ['one', 'two'] | 151 lines = ['one', 'two'] |
| 144 s.write_log_lines('mylog', lines) | 152 s.write_log_lines('mylog', lines) |
| 145 self.assertRaises(ValueError, s.write_log_lines, 'mylog', lines) | 153 self.assertRaises(ValueError, s.write_log_lines, 'mylog', lines) |
| 146 | 154 |
| 147 def testStructured(self): | 155 def testStructured(self): |
| 148 stream = annotator.StructuredAnnotationStream( | 156 stream = annotator.StructuredAnnotationStream( |
| 149 stream=self.buf, flush_before=None) | 157 stream=self.buf, flush_before=None, time_fn=lambda: 123456) |
| 150 step = annotator.StructuredAnnotationStep( | 158 step = annotator.StructuredAnnotationStep( |
| 151 annotation_stream=stream, stream=self.buf, flush_before=None) | 159 annotation_stream=stream, stream=self.buf, flush_before=None) |
| 152 stream.step_cursor('one') | 160 stream.step_cursor('one') |
| 153 step.step_started() | 161 step.step_started() |
| 154 stream.step_cursor('two') | 162 stream.step_cursor('two') |
| 155 step.step_started() | 163 step.step_started() |
| 156 stream.step_cursor('one') | 164 stream.step_cursor('one') |
| 157 step.step_closed() | 165 step.step_closed() |
| 158 stream.step_cursor('two') | 166 stream.step_cursor('two') |
| 159 step.step_closed() | 167 step.step_closed() |
| 160 | 168 |
| 161 result = [ | 169 result = [ |
| 162 '@@@STEP_CURSOR one@@@', | 170 '@@@STEP_CURSOR one@@@', |
| 171 '@@@CURRENT_TIMESTAMP@123456@@@', |
| 163 '@@@STEP_STARTED@@@', | 172 '@@@STEP_STARTED@@@', |
| 164 '@@@STEP_CURSOR two@@@', | 173 '@@@STEP_CURSOR two@@@', |
| 174 '@@@CURRENT_TIMESTAMP@123456@@@', |
| 165 '@@@STEP_STARTED@@@', | 175 '@@@STEP_STARTED@@@', |
| 166 '@@@STEP_CURSOR one@@@', | 176 '@@@STEP_CURSOR one@@@', |
| 167 '@@@STEP_CLOSED@@@', | 177 '@@@STEP_CLOSED@@@', |
| 168 '@@@STEP_CURSOR two@@@', | 178 '@@@STEP_CURSOR two@@@', |
| 169 '@@@STEP_CLOSED@@@', | 179 '@@@STEP_CLOSED@@@', |
| 170 ] | 180 ] |
| 171 | 181 |
| 172 self.assertEquals(result, self._getLines()) | 182 self.assertEquals(result, self._getLines()) |
| 173 | 183 |
| 174 | 184 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 continue | 311 continue |
| 302 implemented.add(name) | 312 implemented.add(name) |
| 303 self.assertIsInstance(fn, types.FunctionType) | 313 self.assertIsInstance(fn, types.FunctionType) |
| 304 expected_num_args = annotator.ALL_ANNOTATIONS[name] | 314 expected_num_args = annotator.ALL_ANNOTATIONS[name] |
| 305 self.assertEqual(expected_num_args, fn.func_code.co_argcount - 1) | 315 self.assertEqual(expected_num_args, fn.func_code.co_argcount - 1) |
| 306 self.assertSetEqual(required, implemented) | 316 self.assertSetEqual(required, implemented) |
| 307 | 317 |
| 308 | 318 |
| 309 if __name__ == '__main__': | 319 if __name__ == '__main__': |
| 310 unittest.main() | 320 unittest.main() |
| OLD | NEW |