Skip to content

Commit

Permalink
Merge pull request #9 from Baseflow/feature/convert_identifier_to_lower
Browse files Browse the repository at this point in the history
Ensure that identifier is not sensitive to casing
  • Loading branch information
mvanbeusekom committed Apr 2, 2024
2 parents 00161a4 + be9ce97 commit 6140da3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/src/instance_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class InstanceManager {
/// This does not remove the weak referenced instance associated with
/// [identifier]. This can be done with [removeWeakReference].
T? remove<T extends Copyable>(String identifier) {
return _strongInstances.remove(identifier) as T?;
return _strongInstances.remove(identifier.toLowerCase()) as T?;
}

/// Retrieves the instance associated with identifier.
Expand All @@ -124,15 +124,16 @@ class InstanceManager {
/// This method also expects the host `InstanceManager` to have a strong
/// reference to the instance the identifier is associated with.
T? getInstanceWithWeakReference<T extends Copyable>(String identifier) {
final Copyable? weakInstance = _weakInstances[identifier]?.target;
final String id = identifier.toLowerCase();
final Copyable? weakInstance = _weakInstances[id]?.target;

if (weakInstance == null) {
final Copyable? strongInstance = _strongInstances[identifier];
final Copyable? strongInstance = _strongInstances[id];
if (strongInstance != null) {
final Copyable copy = strongInstance.copy();
_identifiers[copy] = identifier;
_weakInstances[identifier] = WeakReference<Copyable>(copy);
_finalizer.attach(copy, identifier, detach: copy);
_identifiers[copy] = id;
_weakInstances[id] = WeakReference<Copyable>(copy);
_finalizer.attach(copy, id, detach: copy);
return copy as T;
}
return strongInstance as T?;
Expand All @@ -156,7 +157,7 @@ class InstanceManager {
///
/// Returns unique identifier of the [instance] added.
void addHostCreatedInstance(Copyable instance, String identifier) {
_addInstanceWithIdentifier(instance, identifier);
_addInstanceWithIdentifier(instance, identifier.toLowerCase());
}

void _addInstanceWithIdentifier(Copyable instance, String identifier) {
Expand All @@ -175,8 +176,9 @@ class InstanceManager {

/// Whether this manager contains the given [identifier].
bool containsIdentifier(String identifier) {
return _weakInstances.containsKey(identifier) ||
_strongInstances.containsKey(identifier);
final String id = identifier.toLowerCase();

return _weakInstances.containsKey(id) || _strongInstances.containsKey(id);
}

String _nextUniqueIdentifier() {
Expand Down
50 changes: 50 additions & 0 deletions test/instance_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ void main() {
);
});

test('addHostCreatedInstance with uppercase identifier', () {
final CopyableObject object = CopyableObject();

final InstanceManager instanceManager =
InstanceManager(onWeakReferenceRemoved: (_) {});
const Uuid uuid = Uuid();
final String identifier = uuid.v4();

instanceManager.addHostCreatedInstance(object, identifier.toUpperCase());

expect(instanceManager.getIdentifier(object), identifier);
expect(
instanceManager.getInstanceWithWeakReference(identifier),
object,
);
});

test(
'addHostCreatedInstance prevents empty string to be used as identifier',
() {
Expand Down Expand Up @@ -80,6 +97,24 @@ void main() {
);
});

test('getInstanceWithWeakReference should work with uppercase identifier',
() {
final CopyableObject object = CopyableObject();

final InstanceManager instanceManager =
InstanceManager(onWeakReferenceRemoved: (_) {});
const Uuid uuid = Uuid();
final String identifier = uuid.v4();

instanceManager.addHostCreatedInstance(object, identifier);

expect(instanceManager.getIdentifier(object), identifier);
expect(
instanceManager.getInstanceWithWeakReference(identifier.toUpperCase()),
object,
);
});

test('removeWeakReference', () {
final CopyableObject object = CopyableObject();

Expand Down Expand Up @@ -133,6 +168,21 @@ void main() {
expect(instanceManager.containsIdentifier(identifier), isFalse);
});

test('removeStrongReference with uppercase identifier', () {
final CopyableObject object = CopyableObject();

const Uuid uuid = Uuid();
final String identifier = uuid.v4();
final InstanceManager instanceManager =
InstanceManager(onWeakReferenceRemoved: (_) {});

instanceManager.addHostCreatedInstance(object, identifier);
instanceManager.removeWeakReference(object);
expect(instanceManager.remove(identifier.toUpperCase()),
isA<CopyableObject>());
expect(instanceManager.containsIdentifier(identifier), isFalse);
});

test('removeStrongReference removes only strong reference', () {
final CopyableObject object = CopyableObject();

Expand Down

0 comments on commit 6140da3

Please sign in to comment.