-
Notifications
You must be signed in to change notification settings - Fork 309
Start writing unit tests #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6c9b94c
deps: Add dev:test
gnprice 338f057
content test: Write our first unit test, for model/content
gnprice 6d26241
content: Clean up test, with custom matchers plus providing == and to…
gnprice 444c338
doc: Document our fledgling test suite in README
gnprice 7a9bf81
deps: Add dev:checks
gnprice e72b7e1
content test: Switch to the new `checks` package
gnprice cf51413
content [nfc]: Informally mark ContentNode as sealed
gnprice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,15 @@ import 'package:flutter/foundation.dart'; | |
import 'package:html/dom.dart' as dom; | ||
import 'package:html/parser.dart'; | ||
|
||
// TODO: Implement ==/hashCode for all these classes where O(1), for testing/debugging | ||
// (Skip them for classes containing lists.) | ||
// TODO: Implement toString for all these classes, for testing/debugging; or | ||
// perhaps Diagnosticable instead? | ||
|
||
// This should have no subclasses except the ones defined in this file. | ||
// TODO mark as sealed: https://github.com/dart-lang/language/blob/a374667bc/accepted/future-releases/sealed-types/feature-specification.md | ||
@immutable | ||
class ContentNode { | ||
abstract class ContentNode { | ||
const ContentNode({this.debugHtmlNode}); | ||
|
||
final dom.Node? debugHtmlNode; | ||
|
@@ -28,6 +35,9 @@ class ZulipContent extends ContentNode { | |
const ZulipContent({super.debugHtmlNode, required this.nodes}); | ||
|
||
final List<BlockContentNode> nodes; | ||
|
||
@override | ||
String toString() => '${objectRuntimeType(this, 'ZulipContent')}($nodes)'; | ||
} | ||
|
||
abstract class BlockContentNode extends ContentNode { | ||
|
@@ -45,6 +55,14 @@ class UnimplementedBlockContentNode extends BlockContentNode | |
// A `br` element. | ||
class LineBreakNode extends BlockContentNode { | ||
const LineBreakNode({super.debugHtmlNode}); | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
return other is LineBreakNode; | ||
} | ||
|
||
@override | ||
int get hashCode => 'LineBreakNode'.hashCode; | ||
} | ||
|
||
// A `p` element, or a place where the DOM tree logically wanted one. | ||
|
@@ -63,6 +81,12 @@ class ParagraphNode extends BlockContentNode { | |
final bool wasImplicit; | ||
|
||
final List<InlineContentNode> nodes; | ||
|
||
// No == or hashCode overrides; don't want to walk through [nodes] in | ||
// an operation that looks cheap. | ||
|
||
@override | ||
String toString() => '${objectRuntimeType(this, 'ParagraphNode')}(wasImplicit: $wasImplicit, $nodes)'; | ||
} | ||
|
||
enum ListStyle { ordered, unordered } | ||
|
@@ -122,6 +146,19 @@ class TextNode extends InlineContentNode { | |
const TextNode(this.text, {super.debugHtmlNode}); | ||
|
||
final String text; | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
return other is TextNode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Same comment about the |
||
&& other.text == text; | ||
} | ||
|
||
@override | ||
int get hashCode => Object.hash('TextNode', text); | ||
|
||
// TODO encode unambiguously regardless of text contents | ||
@override | ||
String toString() => '${objectRuntimeType(this, 'TextNode')}(text: $text)'; | ||
} | ||
|
||
class LineBreakInlineNode extends InlineContentNode { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'package:checks/checks.dart'; | ||
import 'package:zulip/model/content.dart'; | ||
|
||
extension ContentNodeChecks on Subject<ContentNode> { | ||
void equalsNode(ContentNode expected) { | ||
if (expected is ZulipContent) { | ||
isA<ZulipContent>() | ||
.nodes.deepEquals(expected.nodes.map( | ||
(e) => it()..isA<BlockContentNode>().equalsNode(e))); | ||
// A shame we need the dynamic `isA` there. This | ||
// version hits a runtime type error: | ||
// .nodes.deepEquals(expected.nodes.map( | ||
// (e) => it<BlockContentNode>()..equalsNode(e))); | ||
// and with `it()` with no type argument, it doesn't type-check. | ||
// TODO: report that as API feedback on deepEquals | ||
} else if (expected is ParagraphNode) { | ||
isA<ParagraphNode>() | ||
..wasImplicit.equals(expected.wasImplicit) | ||
..nodes.deepEquals(expected.nodes.map( | ||
(e) => it()..isA<InlineContentNode>().equalsNode(e))); | ||
} else { | ||
// TODO handle remaining ContentNode subclasses that lack structural == | ||
equals(expected); | ||
} | ||
} | ||
} | ||
|
||
extension ZulipContentChecks on Subject<ZulipContent> { | ||
Subject<List<BlockContentNode>> get nodes => has((n) => n.nodes, 'nodes'); | ||
} | ||
|
||
extension ParagraphNodeChecks on Subject<ParagraphNode> { | ||
Subject<bool> get wasImplicit => has((n) => n.wasImplicit, 'wasImplicit'); | ||
Subject<List<InlineContentNode>> get nodes => has((n) => n.nodes, 'nodes'); | ||
} | ||
|
||
extension TextNodeChecks on Subject<TextNode> { | ||
Subject<String> get text => has((n) => n.text, 'text'); | ||
} | ||
|
||
// TODO write similar extensions for the rest of the content node classes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:checks/checks.dart'; | ||
import 'package:test/scaffolding.dart'; | ||
import 'package:zulip/model/content.dart'; | ||
|
||
import 'content_checks.dart'; | ||
|
||
void main() { | ||
test('parse a plain-text paragraph', () { | ||
check(parseContent('<p>hello world</p>')) | ||
.equalsNode(const ZulipContent(nodes: [ | ||
ParagraphNode(nodes: [TextNode('hello world')]), | ||
])); | ||
}); | ||
|
||
test('parse two plain-text paragraphs', () { | ||
check(parseContent('<p>hello</p><p>world</p>')) | ||
.equalsNode(const ZulipContent(nodes: [ | ||
ParagraphNode(nodes: [TextNode('hello')]), | ||
ParagraphNode(nodes: [TextNode('world')]), | ||
])); | ||
}); | ||
|
||
// TODO write more tests for this code | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LineBreakNode
is unlikely to be subclassed, I think, but isn't that a factor in choosing this implementation withis
over one that equality-checks theruntimeType
s? Theoperator ==
example in the Flutter project's style guide comparesruntimeTypes
:If we have a class
Animal
that overrides==
, and its subclassesDog
andCat
that don't, thensomeDog == someCat
will giveAnimal
's==
override isother is Animal
Animal
's==
override isother.runtimeType == runtimeType
(False is probably the right answer.)
(code that you can paste into dartpad.dev)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if it's worth changing the implementation or commenting about the expectation that it won't be subclassed. Am I being too paranoid? 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and:
Is this right? If so, can I make the same judgment of the other non-
abstract
FooNode
classes in the file (so e.g.LineBreakInlineNode
but notEmojiNode
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, none of these classes should ever have subclasses beyond the ones defined in this file. (So in Dart 3, we'll declare them as
sealed
: https://github.com/dart-lang/language/blob/a374667bcc4095edd22e2e6d454cbe9d9f9d85fa/accepted/future-releases/sealed-types/feature-specification.md .) For most of them, includingLineBreakNode
, that means they'll have no subclasses at all.