Skip to content

Instantly share code, notes, and snippets.

@minikin
Created July 31, 2020 08:38
Show Gist options
  • Save minikin/f15c8b2220a2b7b8aede0d39e7b8ca22 to your computer and use it in GitHub Desktop.
Save minikin/f15c8b2220a2b7b8aede0d39e7b8ca22 to your computer and use it in GitHub Desktop.
Built Value Test
library asset_file;
import 'dart:convert';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'asset_file.g.dart';
abstract class AssetFile implements Built<AssetFile, AssetFileBuilder> {
static Serializer<AssetFile> get serializer => _$assetFileSerializer;
factory AssetFile([void Function(AssetFileBuilder) updates]) = _$AssetFile;
AssetFile._();
String get contentType;
AssetFileDetails get details;
String get fileName;
String get url;
String toJson() {
return json.encode(
contentfulSerializers.serializeWith(AssetFile.serializer, this));
}
static AssetFile fromJson(String jsonString) {
return contentfulSerializers.deserializeWith(
AssetFile.serializer, json.decode(jsonString));
}
}
import 'package:built_value_test/matcher.dart';
import 'package:test/test.dart';
import '../utils/load_fixture.dart';
void main() {
group('AssetFileDetails Tests:', () {
test('returns instance of AssetFile from json', () {
final assetFile = AssetFile.fromJson(loadFixture('asset_file'));
expect(assetFile, const TypeMatcher<AssetFile>());
});
test('returns json string from AssetFile', () {
final assetFile = AssetFile.fromJson(loadFixture('asset_file'));
final assetFileString = assetFile.toJson();
expect(assetFileString, const TypeMatcher<String>());
});
test('built_value matcher', () {
final value = AssetFile(
(b) => b
..contentType = 'contentType'
..fileName = 'fileName'
..url = 'url'
..details = AssetFileDetails(
(b) => b
..size = 100
..image = AssetFileDetailsImage(
(b) => b
..height = 100
..width = 200,
).toBuilder(),
).toBuilder(),
);
expect(value, equalsBuilt(value));
});
test('compared value matcher', () {
final value = AssetFile(
(b) => b
..contentType = 'contentType'
..fileName = 'fileName'
..url = 'url'
..details = AssetFileDetails(
(b) => b
..size = 100
..image = AssetFileDetailsImage(
(b) => b
..height = 100
..width = 200,
).toBuilder(),
).toBuilder(),
);
final otherValue = AssetFile(
(b) => b
..contentType = 'contentType'
..fileName = 'fileName'
..url = 'url'
..details = AssetFileDetails(
(b) => b
..size = 100
..image = AssetFileDetailsImage(
(b) => b
..height = 100
..width = 200,
).toBuilder(),
).toBuilder(),
);
expect(value, otherValue);
});
test('reports if not same', () {
final value = AssetFile(
(b) => b
..contentType = 'contentType'
..fileName = 'fileName'
..url = 'url'
..details = AssetFileDetails(
(b) => b
..size = 100
..image = AssetFileDetailsImage(
(b) => b
..height = 100
..width = 200,
).toBuilder(),
).toBuilder(),
);
final otherValue = value.rebuild(
(b) => b..contentType = 'not a contentType',
);
expectMismatch(value, otherValue, '');
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment