Skip to content

Commit 79ed385

Browse files
committed
Add tests for CompileCommand class
- Test that command and arguments fields are handled correctly. - Test that we can identify which commands we can/should emulate. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 322b798 commit 79ed385

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

tests/compile-command/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (C) 2019-2024 Intel Corporation
2+
# SPDX-License-Identifier: BSD-3-Clause
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)