xdy 0.10.0

Complex RPG dice expression evaluator with histogram support.
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! # Diagnostic test cases
//!
//! Herein are the data-driven test cases for the diagnostics module. The actual
//! test cases are stored in `../../tests/test_parser_errors.txt`, which
//! comprises a series of broken source expressions and their expected
//! diagnostics.

use std::collections::HashSet;

use pretty_assertions::assert_eq;

use crate::{
	Parser,
	diagnostics::{self, DiagnosticKind},
	support::read_error_test_cases
};

////////////////////////////////////////////////////////////////////////////////
//                           Diagnostic test cases.                           //
////////////////////////////////////////////////////////////////////////////////

/// Map a [`DiagnosticKind`] to its test-file name.
fn kind_name(kind: &DiagnosticKind) -> &'static str
{
	match kind
	{
		DiagnosticKind::UnclosedDelimiter { .. } => "UnclosedDelimiter",
		DiagnosticKind::UnopenedDelimiter { .. } => "UnopenedDelimiter",
		DiagnosticKind::MissingRightOperand { .. } => "MissingRightOperand",
		DiagnosticKind::MissingLeftOperand { .. } => "MissingLeftOperand",
		DiagnosticKind::BareIdentifier => "BareIdentifier",
		DiagnosticKind::MissingDiceFaces => "MissingDiceFaces",
		DiagnosticKind::IncompleteDropClause => "IncompleteDropClause",
		DiagnosticKind::IncompleteParameterDefinition =>
		{
			"IncompleteParameterDefinition"
		},
		DiagnosticKind::TrailingInput => "TrailingInput",
		DiagnosticKind::EmptyExpression => "EmptyExpression",
		DiagnosticKind::UnexpectedToken => "UnexpectedToken",
		DiagnosticKind::UnexpectedEof => "UnexpectedEof",
		DiagnosticKind::DuplicateParameter { .. } => "DuplicateParameter",
		DiagnosticKind::BindingCollidesWithParameter { .. } =>
		{
			"BindingCollidesWithParameter"
		},
		DiagnosticKind::DuplicateBinding { .. } => "DuplicateBinding",
		DiagnosticKind::UseBeforeBind { .. } => "UseBeforeBind"
	}
}

/// Test that the diagnostics module produces the expected diagnostics for each
/// test case in `test_parser_errors.txt`.
#[test]
fn test_error_diagnostics()
{
	let test_cases = read_error_test_cases(include_str!(
		"../../tests/test_parser_errors.txt"
	));
	assert!(
		!test_cases.is_empty(),
		"no test cases found in test_parser_errors.txt"
	);

	let mut seen = HashSet::new();
	for (index, case) in test_cases.iter().enumerate()
	{
		assert!(
			seen.insert(case.source),
			"duplicate test case: {:?}",
			case.source
		);

		let result = diagnostics::diagnose(case.source);

		// Check diagnostic count.
		assert_eq!(
			result.diagnostics.len(),
			case.expected_diagnostics.len(),
			"case {}: {:?} — expected {} diagnostics, got {}: {:?}",
			index + 1,
			case.source,
			case.expected_diagnostics.len(),
			result.diagnostics.len(),
			result
				.diagnostics
				.iter()
				.map(|d| format!("{}", d))
				.collect::<Vec<_>>()
		);

		// Check each diagnostic.
		for (di, (actual, expected)) in result
			.diagnostics
			.iter()
			.zip(case.expected_diagnostics.iter())
			.enumerate()
		{
			// Check kind.
			assert_eq!(
				kind_name(&actual.kind),
				expected.kind,
				"case {}: {:?} — diagnostic {} kind mismatch",
				index + 1,
				case.source,
				di + 1
			);

			// Check span.
			assert_eq!(
				(actual.span.start, actual.span.end),
				expected.span,
				"case {}: {:?} — diagnostic {} span mismatch",
				index + 1,
				case.source,
				di + 1
			);

			// Check message.
			assert_eq!(
				actual.message,
				expected.message,
				"case {}: {:?} — diagnostic {} message mismatch",
				index + 1,
				case.source,
				di + 1
			);

			// Check the full Display rendering. This is a redundant but
			// load-bearing assertion: any divergence between the component-wise
			// fields above and the `Display` impl — for any of
			// `DiagnosticKind`, `SourceSpan`, `Diagnostic` — surfaces here with
			// a readable diff.
			assert_eq!(
				format!("{}", actual),
				expected.rendered,
				"case {}: {:?} — diagnostic {} rendered output mismatch",
				index + 1,
				case.source,
				di + 1
			);

			// Check related labels.
			assert_eq!(
				actual.related.len(),
				expected.related.len(),
				"case {}: {:?} — diagnostic {} related count mismatch: \
				 expected {}, got {}",
				index + 1,
				case.source,
				di + 1,
				expected.related.len(),
				actual.related.len()
			);
			for (ri, (actual_rel, expected_rel)) in actual
				.related
				.iter()
				.zip(expected.related.iter())
				.enumerate()
			{
				assert_eq!(
					(actual_rel.span.start, actual_rel.span.end),
					expected_rel.span,
					"case {}: {:?} — diagnostic {} related {} span mismatch",
					index + 1,
					case.source,
					di + 1,
					ri + 1
				);
				assert_eq!(
					actual_rel.message,
					expected_rel.message,
					"case {}: {:?} — diagnostic {} related {} message mismatch",
					index + 1,
					case.source,
					di + 1,
					ri + 1
				);
			}

			// Check suggestions.
			assert_eq!(
				actual.suggestions.len(),
				expected.suggestions.len(),
				"case {}: {:?} — diagnostic {} suggestion count \
				 mismatch: expected {}, got {}",
				index + 1,
				case.source,
				di + 1,
				expected.suggestions.len(),
				actual.suggestions.len()
			);

			for (si, expected_suggestion) in
				expected.suggestions.iter().enumerate()
			{
				let suggestion = &actual.suggestions[si];

				// Check corrected source.
				assert_eq!(
					suggestion.corrected_source,
					expected_suggestion.corrected_source,
					"case {}: {:?} — diagnostic {} suggestion {} \
					 corrected_source mismatch",
					index + 1,
					case.source,
					di + 1,
					si + 1
				);

				// Check that the corrected source parses cleanly
				// if it's a single-diagnostic case.
				if case.expected_diagnostics.len() == 1
				{
					assert!(
						Parser::parse(&suggestion.corrected_source).is_ok(),
						"case {}: {:?} — suggestion {} {:?} does \
						 not parse cleanly",
						index + 1,
						case.source,
						si + 1,
						suggestion.corrected_source
					);
				}

				// Check placeholders.
				assert_eq!(
					suggestion.placeholders.len(),
					expected_suggestion.placeholders.len(),
					"case {}: {:?} — diagnostic {} suggestion {} \
					 placeholder count mismatch",
					index + 1,
					case.source,
					di + 1,
					si + 1
				);

				for (pi, (actual_ph, expected_ph)) in suggestion
					.placeholders
					.iter()
					.zip(expected_suggestion.placeholders.iter())
					.enumerate()
				{
					assert_eq!(
						(actual_ph.span.start, actual_ph.span.end),
						expected_ph.span,
						"case {}: {:?} — diagnostic {} suggestion \
						 {} placeholder {} span mismatch",
						index + 1,
						case.source,
						di + 1,
						si + 1,
						pi + 1
					);
					assert_eq!(
						actual_ph.description,
						expected_ph.description,
						"case {}: {:?} — diagnostic {} suggestion \
						 {} placeholder {} description mismatch",
						index + 1,
						case.source,
						di + 1,
						si + 1,
						pi + 1
					);
					assert_eq!(
						actual_ph.valid_kinds.to_vec(),
						expected_ph.valid_kinds,
						"case {}: {:?} — diagnostic {} suggestion \
						 {} placeholder {} valid_kinds mismatch",
						index + 1,
						case.source,
						di + 1,
						si + 1,
						pi + 1
					);
				}
			}
		}
	}
}

/// Test that all corrected sources produced by the doctor parse cleanly.
#[test]
fn test_corrected_sources_parse()
{
	let test_cases = read_error_test_cases(include_str!(
		"../../tests/test_parser_errors.txt"
	));
	for case in &test_cases
	{
		let result = diagnostics::diagnose(case.source);
		if let Some(corrected) = &result.corrected_source
		{
			assert!(
				Parser::parse(corrected).is_ok(),
				"corrected source for {:?} does not parse: {:?}",
				case.source,
				corrected
			);
		}
	}
}

/// Test that valid programs produce zero diagnostics and an unchanged
/// corrected source.
#[test]
fn test_valid_programs_produce_no_diagnostics()
{
	let valid = vec![
		"0",
		"42",
		"-1",
		"3D6",
		"3d6",
		"1D20 + 5",
		"2D8 - 1D4",
		"3 * 4 + 2",
		"2 ^ 10",
		"10 % 3",
		"(1D6 + 2) * 3",
		"((1 + 2))",
		"{x}",
		"{x}D6",
		"{x}D{y}",
		"x: {x}D6",
		"x, y: {x} + {y}",
		"a, b, c: ({a} + {b}) * {c}",
		"[1:20]",
		"[1:6]",
		"2D[1,2,3]",
		"4D6 drop lowest",
		"4D6 drop highest",
		"4D6 drop lowest 1",
		"8D6 drop lowest 3 drop highest 1",
		"1D6 + 1D8 + 1D10",
	];
	for source in &valid
	{
		let result = diagnostics::diagnose(source);
		assert!(
			result.diagnostics.is_empty(),
			"valid program {:?} produced {} diagnostics: {:?}",
			source,
			result.diagnostics.len(),
			result
				.diagnostics
				.iter()
				.map(|d| format!("{}", d))
				.collect::<Vec<_>>()
		);
		assert_eq!(
			result.corrected_source.as_deref(),
			Some(*source),
			"valid program {:?} corrected_source mismatch",
			source
		);
	}
}

/// A duplicate-parameter program produces exactly one semantic diagnostic,
/// whose [`Display`](std::fmt::Display) rendering surfaces the caret-level
/// position of the duplicate and names the offending identifier.
#[test]
fn test_diagnose_duplicate_parameter_rendered_output()
{
	let result = diagnostics::diagnose("x, x: {x}");
	assert_eq!(result.diagnostics.len(), 1);
	let diag = &result.diagnostics[0];
	assert!(matches!(
		diag.kind,
		DiagnosticKind::DuplicateParameter { ref name } if name == "x"
	));
	assert_eq!(
		format!("{}", diag),
		"duplicate parameter `x` (3..4): parameter `x` is declared more than \
		 once; review references to `x` in the body — one may have meant a \
		 different parameter or an external variable"
	);
	// Related label points at the first occurrence.
	assert_eq!(diag.related.len(), 1);
	assert_eq!(diag.related[0].span.start, 0);
	assert_eq!(diag.related[0].span.end, 1);
	assert_eq!(diag.related[0].message, "first declared here");
}

/// Test that the semantic validator is intentionally bypassed when the
/// fix-and-retry loop had to rewrite the source to obtain a clean parse.
/// Attaching semantic diagnostics (`DuplicateParameter`, etc.) to spans in a
/// fix-synthesized source would point at characters the user never typed, so
/// `diagnose()` runs the validator only when the original parses cleanly.
#[test]
fn test_validator_skipped_after_parse_fix()
{
	// Source has a parser error (`{x` is missing `}`) and a would-be semantic
	// error (`x, x` duplicates a parameter). The fix-and-retry loop synthesizes
	// `x, x: {x}`; we must not then run the validator on that synthesized
	// source and report a duplicate parameter, because the second `x` the user
	// typed is a statement the validator has not been asked to corroborate yet.
	let result = diagnostics::diagnose("x, x: {x");
	assert_eq!(
		result.diagnostics.len(),
		1,
		"expected only the parser diagnostic, got {:?}",
		result
			.diagnostics
			.iter()
			.map(|d| format!("{}", d))
			.collect::<Vec<_>>()
	);
	assert!(
		!matches!(
			result.diagnostics[0].kind,
			DiagnosticKind::DuplicateParameter { .. }
		),
		"validator should not run after the fix-and-retry loop; got {:?}",
		result.diagnostics[0].kind
	);
}

////////////////////////////////////////////////////////////////////////////////
//                       DiagnosticKind Display tests.                        //
////////////////////////////////////////////////////////////////////////////////

/// The [`DiagnosticKind::UnopenedDelimiter`] variant's
/// [`Display`](std::fmt::Display) rendering shows the unmatched closer in
/// backticks. This variant is part of the public API but is not currently
/// emitted by the analyzer (closing brackets without openers surface as
/// [`UnexpectedToken`](DiagnosticKind::UnexpectedToken) today); the direct test
/// guards the `Display` impl regardless.
#[test]
fn test_diagnostic_kind_unopened_delimiter_display()
{
	let kind = DiagnosticKind::UnopenedDelimiter { closer: ')' };
	assert_eq!(format!("{}", kind), "unexpected `)`");
	let kind = DiagnosticKind::UnopenedDelimiter { closer: ']' };
	assert_eq!(format!("{}", kind), "unexpected `]`");
	let kind = DiagnosticKind::UnopenedDelimiter { closer: '}' };
	assert_eq!(format!("{}", kind), "unexpected `}`");
}

/// The [`DiagnosticKind::UnexpectedEof`] variant's
/// [`Display`](std::fmt::Display) renders a fixed human-readable phrase. This
/// variant is reachable only from the catch-all in `analyze_error`, a branch
/// the current pattern set almost never routes to; the direct test guards the
/// `Display` impl regardless.
#[test]
fn test_diagnostic_kind_unexpected_eof_display()
{
	let kind = DiagnosticKind::UnexpectedEof;
	assert_eq!(format!("{}", kind), "unexpected end of input");
}

/// When the catch-all emits an
/// [`UnexpectedToken`](DiagnosticKind::UnexpectedToken) diagnostic, the token
/// span ends at the first whitespace byte after the error position — exercising
/// the `pos + i` arithmetic on the `.find(is_whitespace)` success path that
/// other test cases (whose trailing text contains no interior whitespace) don't
/// reach.
#[test]
fn test_diagnose_unexpected_token_stops_at_whitespace()
{
	let result = diagnostics::diagnose("@\t");
	assert_eq!(result.diagnostics.len(), 1);
	let diag = &result.diagnostics[0];
	assert!(matches!(diag.kind, DiagnosticKind::UnexpectedToken));
	// Span ends at byte offset 1, where the tab begins — not at source.len().
	assert_eq!((diag.span.start, diag.span.end), (0, 1));
	assert_eq!(diag.message, "unexpected `@`");
	assert!(diag.suggestions.is_empty());
}

/// Test that the diagnostics pipeline is fast enough for keystroke-speed
/// invocation.
#[test]
fn test_diagnostics_performance()
{
	let expressions = vec![
		"3D6 + 2",
		"1D20",
		"4D6 drop lowest",
		"2D8 + 1D4 - 3",
		"{x}D{y}",
		"x: {x}D6 + {x}",
		"(1D6 + 2) * 3",
		"[1:20]",
		"1D6 + 1D8 + 1D10",
		"a, b: {a}D{b} + 5",
		// Invalid expressions.
		"3D6 +",
		"(3D6",
		"xD6",
		"3D",
		"4D6 drop",
		"+ 1D6",
		"",
		"3D6)",
		"1 + * 2",
		"3D6 + + 1D3 -",
	];
	let start = std::time::Instant::now();
	for _ in 0..100
	{
		for expr in &expressions
		{
			let _ = diagnostics::diagnose(expr);
		}
	}
	let elapsed = start.elapsed();
	// 2000 diagnose() calls should complete well within 1 second.
	assert!(
		elapsed.as_millis() < 1000,
		"diagnostics too slow: {}ms for 2000 calls",
		elapsed.as_millis()
	);
}