Skip to content

Commit 84007a2

Browse files
committed
test: Use random email in eg.user
This specifically fixes a runtime error from the test "find account among several" in `test/notifications/display_test.dart` due to duplicate email addresses within a realm. This also useful in general in cases when the email addresses do not matter and should be kept boring. The generation approach is similar to an ID, to make collision unlikely (still possible with caller specified values). Signed-off-by: Zixuan James Li <zixuan@zulip.com>
1 parent 34da52f commit 84007a2

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

test/example_data.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@ GetServerSettingsResult serverSettings({
7070
int _nextUserId() => (_lastUserId += 1 + Random().nextInt(100));
7171
int _lastUserId = 1000;
7272

73+
/// A random email address, unique from previously generated ones.
74+
String _nextEmail() => 'mail${_lastEmailSuffix += Random().nextInt(1000)}@example.com';
75+
int _lastEmailSuffix = 1000;
76+
7377
/// Construct an example user.
7478
///
7579
/// If user ID `userId` is not given, it will be generated from a random
7680
/// but increasing sequence.
7781
/// Use an explicit `userId` only if the ID needs to correspond to some
7882
/// other data in the test, or if the IDs need to increase in a different order
7983
/// from the calls to [user].
84+
///
85+
/// If `deliveryEmail` is not given, it will be generated from a random
86+
/// but unique sequence of strings.
87+
/// If `email` is not given, it will be set to `deliveryEmail`.
8088
User user({
8189
int? userId,
8290
String? deliveryEmail,
@@ -87,7 +95,7 @@ User user({
8795
String? avatarUrl,
8896
Map<int, ProfileFieldUserData>? profileData,
8997
}) {
90-
var effectiveDeliveryEmail = deliveryEmail ?? 'name@example.com'; // TODO generate example emails
98+
var effectiveDeliveryEmail = deliveryEmail ?? _nextEmail();
9199
_checkPositive(userId, 'user ID');
92100
return User(
93101
userId: userId ?? _nextUserId(),

0 commit comments

Comments
 (0)