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