| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 from crash.stacktrace import StackFrame | 4 from crash.stacktrace import StackFrame |
| 5 from crash.results import AnalysisInfo |
| 5 from crash.results import Result | 6 from crash.results import Result |
| 6 from crash.results import MatchResult | 7 from crash.results import MatchResult |
| 7 from crash.scorers.min_distance import MinDistance | 8 from crash.scorers.min_distance import MinDistance |
| 8 from crash.scorers.test.scorer_test_suite import ScorerTestSuite | 9 from crash.scorers.test.scorer_test_suite import ScorerTestSuite |
| 9 | 10 |
| 10 | 11 |
| 11 class MinDistanceTest(ScorerTestSuite): | 12 class MinDistanceTest(ScorerTestSuite): |
| 12 | 13 |
| 13 def testGetMetric(self): | 14 def testGetMetric(self): |
| 14 dummy_changelog = self._GetDummyChangeLog() | 15 dummy_changelog = self._GetDummyChangeLog() |
| 15 match_result = MatchResult(dummy_changelog, 'src/', '') | 16 match_result = MatchResult(dummy_changelog, 'src/', '') |
| 16 match_result.file_to_analysis_info = { | 17 match_result.file_to_analysis_info = { |
| 17 'file': {'min_distance': 0, 'min_distance_frame': None} | 18 'file': AnalysisInfo(min_distance=0, min_distance_frame=None) |
| 18 } | 19 } |
| 19 | 20 |
| 20 self.assertEqual(MinDistance().GetMetric(match_result), 0) | 21 self.assertEqual(MinDistance().GetMetric(match_result), 0) |
| 21 | 22 |
| 22 result = Result(dummy_changelog, 'src/', '') | 23 result = Result(dummy_changelog, 'src/', '') |
| 23 self.assertEqual(MinDistance().GetMetric(result), float('inf')) | 24 self.assertEqual(MinDistance().GetMetric(result), float('inf')) |
| 24 | 25 |
| 25 def testScore(self): | 26 def testScore(self): |
| 26 self.assertEqual(MinDistance().Score(0), 1) | 27 self.assertEqual(MinDistance().Score(0), 1) |
| 27 self.assertEqual(MinDistance().Score(30), 0.8) | 28 self.assertEqual(MinDistance().Score(30), 0.8) |
| 28 self.assertEqual(MinDistance().Score(60), 0) | 29 self.assertEqual(MinDistance().Score(60), 0) |
| 29 | 30 |
| 30 def testReason(self): | 31 def testReason(self): |
| 31 self.assertEqual(MinDistance().Reason(0, 1), | 32 self.assertEqual(MinDistance().Reason(0, 1), |
| 32 ('MinDistance', 1, 'Minimum distance is 0')) | 33 ('MinDistance', 1, 'Minimum distance is 0')) |
| 33 self.assertEqual(MinDistance().Reason(60, 0), | 34 self.assertEqual(MinDistance().Reason(60, 0), |
| 34 None) | 35 None) |
| 35 | 36 |
| 36 def testChangedFiles(self): | 37 def testChangedFiles(self): |
| 37 dummy_changelog = self._GetDummyChangeLog() | 38 dummy_changelog = self._GetDummyChangeLog() |
| 38 result = MatchResult(dummy_changelog, 'src/', '') | 39 result = MatchResult(dummy_changelog, 'src/', '') |
| 39 frame = StackFrame(0, 'src/', 'func', 'f.cc', 'a/b/src/f.cc', [2], | 40 frame = StackFrame(0, 'src/', 'func', 'f.cc', 'a/b/src/f.cc', [2], |
| 40 repo_url='https://repo_url') | 41 repo_url='https://repo_url') |
| 41 result.file_to_stack_infos = { | 42 result.file_to_stack_infos = { |
| 42 'src/f.cc': [(frame, 0)] | 43 'src/f.cc': [(frame, 0)] |
| 43 } | 44 } |
| 44 result.file_to_analysis_info = { | 45 result.file_to_analysis_info = { |
| 45 'src/f.cc': {'min_distance': 0, 'min_distance_frame': frame} | 46 'src/f.cc': AnalysisInfo(min_distance=0, min_distance_frame=frame) |
| 46 } | 47 } |
| 47 | 48 |
| 48 self.assertEqual(MinDistance().ChangedFiles(result, 1), | 49 self.assertEqual(MinDistance().ChangedFiles(result, 1), |
| 49 [{'file': 'f.cc', | 50 [{'file': 'f.cc', |
| 50 'blame_url': ('https://repo_url/+blame/%s/f.cc#2' % | 51 'blame_url': ('https://repo_url/+blame/%s/f.cc#2' % |
| 51 dummy_changelog.revision), | 52 dummy_changelog.revision), |
| 52 'info': 'Minimum distance (LOC) 0, frame #0'}]) | 53 'info': 'Minimum distance (LOC) 0, frame #0'}]) |
| 53 | 54 |
| 54 def testChangedFilesInfMinDistance(self): | 55 def testChangedFilesInfMinDistance(self): |
| 55 dummy_changelog = self._GetDummyChangeLog() | 56 dummy_changelog = self._GetDummyChangeLog() |
| 56 result = MatchResult(dummy_changelog, 'src/', '') | 57 result = MatchResult(dummy_changelog, 'src/', '') |
| 57 frame = StackFrame(0, 'src/', 'func', 'f.cc', 'a/b/src/f.cc', [2], | 58 frame = StackFrame(0, 'src/', 'func', 'f.cc', 'a/b/src/f.cc', [2], |
| 58 repo_url='https://repo_url') | 59 repo_url='https://repo_url') |
| 59 result.file_to_stack_infos = { | 60 result.file_to_stack_infos = { |
| 60 'src/f.cc': [(frame, 0)] | 61 'src/f.cc': [(frame, 0)] |
| 61 } | 62 } |
| 62 result.file_to_analysis_info = { | 63 result.file_to_analysis_info = { |
| 63 'src/f.cc': {'min_distance': float('inf'), 'min_distance_frame': frame} | 64 'src/f.cc': AnalysisInfo(min_distance=float('inf'), |
| 65 min_distance_frame=frame) |
| 64 } | 66 } |
| 65 | 67 |
| 66 self.assertIsNone(MinDistance().ChangedFiles(result, 0)) | 68 self.assertIsNone(MinDistance().ChangedFiles(result, 0)) |
| 67 | 69 |
| 68 def testChangedFilesSkipFileInfMinDistance(self): | 70 def testChangedFilesSkipFileInfMinDistance(self): |
| 69 dummy_changelog = self._GetDummyChangeLog() | 71 dummy_changelog = self._GetDummyChangeLog() |
| 70 result = MatchResult(dummy_changelog, 'src/', '') | 72 result = MatchResult(dummy_changelog, 'src/', '') |
| 71 frame0 = StackFrame(0, 'src/', 'func0', 'f0.cc', 'a/b/src/f0.cc', [2], | 73 frame0 = StackFrame(0, 'src/', 'func0', 'f0.cc', 'a/b/src/f0.cc', [2], |
| 72 repo_url='https://repo_url') | 74 repo_url='https://repo_url') |
| 73 frame1 = StackFrame(1, 'src/', 'func1', 'f1.cc', 'a/b/src/f1.cc', [5], | 75 frame1 = StackFrame(1, 'src/', 'func1', 'f1.cc', 'a/b/src/f1.cc', [5], |
| 74 repo_url='https://repo_url') | 76 repo_url='https://repo_url') |
| 75 result.file_to_stack_infos = { | 77 result.file_to_stack_infos = { |
| 76 'src/f0.cc': [(frame0, 0)], | 78 'src/f0.cc': [(frame0, 0)], |
| 77 'src/f1.cc': [(frame1, 0)] | 79 'src/f1.cc': [(frame1, 0)] |
| 78 } | 80 } |
| 79 result.file_to_analysis_info = { | 81 result.file_to_analysis_info = { |
| 80 'src/f0.cc': {'min_distance': 0, | 82 'src/f0.cc': AnalysisInfo(min_distance=0, |
| 81 'min_distance_frame': frame0}, | 83 min_distance_frame=frame0), |
| 82 'src/f1.cc': {'min_distance': float('inf'), | 84 'src/f1.cc': AnalysisInfo(min_distance=float('inf'), |
| 83 'min_distance_frame': frame1}, | 85 min_distance_frame=frame1), |
| 84 } | 86 } |
| 85 | 87 |
| 86 self.assertEqual(MinDistance().ChangedFiles(result, 1), | 88 self.assertEqual(MinDistance().ChangedFiles(result, 1), |
| 87 [{'file': 'f0.cc', | 89 [{'file': 'f0.cc', |
| 88 'blame_url': ('https://repo_url/+blame/%s/f0.cc#2' % | 90 'blame_url': ('https://repo_url/+blame/%s/f0.cc#2' % |
| 89 dummy_changelog.revision), | 91 dummy_changelog.revision), |
| 90 'info': 'Minimum distance (LOC) 0, frame #0'}]) | 92 'info': 'Minimum distance (LOC) 0, frame #0'}]) |
| 91 | 93 |
| OLD | NEW |