1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-10 06:48:26 +00:00

Adding tests, tweaking built_value code. (#5)

This commit is contained in:
Andrew Brogdon
2018-07-20 15:18:23 -07:00
committed by GitHub
parent 166c797671
commit 357d172647
12 changed files with 907 additions and 89 deletions

View File

@@ -0,0 +1,422 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:jsonexample/built_value/built_complex_object.dart';
import 'package:jsonexample/built_value/built_value_serializers.dart';
import 'package:jsonexample/dart_convert/converted_complex_object.dart';
import 'package:jsonexample/json_serializable/serializable_complex_object.dart';
void main() {
const typicalObjectJson = <String, dynamic>{
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0],
'anObject': {
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0]
},
'aListOfObjects': [
{
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0]
},
{
'aString': 'Blah, blah, blah.',
'anInt': 2,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0]
},
{
'aString': 'Blah, blah, blah.',
'anInt': 3,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0]
}
]
};
const emptySimpleObjectsJson = <String, dynamic>{
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0],
'anObject': <String, dynamic>{},
'aListOfObjects': [
<String, dynamic>{},
<String, dynamic>{},
<String, dynamic>{}
]
};
const unexpectedPropertiesJson = <String, dynamic>{
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0],
'unexpectedProperty': 'Whoops!',
'anObject': {
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0],
'unexpectedProperty': 'Whoops!'
},
'aListOfObjects': [
{
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0],
'unexpectedProperty': 'Whoops!'
},
{
'aString': 'Blah, blah, blah.',
'anInt': 2,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0],
'unexpectedProperty': 'Whoops!'
},
{
'aString': 'Blah, blah, blah.',
'anInt': 3,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0],
'unexpectedProperty': 'Whoops!'
}
]
};
const emptyJson = <String, dynamic>{};
group('ConvertedComplexObject unit tests', () {
test('Typical object is converted correctly', () {
final complexObject = ConvertedComplexObject.fromJson(typicalObjectJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, "Blah, blah, blah.");
expect(complexObject.anObject.anInt, 1);
expect(complexObject.anObject.aDouble, 1.0);
expect(complexObject.anObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.anObject.aListOfInts, [1, 2, 3]);
expect(complexObject.anObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, "Blah, blah, blah.");
expect(complexObject.aListOfObjects[i].anInt, i + 1);
expect(complexObject.aListOfObjects[i].aDouble, 1.0);
expect(complexObject.aListOfObjects[i].aListOfStrings,
['one', 'two', 'three']);
expect(complexObject.aListOfObjects[i].aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfObjects[i].aListOfDoubles, [1.0, 2.0, 3.0]);
}
});
test('Empty object results in null fields', () {
final complexObject = ConvertedComplexObject.fromJson(emptyJson);
expect(complexObject.aString, isNull);
expect(complexObject.anInt, isNull);
expect(complexObject.aDouble, isNull);
expect(complexObject.aListOfStrings, isNull);
expect(complexObject.aListOfInts, isNull);
expect(complexObject.aListOfDoubles, isNull);
expect(complexObject.anObject, isNull);
expect(complexObject.aListOfObjects, isNull);
});
test('Empty simple objects result in instances with null fields', () {
final complexObject =
ConvertedComplexObject.fromJson(emptySimpleObjectsJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, isNull);
expect(complexObject.anObject.anInt, isNull);
expect(complexObject.anObject.aDouble, isNull);
expect(complexObject.anObject.aListOfStrings, isNull);
expect(complexObject.anObject.aListOfInts, isNull);
expect(complexObject.anObject.aListOfDoubles, isNull);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, isNull);
expect(complexObject.aListOfObjects[i].anInt, isNull);
expect(complexObject.aListOfObjects[i].aDouble, isNull);
expect(complexObject.aListOfObjects[i].aListOfStrings, isNull);
expect(complexObject.aListOfObjects[i].aListOfInts, isNull);
expect(complexObject.aListOfObjects[i].aListOfDoubles, isNull);
}
});
test('Unexpected properties are ignored', () {
final complexObject =
ConvertedComplexObject.fromJson(unexpectedPropertiesJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, "Blah, blah, blah.");
expect(complexObject.anObject.anInt, 1);
expect(complexObject.anObject.aDouble, 1.0);
expect(complexObject.anObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.anObject.aListOfInts, [1, 2, 3]);
expect(complexObject.anObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, "Blah, blah, blah.");
expect(complexObject.aListOfObjects[i].anInt, i + 1);
expect(complexObject.aListOfObjects[i].aDouble, 1.0);
expect(complexObject.aListOfObjects[i].aListOfStrings,
['one', 'two', 'three']);
expect(complexObject.aListOfObjects[i].aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfObjects[i].aListOfDoubles, [1.0, 2.0, 3.0]);
}
});
});
group('SerializableComplexObject unit tests', () {
test('Typical object is converted correctly', () {
final complexObject =
SerializableComplexObject.fromJson(typicalObjectJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, "Blah, blah, blah.");
expect(complexObject.anObject.anInt, 1);
expect(complexObject.anObject.aDouble, 1.0);
expect(complexObject.anObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.anObject.aListOfInts, [1, 2, 3]);
expect(complexObject.anObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, "Blah, blah, blah.");
expect(complexObject.aListOfObjects[i].anInt, i + 1);
expect(complexObject.aListOfObjects[i].aDouble, 1.0);
expect(complexObject.aListOfObjects[i].aListOfStrings,
['one', 'two', 'three']);
expect(complexObject.aListOfObjects[i].aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfObjects[i].aListOfDoubles, [1.0, 2.0, 3.0]);
}
});
test('Empty object results in null fields', () {
final complexObject = SerializableComplexObject.fromJson(emptyJson);
expect(complexObject.aString, isNull);
expect(complexObject.anInt, isNull);
expect(complexObject.aDouble, isNull);
expect(complexObject.aListOfStrings, isNull);
expect(complexObject.aListOfInts, isNull);
expect(complexObject.aListOfDoubles, isNull);
expect(complexObject.anObject, isNull);
expect(complexObject.aListOfObjects, isNull);
});
test('Empty simple objects result in instances with null fields', () {
final complexObject =
SerializableComplexObject.fromJson(emptySimpleObjectsJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, isNull);
expect(complexObject.anObject.anInt, isNull);
expect(complexObject.anObject.aDouble, isNull);
expect(complexObject.anObject.aListOfStrings, isNull);
expect(complexObject.anObject.aListOfInts, isNull);
expect(complexObject.anObject.aListOfDoubles, isNull);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, isNull);
expect(complexObject.aListOfObjects[i].anInt, isNull);
expect(complexObject.aListOfObjects[i].aDouble, isNull);
expect(complexObject.aListOfObjects[i].aListOfStrings, isNull);
expect(complexObject.aListOfObjects[i].aListOfInts, isNull);
expect(complexObject.aListOfObjects[i].aListOfDoubles, isNull);
}
});
test('Unexpected properties are ignored', () {
final complexObject =
SerializableComplexObject.fromJson(unexpectedPropertiesJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, "Blah, blah, blah.");
expect(complexObject.anObject.anInt, 1);
expect(complexObject.anObject.aDouble, 1.0);
expect(complexObject.anObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.anObject.aListOfInts, [1, 2, 3]);
expect(complexObject.anObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, "Blah, blah, blah.");
expect(complexObject.aListOfObjects[i].anInt, i + 1);
expect(complexObject.aListOfObjects[i].aDouble, 1.0);
expect(complexObject.aListOfObjects[i].aListOfStrings,
['one', 'two', 'three']);
expect(complexObject.aListOfObjects[i].aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfObjects[i].aListOfDoubles, [1.0, 2.0, 3.0]);
}
});
});
group('BuiltComplexObject unit tests', () {
test('Typical object is converted correctly', () {
final complexObject = serializers.deserializeWith(
BuiltComplexObject.serializer, typicalObjectJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, "Blah, blah, blah.");
expect(complexObject.anObject.anInt, 1);
expect(complexObject.anObject.aDouble, 1.0);
expect(complexObject.anObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.anObject.aListOfInts, [1, 2, 3]);
expect(complexObject.anObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, "Blah, blah, blah.");
expect(complexObject.aListOfObjects[i].anInt, i + 1);
expect(complexObject.aListOfObjects[i].aDouble, 1.0);
expect(complexObject.aListOfObjects[i].aListOfStrings,
['one', 'two', 'three']);
expect(complexObject.aListOfObjects[i].aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfObjects[i].aListOfDoubles, [1.0, 2.0, 3.0]);
}
});
test('Empty object results in null fields', () {
final complexObject =
serializers.deserializeWith(BuiltComplexObject.serializer, emptyJson);
expect(complexObject.aString, isNull);
expect(complexObject.anInt, isNull);
expect(complexObject.aDouble, isNull);
expect(complexObject.anObject, isNull);
expect(complexObject.aListOfStrings, isNull);
expect(complexObject.aListOfInts, isNull);
expect(complexObject.aListOfDoubles, isNull);
expect(complexObject.aListOfObjects, isNull);
});
test('Empty simple objects result in instances with null fields', () {
final complexObject = serializers.deserializeWith(
BuiltComplexObject.serializer, emptySimpleObjectsJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, isNull);
expect(complexObject.anObject.anInt, isNull);
expect(complexObject.anObject.aDouble, isNull);
expect(complexObject.anObject.aListOfStrings, isNull);
expect(complexObject.anObject.aListOfInts, isNull);
expect(complexObject.anObject.aListOfDoubles, isNull);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, isNull);
expect(complexObject.aListOfObjects[i].anInt, isNull);
expect(complexObject.aListOfObjects[i].aDouble, isNull);
expect(complexObject.aListOfObjects[i].aListOfStrings, isNull);
expect(complexObject.aListOfObjects[i].aListOfInts, isNull);
expect(complexObject.aListOfObjects[i].aListOfDoubles, isNull);
}
});
test('Unexpected properties are ignored', () {
final complexObject = serializers.deserializeWith(
BuiltComplexObject.serializer, unexpectedPropertiesJson);
expect(complexObject.aString, "Blah, blah, blah.");
expect(complexObject.anInt, 1);
expect(complexObject.aDouble, 1.0);
expect(complexObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.anObject.aString, "Blah, blah, blah.");
expect(complexObject.anObject.anInt, 1);
expect(complexObject.anObject.aDouble, 1.0);
expect(complexObject.anObject.aListOfStrings, ['one', 'two', 'three']);
expect(complexObject.anObject.aListOfInts, [1, 2, 3]);
expect(complexObject.anObject.aListOfDoubles, [1.0, 2.0, 3.0]);
expect(complexObject.aListOfObjects.length, 3);
for (int i = 0; i < 3; i++) {
expect(complexObject.aListOfObjects[i].aString, "Blah, blah, blah.");
expect(complexObject.aListOfObjects[i].anInt, i + 1);
expect(complexObject.aListOfObjects[i].aDouble, 1.0);
expect(complexObject.aListOfObjects[i].aListOfStrings,
['one', 'two', 'three']);
expect(complexObject.aListOfObjects[i].aListOfInts, [1, 2, 3]);
expect(complexObject.aListOfObjects[i].aListOfDoubles, [1.0, 2.0, 3.0]);
}
});
});
}

View File

@@ -1,9 +0,0 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
void main() {
test('This test always passes.', () {});
}

View File

@@ -0,0 +1,197 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:jsonexample/built_value/built_simple_object.dart';
import 'package:jsonexample/built_value/built_value_serializers.dart';
import 'package:jsonexample/dart_convert/converted_simple_object.dart';
import 'package:jsonexample/json_serializable/serializable_simple_object.dart';
void main() {
const typicalObjectJson = <String, dynamic>{
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0]
};
const emptyListJson = <String, dynamic>{
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': [],
'aListOfInts': [],
'aListOfDoubles': []
};
const unexpectedPropertiesJson = <String, dynamic>{
'aString': 'Blah, blah, blah.',
'anInt': 1,
'aDouble': 1.0,
'aListOfStrings': ['one', 'two', 'three'],
'aListOfInts': [1, 2, 3],
'aListOfDoubles': [1.0, 2.0, 3.0],
'unexpectedProperty': 'Whoops!'
};
const emptyJson = <String, dynamic>{};
group('ConvertedSimpleObject unit tests', () {
test('Typical object is converted correctly', () {
final simpleObject = ConvertedSimpleObject.fromJson(typicalObjectJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, ['one', 'two', 'three']);
expect(simpleObject.aListOfInts, [1, 2, 3]);
expect(simpleObject.aListOfDoubles, [1.0, 2.0, 3.0]);
});
test('Empty object', () {
final simpleObject = ConvertedSimpleObject.fromJson(emptyJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, isNull);
expect(simpleObject.anInt, isNull);
expect(simpleObject.aDouble, isNull);
expect(simpleObject.aListOfStrings, isNull);
expect(simpleObject.aListOfInts, isNull);
expect(simpleObject.aListOfDoubles, isNull);
});
test('Empty lists', () {
final simpleObject = ConvertedSimpleObject.fromJson(emptyListJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, []);
expect(simpleObject.aListOfInts, []);
expect(simpleObject.aListOfDoubles, []);
});
test('Extra properties', () {
final simpleObject =
ConvertedSimpleObject.fromJson(unexpectedPropertiesJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, ['one', 'two', 'three']);
expect(simpleObject.aListOfInts, [1, 2, 3]);
expect(simpleObject.aListOfDoubles, [1.0, 2.0, 3.0]);
});
});
group('SerializableSimpleObject unit tests', () {
test('Typical object is converted correctly', () {
final simpleObject = SerializableSimpleObject.fromJson(typicalObjectJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, ['one', 'two', 'three']);
expect(simpleObject.aListOfInts, [1, 2, 3]);
expect(simpleObject.aListOfDoubles, [1.0, 2.0, 3.0]);
});
test('Empty object results in null fields', () {
final simpleObject = SerializableSimpleObject.fromJson(emptyJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, isNull);
expect(simpleObject.anInt, isNull);
expect(simpleObject.aDouble, isNull);
expect(simpleObject.aListOfStrings, isNull);
expect(simpleObject.aListOfInts, isNull);
expect(simpleObject.aListOfDoubles, isNull);
});
test('Empty lists are converted as empty lists', () {
final simpleObject = SerializableSimpleObject.fromJson(emptyListJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, []);
expect(simpleObject.aListOfInts, []);
expect(simpleObject.aListOfDoubles, []);
});
test('Unexpected properties are ignored', () {
final simpleObject =
SerializableSimpleObject.fromJson(unexpectedPropertiesJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, ['one', 'two', 'three']);
expect(simpleObject.aListOfInts, [1, 2, 3]);
expect(simpleObject.aListOfDoubles, [1.0, 2.0, 3.0]);
});
});
group('BuiltSimpleObject unit tests', () {
test('Typical object is converted correctly', () {
final simpleObject = serializers.deserializeWith(
BuiltSimpleObject.serializer, typicalObjectJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, ['one', 'two', 'three']);
expect(simpleObject.aListOfInts, [1, 2, 3]);
expect(simpleObject.aListOfDoubles, [1.0, 2.0, 3.0]);
});
test('Empty object results in null fields', () {
final simpleObject =
serializers.deserializeWith(BuiltSimpleObject.serializer, emptyJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, isNull);
expect(simpleObject.anInt, isNull);
expect(simpleObject.aDouble, isNull);
expect(simpleObject.aListOfStrings, isNull);
expect(simpleObject.aListOfInts, isNull);
expect(simpleObject.aListOfDoubles, isNull);
});
test('Empty lists are converted as empty lists', () {
final simpleObject = serializers.deserializeWith(
BuiltSimpleObject.serializer, emptyListJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, []);
expect(simpleObject.aListOfInts, []);
expect(simpleObject.aListOfDoubles, []);
});
test('Unexpected properties are ignored', () {
final simpleObject = serializers.deserializeWith(
BuiltSimpleObject.serializer, unexpectedPropertiesJson);
expect(simpleObject, isNotNull);
expect(simpleObject.aString, "Blah, blah, blah.");
expect(simpleObject.anInt, 1);
expect(simpleObject.aDouble, 1.0);
expect(simpleObject.aListOfStrings, ['one', 'two', 'three']);
expect(simpleObject.aListOfInts, [1, 2, 3]);
expect(simpleObject.aListOfDoubles, [1.0, 2.0, 3.0]);
});
});
}

View File

@@ -0,0 +1,199 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:jsonexample/dart_convert/converted_simple_object.dart';
import 'package:jsonexample/dart_convert/converted_complex_object.dart';
import 'package:jsonexample/widgets.dart';
void main() {
group('SimpleObjectView widget test', () {
testWidgets('Typical object is displayed correctly',
(WidgetTester tester) async {
final simpleObject = ConvertedSimpleObject(
aString: 'Blah, blah, blah',
anInt: 1,
aDouble: 1.0,
aListOfStrings: ['one', 'two', 'three'],
aListOfInts: [1, 2, 3],
aListOfDoubles: [1.0, 2.0, 3.0],
);
await tester.pumpWidget(
MaterialApp(
home: SimpleObjectView(simpleObject),
),
);
expect(find.text('"Blah, blah, blah"'), findsOneWidget);
expect(find.text('1'), findsOneWidget);
expect(find.text('1.0'), findsOneWidget);
expect(find.text('["one", "two", "three"]'), findsOneWidget);
expect(find.text('[1, 2, 3]'), findsOneWidget);
expect(find.text('[1.0, 2.0, 3.0]'), findsOneWidget);
});
testWidgets('Empty lists are displayed as brackets',
(WidgetTester tester) async {
final simpleObject = ConvertedSimpleObject(
aString: 'Blah, blah, blah',
anInt: 1,
aDouble: 1.0,
aListOfStrings: [],
aListOfInts: [],
aListOfDoubles: [],
);
await tester.pumpWidget(
MaterialApp(
home: SimpleObjectView(simpleObject),
),
);
expect(find.text('[]'), findsNWidgets(3));
});
testWidgets('Null values are displayed as NULL',
(WidgetTester tester) async {
final simpleObject = ConvertedSimpleObject(
aString: null,
anInt: null,
aDouble: null,
aListOfStrings: null,
aListOfInts: null,
aListOfDoubles: null,
);
await tester.pumpWidget(
MaterialApp(
home: SimpleObjectView(simpleObject),
),
);
expect(find.text('NULL'), findsNWidgets(6));
});
});
group('ComplexObjectView widget test', () {
testWidgets('Typical object is displayed correctly',
(WidgetTester tester) async {
final complexObject = ConvertedComplexObject(
aString: 'Blah, blah, blah',
anInt: 1,
aDouble: 1.0,
aListOfStrings: ['one', 'two', 'three'],
aListOfInts: [1, 2, 3],
aListOfDoubles: [1.0, 2.0, 3.0],
anObject: ConvertedSimpleObject(
aString: "Child 1",
anInt: 101,
aDouble: 101.0,
aListOfStrings: ['1011', '1012', '1013'],
aListOfInts: [1011, 1012, 1013],
aListOfDoubles: [1011.0, 1012.0, 1013.0],
),
aListOfObjects: [
ConvertedSimpleObject(
aString: "Child 2",
anInt: 102,
aDouble: 102.0,
aListOfStrings: ['1021', '1022', '1023'],
aListOfInts: [1021, 1022, 1023],
aListOfDoubles: [1021.0, 1022.0, 1023.0],
),
ConvertedSimpleObject(
aString: "Child 3",
anInt: 103,
aDouble: 103.0,
aListOfStrings: ['1031', '1032', '1033'],
aListOfInts: [1031, 1032, 1033],
aListOfDoubles: [1031.0, 1032.0, 1033.0],
),
ConvertedSimpleObject(
aString: "Child 4",
anInt: 104,
aDouble: 104.0,
aListOfStrings: ['1041', '1042', '1043'],
aListOfInts: [1041, 1042, 1043],
aListOfDoubles: [1041.0, 1042.0, 1043.0],
),
],
);
await tester.pumpWidget(
MaterialApp(
home: ComplexObjectView(complexObject),
),
);
expect(find.text('"Blah, blah, blah"'), findsOneWidget);
expect(find.text('1'), findsOneWidget);
expect(find.text('1.0'), findsOneWidget);
expect(find.text('["one", "two", "three"]'), findsOneWidget);
expect(find.text('[1, 2, 3]'), findsOneWidget);
expect(find.text('[1.0, 2.0, 3.0]'), findsOneWidget);
for (int i = 1; i <= 4; i++) {
expect(find.text('"Child $i"'), findsOneWidget);
expect(find.text('10$i'), findsOneWidget);
expect(find.text('10$i.0'), findsOneWidget);
expect(find.text('["10${i}1", "10${i}2", "10${i}3"]'), findsOneWidget);
expect(find.text('[10${i}1, 10${i}2, 10${i}3]'), findsOneWidget);
expect(find.text('[10${i}1.0, 10${i}2.0, 10${i}3.0]'), findsOneWidget);
}
});
testWidgets('Empty lists are displayed as brackets',
(WidgetTester tester) async {
final complexObject = ConvertedComplexObject(
aString: 'Blah, blah, blah',
anInt: 1,
aDouble: 1.0,
aListOfStrings: [],
aListOfInts: [],
aListOfDoubles: [],
anObject: ConvertedSimpleObject(
aString: "Child 1",
anInt: 101,
aDouble: 101.0,
aListOfStrings: ['1011', '1012', '1013'],
aListOfInts: [1011, 1012, 1013],
aListOfDoubles: [1011.0, 1012.0, 1013.0],
),
aListOfObjects: [],
);
await tester.pumpWidget(
MaterialApp(
home: ComplexObjectView(complexObject),
),
);
expect(find.text('[]'), findsNWidgets(4));
});
testWidgets('Null values are displayed as NULL',
(WidgetTester tester) async {
final complexObject = ConvertedComplexObject(
aString: null,
anInt: null,
aDouble: null,
aListOfStrings: null,
aListOfInts: null,
aListOfDoubles: null,
anObject: null,
aListOfObjects: null,
);
await tester.pumpWidget(
MaterialApp(
home: ComplexObjectView(complexObject),
),
);
expect(find.text('NULL'), findsNWidgets(8));
});
});
}