|
| 1 | +# Copyright (C) 2019-2024 Intel Corporation |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +from codebasin import CompileCommand |
| 7 | + |
| 8 | + |
| 9 | +class TestCompileCommand(unittest.TestCase): |
| 10 | + """ |
| 11 | + Test CompileCommand class. |
| 12 | + """ |
| 13 | + |
| 14 | + def test_commands_and_arguments(self): |
| 15 | + """Check commands and arguments are not both None""" |
| 16 | + |
| 17 | + with self.assertRaises(ValueError): |
| 18 | + CompileCommand("file.cpp", command=None, arguments=None) |
| 19 | + |
| 20 | + with self.assertRaises(ValueError): |
| 21 | + instance = { |
| 22 | + "file": "file.cpp", |
| 23 | + } |
| 24 | + CompileCommand.from_json(instance) |
| 25 | + |
| 26 | + def test_command_to_arguments(self): |
| 27 | + """Check commands convert to arguments""" |
| 28 | + command = CompileCommand("file.cpp", command="c++ file.cpp") |
| 29 | + self.assertEqual(command.arguments, ["c++", "file.cpp"]) |
| 30 | + |
| 31 | + instance = { |
| 32 | + "file": "file.cpp", |
| 33 | + "command": "c++ file.cpp", |
| 34 | + } |
| 35 | + command = CompileCommand.from_json(instance) |
| 36 | + self.assertEqual(command.arguments, ["c++", "file.cpp"]) |
| 37 | + |
| 38 | + def test_arguments_to_command(self): |
| 39 | + """Check arguments convert to command""" |
| 40 | + command = CompileCommand("file.cpp", arguments=["c++", "file.cpp"]) |
| 41 | + self.assertEqual(str(command), "c++ file.cpp") |
| 42 | + |
| 43 | + instance = { |
| 44 | + "file": "file.cpp", |
| 45 | + "arguments": [ |
| 46 | + "c++", |
| 47 | + "file.cpp", |
| 48 | + ], |
| 49 | + } |
| 50 | + command = CompileCommand.from_json(instance) |
| 51 | + self.assertEqual(str(command), "c++ file.cpp") |
| 52 | + |
| 53 | + def test_empty_command(self): |
| 54 | + """Check empty commands are not supported""" |
| 55 | + command = CompileCommand("file.cpp", command="") |
| 56 | + self.assertFalse(command.is_supported()) |
| 57 | + |
| 58 | + def test_link_command(self): |
| 59 | + """Check link commands are not supported""" |
| 60 | + command = CompileCommand("file.o", command="c++ -o a.out file.o") |
| 61 | + self.assertFalse(command.is_supported()) |
| 62 | + |
| 63 | + def test_valid_command(self): |
| 64 | + """Check valid commands are supported""" |
| 65 | + command = CompileCommand("file.cpp", command="c++ file.cpp") |
| 66 | + self.assertTrue(command.is_supported()) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + unittest.main() |
0 commit comments