From b6ca936c2160b1889a13d86b60e34a3973ba751f Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Tue, 20 Jun 2023 17:43:03 -0700 Subject: [PATCH] Move flutter service tests to dart Moving it to dart allows them to be re-used between both SDKs. --- src/SDK/Language/Dart.php | 10 ++++++++++ src/SDK/Language/Flutter.php | 2 +- templates/dart/pubspec.yaml.twig | 1 + templates/dart/test/id_test.dart.twig | 4 ++++ templates/dart/test/permission_test.dart.twig | 4 ++++ templates/dart/test/query_test.dart.twig | 4 ++++ templates/dart/test/role_test.dart.twig | 4 ++++ .../test/services/service_test.dart.twig | 8 ++++++-- templates/dart/test/src/enums_test.dart.twig | 4 ++++ templates/dart/test/src/models/model_test.dart.twig | 4 ++++ templates/dart/test/src/response_test.dart.twig | 4 ++++ 11 files changed, 46 insertions(+), 3 deletions(-) rename templates/{flutter => dart}/test/services/service_test.dart.twig (80%) diff --git a/src/SDK/Language/Dart.php b/src/SDK/Language/Dart.php index 51f46c301..3f3c85272 100644 --- a/src/SDK/Language/Dart.php +++ b/src/SDK/Language/Dart.php @@ -401,6 +401,16 @@ public function getFiles(): array 'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md', 'template' => 'dart/docs/example.md.twig', ], + [ + 'scope' => 'service', + 'destination' => '/test/services/{{service.name | caseDash}}_test.dart', + 'template' => 'dart/test/services/service_test.dart.twig', + ], + [ + 'scope' => 'definition', + 'destination' => '/test/src/models/{{definition.name | caseSnake }}_test.dart', + 'template' => 'dart/test/src/models/model_test.dart.twig', + ], [ 'scope' => 'default', 'destination' => '/test/id_test.dart', diff --git a/src/SDK/Language/Flutter.php b/src/SDK/Language/Flutter.php index 6dc3add15..ce563fce6 100644 --- a/src/SDK/Language/Flutter.php +++ b/src/SDK/Language/Flutter.php @@ -243,7 +243,7 @@ public function getFiles(): array [ 'scope' => 'service', 'destination' => '/test/services/{{service.name | caseDash}}_test.dart', - 'template' => 'flutter/test/services/service_test.dart.twig', + 'template' => 'dart/test/services/service_test.dart.twig', ], [ 'scope' => 'definition', diff --git a/templates/dart/pubspec.yaml.twig b/templates/dart/pubspec.yaml.twig index 50968e292..ea1fcadbb 100644 --- a/templates/dart/pubspec.yaml.twig +++ b/templates/dart/pubspec.yaml.twig @@ -13,3 +13,4 @@ dependencies: dev_dependencies: lints: ^2.0.1 test: ^1.22.0 + mockito: ^5.4.2 diff --git a/templates/dart/test/id_test.dart.twig b/templates/dart/test/id_test.dart.twig index 8a5434ab0..d86449f8f 100644 --- a/templates/dart/test/id_test.dart.twig +++ b/templates/dart/test/id_test.dart.twig @@ -1,5 +1,9 @@ import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart'; +{% if 'dart' in language.params.packageName %} +import 'package:test/test.dart'; +{% else %} import 'package:flutter_test/flutter_test.dart'; +{% endif %} void main() { group('unique()', () { diff --git a/templates/dart/test/permission_test.dart.twig b/templates/dart/test/permission_test.dart.twig index 4e784b879..c8fa58e00 100644 --- a/templates/dart/test/permission_test.dart.twig +++ b/templates/dart/test/permission_test.dart.twig @@ -1,5 +1,9 @@ import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart'; +{% if 'dart' in language.params.packageName %} +import 'package:test/test.dart'; +{% else %} import 'package:flutter_test/flutter_test.dart'; +{% endif %} void main() { group('read()', () { diff --git a/templates/dart/test/query_test.dart.twig b/templates/dart/test/query_test.dart.twig index f74be35bc..9ed2f12e4 100644 --- a/templates/dart/test/query_test.dart.twig +++ b/templates/dart/test/query_test.dart.twig @@ -1,5 +1,9 @@ import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart'; +{% if 'dart' in language.params.packageName %} +import 'package:test/test.dart'; +{% else %} import 'package:flutter_test/flutter_test.dart'; +{% endif %} class BasicFilterQueryTest { final String description; diff --git a/templates/dart/test/role_test.dart.twig b/templates/dart/test/role_test.dart.twig index 302d5e7d0..1c06086b7 100644 --- a/templates/dart/test/role_test.dart.twig +++ b/templates/dart/test/role_test.dart.twig @@ -1,5 +1,9 @@ import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart'; +{% if 'dart' in language.params.packageName %} +import 'package:test/test.dart'; +{% else %} import 'package:flutter_test/flutter_test.dart'; +{% endif %} void main() { group('any()', () { diff --git a/templates/flutter/test/services/service_test.dart.twig b/templates/dart/test/services/service_test.dart.twig similarity index 80% rename from templates/flutter/test/services/service_test.dart.twig rename to templates/dart/test/services/service_test.dart.twig index 212bf833d..b5997121a 100644 --- a/templates/flutter/test/services/service_test.dart.twig +++ b/templates/dart/test/services/service_test.dart.twig @@ -1,5 +1,9 @@ {% import 'flutter/base/utils.twig' as utils %} +{% if 'dart' in language.params.packageName %} +import 'package:test/test.dart'; +{% else %} import 'package:flutter_test/flutter_test.dart'; +{% endif %} import 'package:mockito/mockito.dart'; import 'package:{{ language.params.packageName }}/models.dart' as models; import 'package:{{ language.params.packageName }}/src/enums.dart'; @@ -65,7 +69,7 @@ void main() { {%~ if method.responseModel and method.responseModel != 'any' ~%} final Map data = { {%- for definition in spec.definitions ~%}{%~ if definition.name == method.responseModel -%}{%~ for property in definition.properties | filter((param) => param.required) ~%} - '{{property.name | escapeKeyword | escapeDollarSign}}': {% if property.type == 'object' %}{}{% elseif property.type == 'array' %}[]{% elseif property.type == 'string' %}'{{property.example}}'{% elseif property.type == 'boolean' %}true{% else %}{{property.example}}{% endif %},{%~ endfor ~%}{% set break = true %}{%- else -%}{% set continue = true %}{%- endif -%}{%~ endfor -%} + '{{property.name | escapeDollarSign}}': {% if property.type == 'object' %}{}{% elseif property.type == 'array' %}[]{% elseif property.type == 'string' %}'{{property.example | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property.example}}{% endif %},{%~ endfor ~%}{% set break = true %}{%- else -%}{% set continue = true %}{%- endif -%}{%~ endfor -%} }; {%~ else ~%} @@ -92,7 +96,7 @@ void main() { {%~ endif ~%} final response = await {{service.name | caseCamel}}.{{method.name | caseCamel}}({%~ for parameter in method.parameters.all | filter((param) => param.required) ~%} - {{parameter.name | escapeKeyword | caseCamel}}: {% if parameter.type == 'object' %}{}{% elseif parameter.type == 'file' %}InputFile.fromPath(path: './image.png'){% else %}'{{parameter.example}}'{%~ endif ~%},{%~ endfor ~%} + {{parameter.name | escapeKeyword | caseCamel}}: {% if parameter.type == 'object' %}{}{% elseif parameter.type == 'array' %}[]{% elseif parameter.type == 'file' %}InputFile.fromPath(path: './image.png'){% elseif parameter.type == 'boolean' %}true{% elseif parameter.type == 'string' %}'{% if parameter.example is not empty %}{{parameter.example | escapeDollarSign}}{% endif %}'{% elseif parameter.type == 'integer' and parameter['x-example'] is empty %}1{% elseif parameter.type == 'number' and parameter['x-example'] is empty %}1.0{% else %}{{parameter.example}}{%~ endif ~%},{%~ endfor ~%} ); {%- if method.type == 'location' ~%} diff --git a/templates/dart/test/src/enums_test.dart.twig b/templates/dart/test/src/enums_test.dart.twig index 91d1dd6b3..de09cd6e8 100644 --- a/templates/dart/test/src/enums_test.dart.twig +++ b/templates/dart/test/src/enums_test.dart.twig @@ -1,5 +1,9 @@ import 'package:{{ language.params.packageName }}/src/enums.dart'; +{% if 'dart' in language.params.packageName %} +import 'package:test/test.dart'; +{% else %} import 'package:flutter_test/flutter_test.dart'; +{% endif %} void main() { group('name()', () { diff --git a/templates/dart/test/src/models/model_test.dart.twig b/templates/dart/test/src/models/model_test.dart.twig index 485d26a7f..ef1dad283 100644 --- a/templates/dart/test/src/models/model_test.dart.twig +++ b/templates/dart/test/src/models/model_test.dart.twig @@ -1,6 +1,10 @@ {% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier }}{% endif %}{% else %}{% if property.type == 'object' and property.additionalProperties %}Map{% else %}{{property | typeName}}{% endif %}{% endif %}{% endmacro %} import 'package:{{ language.params.packageName }}/models.dart'; +{% if 'dart' in language.params.packageName %} +import 'package:test/test.dart'; +{% else %} import 'package:flutter_test/flutter_test.dart'; +{% endif %} void main() { group('{{ definition.name | caseUcfirst | overrideIdentifier }}', () { diff --git a/templates/dart/test/src/response_test.dart.twig b/templates/dart/test/src/response_test.dart.twig index fb7ebfef4..d217b4ac0 100644 --- a/templates/dart/test/src/response_test.dart.twig +++ b/templates/dart/test/src/response_test.dart.twig @@ -1,5 +1,9 @@ import 'package:{{ language.params.packageName }}/src/response.dart'; +{% if 'dart' in language.params.packageName %} +import 'package:test/test.dart'; +{% else %} import 'package:flutter_test/flutter_test.dart'; +{% endif %} void main() { group('toString()', () {