Skip to content

Commit 860d5f2

Browse files
committed
(optional) test: Add a test helper for inspecting logs.
This is inspired by `TestCase.assertLogs`, which is widely adapted in the Zulip server tests. This doesn't address the TODO comment in lib/log.dart, because the feature is not really used for debugging. Signed-off-by: Zixuan James Li <[email protected]>
1 parent 92d4e5f commit 860d5f2

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

lib/log.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import 'package:flutter/foundation.dart';
12

23
/// Whether [debugLog] should do anything.
34
///
45
/// This has an effect only in a debug build.
56
bool debugLogEnabled = false;
67

8+
/// Used for log inspection in tests.
9+
@visibleForTesting
10+
List<String>? debugLogHistory;
11+
712
/// Print a log message, if debug logging is enabled.
813
///
914
/// In a debug build, if [debugLogEnabled] is true, this prints the given
@@ -26,6 +31,7 @@ bool debugLog(String message) {
2631
if (debugLogEnabled) {
2732
print(message); // ignore: avoid_print
2833
}
34+
debugLogHistory?.add(message);
2935
return true;
3036
}());
3137
return true;

test/model/message_test.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '../api/model/model_checks.dart';
1212
import '../api/model/submessage_checks.dart';
1313
import '../example_data.dart' as eg;
1414
import '../stdlib_checks.dart';
15+
import '../test_log.dart';
1516
import 'message_list_test.dart';
1617
import 'store_checks.dart';
1718
import 'test_store.dart';
@@ -700,12 +701,15 @@ void main() {
700701

701702
test('ignore submessage event with malformed content', () async {
702703
final message = await preparePollMessage(question: 'Old question');
703-
await store.handleEvent(eg.submessageEvent(message.id, eg.selfUser.userId,
704-
content: {
705-
'type': 'question',
706-
// Invalid type for question
707-
'question': 123,
708-
}));
704+
await checkLogs(() async {
705+
await store.handleEvent(eg.submessageEvent(message.id, eg.selfUser.userId,
706+
content: {
707+
'type': 'question',
708+
// Invalid type for question
709+
'question': 123,
710+
}));
711+
})..length.equals(2)
712+
..last.contains('Malformed submessage event data for poll');
709713
checkNotifiedOnce();
710714
checkPoll(message).question.equals('Old question');
711715
});

test/test_log.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'dart:async';
2+
3+
import 'package:checks/checks.dart';
4+
import 'package:zulip/log.dart';
5+
6+
Future<Subject<List<String>>> checkLogs(FutureOr<void> Function() callback) async {
7+
assert(debugLogHistory == null);
8+
debugLogHistory = [];
9+
try {
10+
await callback();
11+
return check(debugLogHistory!);
12+
} finally {
13+
debugLogHistory = null;
14+
}
15+
}

0 commit comments

Comments
 (0)