Skip to content

Commit ac57a34

Browse files
committed
internal_link [nfc]: Add generic support for is operands.
This also helps us with having more thorough test coverage for different `is` operands. Signed-off-by: Zixuan James Li <[email protected]>
1 parent fdb78be commit ac57a34

File tree

2 files changed

+52
-22
lines changed

2 files changed

+52
-22
lines changed

lib/model/internal_link.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
152152
ApiNarrowStream? streamElement;
153153
ApiNarrowTopic? topicElement;
154154
ApiNarrowDm? dmElement;
155-
ApiNarrowIs? isMentionedElement;
155+
Set<IsOperand> isElementOperands = {};
156156

157157
for (var i = 0; i < segments.length; i += 2) {
158158
final (operator, negated) = _parseOperator(segments[i]);
@@ -181,12 +181,9 @@ Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
181181
dmElement = ApiNarrowDm(dmIds, negated: negated);
182182

183183
case _NarrowOperator.is_:
184-
if (isMentionedElement != null) return null;
185-
if (operand == 'mentioned') {
186-
isMentionedElement = ApiNarrowIs(IsOperand.mentioned);
187-
} else {
188-
return null;
189-
}
184+
final isElementOperand = IsOperand.fromRawString(operand);
185+
if (isElementOperands.contains(isElementOperand)) return null;
186+
isElementOperands.add(isElementOperand);
190187

191188
case _NarrowOperator.near: // TODO(#82): support for near
192189
case _NarrowOperator.with_: // TODO(#683): support for with
@@ -197,9 +194,22 @@ Narrow? _interpretNarrowSegments(List<String> segments, PerAccountStore store) {
197194
}
198195
}
199196

200-
if (isMentionedElement != null) {
197+
if (isElementOperands.isNotEmpty) {
201198
if (streamElement != null || topicElement != null || dmElement != null) return null;
202-
return const MentionsNarrow();
199+
if (isElementOperands.length > 1) return null;
200+
switch (isElementOperands.single) {
201+
case IsOperand.mentioned:
202+
return const MentionsNarrow();
203+
case IsOperand.dm:
204+
case IsOperand.private:
205+
case IsOperand.alerted:
206+
case IsOperand.starred:
207+
case IsOperand.followed:
208+
case IsOperand.resolved:
209+
case IsOperand.unread:
210+
case IsOperand.unknown:
211+
return null;
212+
}
203213
} else if (dmElement != null) {
204214
if (streamElement != null || topicElement != null) return null;
205215
return DmNarrow.withUsers(dmElement.operand, selfUserId: store.selfUserId);

test/model/internal_link_test.dart

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import 'package:checks/checks.dart';
33
import 'package:test/scaffolding.dart';
44
import 'package:zulip/api/model/model.dart';
5+
import 'package:zulip/api/model/narrow.dart';
56
import 'package:zulip/model/internal_link.dart';
67
import 'package:zulip/model/narrow.dart';
78
import 'package:zulip/model/store.dart';
@@ -221,25 +222,44 @@ void main() {
221222
testExpectedNarrows(testCases, streams: streams);
222223
});
223224

224-
group('"/#narrow/is/mentioned returns expected MentionsNarrow', () {
225-
final testCases = [
226-
('/#narrow/is/mentioned', const MentionsNarrow()),
227-
('/#narrow/is/mentioned/near/1', const MentionsNarrow()),
228-
('/#narrow/is/mentioned/with/2', const MentionsNarrow()),
229-
('/#narrow/channel/7-test-here/is/mentioned', null),
230-
('/#narrow/channel/check/topic/test/is/mentioned', null),
231-
('/#narrow/topic/test/is/mentioned', null),
232-
('/#narrow/dm/17327-Chris-Bobbe-(Test-Account)/is/mentioned', null),
233-
('/#narrow/-is/mentioned', null),
234-
];
235-
testExpectedNarrows(testCases, streams: streams);
225+
group('/#narrow/is/<...> returns corresponding narrow', () {
226+
// For these tests, we are more interested in the internal links
227+
// containing a single effective `is` operator.
228+
// Internal links with multiple operators should be tested separately.
229+
for (final operand in IsOperand.values) {
230+
List<(String, Narrow?)> sharedCases(Narrow? narrow) => [
231+
('/#narrow/is/$operand', narrow),
232+
('/#narrow/is/$operand/near/1', narrow),
233+
('/#narrow/is/$operand/with/2', narrow),
234+
('/#narrow/channel/7-test-here/is/$operand', null),
235+
('/#narrow/channel/check/topic/test/is/$operand', null),
236+
('/#narrow/topic/test/is/$operand', null),
237+
('/#narrow/dm/17327-Chris-Bobbe-(Test-Account)/is/$operand', null),
238+
('/#narrow/-is/$operand', null),
239+
];
240+
final List<(String, Narrow?)> testCases;
241+
switch (operand) {
242+
case IsOperand.mentioned:
243+
testCases = sharedCases(const MentionsNarrow());
244+
case IsOperand.dm:
245+
case IsOperand.private:
246+
case IsOperand.alerted:
247+
case IsOperand.starred:
248+
case IsOperand.followed:
249+
case IsOperand.resolved:
250+
case IsOperand.unread:
251+
case IsOperand.unknown:
252+
// Unsupported operands should not return any narrow.
253+
testCases = sharedCases(null);
254+
}
255+
testExpectedNarrows(testCases, streams: streams);
256+
}
236257
});
237258

238259
group('unexpected link shapes are rejected', () {
239260
final testCases = [
240261
('/#narrow/stream/name/topic/', null), // missing operand
241262
('/#narrow/stream/name/unknown/operand/', null), // unknown operator
242-
('/#narrow/is/starred', null), // unknown `is` operand
243263
];
244264
testExpectedNarrows(testCases, streams: streams);
245265
});

0 commit comments

Comments
 (0)