-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_clang_format.py
More file actions
98 lines (82 loc) · 3.03 KB
/
test_clang_format.py
File metadata and controls
98 lines (82 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import pytest
from pathlib import Path
from cpp_linter_hooks.clang_format import run_clang_format
@pytest.mark.parametrize(
("args", "expected_retval"),
(
(["--style=Google"], (0, "")),
(["--style=Google", "--version=16"], (0, "")),
(["--style=Google", "--version=17"], (0, "")),
(["--style=Google", "--version=18"], (0, "")),
(["--style=Google", "--version=19"], (0, "")),
(["--style=Google", "--version=20"], (0, "")),
),
)
def test_run_clang_format_valid(args, expected_retval, tmp_path):
# copy test file to tmp_path to prevent modifying repo data
test_file = tmp_path / "main.c"
test_file.write_bytes(Path("testing/main.c").read_bytes())
ret = run_clang_format(args + [str(test_file)])
assert ret == expected_retval
assert test_file.read_text() == Path("testing/good.c").read_text()
@pytest.mark.parametrize(
("args", "expected_retval"),
(
(
[
"--style=Google",
],
1,
),
(["--style=Google", "--version=16"], 1),
(["--style=Google", "--version=17"], 1),
(["--style=Google", "--version=18"], 1),
(["--style=Google", "--version=19"], 1),
(["--style=Google", "--version=20"], 1),
),
)
def test_run_clang_format_invalid(args, expected_retval, tmp_path):
# non existent file
test_file = tmp_path / "main.c"
ret, _ = run_clang_format(args + [str(test_file)])
assert ret == expected_retval
@pytest.mark.parametrize(
("args", "expected_retval"),
(
(
[
"--style=Google",
],
1,
),
),
)
def test_run_clang_format_dry_run(args, expected_retval, tmp_path):
# copy test file to tmp_path to prevent modifying repo data
test_file = tmp_path / "main.c"
ret, _ = run_clang_format(["--dry-run", str(test_file)])
assert ret == -1 # Dry run should not fail
def test_run_clang_format_verbose(tmp_path):
"""Test that verbose option works and provides detailed output."""
# copy test file to tmp_path to prevent modifying repo data
test_file = tmp_path / "main.c"
test_file.write_bytes(Path("testing/main.c").read_bytes())
# Test with verbose flag
ret, _ = run_clang_format(["--verbose", "--style=Google", str(test_file)])
# Should succeed
assert ret == 0
# Should have verbose output (will be printed to stderr, not returned)
# The function should still return successfully
assert test_file.read_text() == Path("testing/good.c").read_text()
def test_run_clang_format_verbose_error(tmp_path):
"""Test that verbose option provides useful error information."""
test_file = tmp_path / "main.c"
test_file.write_bytes(Path("testing/main.c").read_bytes())
# Test with verbose flag and invalid style
ret, output = run_clang_format(
["--verbose", "--style=InvalidStyle", str(test_file)]
)
# Should fail
assert ret != 0
# Should have error message in output
assert "Invalid value for -style" in output