File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ import 'dart:collection' ;
2+
13import 'package:flutter/foundation.dart' ;
24
35import '../api/model/events.dart' ;
@@ -369,3 +371,21 @@ class ChannelStoreImpl with ChannelStore {
369371 }
370372 }
371373}
374+
375+ /// A [Map] with [TopicName] keys and [V] values.
376+ ///
377+ /// When one of these is created by [makeTopicKeyedMap] ,
378+ /// key equality is done case-insensitively; see there.
379+ ///
380+ /// This type should only be used for maps created by [makeTopicKeyedMap] .
381+ /// It would be nice to enforce that.
382+ typedef TopicKeyedMap <V > = Map <TopicName , V >;
383+
384+ /// Make a case-insensitive, case-preserving [TopicName] -keyed [LinkedHashMap] .
385+ ///
386+ /// The equality function is [TopicName.isSameAs] ,
387+ /// and the hash code is [String.hashCode] of [TopicName.canonicalize] .
388+ TopicKeyedMap <V > makeTopicKeyedMap <V >() => LinkedHashMap <TopicName , V >(
389+ equals: (a, b) => a.isSameAs (b),
390+ hashCode: (k) => k.canonicalize ().hashCode,
391+ );
Original file line number Diff line number Diff line change @@ -7,7 +7,9 @@ import 'package:zulip/api/model/model.dart';
77import 'package:zulip/model/store.dart' ;
88import 'package:zulip/model/channel.dart' ;
99
10+ import '../api/model/model_checks.dart' ;
1011import '../example_data.dart' as eg;
12+ import '../stdlib_checks.dart' ;
1113import 'test_store.dart' ;
1214
1315void main () {
@@ -411,4 +413,30 @@ void main() {
411413 .equals (UserTopicVisibilityPolicy .none);
412414 });
413415 });
416+
417+ group ('makeTopicKeyedMap' , () {
418+ test ('"a" equals "A"' , () {
419+ final map = makeTopicKeyedMap <int >()
420+ ..[eg.t ('a' )] = 1
421+ ..[eg.t ('A' )] = 2 ;
422+ check (map)
423+ ..[eg.t ('a' )].equals (2 )
424+ ..[eg.t ('A' )].equals (2 )
425+ ..entries.which ((it) => it.single
426+ ..key.apiName.equals ('a' )
427+ ..value.equals (2 ));
428+ });
429+
430+ test ('"A" equals "a"' , () {
431+ final map = makeTopicKeyedMap <int >()
432+ ..[eg.t ('A' )] = 1
433+ ..[eg.t ('a' )] = 2 ;
434+ check (map)
435+ ..[eg.t ('A' )].equals (2 )
436+ ..[eg.t ('a' )].equals (2 )
437+ ..entries.which ((it) => it.single
438+ ..key.apiName.equals ('A' )
439+ ..value.equals (2 ));
440+ });
441+ });
414442}
Original file line number Diff line number Diff line change @@ -16,6 +16,11 @@ extension ListChecks<T> on Subject<List<T>> {
1616 Subject <T > operator [](int index) => has ((l) => l[index], '[$index ]' );
1717}
1818
19+ extension MapEntryChecks <K , V > on Subject <MapEntry <K , V >> {
20+ Subject <K > get key => has ((e) => e.key, 'key' );
21+ Subject <V > get value => has ((e) => e.value, 'value' );
22+ }
23+
1924extension NullableMapChecks <K , V > on Subject <Map <K , V >?> {
2025 void deepEquals (Map <Object ?, Object ?>? expected) {
2126 if (expected == null ) {
You can’t perform that action at this time.
0 commit comments