Skip to content

Commit acbd557

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 de62dba commit acbd557

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
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>? logHistory;
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+
logHistory?.add(message);
2935
return true;
3036
}());
3137
return true;

test/model/message_test.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import '../api/model/model_checks.dart';
1414
import '../api/model/submessage_checks.dart';
1515
import '../example_data.dart' as eg;
1616
import '../stdlib_checks.dart';
17+
import '../test_log.dart';
1718
import 'message_list_test.dart';
1819
import 'store_checks.dart';
1920
import 'test_store.dart';
@@ -662,15 +663,18 @@ void main() {
662663

663664
test('ignore submessage event with malformed content', () async {
664665
final messageId = await preparePollMessage();
665-
await store.handleEvent(
666-
eg.submessageEvent(
667-
streamMessage.id,
668-
eg.selfUser.userId,
669-
content: jsonEncode({
670-
'type': 'question',
671-
// Invalid type for question
672-
'question': 123,
673-
})));
666+
await checkLogs(() async {
667+
await store.handleEvent(
668+
eg.submessageEvent(
669+
streamMessage.id,
670+
eg.selfUser.userId,
671+
content: jsonEncode({
672+
'type': 'question',
673+
// Invalid type for question
674+
'question': 123,
675+
})));
676+
})..length.equals(2)
677+
..last.contains('Malformed submessage event data for poll');
674678
checkNotNotified();
675679
check(store.messages[messageId]).isNotNull().poll.isNotNull();
676680
});

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(logHistory == null);
8+
logHistory = [];
9+
try {
10+
await callback();
11+
return check(logHistory).isA<List<String>>();
12+
} finally {
13+
logHistory = null;
14+
}
15+
}

0 commit comments

Comments
 (0)