Skip to content

Commit f0dcba2

Browse files
committed
[macros] Add golden tests for model JSON.
1 parent 341764a commit f0dcba2

File tree

7 files changed

+199
-0
lines changed

7 files changed

+199
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"configVersion": 2,
3+
"packages": [
4+
{
5+
"name": "goldens",
6+
"rootUri": "../",
7+
"packageUri": "lib/",
8+
"languageVersion": "3.4"
9+
}
10+
],
11+
"generated": "2024-06-06T15:09:11.556114Z",
12+
"generator": "pub",
13+
"generatorVersion": "3.5.0-edge"
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
abstract class Foo {
2+
abstract int bar;
3+
}
4+
5+
class Baz {
6+
int get x => 0;
7+
final int foo = 3;
8+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"package:goldens/g1.dart": {
3+
"Foo": {
4+
"properties": [
5+
"class",
6+
"abstract"
7+
],
8+
"annotations": [],
9+
"supertype": "dart:core/object.dart#Object",
10+
"interfaces": [],
11+
"members": {
12+
"bar": {
13+
"properties": [
14+
"abstract",
15+
"field"
16+
]
17+
}
18+
}
19+
},
20+
"Baz": {
21+
"properties": [
22+
"class"
23+
],
24+
"annotations": [],
25+
"supertype": "dart:core/object.dart#Object",
26+
"interfaces": [],
27+
"members": {
28+
"foo": {
29+
"properties": [
30+
"field"
31+
]
32+
},
33+
"x": {
34+
"properties": [
35+
"getter"
36+
]
37+
}
38+
}
39+
}
40+
},
41+
"dart:core": {
42+
"Object": {
43+
"properties": [
44+
"class"
45+
],
46+
"annotations": [
47+
{
48+
"type": "dart:core/annotations.dart#pragma",
49+
"value": {
50+
"options": null,
51+
"name": "vm:entry-point"
52+
}
53+
}
54+
],
55+
"interfaces": [],
56+
"members": {
57+
"hashCode": {
58+
"properties": [
59+
"getter"
60+
]
61+
},
62+
"runtimeType": {
63+
"properties": [
64+
"getter"
65+
]
66+
},
67+
"==": {
68+
"properties": [
69+
"method"
70+
]
71+
},
72+
"toString": {
73+
"properties": [
74+
"method"
75+
]
76+
},
77+
"noSuchMethod": {
78+
"properties": [
79+
"method"
80+
]
81+
},
82+
"hash": {
83+
"properties": [
84+
"method",
85+
"static"
86+
]
87+
},
88+
"hashAll": {
89+
"properties": [
90+
"method",
91+
"static"
92+
]
93+
},
94+
"hashAllUnordered": {
95+
"properties": [
96+
"method",
97+
"static"
98+
]
99+
}
100+
}
101+
}
102+
}
103+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Generated by pub
2+
# See https://dart.dev/tools/pub/glossary#lockfile
3+
packages: {}
4+
sdks:
5+
dart: ">=3.4.0 <4.0.0"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: goldens
2+
publish-to: none
3+
4+
environment:
5+
sdk: ^3.4.0

working/macros/dart_model/dart_model_analyzer_service/pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ dependencies:
1010
pool: ^1.5.1
1111
stream_transform: any
1212

13+
dev_dependencies:
14+
test: ^1.25.7
15+
1316
dependency_overrides:
1417
dart_model:
1518
path:
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:convert';
6+
import 'dart:io';
7+
8+
import 'package:analyzer/dart/analysis/context_builder.dart';
9+
import 'package:analyzer/dart/analysis/context_locator.dart';
10+
import 'package:dart_model/model.dart';
11+
import 'package:dart_model/query.dart';
12+
import 'package:dart_model_analyzer_service/dart_model_analyzer_service.dart';
13+
import 'package:test/test.dart';
14+
15+
void main() {
16+
final directory = Directory('goldens/lib');
17+
final dartFiles = directory
18+
.listSync()
19+
.whereType<File>()
20+
.where((f) => f.path.endsWith('.dart'))
21+
.toList();
22+
23+
final contextBuilder = ContextBuilder();
24+
final contextRoot = ContextLocator()
25+
.locateRoots(includedPaths: [directory.absolute.path]).first;
26+
final analysisContext =
27+
contextBuilder.createContext(contextRoot: contextRoot);
28+
final service = DartModelAnalyzerService(context: analysisContext);
29+
30+
for (final file in dartFiles) {
31+
final path = file.path.replaceAll('goldens/lib/', '');
32+
test(path, () async {
33+
final golden =
34+
File(file.path.replaceAll('.dart', '.json')).readAsStringSync();
35+
await service.changeFiles([file.absolute.path]);
36+
final model = await service.query(Query.uri('package:goldens/$path'));
37+
compare(path: path, model: model, golden: golden);
38+
});
39+
}
40+
}
41+
42+
void compare(
43+
{required String path, required Model model, required String golden}) {
44+
final prettyEncoder = JsonEncoder.withIndent(' ');
45+
final modelJson = prettyEncoder.convert(model);
46+
final normalizedGoldenJson = prettyEncoder.convert(json.decode(golden));
47+
48+
if (modelJson == normalizedGoldenJson) return;
49+
50+
final jsonPath = path.replaceAll('.dart', '.json');
51+
print('''
52+
=== current golden
53+
$normalizedGoldenJson
54+
=== actual output, with command to update golden
55+
cat > goldens/lib/$jsonPath <<EOF
56+
$modelJson
57+
EOF
58+
===
59+
''');
60+
fail('Difference found for $path model compared to $jsonPath, see above.');
61+
}

0 commit comments

Comments
 (0)