Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macros] Add golden tests for model JSON. #3885

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
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;
}
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"
]
}
}
}
}
}
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: goldens
publish-to: none

environment:
sdk: ^3.4.0
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ dependencies:
pool: ^1.5.1
stream_transform: any

dev_dependencies:
test: ^1.25.7

dependency_overrides:
dart_model:
path:
Expand Down
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.');
}