Skip to content

Commit a5449d9

Browse files
committed
Reimplement basic golden testing
1 parent d8ffc6e commit a5449d9

File tree

19 files changed

+1243
-0
lines changed

19 files changed

+1243
-0
lines changed

pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ dependencies:
1212
meta: ^1.16.0
1313

1414
dev_dependencies:
15+
collection: ^1.19.1
1516
night_raven_insight: ^4.0.0
17+
path: ^1.9.1
1618
test: ^1.26.3

test/golden_test.dart

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/// End-to-end golden testing for `package:opal`.
2+
///
3+
/// ## Overview
4+
///
5+
/// Golden tests verify that language tokenization produces consistent,
6+
/// correct output by comparing against pre-generated reference files.
7+
///
8+
/// ## Testing structure
9+
///
10+
/// Tests are organized by language in `test/goldens/<language>/`:
11+
///
12+
/// - `.in` files contain input source code to tokenize.
13+
/// - `.out` files contain expected tokenization output.
14+
///
15+
/// Tests are automatically discovered based on the directory structure.
16+
/// Each language directory becomes a test group and
17+
/// each `.in` file becomes a test case.
18+
///
19+
/// ## Running tests
20+
///
21+
/// Run all golden tests:
22+
///
23+
/// ```bash
24+
/// dart test test/golden_test.dart
25+
/// ```
26+
///
27+
/// Run tests for a specific language:
28+
///
29+
/// ```bash
30+
/// dart test test/golden_test.dart --plain-name "dart"
31+
/// ```
32+
///
33+
/// ## Generating golden files
34+
///
35+
/// To create or update golden files:
36+
///
37+
/// 1. Create/edit the `.in` file in `test/goldens/<language>/`.
38+
/// 2. Set [generateGoldens] to `true` in this file.
39+
/// 3. Run: `dart test test/golden_test.dart`.
40+
/// 4. Review the generated `.out` files.
41+
/// 5. Set [generateGoldens] back to `false`.
42+
///
43+
/// The generator automatically discovers all `.in` files and
44+
/// generates the corresponding `.out` files.
45+
library;
46+
47+
import 'package:test/test.dart';
48+
49+
import 'goldens/golden_test_util.dart';
50+
51+
/// Set to `true` to generate golden files instead of running tests.
52+
const generateGoldens = false;
53+
54+
void main() {
55+
// Discover all languages and their test cases from the file system.
56+
final suite = GoldenTestSuite.discover();
57+
58+
if (generateGoldens) {
59+
// Create or update all golden files.
60+
group('Generate golden files', () {
61+
test('generate all', () async {
62+
for (final language in suite.languages) {
63+
for (final goldenTest in language.tests) {
64+
await goldenTest.generate(suite.registry);
65+
print('Generated: ${language.name}/${goldenTest.name}.out');
66+
}
67+
}
68+
});
69+
});
70+
71+
return;
72+
}
73+
74+
// Run all discovered golden tests.
75+
for (final language in suite.languages) {
76+
group(language.name, () {
77+
for (final goldenTest in language.tests) {
78+
test(goldenTest.name, () async {
79+
await goldenTest.run(suite.registry);
80+
});
81+
}
82+
});
83+
}
84+
}

test/goldens/dart/hello-world.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const String message = 'Hello, world!';
2+
3+
void main() {
4+
// Print the message.
5+
print('The message is: $message');
6+
}

test/goldens/dart/hello-world.out

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const String message = 'Hello, world!';
2+
^^^^^
3+
source-code-dart-keyword-const
4+
^
5+
source-code-dart-whitespace
6+
^^^^^^
7+
source-code-dart-identifier-type-built-in
8+
^
9+
source-code-dart-whitespace
10+
^^^^^^^
11+
source-code-dart-identifier
12+
^
13+
source-code-dart-whitespace
14+
^
15+
source-code-dart-keyword-operator
16+
^
17+
source-code-dart-whitespace
18+
^
19+
source-code-dart-literal-string-literal-string-begin
20+
^^^^^^^^^^^^^
21+
source-code-dart-literal-string-literal-string-unquoted
22+
^
23+
source-code-dart-literal-string-literal-string-end
24+
^
25+
source-code-dart-punctuation
26+
27+
void main() {
28+
^^^^
29+
source-code-dart-identifier-type-built-in
30+
^
31+
source-code-dart-whitespace
32+
^^^^
33+
source-code-dart-identifier-function
34+
^
35+
source-code-dart-punctuation
36+
^
37+
source-code-dart-punctuation
38+
^
39+
source-code-dart-whitespace
40+
^
41+
source-code-dart-punctuation
42+
// Print the message.
43+
^^
44+
source-code-dart-whitespace
45+
^^^^^^^^^^^^^^^^^^^^^
46+
source-code-dart-comment-line
47+
print('The message is: $message');
48+
^^
49+
source-code-dart-whitespace
50+
^^^^^
51+
source-code-dart-identifier-function
52+
^
53+
source-code-dart-punctuation
54+
^
55+
source-code-dart-literal-string-literal-string-begin
56+
^^^^^^^^^^^^^^^^
57+
source-code-dart-literal-string-literal-string-unquoted
58+
^^^^^^^^
59+
source-code-dart-literal-string-literal-string-unquoted-literal-string-interpolation
60+
^
61+
source-code-dart-literal-string-literal-string-end
62+
^
63+
source-code-dart-punctuation
64+
^
65+
source-code-dart-punctuation
66+
}
67+
^
68+
source-code-dart-punctuation

0 commit comments

Comments
 (0)