Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c committed Oct 16, 2023
1 parent 72a881a commit 5a05216
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
32 changes: 19 additions & 13 deletions pkgs/leak_tracker/lib/src/leak_tracking/_primitives/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ class Switches {
}

/// Set of classes to skip during leak tracking.
class LeakSkipSet {
const LeakSkipSet({this.byClass = const {}, this.skipAll = false});
class SkippedLeaksSet {
/// Creates instance of [SkippedLeaksSet].
///
/// Use this constructor to provide both [byClass] and [skipAll]
/// in case when you want to preserve list of classes, while temporarily turning off
/// the entire leak tracking, so that when you turn it back on for a subset of tests
/// with `copyWith(skipAll: false)`, the list of classes is set to needed value.
const SkippedLeaksSet({this.byClass = const {}, this.skipAll = false});

const LeakSkipSet.skipAll() : this(skipAll: true, byClass: const {});
const SkippedLeaksSet.skipAll() : this(skipAll: true, byClass: const {});

const LeakSkipSet.byClass(this.byClass) : skipAll = false;
const SkippedLeaksSet.byClass(this.byClass) : skipAll = false;

/// Classes to skip during leak tracking.
///
Expand All @@ -61,8 +67,8 @@ class LeakSkipSet {

/// Creates a copy of this object with the given fields replaced
/// with the new values.
LeakSkipSet copyWith({Map<String, int?>? byClass, bool? skipAll}) {
return LeakSkipSet(
SkippedLeaksSet copyWith({Map<String, int?>? byClass, bool? skipAll}) {
return SkippedLeaksSet(
skipAll: skipAll ?? this.skipAll,
byClass: byClass ?? this.byClass,
);
Expand All @@ -71,7 +77,7 @@ class LeakSkipSet {
/// Merges two skip lists.
///
/// In the result object the skip limit for a class is maximum of two original skip limits.
LeakSkipSet merge(LeakSkipSet? other) {
SkippedLeaksSet merge(SkippedLeaksSet? other) {
if (other == null) return this;
final map = {...byClass};
for (final theClass in other.byClass.keys) {
Expand All @@ -87,14 +93,14 @@ class LeakSkipSet {
}
map[theClass] = max(thisCount, otherCount);
}
return LeakSkipSet(
return SkippedLeaksSet(
byClass: map,
skipAll: skipAll || other.skipAll,
);
}

/// Removes the classes from skip lists.
LeakSkipSet track(List<String> list) {
SkippedLeaksSet track(List<String> list) {
if (list.isEmpty) return this;
final map = {...byClass};
list.forEach(map.remove);
Expand All @@ -111,15 +117,15 @@ class LeakSkipSet {
/// The total set of skipped leaks for both [notGCed] and [notDisposed] leaks.
class SkippedLeaks {
const SkippedLeaks({
this.notGCed = const LeakSkipSet(),
this.notDisposed = const LeakSkipSet(),
this.notGCed = const SkippedLeaksSet(),
this.notDisposed = const SkippedLeaksSet(),
});

/// Skip list for notGCed leaks.
final LeakSkipSet notGCed;
final SkippedLeaksSet notGCed;

/// Skip list for notDisposed leaks.
final LeakSkipSet notDisposed;
final SkippedLeaksSet notDisposed;

/// Returns true if the class is skipped.
///
Expand Down
8 changes: 4 additions & 4 deletions pkgs/leak_tracker/test/tests/leak_tracking/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ void main() {
});
});

group('$LeakSkipSet', () {
group('$SkippedLeaksSet', () {
test('merges', () {
const list1 = LeakSkipSet.byClass({'class1': null});
const list2 = LeakSkipSet.byClass({'class2': null});
const list1 = SkippedLeaksSet.byClass({'class1': null});
const list2 = SkippedLeaksSet.byClass({'class2': null});

final result = list1.merge(list2);

Expand All @@ -123,7 +123,7 @@ void main() {
});

test('removes', () {
const list = LeakSkipSet.byClass({'class1': null, 'class2': null});
const list = SkippedLeaksSet.byClass({'class1': null, 'class2': null});

final result = list.track(['class1']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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/leak_tracking_for_tests.dart';
export 'src/matchers.dart';
export 'src/model.dart';
export 'src/test_widgets.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ abstract class LeakTrackingForTests {
return settings.copyWith(
skippedLeaks: SkippedLeaks(
notGCed: settings.skippedLeaks.notGCed.merge(
LeakSkipSet(
SkippedLeaksSet(
byClass: addClassesToMap(notGCed, classes),
skipAll: allNotGCed,
),
),
notDisposed: settings.skippedLeaks.notDisposed.merge(
LeakSkipSet(
SkippedLeaksSet(
byClass: addClassesToMap(notDisposed, classes),
skipAll: allNotDisposed,
),
Expand Down

0 comments on commit 5a05216

Please sign in to comment.