Skip to content
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

Improve testability of structs in Dart #1048

Merged
merged 2 commits into from
Aug 26, 2021
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Gluecodium project Release Notes

## Unreleased
### Bug fixes:
* Improved mockability of `$prototype` for structs with methods in Dart.

## 9.4.3
Release date: 2021-08-20
### Bug fixes:
Expand All @@ -17,7 +21,8 @@ Release date: 2021-08-19
## 9.4.1
Release date: 2021-08-17
### Features:
* Added `$prototype` field to types with static functions/properties, to allow for mocking of these statics in tests.
* Added `$prototype` field to types with static functions/properties in Dart, to allow for mocking of these statics in
tests.
### Removed:
* Dart "-stubs" testable mode was removed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal class DartDeclarationImportResolver(srcPath: String) : DartImportResolv
) {
result += collectionPackageImport
}
if (limeStruct.attributes.have(LimeAttributeType.IMMUTABLE) || hasStaticFunctions(limeStruct)) {
if (limeStruct.attributes.have(LimeAttributeType.IMMUTABLE) || limeStruct.functions.isNotEmpty()) {
result += metaPackageImport
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
{{#parameters}}
final _{{resolveName}}Handle = {{#set call="ToFfi"}}{{>dart/DartFfiConversionCall}}{{/set}}({{resolveName}});
{{/parameters}}{{#unless isStatic}}
final _handle = {{#if isStruct}}{{resolveName parent "Ffi"}}ToFfi(this){{/if}}{{!!
}}{{#unless isStruct}}this.handle{{/unless}};
final _handle = {{#if isStruct}}{{resolveName parent "Ffi"}}ToFfi($that){{/if}}{{#unless isStruct}}this.handle{{/unless}};
{{/unless}}
{{#if thrownType}}final __callResultHandle = {{/if}}{{!!
}}{{#unless thrownType returnType.isVoid logic="and"}}final __resultHandle = {{/unless}}{{!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
! License-Filename: LICENSE
!
!}}
{{#unless isConstructor}}{{#if isStatic}}{{#unless ignoreStatic}}static {{/unless}}{{/if}}{{/unless}}{{!!
}}{{#if isConstructor}}{{#if isStruct}}{{resolveName returnType}} {{/if}}{{!!
}}{{#unless isStruct}}static Pointer<Void> {{/unless}}{{/if}}{{!!
}}{{#unless isConstructor}}{{resolveName returnType}} {{/unless}}{{!!
}}{{#if isConstructor}}{{#unless isStruct}}_{{/unless}}{{/if}}{{!!
}}{{#unless isConstructor}}{{resolveName visibility}}{{/unless}}{{resolveName}}({{!!
{{#if isConstructor}}{{!!
}}{{#if isStruct}}{{resolveName returnType}} {{/if}}{{#unless isStruct}}static Pointer<Void> _{{/unless}}{{resolveName}}({{!!
}}{{/if}}{{!!

}}{{#unless isConstructor}}{{!!
}}{{#if isStatic}}{{#unless ignoreStatic}}static {{/unless}}{{/if}}{{resolveName returnType}} {{resolveName visibility}}{{resolveName}}({{!!
}}{{#if addThat}}{{#unless isStatic}}{{resolveName parent}}{{#if parent.external.dart.converter}}Internal{{/if}} $that{{!!
}}{{#if parameters}}, {{/if}}{{/unless}}{{/if}}{{!!
}}{{/unless}}{{!!

}}{{#parameters}}{{>dart/DartAttributesInline}}{{resolveName typeRef}} {{resolveName}}{{#if iter.hasNext}}, {{/if}}{{/parameters}})
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
!
!}}
=> {{#if isConstructor}}{{resolveName parent}}$Impl{{/if}}{{#unless isConstructor}}$prototype{{/unless}}{{!!
}}.{{resolveName visibility}}{{resolveName}}({{#parameters}}{{resolveName}}{{#if iter.hasNext}}, {{/if}}{{/parameters}})
}}.{{resolveName visibility}}{{resolveName}}({{#if isStruct}}{{#unless isStatic}}this{{#if parameters}}, {{/if}}{{/unless}}{{/if}}{{!!
}}{{#parameters}}{{resolveName}}{{#if iter.hasNext}}, {{/if}}{{/parameters}})
17 changes: 9 additions & 8 deletions gluecodium/src/main/resources/templates/dart/DartStruct.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class {{resolveName}}{{#if external.dart.converter}}Internal{{/if}} {
{{/constructors}}
{{#functions}}{{#unless isConstructor}}
{{prefixPartial "dart/DartFunctionDocs" " "}}
{{#if isStatic}}{{prefixPartial "dart/DartFunctionSignature" " "}}{{>dart/DartRedirectImpl}};{{/if}}{{!!
}}{{#unless isStatic}}{{prefixPartial "dart/DartFunction" " "}}{{/unless}}
{{prefixPartial "dart/DartFunctionSignature" " "}}{{>dart/DartRedirectImpl}};
{{/unless}}{{/functions}}{{/set}}
{{#if attributes.equatable}}{{#unless external.dart.converter}}
@override
Expand All @@ -62,12 +61,12 @@ class {{resolveName}}{{#if external.dart.converter}}Internal{{/if}} {
return result;
}
{{/unless}}{{/if}}
{{#ifPredicate "hasStaticFunctions"}}
{{#if functions}}

/// @nodoc
@visibleForTesting
static dynamic $prototype = {{resolveName}}$Impl();
{{/ifPredicate}}
{{/if}}
}
{{/unlessPredicate}}

Expand Down Expand Up @@ -104,18 +103,20 @@ final _{{resolveName parent "Ffi"}}GetField{{resolveName "Ffi"}} = __lib.catchAr
>('{{libraryName}}_{{resolveName parent "FfiSnakeCase"}}_get_field_{{resolveName "FfiSnakeCase"}}'));
{{/fields}}{{/set}}

{{#unlessPredicate "skipDeclaration"}}
{{>dart/DartOptimizedLists}}

{{#ifPredicate "hasStaticFunctions"}}
{{#if functions}}

/// @nodoc
@visibleForTesting
class {{resolveName}}$Impl {
{{#set parent=this isStruct=true ignoreStatic=true}}{{#functions}}{{#if isStatic}}
{{#set parent=this isStruct=true ignoreStatic=true addThat=true}}{{#functions}}
{{prefixPartial "dart/DartFunction" " "}}
{{/if}}{{/functions}}{{/set}}
{{/functions}}{{/set}}
}
{{/ifPredicate}}
{{/if}}
{{/unlessPredicate}}

Pointer<Void> {{resolveName "Ffi"}}ToFfi({{resolveName this "" "ref"}} value{{#if external.dart.converter}}External{{/if}}) {
{{#if external.dart.converter}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:ffi';
import 'package:library/src/_library_context.dart' as __lib;
import 'package:library/src/builtin_types__conversion.dart';
import 'package:meta/meta.dart';
@OnStruct
class AttributesStruct {
@OnField
Expand All @@ -9,14 +10,10 @@ class AttributesStruct {
@OnConstInStruct
static final bool pi = false;
@OnFunctionInStruct
void veryFun(@OnParameterInStruct String param) {
final _veryFunFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Void Function(Pointer<Void>, Int32, Pointer<Void>), void Function(Pointer<Void>, int, Pointer<Void>)>('library_smoke_AttributesStruct_veryFun__String'));
final _paramHandle = stringToFfi(param);
final _handle = smokeAttributesstructToFfi(this);
_veryFunFfi(_handle, __lib.LibraryContext.isolateId, _paramHandle);
smokeAttributesstructReleaseFfiHandle(_handle);
stringReleaseFfiHandle(_paramHandle);
}
void veryFun(@OnParameterInStruct String param) => $prototype.veryFun(this, param);
/// @nodoc
@visibleForTesting
static dynamic $prototype = AttributesStruct$Impl();
}
// AttributesStruct "private" section, not exported.
final _smokeAttributesstructCreateHandle = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<
Expand All @@ -31,6 +28,18 @@ final _smokeAttributesstructGetFieldfield = __lib.catchArgumentError(() => __lib
Pointer<Void> Function(Pointer<Void>),
Pointer<Void> Function(Pointer<Void>)
>('library_smoke_AttributesStruct_get_field_field'));
/// @nodoc
@visibleForTesting
class AttributesStruct$Impl {
void veryFun(AttributesStruct $that, @OnParameterInStruct String param) {
final _veryFunFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Void Function(Pointer<Void>, Int32, Pointer<Void>), void Function(Pointer<Void>, int, Pointer<Void>)>('library_smoke_AttributesStruct_veryFun__String'));
final _paramHandle = stringToFfi(param);
final _handle = smokeAttributesstructToFfi($that);
_veryFunFfi(_handle, __lib.LibraryContext.isolateId, _paramHandle);
smokeAttributesstructReleaseFfiHandle(_handle);
stringReleaseFfiHandle(_paramHandle);
}
}
Pointer<Void> smokeAttributesstructToFfi(AttributesStruct value) {
final _fieldHandle = stringToFfi(value.field);
final _result = _smokeAttributesstructCreateHandle(_fieldHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ class Comments_SomeStruct {
Comments_SomeStruct(this.someField, this.nullableField);
/// This is some struct method that does nothing.
///
void someStructMethod() {
final _someStructMethodFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Void Function(Pointer<Void>, Int32), void Function(Pointer<Void>, int)>('library_smoke_Comments_SomeStruct_someStructMethod'));
final _handle = smokeCommentsSomestructToFfi(this);
_someStructMethodFfi(_handle, __lib.LibraryContext.isolateId);
smokeCommentsSomestructReleaseFfiHandle(_handle);
}
void someStructMethod() => $prototype.someStructMethod(this);
/// This is some static struct method that does nothing.
///
static void someStaticStructMethod() => $prototype.someStaticStructMethod();
Expand Down Expand Up @@ -174,6 +169,12 @@ final _smokeCommentsSomestructGetFieldnullableField = __lib.catchArgumentError((
/// @nodoc
@visibleForTesting
class Comments_SomeStruct$Impl {
void someStructMethod(Comments_SomeStruct $that) {
final _someStructMethodFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Void Function(Pointer<Void>, Int32), void Function(Pointer<Void>, int)>('library_smoke_Comments_SomeStruct_someStructMethod'));
final _handle = smokeCommentsSomestructToFfi($that);
_someStructMethodFfi(_handle, __lib.LibraryContext.isolateId);
smokeCommentsSomestructReleaseFfiHandle(_handle);
}
void someStaticStructMethod() {
final _someStaticStructMethodFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Void Function(Int32), void Function(int)>('library_smoke_Comments_SomeStruct_someStaticStructMethod'));
_someStaticStructMethodFfi(__lib.LibraryContext.isolateId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import 'dart:ffi';
import 'package:library/src/_library_context.dart' as __lib;
import 'package:library/src/smoke/free_enum.dart';
import 'package:meta/meta.dart';
class FreePoint {
double x;
double y;
FreePoint(this.x, this.y);
static final FreeEnum aBar = FreeEnum.bar;
FreePoint flip() {
final _flipFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Pointer<Void> Function(Pointer<Void>, Int32), Pointer<Void> Function(Pointer<Void>, int)>('library_smoke_FreePoint_flip'));
final _handle = smokeFreepointToFfi(this);
final __resultHandle = _flipFfi(_handle, __lib.LibraryContext.isolateId);
smokeFreepointReleaseFfiHandle(_handle);
try {
return smokeFreepointFromFfi(__resultHandle);
} finally {
smokeFreepointReleaseFfiHandle(__resultHandle);
}
}
FreePoint flip() => $prototype.flip(this);
/// @nodoc
@visibleForTesting
static dynamic $prototype = FreePoint$Impl();
}
// FreePoint "private" section, not exported.
final _smokeFreepointCreateHandle = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<
Expand All @@ -35,6 +29,21 @@ final _smokeFreepointGetFieldy = __lib.catchArgumentError(() => __lib.nativeLibr
Double Function(Pointer<Void>),
double Function(Pointer<Void>)
>('library_smoke_FreePoint_get_field_y'));
/// @nodoc
@visibleForTesting
class FreePoint$Impl {
FreePoint flip(FreePoint $that) {
final _flipFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Pointer<Void> Function(Pointer<Void>, Int32), Pointer<Void> Function(Pointer<Void>, int)>('library_smoke_FreePoint_flip'));
final _handle = smokeFreepointToFfi($that);
final __resultHandle = _flipFfi(_handle, __lib.LibraryContext.isolateId);
smokeFreepointReleaseFfiHandle(_handle);
try {
return smokeFreepointFromFfi(__resultHandle);
} finally {
smokeFreepointReleaseFfiHandle(__resultHandle);
}
}
}
Pointer<Void> smokeFreepointToFfi(FreePoint value) {
final _xHandle = (value.x);
final _yHandle = (value.y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:library/src/_token_cache.dart' as __lib;
import 'package:library/src/_type_repository.dart' as __lib;
import 'package:library/src/builtin_types__conversion.dart';
import 'package:library/src/generic_types__conversion.dart';
import 'package:meta/meta.dart';
final _doNothingReturnReleaseHandle = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<
Void Function(Pointer<Void>),
void Function(Pointer<Void>)
Expand All @@ -22,22 +23,10 @@ final _doNothingReturnHasError = __lib.catchArgumentError(() => __lib.nativeLibr
class OuterStruct {
String field;
OuterStruct(this.field);
void doNothing() {
final _doNothingFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Pointer<Void> Function(Pointer<Void>, Int32), Pointer<Void> Function(Pointer<Void>, int)>('library_smoke_OuterStruct_doNothing'));
final _handle = smokeOuterstructToFfi(this);
final __callResultHandle = _doNothingFfi(_handle, __lib.LibraryContext.isolateId);
smokeOuterstructReleaseFfiHandle(_handle);
if (_doNothingReturnHasError(__callResultHandle) != 0) {
final __errorHandle = _doNothingReturnGetError(__callResultHandle);
_doNothingReturnReleaseHandle(__callResultHandle);
try {
throw OuterStruct_InstantiationException(smokeOuterstructInnerenumFromFfi(__errorHandle));
} finally {
smokeOuterstructInnerenumReleaseFfiHandle(__errorHandle);
}
}
_doNothingReturnReleaseHandle(__callResultHandle);
}
void doNothing() => $prototype.doNothing(this);
/// @nodoc
@visibleForTesting
static dynamic $prototype = OuterStruct$Impl();
}
enum OuterStruct_InnerEnum {
foo,
Expand Down Expand Up @@ -101,12 +90,10 @@ class OuterStruct_InstantiationException implements Exception {
class OuterStruct_InnerStruct {
List<DateTime> otherField;
OuterStruct_InnerStruct(this.otherField);
void doSomething() {
final _doSomethingFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Void Function(Pointer<Void>, Int32), void Function(Pointer<Void>, int)>('library_smoke_OuterStruct_InnerStruct_doSomething'));
final _handle = smokeOuterstructInnerstructToFfi(this);
_doSomethingFfi(_handle, __lib.LibraryContext.isolateId);
smokeOuterstructInnerstructReleaseFfiHandle(_handle);
}
void doSomething() => $prototype.doSomething(this);
/// @nodoc
@visibleForTesting
static dynamic $prototype = OuterStruct_InnerStruct$Impl();
}
// OuterStruct_InnerStruct "private" section, not exported.
final _smokeOuterstructInnerstructCreateHandle = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<
Expand All @@ -121,6 +108,16 @@ final _smokeOuterstructInnerstructGetFieldotherField = __lib.catchArgumentError(
Pointer<Void> Function(Pointer<Void>),
Pointer<Void> Function(Pointer<Void>)
>('library_smoke_OuterStruct_InnerStruct_get_field_otherField'));
/// @nodoc
@visibleForTesting
class OuterStruct_InnerStruct$Impl {
void doSomething(OuterStruct_InnerStruct $that) {
final _doSomethingFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Void Function(Pointer<Void>, Int32), void Function(Pointer<Void>, int)>('library_smoke_OuterStruct_InnerStruct_doSomething'));
final _handle = smokeOuterstructInnerstructToFfi($that);
_doSomethingFfi(_handle, __lib.LibraryContext.isolateId);
smokeOuterstructInnerstructReleaseFfiHandle(_handle);
}
}
Pointer<Void> smokeOuterstructInnerstructToFfi(OuterStruct_InnerStruct value) {
final _otherFieldHandle = foobarListofDateToFfi(value.otherField);
final _result = _smokeOuterstructInnerstructCreateHandle(_otherFieldHandle);
Expand Down Expand Up @@ -337,6 +334,26 @@ final _smokeOuterstructGetFieldfield = __lib.catchArgumentError(() => __lib.nati
Pointer<Void> Function(Pointer<Void>),
Pointer<Void> Function(Pointer<Void>)
>('library_smoke_OuterStruct_get_field_field'));
/// @nodoc
@visibleForTesting
class OuterStruct$Impl {
void doNothing(OuterStruct $that) {
final _doNothingFfi = __lib.catchArgumentError(() => __lib.nativeLibrary.lookupFunction<Pointer<Void> Function(Pointer<Void>, Int32), Pointer<Void> Function(Pointer<Void>, int)>('library_smoke_OuterStruct_doNothing'));
final _handle = smokeOuterstructToFfi($that);
final __callResultHandle = _doNothingFfi(_handle, __lib.LibraryContext.isolateId);
smokeOuterstructReleaseFfiHandle(_handle);
if (_doNothingReturnHasError(__callResultHandle) != 0) {
final __errorHandle = _doNothingReturnGetError(__callResultHandle);
_doNothingReturnReleaseHandle(__callResultHandle);
try {
throw OuterStruct_InstantiationException(smokeOuterstructInnerenumFromFfi(__errorHandle));
} finally {
smokeOuterstructInnerenumReleaseFfiHandle(__errorHandle);
}
}
_doNothingReturnReleaseHandle(__callResultHandle);
}
}
Pointer<Void> smokeOuterstructToFfi(OuterStruct value) {
final _fieldHandle = stringToFfi(value.field);
final _result = _smokeOuterstructCreateHandle(_fieldHandle);
Expand Down
Loading