Skip to content

Commit

Permalink
Add tests for CompileCommand class
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
Pennycook committed Mar 18, 2024
1 parent 322b798 commit 79ed385
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/compile-command/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright (C) 2019-2024 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
70 changes: 70 additions & 0 deletions tests/compile-command/test_compile_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright (C) 2019-2024 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause

import unittest

from codebasin import CompileCommand


class TestCompileCommand(unittest.TestCase):
"""
Test CompileCommand class.
"""

def test_commands_and_arguments(self):
"""Check commands and arguments are not both None"""

with self.assertRaises(ValueError):
CompileCommand("file.cpp", command=None, arguments=None)

with self.assertRaises(ValueError):
instance = {
"file": "file.cpp",
}
CompileCommand.from_json(instance)

def test_command_to_arguments(self):
"""Check commands convert to arguments"""
command = CompileCommand("file.cpp", command="c++ file.cpp")
self.assertEqual(command.arguments, ["c++", "file.cpp"])

instance = {
"file": "file.cpp",
"command": "c++ file.cpp",
}
command = CompileCommand.from_json(instance)
self.assertEqual(command.arguments, ["c++", "file.cpp"])

def test_arguments_to_command(self):
"""Check arguments convert to command"""
command = CompileCommand("file.cpp", arguments=["c++", "file.cpp"])
self.assertEqual(str(command), "c++ file.cpp")

instance = {
"file": "file.cpp",
"arguments": [
"c++",
"file.cpp",
],
}
command = CompileCommand.from_json(instance)
self.assertEqual(str(command), "c++ file.cpp")

def test_empty_command(self):
"""Check empty commands are not supported"""
command = CompileCommand("file.cpp", command="")
self.assertFalse(command.is_supported())

def test_link_command(self):
"""Check link commands are not supported"""
command = CompileCommand("file.o", command="c++ -o a.out file.o")
self.assertFalse(command.is_supported())

def test_valid_command(self):
"""Check valid commands are supported"""
command = CompileCommand("file.cpp", command="c++ file.cpp")
self.assertTrue(command.is_supported())


if __name__ == "__main__":
unittest.main()

0 comments on commit 79ed385

Please sign in to comment.