Skip to content

Commit

Permalink
[native_assets_builder] Implement equals and hashCode on asset paths (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp committed Mar 6, 2024
1 parent 7eefc74 commit 001910c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pkgs/native_assets_builder/lib/src/model/kernel_assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class KernelAssetAbsolutePath implements KernelAssetPath {

static const _pathTypeValue = 'absolute';

@override
bool operator ==(Object other) =>
identical(this, other) ||
(other is KernelAssetAbsolutePath && other.uri == uri);

@override
int get hashCode => uri.hashCode;

@override
List<String> toYaml() => [_pathTypeValue, uri.toFilePath()];
}
Expand All @@ -94,11 +102,19 @@ class KernelAssetRelativePath implements KernelAssetPath {

static const _pathTypeValue = 'relative';

@override
bool operator ==(Object other) =>
identical(this, other) ||
(other is KernelAssetRelativePath && other.uri == uri);

@override
int get hashCode => uri.hashCode;

@override
List<String> toYaml() => [_pathTypeValue, uri.toFilePath()];
}

/// Asset is avaliable on the system `PATH`.
/// Asset is available on the system `PATH`.
///
/// [uri] only contains a file name.
class KernelAssetSystemPath implements KernelAssetPath {
Expand All @@ -108,6 +124,17 @@ class KernelAssetSystemPath implements KernelAssetPath {

static const _pathTypeValue = 'system';

@override
bool operator ==(Object other) =>
identical(this, other) ||
(other is KernelAssetSystemPath && other.uri == uri);

@override
int get hashCode => uri.hashCode;

@override
String toString() => 'KernelAssetAbsolutePath($uri)';

@override
List<String> toYaml() => [_pathTypeValue, uri.toFilePath()];
}
Expand Down
27 changes: 27 additions & 0 deletions pkgs/native_assets_builder/test/model/kernel_assets_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,31 @@ native-assets:
final fileContents = assets.toNativeAssetsFile();
expect(fileContents, assetsDartEncoding);
});

test('path equality', () {
expect(
KernelAssetAbsolutePath(Uri.parse('/path/to/libbar.a')),
KernelAssetAbsolutePath(Uri.parse('/path/to/libbar.a')),
);
expect(
KernelAssetAbsolutePath(Uri.parse('/path/to/libbar.a')),
isNot(KernelAssetAbsolutePath(Uri.parse('/path/to/libbar2.a'))),
);
expect(
KernelAssetRelativePath(Uri.parse('path/to/libbar.a')),
KernelAssetRelativePath(Uri.parse('path/to/libbar.a')),
);
expect(
KernelAssetRelativePath(Uri.parse('path/to/libbar.a')),
isNot(KernelAssetRelativePath(Uri.parse('path/to/libbar2.a'))),
);
expect(
KernelAssetSystemPath(Uri.parse('path/to/libbar.a')),
KernelAssetSystemPath(Uri.parse('path/to/libbar.a')),
);
expect(
KernelAssetSystemPath(Uri.parse('path/to/libbar.a')),
isNot(KernelAssetSystemPath(Uri.parse('path/to/libbar2.a'))),
);
});
}

0 comments on commit 001910c

Please sign in to comment.