Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/flutterfire_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Refactor `FieldElementParser` to enable to write unit test.
* Add `field_element_parser_test.dart`.
* Add `dart_type_util.dart` tests.

## 0.3.0-dev.5

Expand Down
9 changes: 0 additions & 9 deletions packages/flutterfire_gen/lib/src/utils/dart_type_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ extension DartTypeExtension on DartType {
return code;
}
}

if (type is TypeParameterType) {
final code = type.getDisplayString(withNullability: false);
if (wrapByFirestoreData) {
return _wrapByFirestoreData(code);
} else {
return code;
}
}
throw UnimplementedError('(${type.runtimeType}) $type');
}

Expand Down
185 changes: 143 additions & 42 deletions packages/flutterfire_gen/test/utils/dart_type_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,104 @@ import 'package:test/test.dart';

import 'dart_type_util_test.mocks.dart';

@GenerateMocks([DynamicType, InterfaceType, InterfaceElement])
@GenerateMocks([
DynamicType,
InterfaceType,
InterfaceElement,
TypeParameterType,
])
void main() {
group('DartTypeExtension tests', () {
final intType = MockInterfaceType();
final intElement = MockInterfaceElement();

when(intElement.name).thenReturn('int');
when(intType.element).thenReturn(intElement);
when(intType.typeArguments).thenReturn([]);
when(intType.nullabilitySuffix).thenReturn(NullabilitySuffix.none);

final nullableIntType = MockInterfaceType();
final nullableIntElement = MockInterfaceElement();
when(nullableIntElement.name).thenReturn('int');
when(nullableIntType.element).thenReturn(nullableIntElement);
when(nullableIntType.typeArguments).thenReturn([]);
when(nullableIntType.nullabilitySuffix)
.thenReturn(NullabilitySuffix.question);

final stringType = MockInterfaceType();
final stringElement = MockInterfaceElement();
when(stringElement.name).thenReturn('String');
when(stringType.element).thenReturn(stringElement);
when(stringType.typeArguments).thenReturn([]);
when(stringType.nullabilitySuffix).thenReturn(NullabilitySuffix.none);

final nullableStringType = MockInterfaceType();
final nullableStringElement = MockInterfaceElement();
when(nullableStringElement.name).thenReturn('String');
when(nullableStringType.element).thenReturn(nullableStringElement);
when(nullableStringType.typeArguments).thenReturn([]);
when(nullableStringType.nullabilitySuffix)
.thenReturn(NullabilitySuffix.question);

final listOfStringsType = MockInterfaceType();
final listOfStringsElement = MockInterfaceElement();
when(listOfStringsElement.name).thenReturn('List');
when(listOfStringsType.element).thenReturn(listOfStringsElement);
when(listOfStringsType.typeArguments).thenReturn([
stringType,
]);
when(listOfStringsType.nullabilitySuffix)
.thenReturn(NullabilitySuffix.none);
late final MockInterfaceType intType;
late final MockInterfaceElement intElement;
late final MockInterfaceType nullableIntType;
late final MockInterfaceElement nullableIntElement;
late final MockInterfaceType stringType;
late final MockInterfaceElement stringElement;
late final MockInterfaceType nullableStringType;
late final MockInterfaceElement nullableStringElement;
late final MockInterfaceType dateTimeType;
late final MockInterfaceElement dateTimeElement;
late final MockInterfaceType listOfStringsType;
late final MockInterfaceElement listOfStringsElement;
late final MockInterfaceType jsonMapType;
late final MockDynamicType dynamicType;
late final MockInterfaceElement mapElement;
late final MockInterfaceType intDynamicMapType;
late final MockInterfaceElement intDynamicMapElement;

setUpAll(() {
intType = MockInterfaceType();
intElement = MockInterfaceElement();
when(intElement.name).thenReturn('int');
when(intType.element).thenReturn(intElement);
when(intType.typeArguments).thenReturn([]);
when(intType.nullabilitySuffix).thenReturn(NullabilitySuffix.none);
when(intType.isDartCoreString).thenReturn(false);

nullableIntType = MockInterfaceType();
nullableIntElement = MockInterfaceElement();
when(nullableIntElement.name).thenReturn('int');
when(nullableIntType.element).thenReturn(nullableIntElement);
when(nullableIntType.typeArguments).thenReturn([]);
when(nullableIntType.nullabilitySuffix)
.thenReturn(NullabilitySuffix.question);

stringType = MockInterfaceType();
stringElement = MockInterfaceElement();
when(stringElement.name).thenReturn('String');
when(stringType.element).thenReturn(stringElement);
when(stringType.typeArguments).thenReturn([]);
when(stringType.nullabilitySuffix).thenReturn(NullabilitySuffix.none);
when(stringType.isDartCoreString).thenReturn(true);
when(stringType.isDartCoreList).thenReturn(false);

nullableStringType = MockInterfaceType();
nullableStringElement = MockInterfaceElement();
when(nullableStringElement.name).thenReturn('String');
when(nullableStringType.element).thenReturn(nullableStringElement);
when(nullableStringType.typeArguments).thenReturn([]);
when(nullableStringType.nullabilitySuffix)
.thenReturn(NullabilitySuffix.question);

dateTimeType = MockInterfaceType();
dateTimeElement = MockInterfaceElement();
when(dateTimeElement.name).thenReturn('DateTime');
when(dateTimeType.element).thenReturn(dateTimeElement);
when(dateTimeType.typeArguments).thenReturn([]);
when(dateTimeType.nullabilitySuffix).thenReturn(NullabilitySuffix.none);

listOfStringsType = MockInterfaceType();
listOfStringsElement = MockInterfaceElement();
when(listOfStringsElement.name).thenReturn('List');
when(listOfStringsType.element).thenReturn(listOfStringsElement);
when(listOfStringsType.typeArguments).thenReturn([stringType]);
when(listOfStringsType.nullabilitySuffix)
.thenReturn(NullabilitySuffix.none);
when(listOfStringsType.isDartCoreList).thenReturn(true);

jsonMapType = MockInterfaceType();
dynamicType = MockDynamicType();
mapElement = MockInterfaceElement();
when(mapElement.name).thenReturn('Map');
when(jsonMapType.isDartCoreMap).thenReturn(true);
when(jsonMapType.element).thenReturn(mapElement);
when(jsonMapType.typeArguments).thenReturn([stringType, dynamicType]);
when(jsonMapType.nullabilitySuffix).thenReturn(NullabilitySuffix.none);

intDynamicMapType = MockInterfaceType();
intDynamicMapElement = MockInterfaceElement();
when(intDynamicMapElement.name).thenReturn('Map');
when(intDynamicMapType.isDartCoreMap).thenReturn(true);
when(intDynamicMapType.element).thenReturn(mapElement);
when(intDynamicMapType.typeArguments).thenReturn([intType, dynamicType]);
when(intDynamicMapType.nullabilitySuffix)
.thenReturn(NullabilitySuffix.none);
});

group('DartType.typeName tests', () {
test('dynamic type returns "dynamic"', () {
final dynamicType = MockDynamicType();

expect(dynamicType.typeName(), 'dynamic');
});

Expand Down Expand Up @@ -88,6 +138,57 @@ void main() {
'FirestoreData<List<String>>',
);
});

test(
'TypeParameterType (Generics T) type throws Unimplemented error',
() {
final typeParameterType = MockTypeParameterType();
expect(
typeParameterType.typeName,
throwsA(isA<UnimplementedError>()),
);
},
);
});

test('isDateTimeType test', () {
expect(dateTimeType.isDateTimeType, true);
expect(intType.isDateTimeType, false);
});

group('firstTypeArgumentOfList test', () {
test('List<String> type returns String type', () {
expect(listOfStringsType.firstTypeArgumentOfList, stringType);
});

test('String type returns null', () {
expect(stringType.firstTypeArgumentOfList, null);
});
});

group('isJsonMap test', () {
test('Map<String, dynamic> type is JSON map', () {
expect(jsonMapType.isJsonMap, true);
});

test('Map<int, dynamic> type is not JSON map', () {
expect(intDynamicMapType.isJsonMap, false);
});
});

group('keyValueOfMap test', () {
test(
'Map<String, dynamic> type returns '
'Record of (key: stringType, value: dynamicType)', () {
expect(
jsonMapType.keyValueOfMap,
(key: stringType, value: dynamicType),
);
});

test('Map<int, dynamic> type returns null', () {
expect(intDynamicMapType.keyValueOfMap, null);
});
});
});
}
Loading