From f62423f77cce725a75a055800fb6c5f35db1e91e Mon Sep 17 00:00:00 2001 From: David Morgan Date: Thu, 6 Jun 2024 17:43:48 +0200 Subject: [PATCH] [macros] Add golden tests for model JSON. (#3885) --- .../goldens/.dart_tool/package_config.json | 14 +++ .../goldens/lib/g1.dart | 8 ++ .../goldens/lib/g1.json | 103 ++++++++++++++++++ .../goldens/pubspec.lock | 5 + .../goldens/pubspec.yaml | 5 + .../dart_model_analyzer_service/pubspec.yaml | 3 + .../test/golden_test.dart | 61 +++++++++++ 7 files changed, 199 insertions(+) create mode 100644 working/macros/dart_model/dart_model_analyzer_service/goldens/.dart_tool/package_config.json create mode 100644 working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.dart create mode 100644 working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.json create mode 100644 working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.lock create mode 100644 working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.yaml create mode 100644 working/macros/dart_model/dart_model_analyzer_service/test/golden_test.dart diff --git a/working/macros/dart_model/dart_model_analyzer_service/goldens/.dart_tool/package_config.json b/working/macros/dart_model/dart_model_analyzer_service/goldens/.dart_tool/package_config.json new file mode 100644 index 000000000..012f38881 --- /dev/null +++ b/working/macros/dart_model/dart_model_analyzer_service/goldens/.dart_tool/package_config.json @@ -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" +} diff --git a/working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.dart b/working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.dart new file mode 100644 index 000000000..33c21de80 --- /dev/null +++ b/working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.dart @@ -0,0 +1,8 @@ +abstract class Foo { + abstract int bar; +} + +class Baz { + int get x => 0; + final int foo = 3; +} diff --git a/working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.json b/working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.json new file mode 100644 index 000000000..46529e09f --- /dev/null +++ b/working/macros/dart_model/dart_model_analyzer_service/goldens/lib/g1.json @@ -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" + ] + } + } + } + } +} diff --git a/working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.lock b/working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.lock new file mode 100644 index 000000000..0f7d59e56 --- /dev/null +++ b/working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.lock @@ -0,0 +1,5 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: {} +sdks: + dart: ">=3.4.0 <4.0.0" diff --git a/working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.yaml b/working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.yaml new file mode 100644 index 000000000..1c2a6655e --- /dev/null +++ b/working/macros/dart_model/dart_model_analyzer_service/goldens/pubspec.yaml @@ -0,0 +1,5 @@ +name: goldens +publish-to: none + +environment: + sdk: ^3.4.0 diff --git a/working/macros/dart_model/dart_model_analyzer_service/pubspec.yaml b/working/macros/dart_model/dart_model_analyzer_service/pubspec.yaml index 76028b360..186447b6c 100644 --- a/working/macros/dart_model/dart_model_analyzer_service/pubspec.yaml +++ b/working/macros/dart_model/dart_model_analyzer_service/pubspec.yaml @@ -10,6 +10,9 @@ dependencies: pool: ^1.5.1 stream_transform: any +dev_dependencies: + test: ^1.25.7 + dependency_overrides: dart_model: path: diff --git a/working/macros/dart_model/dart_model_analyzer_service/test/golden_test.dart b/working/macros/dart_model/dart_model_analyzer_service/test/golden_test.dart new file mode 100644 index 000000000..f045632b6 --- /dev/null +++ b/working/macros/dart_model/dart_model_analyzer_service/test/golden_test.dart @@ -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() + .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 <