From d232bcbb334d0df55ecfdf295afd5315bcd31917 Mon Sep 17 00:00:00 2001 From: Moritz Date: Wed, 19 Jun 2024 11:33:13 +0200 Subject: [PATCH] Require uniqueness --- pkgs/record_use/lib/record_use_internal.dart | 1 + .../lib/src/data_classes/extensions.dart | 19 ++--- pkgs/record_use/test/storage_test.dart | 7 +- pkgs/record_use/test/testdata/data.dart | 69 ++++++++++--------- 4 files changed, 55 insertions(+), 41 deletions(-) diff --git a/pkgs/record_use/lib/record_use_internal.dart b/pkgs/record_use/lib/record_use_internal.dart index b8b89b4d..c5127a1f 100644 --- a/pkgs/record_use/lib/record_use_internal.dart +++ b/pkgs/record_use/lib/record_use_internal.dart @@ -2,5 +2,6 @@ // 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. +export 'src/data_classes/extensions.dart'; export 'src/proto/usages_read.pb.dart'; export 'src/proto/usages_shared.pb.dart'; diff --git a/pkgs/record_use/lib/src/data_classes/extensions.dart b/pkgs/record_use/lib/src/data_classes/extensions.dart index 00fca219..34efc474 100644 --- a/pkgs/record_use/lib/src/data_classes/extensions.dart +++ b/pkgs/record_use/lib/src/data_classes/extensions.dart @@ -15,22 +15,23 @@ extension UsagesExt on pb.Usages { const JsonEncoder.withIndent(' ').convert(_toStorage().writeToJsonMap()); pb_storage.Usages _toStorage() { + final usageList = [...calls, ...instances]; final definitions = - [...calls, ...instances].map((e) => e.definition).toList(); - final uris = [...calls, ...instances] - .expand((e) => [ - e.definition.identifier.uri, - ...e.references.map( - (e) => e.location.uri, - ) + usageList.map((usage) => usage.definition).toSet().toList(); + final uris = usageList + .expand((usage) => [ + usage.definition.identifier.uri, + ...usage.references.map((reference) => reference.location.uri) ]) + .toSet() .toList(); return pb_storage.Usages( metadata: metadata, definitions: definitions, uris: uris, - instances: instances.map((e) => e.toStorage(definitions, uris)), - calls: calls.map((e) => e.toStorage(definitions, uris)), + instances: + instances.map((instance) => instance.toStorage(definitions, uris)), + calls: calls.map((call) => call.toStorage(definitions, uris)), ); } } diff --git a/pkgs/record_use/test/storage_test.dart b/pkgs/record_use/test/storage_test.dart index 04fe8d4f..94ede726 100644 --- a/pkgs/record_use/test/storage_test.dart +++ b/pkgs/record_use/test/storage_test.dart @@ -5,7 +5,6 @@ import 'dart:io'; import 'package:record_use/record_use_internal.dart'; -import 'package:record_use/src/data_classes/extensions.dart'; import 'package:record_use/src/record_use.dart'; import 'package:test/test.dart'; @@ -23,4 +22,10 @@ void main() { recordedUses, ); }); + test('empty Object->Buffer->Object', () { + expect( + RecordedUsages.fromFile(emptyUsages.toBuffer()), + emptyUsages, + ); + }); } diff --git a/pkgs/record_use/test/testdata/data.dart b/pkgs/record_use/test/testdata/data.dart index 9fe132a5..a4405c58 100644 --- a/pkgs/record_use/test/testdata/data.dart +++ b/pkgs/record_use/test/testdata/data.dart @@ -3,19 +3,26 @@ // BSD-style license that can be found in the LICENSE file. import 'package:pub_semver/pub_semver.dart'; -import 'package:record_use/record_use_internal.dart' as pb; -final pb.Usages recordedUses = pb.Usages( - metadata: pb.Metadata( +import 'package:record_use/record_use_internal.dart'; + +final Usages emptyUsages = Usages( + metadata: Metadata(version: Version(1, 2, 3).toString()), + calls: [], + instances: [], +); + +final Usages recordedUses = Usages( + metadata: Metadata( version: Version(1, 6, 2, pre: 'wip', build: '5.-.2.z').toString(), comment: 'Recorded references at compile time and their argument values, as far' ' as known, to definitions annotated with @RecordReference', ), instances: [ - pb.Usage( - definition: pb.Definition( - identifier: pb.Identifier( + Usage( + definition: Definition( + identifier: Identifier( uri: Uri.parse('file://lib/_internal/js_runtime/lib/js_helper.dart') .toString(), name: 'MyAnnotation', @@ -24,18 +31,18 @@ final pb.Usages recordedUses = pb.Usages( column: 30, ), references: [ - pb.Reference( - location: pb.Location( + Reference( + location: Location( uri: Uri.parse('file://lib/_internal/js_runtime/lib/js_helper.dart') .toString(), line: 40, column: 30, ), - fields: pb.Fields(fields: [ - pb.Field( + fields: Fields(fields: [ + Field( className: 'className', name: 'a', - value: pb.FieldValue(intValue: 42), + value: FieldValue(intValue: 42), ), ]), loadingUnit: '3', @@ -44,9 +51,9 @@ final pb.Usages recordedUses = pb.Usages( ), ], calls: [ - pb.Usage( - definition: pb.Definition( - identifier: pb.Identifier( + Usage( + definition: Definition( + identifier: Identifier( uri: Uri.parse('file://lib/_internal/js_runtime/lib/js_helper.dart') .toString(), parent: 'MyClass', @@ -57,21 +64,21 @@ final pb.Usages recordedUses = pb.Usages( loadingUnit: 'part_15.js', ), references: [ - pb.Reference( - arguments: pb.Arguments( - constArguments: pb.ConstArguments( + Reference( + arguments: Arguments( + constArguments: ConstArguments( positional: { - 0: pb.FieldValue(stringValue: 'lib_SHA1'), - 1: pb.FieldValue(boolValue: false), - 2: pb.FieldValue(intValue: 1), + 0: FieldValue(stringValue: 'lib_SHA1'), + 1: FieldValue(boolValue: false), + 2: FieldValue(intValue: 1), }, named: { - 'leroy': pb.FieldValue(stringValue: 'jenkins'), - 'freddy': pb.FieldValue(stringValue: 'mercury'), + 'leroy': FieldValue(stringValue: 'jenkins'), + 'freddy': FieldValue(stringValue: 'mercury'), }, ), ), - location: pb.Location( + location: Location( uri: Uri.parse( 'file://benchmarks/OmnibusDeferred/dart/OmnibusDeferred.dart') .toString(), @@ -80,23 +87,23 @@ final pb.Usages recordedUses = pb.Usages( ), loadingUnit: 'o.js', ), - pb.Reference( - arguments: pb.Arguments( - constArguments: pb.ConstArguments( + Reference( + arguments: Arguments( + constArguments: ConstArguments( positional: { - 0: pb.FieldValue(stringValue: 'lib_SHA1'), - 2: pb.FieldValue(intValue: 0), + 0: FieldValue(stringValue: 'lib_SHA1'), + 2: FieldValue(intValue: 0), }, named: { - 'leroy': pb.FieldValue(stringValue: 'jenkins'), + 'leroy': FieldValue(stringValue: 'jenkins'), }, ), - nonConstArguments: pb.NonConstArguments( + nonConstArguments: NonConstArguments( positional: [1], named: ['freddy'], ), ), - location: pb.Location( + location: Location( uri: Uri.parse( 'file://benchmarks/OmnibusDeferred/dart/OmnibusDeferred.dart') .toString(),