Skip to content

Commit 0d5aa72

Browse files
committed
content test: Switch to the new checks package
Definitely spiffier than `matcher`. Has useful static types, and also a better API for producing clear diagnostics on failure.
1 parent 83fa720 commit 0d5aa72

File tree

4 files changed

+79
-79
lines changed

4 files changed

+79
-79
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ See [upstream docs on `flutter test`][flutter-cookbook-unit-tests].
9494

9595
## Notes
9696

97+
### Writing tests
98+
99+
For unit tests, we use [the `checks` package][package-checks].
100+
This is a new package from the Dart team, currently in preview,
101+
which is [intended to replace][package-checks-migration] the
102+
old `matcher` package.
103+
104+
This means that if you see example test code elsewhere that
105+
uses the `expect` function, we'd prefer to translate it into
106+
something in terms of `check`. For help with that,
107+
see the [`package:checks` migration guide][package-checks-migration]
108+
and the package's [API docs][package-checks-api].
109+
110+
Because `package:checks` is still in preview, the Dart team is
111+
open to feedback on the API to a degree that they won't be
112+
after it reaches 1.0. So where we find rough edges, now is a
113+
good time to [report them as issues][dart-test-tracker].
114+
115+
[package-checks]: https://pub.dev/packages/checks
116+
[package-checks-api]: https://pub.dev/documentation/checks/latest/checks/checks-library.html
117+
[package-checks-migration]: https://github.com/dart-lang/test/blob/master/pkgs/checks/doc/migrating_from_matcher.md
118+
[dart-test-tracker]: https://github.com/dart-lang/test/issues
119+
120+
97121
### Editing API types
98122

99123
We support Zulip Server 4.0 and later. For API features added in

test/model/content_checks.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:checks/checks.dart';
2+
import 'package:zulip/model/content.dart';
3+
4+
extension ContentNodeChecks on Subject<ContentNode> {
5+
void equalsNode(ContentNode expected) {
6+
if (expected is ZulipContent) {
7+
isA<ZulipContent>()
8+
.nodes.deepEquals(expected.nodes.map(
9+
(e) => it()..isA<BlockContentNode>().equalsNode(e)));
10+
// A shame we need the dynamic `isA` there. This
11+
// version hits a runtime type error:
12+
// .nodes.deepEquals(expected.nodes.map(
13+
// (e) => it<BlockContentNode>()..equalsNode(e)));
14+
// and with `it()` with no type argument, it doesn't type-check.
15+
// TODO: report that as API feedback on deepEquals
16+
} else if (expected is ParagraphNode) {
17+
isA<ParagraphNode>()
18+
..wasImplicit.equals(expected.wasImplicit)
19+
..nodes.deepEquals(expected.nodes.map(
20+
(e) => it()..isA<InlineContentNode>().equalsNode(e)));
21+
} else {
22+
// TODO handle remaining ContentNode subclasses that lack structural ==
23+
equals(expected);
24+
}
25+
}
26+
}
27+
28+
extension ZulipContentChecks on Subject<ZulipContent> {
29+
Subject<List<BlockContentNode>> get nodes => has((n) => n.nodes, 'nodes');
30+
}
31+
32+
extension ParagraphNodeChecks on Subject<ParagraphNode> {
33+
Subject<bool> get wasImplicit => has((n) => n.wasImplicit, 'wasImplicit');
34+
Subject<List<InlineContentNode>> get nodes => has((n) => n.nodes, 'nodes');
35+
}
36+
37+
extension TextNodeChecks on Subject<TextNode> {
38+
Subject<String> get text => has((n) => n.text, 'text');
39+
}
40+
41+
// TODO write similar extensions for the rest of the content node classes

test/model/content_matchers.dart

Lines changed: 0 additions & 70 deletions
This file was deleted.

test/model/content_test.dart

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import 'package:test/test.dart';
1+
import 'package:checks/checks.dart';
2+
import 'package:test/scaffolding.dart';
23
import 'package:zulip/model/content.dart';
34

4-
import 'content_matchers.dart';
5+
import 'content_checks.dart';
56

67
void main() {
78
test('parse a plain-text paragraph', () {
8-
// TODO try to compact this further
9-
expect(
10-
parseContent('<p>hello world</p>'),
11-
ZulipContentMatcher([
12-
ParagraphNodeMatcher(
13-
wasImplicit: equals(false),
14-
nodes: [equals(const TextNode('hello world'))])
9+
check(parseContent('<p>hello world</p>'))
10+
.equalsNode(const ZulipContent(nodes: [
11+
ParagraphNode(nodes: [TextNode('hello world')]),
12+
]));
13+
});
14+
15+
test('parse two plain-text paragraphs', () {
16+
check(parseContent('<p>hello</p><p>world</p>'))
17+
.equalsNode(const ZulipContent(nodes: [
18+
ParagraphNode(nodes: [TextNode('hello')]),
19+
ParagraphNode(nodes: [TextNode('world')]),
1520
]));
1621
});
1722

0 commit comments

Comments
 (0)