-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macros] Add golden tests for model JSON. (#3885)
- Loading branch information
1 parent
341764a
commit f62423f
Showing
7 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
working/macros/dart_model/dart_model_analyzer_service/goldens/.dart_tool/package_config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"configVersion": 2, | ||
"packages": [ | ||
{ | ||
"name": "goldens", | ||
"rootUri": "../", | ||
"packageUri": "lib/", | ||
"languageVersion": "3.4" | ||
} | ||
], | ||
"generated": "2024-06-06T15:09:11.556114Z", | ||
"generator": "pub", | ||
"generatorVersion": "3.5.0-edge" | ||
} |
8 changes: 8 additions & 0 deletions
8
working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
abstract class Foo { | ||
abstract int bar; | ||
} | ||
|
||
class Baz { | ||
int get x => 0; | ||
final int foo = 3; | ||
} |
103 changes: 103 additions & 0 deletions
103
working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
{ | ||
"package:goldens/g1.dart": { | ||
"Foo": { | ||
"properties": [ | ||
"class", | ||
"abstract" | ||
], | ||
"annotations": [], | ||
"supertype": "dart:core/object.dart#Object", | ||
"interfaces": [], | ||
"members": { | ||
"bar": { | ||
"properties": [ | ||
"abstract", | ||
"field" | ||
] | ||
} | ||
} | ||
}, | ||
"Baz": { | ||
"properties": [ | ||
"class" | ||
], | ||
"annotations": [], | ||
"supertype": "dart:core/object.dart#Object", | ||
"interfaces": [], | ||
"members": { | ||
"foo": { | ||
"properties": [ | ||
"field" | ||
] | ||
}, | ||
"x": { | ||
"properties": [ | ||
"getter" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"dart:core": { | ||
"Object": { | ||
"properties": [ | ||
"class" | ||
], | ||
"annotations": [ | ||
{ | ||
"type": "dart:core/annotations.dart#pragma", | ||
"value": { | ||
"options": null, | ||
"name": "vm:entry-point" | ||
} | ||
} | ||
], | ||
"interfaces": [], | ||
"members": { | ||
"hashCode": { | ||
"properties": [ | ||
"getter" | ||
] | ||
}, | ||
"runtimeType": { | ||
"properties": [ | ||
"getter" | ||
] | ||
}, | ||
"==": { | ||
"properties": [ | ||
"method" | ||
] | ||
}, | ||
"toString": { | ||
"properties": [ | ||
"method" | ||
] | ||
}, | ||
"noSuchMethod": { | ||
"properties": [ | ||
"method" | ||
] | ||
}, | ||
"hash": { | ||
"properties": [ | ||
"method", | ||
"static" | ||
] | ||
}, | ||
"hashAll": { | ||
"properties": [ | ||
"method", | ||
"static" | ||
] | ||
}, | ||
"hashAllUnordered": { | ||
"properties": [ | ||
"method", | ||
"static" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Generated by pub | ||
# See https://dart.dev/tools/pub/glossary#lockfile | ||
packages: {} | ||
sdks: | ||
dart: ">=3.4.0 <4.0.0" |
5 changes: 5 additions & 0 deletions
5
working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: goldens | ||
publish-to: none | ||
|
||
environment: | ||
sdk: ^3.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
working/macros/dart_model/dart_model_analyzer_service/test/golden_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:analyzer/dart/analysis/context_builder.dart'; | ||
import 'package:analyzer/dart/analysis/context_locator.dart'; | ||
import 'package:dart_model/model.dart'; | ||
import 'package:dart_model/query.dart'; | ||
import 'package:dart_model_analyzer_service/dart_model_analyzer_service.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
final directory = Directory('goldens/lib'); | ||
final dartFiles = directory | ||
.listSync() | ||
.whereType<File>() | ||
.where((f) => f.path.endsWith('.dart')) | ||
.toList(); | ||
|
||
final contextBuilder = ContextBuilder(); | ||
final contextRoot = ContextLocator() | ||
.locateRoots(includedPaths: [directory.absolute.path]).first; | ||
final analysisContext = | ||
contextBuilder.createContext(contextRoot: contextRoot); | ||
final service = DartModelAnalyzerService(context: analysisContext); | ||
|
||
for (final file in dartFiles) { | ||
final path = file.path.replaceAll('goldens/lib/', ''); | ||
test(path, () async { | ||
final golden = | ||
File(file.path.replaceAll('.dart', '.json')).readAsStringSync(); | ||
await service.changeFiles([file.absolute.path]); | ||
final model = await service.query(Query.uri('package:goldens/$path')); | ||
compare(path: path, model: model, golden: golden); | ||
}); | ||
} | ||
} | ||
|
||
void compare( | ||
{required String path, required Model model, required String golden}) { | ||
final prettyEncoder = JsonEncoder.withIndent(' '); | ||
final modelJson = prettyEncoder.convert(model); | ||
final normalizedGoldenJson = prettyEncoder.convert(json.decode(golden)); | ||
|
||
if (modelJson == normalizedGoldenJson) return; | ||
|
||
final jsonPath = path.replaceAll('.dart', '.json'); | ||
print(''' | ||
=== current golden | ||
$normalizedGoldenJson | ||
=== actual output, with command to update golden | ||
cat > goldens/lib/$jsonPath <<EOF | ||
$modelJson | ||
EOF | ||
=== | ||
'''); | ||
fail('Difference found for $path model compared to $jsonPath, see above.'); | ||
} |