2022-11-01 08:45:57 +01:00
|
|
|
// SPDX-FileCopyrightText: 2022 Iconica
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2022-10-07 12:16:17 +02:00
|
|
|
import 'package:flutter_data_interface/flutter_data_interface.dart';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
2023-04-01 19:36:44 +02:00
|
|
|
import 'package:mocktail/mocktail.dart';
|
2022-10-07 12:16:17 +02:00
|
|
|
|
|
|
|
class SamplePluginPlatform extends DataInterface {
|
|
|
|
SamplePluginPlatform() : super(token: _token);
|
|
|
|
|
|
|
|
static final Object _token = Object();
|
|
|
|
|
|
|
|
// ignore: avoid_setters_without_getters
|
|
|
|
static set instance(SamplePluginPlatform instance) {
|
|
|
|
DataInterface.verify(instance, _token);
|
|
|
|
// A real implementation would set a static instance field here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 19:36:44 +02:00
|
|
|
class MockSamplePluginPlatform extends Mock implements SamplePluginPlatform {}
|
2022-10-07 12:16:17 +02:00
|
|
|
|
2023-04-01 19:36:44 +02:00
|
|
|
class MockSamplePluginPlatformUsingNoSuchMethod
|
2022-10-07 12:16:17 +02:00
|
|
|
implements SamplePluginPlatform {
|
|
|
|
@override
|
|
|
|
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
|
|
|
}
|
|
|
|
|
2023-04-01 19:36:44 +02:00
|
|
|
class MockSamplePluginPlatformUsingMockPlatformInterfaceMixin extends Mock
|
2022-10-07 12:16:17 +02:00
|
|
|
with MockDataInterfaceMixin
|
|
|
|
implements SamplePluginPlatform {}
|
|
|
|
|
2023-04-01 19:36:44 +02:00
|
|
|
class MockSamplePluginPlatformUsingFakePlatformInterfaceMixin extends Fake
|
2022-10-07 12:16:17 +02:00
|
|
|
with MockDataInterfaceMixin
|
|
|
|
implements SamplePluginPlatform {}
|
|
|
|
|
|
|
|
class ExtendsSamplePluginPlatform extends SamplePluginPlatform {}
|
|
|
|
|
|
|
|
class ConstTokenPluginPlatform extends DataInterface {
|
|
|
|
ConstTokenPluginPlatform() : super(token: _token);
|
|
|
|
|
|
|
|
static const Object _token = Object(); // invalid
|
|
|
|
|
|
|
|
// ignore: avoid_setters_without_getters
|
|
|
|
static set instance(ConstTokenPluginPlatform instance) {
|
|
|
|
DataInterface.verify(instance, _token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ExtendsConstTokenPluginPlatform extends ConstTokenPluginPlatform {}
|
|
|
|
|
|
|
|
class VerifyTokenPluginPlatform extends DataInterface {
|
|
|
|
VerifyTokenPluginPlatform() : super(token: _token);
|
|
|
|
|
|
|
|
static final Object _token = Object();
|
|
|
|
|
|
|
|
// ignore: avoid_setters_without_getters
|
|
|
|
static set instance(VerifyTokenPluginPlatform instance) {
|
|
|
|
DataInterface.verifyToken(instance, _token);
|
|
|
|
// A real implementation would set a static instance field here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 19:36:44 +02:00
|
|
|
class MockVerifyTokenPluginPlatform extends Mock
|
2022-10-07 12:16:17 +02:00
|
|
|
implements VerifyTokenPluginPlatform {}
|
|
|
|
|
2023-04-01 19:36:44 +02:00
|
|
|
class MockVerifyTokenPluginPlatformUsingMockPlatformInterfaceMixin extends Mock
|
2022-10-07 12:16:17 +02:00
|
|
|
with MockDataInterfaceMixin
|
|
|
|
implements VerifyTokenPluginPlatform {}
|
|
|
|
|
|
|
|
class ExtendsVerifyTokenPluginPlatform extends VerifyTokenPluginPlatform {}
|
|
|
|
|
|
|
|
class ConstVerifyTokenPluginPlatform extends DataInterface {
|
|
|
|
ConstVerifyTokenPluginPlatform() : super(token: _token);
|
|
|
|
|
|
|
|
static const Object _token = Object(); // invalid
|
|
|
|
|
|
|
|
// ignore: avoid_setters_without_getters
|
|
|
|
static set instance(ConstVerifyTokenPluginPlatform instance) {
|
|
|
|
DataInterface.verifyToken(instance, _token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 19:36:44 +02:00
|
|
|
class MockConstVerifyTokenPluginPlatform extends DataInterface
|
2022-10-07 12:16:17 +02:00
|
|
|
implements ConstVerifyTokenPluginPlatform {
|
2023-04-01 19:36:44 +02:00
|
|
|
MockConstVerifyTokenPluginPlatform() : super(token: const Object());
|
2022-10-07 12:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures that `PlatformInterface` has no instance methods. Adding an
|
|
|
|
// instance method is discouraged and may be a breaking change if it
|
|
|
|
// conflicts with instance methods in subclasses.
|
|
|
|
class StaticMethodsOnlyPlatformInterfaceTest implements DataInterface {}
|
|
|
|
|
|
|
|
class StaticMethodsOnlyMockPlatformInterfaceMixinTest
|
|
|
|
implements MockDataInterfaceMixin {}
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
group('`verify`', () {
|
|
|
|
test('prevents implementation with `implements`', () {
|
|
|
|
expect(() {
|
2023-04-01 19:36:44 +02:00
|
|
|
SamplePluginPlatform.instance = MockSamplePluginPlatform();
|
2022-10-07 12:16:17 +02:00
|
|
|
}, throwsA(isA<AssertionError>()));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('prevents implmentation with `implements` and `noSuchMethod`', () {
|
|
|
|
expect(() {
|
|
|
|
SamplePluginPlatform.instance =
|
2023-04-01 19:36:44 +02:00
|
|
|
MockSamplePluginPlatformUsingNoSuchMethod();
|
2022-10-07 12:16:17 +02:00
|
|
|
}, throwsA(isA<AssertionError>()));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allows mocking with `implements`', () {
|
|
|
|
final SamplePluginPlatform mock =
|
2023-04-01 19:36:44 +02:00
|
|
|
MockSamplePluginPlatformUsingMockPlatformInterfaceMixin();
|
2022-10-07 12:16:17 +02:00
|
|
|
SamplePluginPlatform.instance = mock;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allows faking with `implements`', () {
|
|
|
|
final SamplePluginPlatform fake =
|
2023-04-01 19:36:44 +02:00
|
|
|
MockSamplePluginPlatformUsingFakePlatformInterfaceMixin();
|
2022-10-07 12:16:17 +02:00
|
|
|
SamplePluginPlatform.instance = fake;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allows extending', () {
|
|
|
|
SamplePluginPlatform.instance = ExtendsSamplePluginPlatform();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('prevents `const Object()` token', () {
|
|
|
|
expect(() {
|
|
|
|
ConstTokenPluginPlatform.instance = ExtendsConstTokenPluginPlatform();
|
|
|
|
}, throwsA(isA<AssertionError>()));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Tests of the earlier, to-be-deprecated `verifyToken` method
|
|
|
|
group('`verifyToken`', () {
|
|
|
|
test('prevents implementation with `implements`', () {
|
|
|
|
expect(() {
|
2023-04-01 19:36:44 +02:00
|
|
|
VerifyTokenPluginPlatform.instance = MockVerifyTokenPluginPlatform();
|
2022-10-07 12:16:17 +02:00
|
|
|
}, throwsA(isA<AssertionError>()));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allows mocking with `implements`', () {
|
|
|
|
final VerifyTokenPluginPlatform mock =
|
2023-04-01 19:36:44 +02:00
|
|
|
MockVerifyTokenPluginPlatformUsingMockPlatformInterfaceMixin();
|
2022-10-07 12:16:17 +02:00
|
|
|
VerifyTokenPluginPlatform.instance = mock;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('allows extending', () {
|
|
|
|
VerifyTokenPluginPlatform.instance = ExtendsVerifyTokenPluginPlatform();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('does not prevent `const Object()` token', () {
|
|
|
|
ConstVerifyTokenPluginPlatform.instance =
|
2023-04-01 19:36:44 +02:00
|
|
|
MockConstVerifyTokenPluginPlatform();
|
2022-10-07 12:16:17 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|