Skip to content

Adding tests, tweaking built_value code. #5

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 2 commits into from
Jul 20, 2018

Conversation

RedBrogdon
Copy link
Contributor

No description provided.

@RedBrogdon RedBrogdon requested a review from filiph July 19, 2018 20:26
@Sfshaza
Copy link

Sfshaza commented Jul 19, 2018

Please review, @filiph!

Copy link
Contributor

@filiph filiph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really good. I left some notes, but mostly nits: feel free to ignore. LGTM.

@@ -23,14 +23,19 @@ abstract class BuiltComplexObject
@nullable
double get aDouble;

@nullable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me sad, but I know you're doing it for a reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I did the other two first, so it's been changed to match. I'm annoyed that I didn't catch it earlier, but that's why one writes tests, I guess. 😄

import 'package:jsonexample/json_serializable/serializable_simple_object.dart';

void main() {
const Map<String, dynamic> typicalObjectJson = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I think the Dart style, if you want to type-annotate, is:

const typicalObjectJson = <String,dynamic>{
  // ...
}

And in this particular case, you don't even need to do that, since it's going to be inferred.

Types on the left are encourages for top-level members, but local variables tend to rely on inferred types.
https://www.dartlang.org/guides/language/effective-dart/design#prefer-type-annotating-public-fields-and-top-level-variables-if-the-type-isnt-obvious

If the second parameter wasn't dynamic, you could just leave generics altogether. But because of this rule, you want to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


test('Empty object', () {
final simpleObject =
SerializableSimpleObject.fromJson(<String, dynamic>{});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Why not have this empty map among the constants as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally it was just the brackets {}, so it felt silly. Makes more sense now, though, so fixed.


test('Extra properties', () {
final simpleObject =
ConvertedSimpleObject.fromJson(unexpectedPropertyjson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you're testing with the unexpectedPropertyjson. Are you testing that it returnsNormally? In that case, the test should say "Extra properties are ignored".

Is there a way to force the deserialization to throw when the object has extra properties? In that case, we'd want to test that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just testing that an extra, unexpected property is ignored and doesn't break anything. Updated the test description to better state this.

'aListOfDoubles': []
};

const Map<String, dynamic> unexpectedPropertyjson = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpectedPropertyJson

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

};

group('ConvertedSimpleObject unit tests', () {
test('Typical object', () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] The concatenated message should ideally read like a sentence. For example: "ConvertedSimpleObject deserializes a typical object".

group('ConvertedSimpleObject', () {
  test('deserializes a typical object', () {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrote all of 'em.

test('Typical object', () {
final simpleObject = ConvertedSimpleObject.fromJson(typicalObjectJson);

expect(simpleObject, isNotNull);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Purists would say that there should be one expect statement per unit test. I'm not a purist.

I do think these tests may be unnecessarily thorough. Especially below, where you're using json_serializable. Those tests look like something I could find in the json_serializable package. Here we could just make sure that the things don't throw, or that one of the members is what we expect it to be.

Definitely a nit, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but I'm not sure what other tests would go in here beyond the type of thing I have. I do like that it effectively compares all three approaches to ensure that they all pass the same tests, which is nice.


void main() {
group('SimpleObjectView widget test', () {
testWidgets('Typical object', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtto about test descriptions reading like sentences:

"SimpleObjectView shows JSON contents" or something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewritten.

expect(find.text('[1, 2, 3]'), findsOneWidget);
expect(find.text('[1.0, 2.0, 3.0]'), findsOneWidget);

for (int i = 1; i <= 4; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I think that logic like this in unit tests is generally frowned upon. No big deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was torn about it myself. I don't like having loops in the tests, but these get enormous otherwise. Leaving as-is for now, but I'll more than likely come back and clean this up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants