forked from zulip/zulip-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose_test.dart
303 lines (271 loc) · 7.33 KB
/
compose_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import 'package:checks/checks.dart';
import 'package:test/scaffolding.dart';
import 'package:zulip/model/compose.dart';
import 'package:zulip/model/narrow.dart';
import '../example_data.dart' as eg;
import 'test_store.dart';
void main() {
group('wrapWithBacktickFence', () {
/// Check `wrapWithBacktickFence` on example input and expected output.
///
/// The intended input (content passed to `wrapWithBacktickFence`)
/// is straightforward to infer from `expected`.
/// To do that, this helper takes `expected` and removes the opening and
/// closing fences.
///
/// Then we have the input to the test, as well as the expected output.
void checkFenceWrap(String expected, {String? infoString, bool chopNewline = false}) {
final re = RegExp(r'^.*?\n(.*\n|).*\n$', dotAll: true);
String content = re.firstMatch(expected)![1]!;
if (chopNewline) content = content.substring(0, content.length - 1);
check(wrapWithBacktickFence(content: content, infoString: infoString)).equals(expected);
}
test('empty content', () {
checkFenceWrap('''
```
```
''');
});
test('content consisting of blank lines', () {
checkFenceWrap('''
```
```
''');
});
test('single line with no code blocks', () {
checkFenceWrap('''
```
hello world
```
''');
});
test('multiple lines with no code blocks', () {
checkFenceWrap('''
```
hello
world
```
''');
});
test('no code blocks; incomplete final line', () {
checkFenceWrap(chopNewline: true, '''
```
hello
world
```
''');
});
test('three-backtick block', () {
checkFenceWrap('''
````
hello
```
code
```
world
````
''');
});
test('multiple three-backtick blocks; one has info string', () {
checkFenceWrap('''
````
hello
```
code
```
world
```javascript
// more code
```
````
''');
});
test('whitespace around info string', () {
checkFenceWrap('''
````
``` javascript
// hello world
```
````
''');
});
test('four-backtick block', () {
checkFenceWrap('''
`````
````
hello world
````
`````
''');
});
test('five-backtick block', () {
checkFenceWrap('''
``````
`````
hello world
`````
``````
''');
});
test('five-backtick block; incomplete final line', () {
checkFenceWrap(chopNewline: true, '''
``````
`````
hello world
`````
``````
''');
});
test('three-, four-, and five-backtick blocks', () {
checkFenceWrap('''
``````
```
hello world
```
````
hello world
````
`````
hello world
`````
``````
''');
});
test('dangling opening fence', () {
checkFenceWrap('''
`````
````javascript
// hello world
`````
''');
});
test('code blocks marked by indentation or tilde fences don\'t affect result', () {
checkFenceWrap('''
```
// hello world
~~~~~~
code
~~~~~~
```
''');
});
test('backtick fences may be indented up to three spaces', () {
checkFenceWrap('''
````
```
````
''');
checkFenceWrap('''
````
```
````
''');
checkFenceWrap('''
````
```
````
''');
// but at 4 spaces of indentation it no longer counts:
checkFenceWrap('''
```
```
```
''');
});
test('fence ignored if info string has backtick', () {
checkFenceWrap('''
```
```java`script
hello
```
''');
});
test('with info string', () {
checkFenceWrap(infoString: 'info', '''
`````info
```
hello
```
info
````python
hello
````
`````
''');
});
});
group('narrowLink', () {
test('AllMessagesNarrow', () {
final store = eg.store();
check(narrowLink(store, const AllMessagesNarrow()))
.equals(store.account.realmUrl.resolve('#narrow'));
check(narrowLink(store, const AllMessagesNarrow(), nearMessageId: 1))
.equals(store.account.realmUrl.resolve('#narrow/near/1'));
});
test('StreamNarrow / TopicNarrow', () {
void checkNarrow(String expectedFragment, {
required int streamId,
required String name,
String? topic,
int? nearMessageId,
}) {
assert(expectedFragment.startsWith('#'), 'wrong-looking expectedFragment');
final store = eg.store();
store.addStream(eg.stream(streamId: streamId, name: name));
final narrow = topic == null
? StreamNarrow(streamId)
: TopicNarrow(streamId, topic);
check(narrowLink(store, narrow, nearMessageId: nearMessageId))
.equals(store.account.realmUrl.resolve(expectedFragment));
}
checkNarrow(streamId: 1, name: 'announce', '#narrow/stream/1-announce');
checkNarrow(streamId: 378, name: 'api design', '#narrow/stream/378-api-design');
checkNarrow(streamId: 391, name: 'Outreachy', '#narrow/stream/391-Outreachy');
checkNarrow(streamId: 415, name: 'chat.zulip.org', '#narrow/stream/415-chat.2Ezulip.2Eorg');
checkNarrow(streamId: 419, name: 'français', '#narrow/stream/419-fran.C3.A7ais');
checkNarrow(streamId: 403, name: 'Hshs[™~}(.', '#narrow/stream/403-Hshs.5B.E2.84.A2~.7D.28.2E');
checkNarrow(streamId: 60, name: 'twitter', nearMessageId: 1570686, '#narrow/stream/60-twitter/near/1570686');
checkNarrow(streamId: 48, name: 'mobile', topic: 'Welcome screen UI',
'#narrow/stream/48-mobile/topic/Welcome.20screen.20UI');
checkNarrow(streamId: 243, name: 'mobile-team', topic: 'Podfile.lock clash #F92',
'#narrow/stream/243-mobile-team/topic/Podfile.2Elock.20clash.20.23F92');
checkNarrow(streamId: 377, name: 'translation/zh_tw', topic: '翻譯 "stream"',
'#narrow/stream/377-translation.2Fzh_tw/topic/.E7.BF.BB.E8.AD.AF.20.22stream.22');
checkNarrow(streamId: 42, name: 'Outreachy 2016-2017', topic: '2017-18 Stream?', nearMessageId: 302690,
'#narrow/stream/42-Outreachy-2016-2017/topic/2017-18.20Stream.3F/near/302690');
});
test('DmNarrow', () {
void checkNarrow(String expectedFragment, String legacyExpectedFragment, {
required List<int> allRecipientIds,
required int selfUserId,
int? nearMessageId,
}) {
assert(expectedFragment.startsWith('#'), 'wrong-looking expectedFragment');
final store = eg.store();
final narrow = DmNarrow(allRecipientIds: allRecipientIds, selfUserId: selfUserId);
check(narrowLink(store, narrow, nearMessageId: nearMessageId))
.equals(store.account.realmUrl.resolve(expectedFragment));
store.connection.zulipFeatureLevel = 176;
check(narrowLink(store, narrow, nearMessageId: nearMessageId))
.equals(store.account.realmUrl.resolve(legacyExpectedFragment));
}
checkNarrow(allRecipientIds: [1], selfUserId: 1,
'#narrow/dm/1-dm',
'#narrow/pm-with/1-pm');
checkNarrow(allRecipientIds: [1, 2], selfUserId: 1,
'#narrow/dm/1,2-dm',
'#narrow/pm-with/1,2-pm');
checkNarrow(allRecipientIds: [1, 2, 3], selfUserId: 1,
'#narrow/dm/1,2,3-group',
'#narrow/pm-with/1,2,3-group');
checkNarrow(allRecipientIds: [1, 2, 3, 4], selfUserId: 4,
'#narrow/dm/1,2,3,4-group',
'#narrow/pm-with/1,2,3,4-group');
checkNarrow(allRecipientIds: [1, 2], selfUserId: 1, nearMessageId: 12345,
'#narrow/dm/1,2-dm/near/12345',
'#narrow/pm-with/1,2-pm/near/12345');
});
// TODO other Narrow subclasses as we add them:
// starred, mentioned; searches; arbitrary
});
}