Skip to content

Commit 8a59cd4

Browse files
committed
test: unreachable warning rendering tests
1 parent e36f4b6 commit 8a59cd4

12 files changed

Lines changed: 96 additions & 10 deletions

File tree

guppylang-internals/src/guppylang_internals/cfg/builder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727
from guppylang_internals.checker.errors.type_errors import WrongNumberOfArgsError
2828
from guppylang_internals.diagnostic import Error, Warning
29-
from guppylang_internals.error import GuppyError, InternalGuppyError, emit_warning
29+
from guppylang_internals.error import GuppyError, InternalGuppyError
3030
from guppylang_internals.experimental import (
3131
check_lists_enabled,
3232
check_modifiers_enabled,
@@ -48,6 +48,7 @@
4848
)
4949
from guppylang_internals.span import Span, to_span
5050
from guppylang_internals.tys.ty import NoneType, UnitaryFlags
51+
from guppylang_internals.warning import emit_warning
5152

5253
# In order to build expressions, need an endless stream of unique temporary variables
5354
# to store intermediate results
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
! Unreachable
2+
,-[3:5]
3+
2 | return 0
4+
3 | x = 1
5+
: ^^|^^
6+
: `-- This code is not reachable
7+
`----
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Warning: Unreachable (at <unknown>:3:4)
2+
|
3+
1 | def foo():
4+
2 | return 0
5+
3 | x = 1
6+
| ^^^^^ This code is not reachable

tests/diagnostics/test_diagnostics_rendering.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from typing import ClassVar
66

7+
from guppylang_internals.cfg.builder import UnreachableWarning
78
from guppylang_internals.diagnostic import (
89
Diagnostic,
910
DiagnosticsRenderer,
@@ -164,6 +165,13 @@ class MySubDiagnostic(Note):
164165
run_test(source, diagnostic, snapshot, request)
165166

166167

168+
def test_unreachable_warning(snapshot, request):
169+
source = "def foo():\n return 0\n x = 1\n"
170+
span = Span(Loc(file, 3, 4), Loc(file, 3, 9))
171+
diagnostic = UnreachableWarning(span)
172+
run_test(source, diagnostic, snapshot, request)
173+
174+
167175
def test_context(snapshot, request):
168176
source = "super_apple := apple ** 2\nlemon := orange - apple\napple == orange"
169177
span = Span(Loc(file, 3, 6), Loc(file, 3, 8))

tests/diagnostics/test_miette_rendering.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import ClassVar
66

77
import pytest
8+
from guppylang_internals.cfg.builder import UnreachableWarning
89
from guppylang_internals.diagnostic import (
910
Diagnostic,
1011
DiagnosticLevel,
@@ -107,6 +108,15 @@ class WarningDiag(Error):
107108
run_miette_test(source, diagnostic, snapshot, request)
108109

109110

111+
def test_unreachable_warning(snapshot, request):
112+
"""Test rendering of the concrete unreachable-code warning."""
113+
114+
source = "def foo():\n return 0\n x = 1\n"
115+
span = Span(Loc(file, 3, 4), Loc(file, 3, 9))
116+
diagnostic = UnreachableWarning(span)
117+
run_miette_test(source, diagnostic, snapshot, request)
118+
119+
110120
def test_complete_issue_example(snapshot, request):
111121
"""Test complete example from issue #968 with primary + sub-diagnostics."""
112122

tests/error/test_misc_errors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import pathlib
2-
import pytest
32

3+
import pytest
44
from guppylang import guppy
5+
56
from tests.error.util import run_error_test
67

78
path = pathlib.Path(__file__).parent.resolve() / "misc_errors"

tests/error/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def filter_traceback_not_containing(s: str, disallowed_regex: re.Pattern[str]) -
3737

3838
return "\n".join(result)
3939

40+
4041
def run_error_test(file, capsys, snapshot):
4142
file = pathlib.Path(file)
4243

tests/integration/test_unreachable.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from guppylang import GuppyWarning
77
from guppylang_internals.error import GuppyError
88
from guppylang.std.quantum import discard, h
9-
from tests.util import compile_guppy
9+
from tests.util import compile_guppy, guppy_warning_records
1010

1111

1212
def assert_unreachable_warning_emitted(fn):
@@ -16,8 +16,9 @@ def assert_unreachable_warning_emitted(fn):
1616
warnings.simplefilter("always")
1717
result = fn()
1818

19-
assert len(records) == 1
20-
warning = records[0]
19+
guppy_records = guppy_warning_records(records)
20+
assert len(guppy_records) == 1
21+
warning = guppy_records[0]
2122
assert warning.category is GuppyWarning
2223
assert str(warning.message) == "Unreachable: This code is not reachable"
2324
return result
@@ -166,8 +167,9 @@ def test() -> int:
166167
return 1
167168
return 0
168169

169-
assert len(records) == 1
170-
warning = records[0]
170+
guppy_records = guppy_warning_records(records)
171+
assert len(guppy_records) == 1
172+
warning = guppy_records[0]
171173
assert warning.category is GuppyWarning
172174
assert warning.filename.endswith("test_unreachable.py")
173175
assert str(warning.message) == "Unreachable: This code is not reachable"
@@ -185,8 +187,9 @@ def test() -> int:
185187
x = 1
186188
return x
187189

188-
assert len(records) == 1
189-
warning = records[0]
190+
guppy_records = guppy_warning_records(records)
191+
assert len(guppy_records) == 1
192+
warning = guppy_records[0]
190193
assert warning.category is GuppyWarning
191194
assert warning.filename.endswith("test_unreachable.py")
192195
assert str(warning.message) == "Unreachable: This code is not reachable"
@@ -205,4 +208,5 @@ def test() -> int:
205208
return 1.0
206209
return 0
207210

208-
assert len(records) == 0
211+
guppy_records = guppy_warning_records(records)
212+
assert len(guppy_records) == 0
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import importlib
2+
import pathlib
3+
import warnings
4+
5+
from tests.util import guppy_warning_records
6+
7+
8+
def run_warning_test(file: pathlib.Path, capsys, snapshot) -> None:
9+
"""Snapshot rich warning rendering for a module-level compiler warning."""
10+
with warnings.catch_warnings(record=True) as records:
11+
warnings.simplefilter("always")
12+
importlib.import_module(f"tests.integration.{file.parent.name}.{file.stem}")
13+
14+
guppy_records = guppy_warning_records(records)
15+
assert len(guppy_records) == 1
16+
err = capsys.readouterr().err
17+
err = err.replace(str(file), "$FILE")
18+
19+
snapshot.snapshot_dir = str(file.parent)
20+
snapshot.assert_match(err, file.with_suffix(".err").name)
21+
22+
23+
def test_check_warning(capsys, snapshot):
24+
"""Rich warnings should snapshot the rendered diagnostic output."""
25+
file = (
26+
pathlib.Path(__file__).parent.resolve() / "warning_cases" / "check_warning.py"
27+
)
28+
run_warning_test(file, capsys, snapshot)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)