Skip to content

Commit

Permalink
Merge pull request #5 from ziqq/feat/test
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
PlugFox authored Sep 25, 2024
2 parents e2edf23 + 82fd88b commit 3611123
Show file tree
Hide file tree
Showing 7 changed files with 719 additions and 182 deletions.
6 changes: 3 additions & 3 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ abstract base class Controller with ChangeNotifier implements IController {
ControllerRegistry().insert<Controller>(this);
runZonedGuarded<void>(
() => Controller.observer?.onCreate(this),
(error, stackTrace) {/* ignore */},
(error, stackTrace) {/* ignore */}, // coverage:ignore-line
);
}

Expand All @@ -96,7 +96,7 @@ abstract base class Controller with ChangeNotifier implements IController {
@protected
void onError(Object error, StackTrace stackTrace) => runZonedGuarded<void>(
() => Controller.observer?.onError(this, error, stackTrace),
(error, stackTrace) {/* ignore */},
(error, stackTrace) {/* ignore */}, // coverage:ignore-line
);

@protected
Expand Down Expand Up @@ -144,7 +144,7 @@ abstract base class Controller with ChangeNotifier implements IController {
_$subscribers = 0;
runZonedGuarded<void>(
() => Controller.observer?.onDispose(this),
(error, stackTrace) {/* ignore */},
(error, stackTrace) {/* ignore */}, // coverage:ignore-line
);
ControllerRegistry().remove<Controller>();
super.dispose();
Expand Down
46 changes: 29 additions & 17 deletions lib/src/controller_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ class ControllerScope<C extends Listenable> extends InheritedWidget {
C controller, {
Widget? child,
super.key,
}) : _dependency = _ControllerDependency$Value<C>(
controller: controller,
),
}) : _dependency = _ControllerDependency$Value<C>(controller: controller),
super(child: child ?? const SizedBox.shrink());

final _ControllerDependency<C> _dependency;

/// The state from the closest instance of this class
/// that encloses the given context, if any.
/// e.g. `ControllerScope.maybeOf<MyStateController>(context)`.
static C? maybeOf<C extends Listenable>(BuildContext context,
{bool listen = false}) {
static C? maybeOf<C extends Listenable>(
BuildContext context, {
bool listen = false,
}) {
final element =
context.getElementForInheritedWidgetOfExactType<ControllerScope<C>>();
if (listen && element != null) context.dependOnInheritedElement(element);
Expand All @@ -63,8 +63,10 @@ class ControllerScope<C extends Listenable> extends InheritedWidget {
/// The state from the closest instance of this class
/// that encloses the given context.
/// e.g. `ControllerScope.of<MyStateController>(context)`
static C of<C extends Listenable>(BuildContext context,
{bool listen = false}) =>
static C of<C extends Listenable>(
BuildContext context, {
bool listen = false,
}) =>
maybeOf<C>(context, listen: listen) ??
_notFoundInheritedWidgetOfExactType();

Expand All @@ -87,8 +89,10 @@ final class ControllerScope$Element<C extends Listenable>

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) =>
super.debugFillProperties(
_debugFillPropertiesBuilder(_controller, properties));
super.debugFillProperties(_debugFillPropertiesBuilder(
_controller,
properties,
));

@nonVirtual
C? _controller;
Expand Down Expand Up @@ -224,8 +228,10 @@ sealed class _ControllerDependency<C extends Listenable> {

final class _ControllerDependency$Create<C extends Listenable>
extends _ControllerDependency<C> {
const _ControllerDependency$Create(
{required this.create, required this.lazy});
const _ControllerDependency$Create({
required this.create,
required this.lazy,
});

final C Function() create;

Expand Down Expand Up @@ -256,19 +262,23 @@ final class _ControllerDependency$Value<C extends Listenable>
}

DiagnosticPropertiesBuilder _debugFillPropertiesBuilder(
Listenable? controller, DiagnosticPropertiesBuilder properties) {
Listenable? controller,
DiagnosticPropertiesBuilder properties,
) {
if (controller == null) return properties;

switch (controller) {
case StateController<Object> sc:
properties
..add(
DiagnosticsProperty<StateController<Object>>('StateController', sc))
..add(DiagnosticsProperty<StateController<Object>>(
'StateController',
sc,
))
..add(StringProperty('State', sc.state.toString()))
..add(IntProperty('Subscribers', sc.subscribers))
..add(FlagProperty(
'isDisposed',
value: sc.isProcessing,
value: sc.isDisposed,
ifTrue: 'Disposed',
ifFalse: 'Not disposed',
))
Expand All @@ -284,7 +294,7 @@ DiagnosticPropertiesBuilder _debugFillPropertiesBuilder(
..add(IntProperty('Subscribers', c.subscribers))
..add(FlagProperty(
'isDisposed',
value: c.isProcessing,
value: c.isDisposed,
ifTrue: 'Disposed',
ifFalse: 'Not disposed',
))
Expand All @@ -297,7 +307,9 @@ DiagnosticPropertiesBuilder _debugFillPropertiesBuilder(
case ValueListenable<Object?> vl:
properties
..add(DiagnosticsProperty<ValueListenable<Object?>>.lazy(
'ValueListenable', () => vl))
'ValueListenable',
() => vl,
))
..add(StringProperty('Value', vl.value?.toString() ?? 'null'));
case ChangeNotifier cn:
properties.add(DiagnosticsProperty<ChangeNotifier>('ChangeNotifier', cn));
Expand Down
2 changes: 2 additions & 0 deletions test/control_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';

import 'unit/state_controller_test.dart' as state_controller_test;
import 'widget/controller_scope_test.dart' as state_scope_test;
import 'widget/state_consumer_test.dart' as state_consumer_test;

void main() {
group('unit', () {
Expand All @@ -12,5 +13,6 @@ void main() {

group('widget', () {
state_scope_test.main();
state_consumer_test.main();
});
}
Loading

0 comments on commit 3611123

Please sign in to comment.