|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | +import unittest |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | +from cbmc_json_parser import SourceLocation |
| 7 | + |
| 8 | + |
| 9 | +def source_json(filename=None, function=None, line=None, column=None): |
| 10 | + result = dict() |
| 11 | + if filename: |
| 12 | + result["file"] = filename |
| 13 | + if function: |
| 14 | + result["function"] = function |
| 15 | + if column: |
| 16 | + result["column"] = column |
| 17 | + if line: |
| 18 | + result["line"] = line |
| 19 | + return result |
| 20 | + |
| 21 | + |
| 22 | +class SourceLocationTest(unittest.TestCase): |
| 23 | + """ Unit tests for SourceLocation """ |
| 24 | + |
| 25 | + def test_source_location_valid_path(self): |
| 26 | + """Path returned by filepath() works for valid paths |
| 27 | +
|
| 28 | + Note: Check is loose because I couldn't find a reliable way to control a real path location. |
| 29 | + """ |
| 30 | + path = tempfile.gettempdir() |
| 31 | + json = source_json(path) |
| 32 | + src_loc = SourceLocation(json) |
| 33 | + possible_output = {path, os.path.relpath(path), os.path.relpath(path, os.path.expanduser("~"))} |
| 34 | + self.assertIn(src_loc.filepath(), possible_output) |
| 35 | + |
| 36 | + def test_source_location_invalid_path(self): |
| 37 | + """Path returned by filepath() returns the input path if it doesn't exist""" |
| 38 | + path = "/rust/made/up/path/lib.rs" |
| 39 | + json = source_json(path) |
| 40 | + src_loc = SourceLocation(json) |
| 41 | + self.assertEqual(src_loc.filepath(), path) |
| 42 | + |
| 43 | + def test_source_location_relative_path(self): |
| 44 | + """Path returned by filepath() is relative if the input is also relative""" |
| 45 | + relpath = "dummy/target.rs" |
| 46 | + json = source_json(relpath) |
| 47 | + src_loc = SourceLocation(json) |
| 48 | + self.assertEqual(src_loc.filepath(), relpath) |
| 49 | + |
| 50 | + def test_source_location_absolute_cwd_path(self): |
| 51 | + """Path returned by filepath() is relative to current directory |
| 52 | +
|
| 53 | + Note that the file may not exist that this should still hold. |
| 54 | + """ |
| 55 | + relpath = "dummy/target.rs" |
| 56 | + path = os.path.join(os.getcwd(), relpath) |
| 57 | + self.assertNotEqual(relpath, path) |
| 58 | + |
| 59 | + json = source_json(path) |
| 60 | + src_loc = SourceLocation(json) |
| 61 | + self.assertEqual(src_loc.filepath(), relpath) |
| 62 | + |
| 63 | + def test_source_location_absolute_user_path(self): |
| 64 | + """Path returned by filepath() is relative to current directory |
| 65 | +
|
| 66 | + Note that the file may not exist that this should still hold. |
| 67 | + """ |
| 68 | + relpath = "~/dummy/target.rs" |
| 69 | + path = os.path.expanduser(relpath) |
| 70 | + self.assertNotEqual(relpath, path) |
| 71 | + |
| 72 | + json = source_json(path) |
| 73 | + src_loc = SourceLocation(json) |
| 74 | + self.assertEqual(src_loc.filepath(), relpath) |
| 75 | + |
| 76 | + def test_source_location_relative_user_path(self): |
| 77 | + """Path returned by filepath() is relative to current directory |
| 78 | +
|
| 79 | + Note that the file may not exist that this should still hold. |
| 80 | + """ |
| 81 | + relpath = "~/dummy/target.rs" |
| 82 | + json = source_json(relpath) |
| 83 | + src_loc = SourceLocation(json) |
| 84 | + self.assertEqual(src_loc.filepath(), relpath) |
| 85 | + |
| 86 | + def test_source_location_with_no_path(self): |
| 87 | + """Function filepath() is None if no file is given in the input""" |
| 88 | + json = source_json(function="main") |
| 89 | + src_loc = SourceLocation(json) |
| 90 | + self.assertIsNone(src_loc.filepath()) |
| 91 | + |
| 92 | + def test_source_location_output_format(self): |
| 93 | + """Check that output includes all the information provided""" |
| 94 | + path = "/rust/made/up/path/lib.rs" |
| 95 | + function = "harness" |
| 96 | + line = 10 |
| 97 | + column = 8 |
| 98 | + json = source_json(path, function, line, column) |
| 99 | + src_loc = str(SourceLocation(json)) |
| 100 | + self.assertIn(path, src_loc) |
| 101 | + self.assertIn(f"{path}:{line}:{column}", src_loc) |
| 102 | + self.assertIn(function, src_loc) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == '__main__': |
| 106 | + unittest.main() |
0 commit comments