Skip to content

Commit

Permalink
feat: added alice_test, updated alice_dio tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhomlala committed Jul 7, 2024
1 parent e98bb48 commit bc3977d
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 156 deletions.
3 changes: 2 additions & 1 deletion packages/alice_dio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ dev_dependencies:
flutter_lints: ^4.0.0
test: ^1.25.2
mocktail: ^1.0.4
http_mock_adapter: ^0.6.1
http_mock_adapter: ^0.6.1
alice_test: ^1.0.0
157 changes: 2 additions & 155 deletions packages/alice_dio/test/alice_dio_adapter_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:alice/core/alice_core.dart';
import 'package:alice/model/alice_http_call.dart';
import 'package:alice/model/alice_http_request.dart';
import 'package:alice/model/alice_http_response.dart';
import 'package:alice_dio/alice_dio_adapter.dart';
import 'package:alice_test/alice_test.dart';
import 'package:dio/dio.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart' as http_mock_adapter;
import 'package:mocktail/mocktail.dart';
Expand Down Expand Up @@ -80,164 +80,11 @@ void main() {
size: 16,
checkTime: true,
body: '{"result": "ok"}',
headers: {
'content-type': '[application/json]'
},
headers: {'content-type': '[application/json]'},
);

verify(
() => aliceCore.addResponse(any(that: nextResponseMatcher), any()));
});
});
}

TypeMatcher<AliceHttpCall> buildCallMatcher({
bool? checkId,
bool? checkTime,
bool? secured,
bool? loading,
String? client,
String? method,
String? endpoint,
String? server,
String? uri,
int? duration,
TypeMatcher<AliceHttpRequest>? request,
TypeMatcher<AliceHttpResponse>? response,
}) {
var matcher = const TypeMatcher<AliceHttpCall>();
if (checkId == true) {
matcher = matcher.having((call) => call.id, "id", greaterThan(0));
}
if (checkTime == true) {
matcher = matcher.having((call) => call.createdTime.millisecondsSinceEpoch,
"createdTime", greaterThan(0));
}
if (secured != null) {
matcher = matcher.having((call) => call.secure, "secure", equals(secured));
}
if (loading != null) {
matcher =
matcher.having((call) => call.loading, "loading", equals(loading));
}
if (client != null) {
matcher = matcher.having((call) => call.client, "client", equals(client));
}
if (method != null) {
matcher = matcher.having((call) => call.method, "method", equals(method));
}
if (endpoint != null) {
matcher =
matcher.having((call) => call.endpoint, "endpoint", equals(endpoint));
}
if (server != null) {
matcher = matcher.having((call) => call.server, "server", equals(server));
}
if (uri != null) {
matcher = matcher.having((call) => call.uri, "uri", equals(uri));
}
if (duration != null) {
matcher =
matcher.having((call) => call.duration, "duration", equals(duration));
}
if (request != null) {
matcher =
matcher.having((call) => call.request, "request", equals(request));
}
if (response != null) {
matcher =
matcher.having((call) => call.response, "response", equals(response));
}
return matcher;
}

TypeMatcher<AliceHttpRequest> buildRequestMatcher({
bool? checkTime,
Map<String, String>? headers,
String? contentType,
Map<String, dynamic>? queryParameters,
}) {
var matcher = const TypeMatcher<AliceHttpRequest>();
if (checkTime == true) {
matcher = matcher.having((request) => request.time.millisecondsSinceEpoch,
"time", greaterThan(0));
}
if (headers != null) {
for (var header in headers.entries) {
matcher = matcher.having((request) {
return request.headers;
}, "header", ContainsHeader(header));
}
}
if (contentType != null) {
matcher = matcher.having(
(request) => request.contentType, "contentType", equals(contentType));
}
if (queryParameters != null) {
matcher = matcher.having((request) => request.queryParameters,
"queryParameters", equals(queryParameters));
}

return matcher;
}

TypeMatcher<AliceHttpResponse> buildResponseMatcher({
int? status,
int? size,
bool? checkTime,
String? body,
Map<String, String>? headers,
}) {
var matcher = const TypeMatcher<AliceHttpResponse>();
if (status != null) {
matcher =
matcher.having((response) => response.status, "status", equals(status));
}
if (size != null) {
matcher = matcher.having((response) => response.size, "size", equals(size));
}
if (checkTime == true) {
matcher = matcher.having((response) => response.time.millisecondsSinceEpoch,
"time", greaterThan(0));
}
if (body != null) {
matcher = matcher.having(
(response) => response.body.toString(), "body", equals(body));
}
if (headers != null) {
for (var header in headers.entries) {
matcher = matcher.having((response) {
return response.headers;
}, "header", ContainsHeader(header));
}
}
return matcher;
}

class ContainsHeader extends Matcher {
final MapEntry<String, String>? _expected;

const ContainsHeader(this._expected);

@override
bool matches(Object? item, Map matchState) {
if (item is Map<String, String>) {
final mapItem = item[_expected?.key];
return mapItem == _expected?.value;
}
return false;
}

@override
Description describe(Description description) =>
description.add('contains header').addDescriptionOf(_expected);

@override
Description describeMismatch(Object? item, Description mismatchDescription,
Map matchState, bool verbose) {
mismatchDescription
.add('does not contain header')
.addDescriptionOf(_expected);
return mismatchDescription;
}
}
29 changes: 29 additions & 0 deletions packages/alice_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
build/
10 changes: 10 additions & 0 deletions packages/alice_test/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "a14f74ff3a1cbd521163c5f03d68113d50af93d3"
channel: "stable"

project_type: package
Loading

0 comments on commit bc3977d

Please sign in to comment.