Skip to content

Commit b207244

Browse files
committed
Add documentation
1 parent 7e881d7 commit b207244

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

docs/_docs/contributing/testing.md

+132
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ of the `tests/` directory. A small selection of test categories include:
4141
- `tests/pos` – tests that should compile: pass if compiles successfully.
4242
- `tests/neg` – should not compile: pass if fails compilation. Useful, e.g., to test an expected compiler error.
4343
- `tests/run` – these tests not only compile but are also run. Must include at least a `@main def Test = ...`.
44+
- `tests/debug` – these tests are compiled but also debugged. As for `tests/run` they must include at least a `@main def Test = ...`
45+
See [Debug Tests](#debug-tests).
4446

4547
### Naming and Running a Test Case
4648

@@ -205,6 +207,136 @@ $ sbt
205207
> testCompilation --from-tasty
206208
```
207209

210+
## Debug Tests
211+
212+
Debug tests are a variant of compilation tests located in `compiler/tests/debug`.
213+
Similar to `tests/run`, each test case is executed.
214+
However, instead of verifying the program's output, a debugger is attached to the running program to validate a predefined debug scenario.
215+
216+
The debug scenario is specified in the `.check` file associated with each test case.
217+
It consists of a sequence of debug steps that described the debugger interactions and outcomes.
218+
219+
**Example debug scenario**:
220+
```
221+
// Pause on a breakpoint in class Test$ on line 5
222+
break Test$ 5
223+
224+
// Stepping in should go to line 10
225+
step 10
226+
227+
// Next should go to line 11
228+
next 11
229+
230+
// Evaluating the expression x should return 42
231+
eval x
232+
result 42
233+
```
234+
235+
To run all the debug tests:
236+
```
237+
sbt 'scala3-compiler/testOnly dotty.tools.debug.DebugTests'
238+
```
239+
240+
### Debug Steps
241+
242+
#### Breakpoint
243+
244+
Syntax:
245+
246+
```
247+
break ${runtime class} ${line number}
248+
```
249+
250+
Examples:
251+
252+
```
253+
break Test$ 5
254+
break example.A 10
255+
break example.A$B$1 12
256+
```
257+
258+
A breakpoint is defined by a fully-qualified class name and a source line.
259+
260+
All breakpoints of a debug scenario are configured before the program starts.
261+
262+
When the program pauses on a breakpoint, we check the class name and source line of the current frame.
263+
264+
### Step in
265+
266+
Syntax:
267+
```
268+
step ${expected line number or method name}
269+
```
270+
271+
Examples:
272+
```
273+
step 10
274+
step println
275+
```
276+
277+
A `step` request expects the program to enter into the called method or go to the next instruction.
278+
After a step request, we check that the source line (or method name) of the current frame matches the expected one.
279+
280+
Typically we use a source line when we stay in the same source file and a method name when we step in a library or JDK class.
281+
282+
### Next
283+
284+
A `next` request behaves similarly to `step` but jumps over a method call and stops on the next instruction.
285+
286+
Syntax:
287+
```
288+
next ${expected line number or method name}
289+
```
290+
291+
Examples:
292+
```
293+
next 10
294+
next println
295+
```
296+
297+
### Evaluation
298+
299+
Syntax:
300+
```
301+
eval ${expression}
302+
result ${expected output}
303+
304+
// or in case an error is expected
305+
eval ${expression}
306+
error ${expected message}
307+
```
308+
309+
It also supports multi-line expressions and multi-line error messages.
310+
311+
Examples:
312+
```
313+
eval finobacci(2)
314+
result 55
315+
316+
eval
317+
def square(x: Int): Int =
318+
x * x
319+
square(2)
320+
result 4
321+
322+
eval foo
323+
error
324+
<expression>:1:0
325+
1 |foo
326+
|^^^
327+
| Not found: foo
328+
```
329+
330+
An `eval` request verifies that an expression can be evaluated by the `ExpressionCompiler` during a debugging session.
331+
A `result` assertion checks the evaluation produced the expected output, while an `error` assertion checks the compilation failed with the expected error message.
332+
333+
When the evaluation throws an exception, the exception is returned as a result, not an error.
334+
335+
```
336+
eval throw new Exception("foo")
337+
result java.lang.Exception: foo
338+
```
339+
208340
## Unit Tests
209341

210342
Unit tests cover the other areas of the compiler, such as interactions with the REPL, scripting tools and more.

0 commit comments

Comments
 (0)