From bc3632ab73f57719e4fccf65b47441cab6d9f3a3 Mon Sep 17 00:00:00 2001 From: Luis Burgos Date: Sat, 9 Jul 2022 19:04:10 -0500 Subject: [PATCH] feat: add green test suite --- lib/buzz_impl.dart | 22 +++++++----- lib/event_bus_holder_impl.dart | 10 +++--- lib/infra/event_bus_holder.dart | 2 +- lib/infra/typed_event_bus.dart | 4 +-- lib/utils/subtype_checker.dart | 28 ++-------------- test/buzz_test.dart | 59 +++++++++++++++------------------ test/event_bus_holder_test.dart | 4 +-- test/helpers/test_events.dart | 46 +++++++++++++++++++++++++ test/subtype_checker_test.dart | 55 ------------------------------ 9 files changed, 99 insertions(+), 131 deletions(-) diff --git a/lib/buzz_impl.dart b/lib/buzz_impl.dart index 5309544..564ae44 100644 --- a/lib/buzz_impl.dart +++ b/lib/buzz_impl.dart @@ -2,7 +2,6 @@ import 'package:buzz/buzz.dart'; import 'package:buzz/feedbacks.dart'; import 'infra/registries.dart'; -import 'utils.dart'; IBuzzBase? _buzz; @@ -32,7 +31,7 @@ abstract class IBuzzBase { List? moduleRegistries, }); - void fire(X event); + void fire(dynamic message); void destroy(); } @@ -71,12 +70,15 @@ class BuzzBase implements IBuzzBase { } @override - void fire(X event) { - print('fire $event'); - try { - EventBusHolder.ofType().fire(event); - } catch (e) { - buzzLog('$runtimeType $e'); + void fire(dynamic message) { + if (message is UiEvent) { + uiEvents.fire(message); + } else if (message is AppEvent) { + appEvents.fire(message); + } else if (message is Command) { + commands.fire(message); + } else { + throw UnsupportedBuzzMessage(message); } } @@ -102,3 +104,7 @@ class BuzzBase implements IBuzzBase { }); } } + +class UnsupportedBuzzMessage extends BuzzError { + UnsupportedBuzzMessage(dynamic message) : super('$message is not supported'); +} diff --git a/lib/event_bus_holder_impl.dart b/lib/event_bus_holder_impl.dart index bbdee79..04bd99e 100644 --- a/lib/event_bus_holder_impl.dart +++ b/lib/event_bus_holder_impl.dart @@ -14,14 +14,14 @@ class EventBusHolderImpl implements IEventBusHolder { } @override - TypedEventBus ofType() { + TypedEventBus forType() { TypedEventBus? eventBus; - for (var value in _allStreamBuses.values) { - print('$value - $T - ${T.runtimeType}'); - bool isSupported = value.isTypeSupported(); + for (var bus in _allStreamBuses.values) { + print('\n$bus - ${T}'); + bool isSupported = bus.isTypeSupported(); if (isSupported) { - eventBus = value; + eventBus = bus; } if (eventBus != null) { diff --git a/lib/infra/event_bus_holder.dart b/lib/infra/event_bus_holder.dart index 48af50a..683190b 100644 --- a/lib/infra/event_bus_holder.dart +++ b/lib/infra/event_bus_holder.dart @@ -23,7 +23,7 @@ abstract class IEventBusHolder { X of(); void addEventBus(TypedEventBus eventBus); void destroy(); - TypedEventBus ofType(); + TypedEventBus forType(); } class BusNotFound extends BuzzError { diff --git a/lib/infra/typed_event_bus.dart b/lib/infra/typed_event_bus.dart index 626f6c8..a33f8d6 100644 --- a/lib/infra/typed_event_bus.dart +++ b/lib/infra/typed_event_bus.dart @@ -6,7 +6,7 @@ import '../utils.dart'; abstract class SupportedTyped {} -abstract class TypedEventBus { +abstract class TypedEventBus { final _eventBus = EventBus(); bool isTypeSupported() { @@ -17,7 +17,7 @@ abstract class TypedEventBus { dynamic get supportedType => T; - void fire(X event) { + void fire(T event) { buzzLog('$runtimeType - $event'); _eventBus.fire(event); } diff --git a/lib/utils/subtype_checker.dart b/lib/utils/subtype_checker.dart index 3e1deda..3fa4af8 100644 --- a/lib/utils/subtype_checker.dart +++ b/lib/utils/subtype_checker.dart @@ -1,14 +1,9 @@ class SubtypeChecker { - bool isValidSubtype(dynamic type) { - return isSubtype2(type); + bool isValid() { + return isSubtype1(); } - bool isValid({dynamic type = null}) { - return isSubtype1(type); - //return isSubtype1(type); - } - - bool isSubtype1(dynamic type) { + bool isSubtype1() { final isSASubtypeOfT = [] is List; print('isSubtype1 - is $S ASubtypeOf $T: $isSASubtypeOfT'); @@ -17,21 +12,4 @@ class SubtypeChecker { return isSASubtypeOfT && !isTASubtypeOfS; } - - bool isSubtype2(dynamic type) { - var isSubtype = false; - try { - isSubtype = isSubtype3(type); - } catch (e) { - print(e); - } - print('isSubtype2 - isSubtype: $isSubtype'); - return isSubtype; - } - - bool isSubtype3(dynamic type) { - final listFromS = List.from([type]); - print('isSubtype3 - listFromS: $listFromS'); - return listFromS is List; - } } diff --git a/test/buzz_test.dart b/test/buzz_test.dart index be8ebb0..342d9cc 100644 --- a/test/buzz_test.dart +++ b/test/buzz_test.dart @@ -1,40 +1,33 @@ -import 'package:buzz/buzz.dart'; -import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'helpers/test_events.dart'; -class MockNavigator extends Mock implements Navigator {} - void main() { - test('Buzz emits AppEvents and UiEvents', () { - //Given - Buzz.init( - navigator: MockNavigator(), - ); - - //Then - expectLater( - Buzz.appEvents.on(), - emitsInAnyOrder([ - isA(), - isA(), - ]), - ); - - expectLater( - Buzz.uiEvents.on(), - emitsInAnyOrder([ - isA(), - isA(), - ]), - ); + buzzTest( + 'Buzz emits only one AppEvent', + expectAppEvents: () => [ + isA(), + ], + fire: () => [ + AppEvent1(), + ], + ); - //When - Buzz - ..fire(AppEvent1()) - ..fire(UiEvent1()) - ..fire(AppEvent2()) - ..fire(UiEvent2()); - }); + buzzTest( + 'Buzz alternately emits two AppEvents and two UiEvents', + expectAppEvents: () => [ + isA(), + isA(), + ], + expectUiEvents: () => [ + isA(), + isA(), + ], + fire: () => [ + UiEvent1(), + AppEvent1(), + UiEvent2(), + AppEvent2(), + ], + ); } diff --git a/test/event_bus_holder_test.dart b/test/event_bus_holder_test.dart index 29fb5a9..8e87f14 100644 --- a/test/event_bus_holder_test.dart +++ b/test/event_bus_holder_test.dart @@ -21,7 +21,7 @@ void main() { test('', () { final eventBusHolder = EventBusHolderImpl(); expect( - () => eventBusHolder.ofType(), + () => eventBusHolder.forType(), throwsA(isA()), ); }); @@ -30,7 +30,7 @@ void main() { final eventBusHolder = EventBusHolderImpl()..addEventBus(AppEventBus()); expect( - eventBusHolder.ofType(), + eventBusHolder.forType(), isA(), ); }); diff --git a/test/helpers/test_events.dart b/test/helpers/test_events.dart index 06d8622..78a8ae2 100644 --- a/test/helpers/test_events.dart +++ b/test/helpers/test_events.dart @@ -1,7 +1,9 @@ +import 'package:buzz/buzz.dart'; import 'package:buzz/infra/typed_event_handler.dart'; import 'package:buzz/kinds/app_events.dart'; import 'package:buzz/kinds/ui_events.dart'; import 'package:mockito/mockito.dart'; +import 'package:test/test.dart'; class BaseAppEvent extends AppEvent {} @@ -24,3 +26,47 @@ class UiEventHandler extends Mock implements TypedEventHandler { print(event.toString()); } } + +class MockNavigator extends Mock implements Navigator {} + +void buzzTest( + String message, { + List Function()? expectUiEvents, + List Function()? expectAppEvents, + List Function()? expectCommands, + required List Function() fire, +}) { + test(message, () { + /// TODO: Allow customization. + /// Default + Buzz.init( + navigator: MockNavigator(), + ); + + if (expectUiEvents != null) { + expectLater( + Buzz.uiEvents.on(), + emitsInOrder(expectUiEvents()), + ); + } + + if (expectAppEvents != null) { + expectLater( + Buzz.appEvents.on(), + emitsInOrder(expectAppEvents()), + ); + } + + if (expectCommands != null) { + expectLater( + Buzz.commands.on(), + emitsInOrder(expectCommands()), + ); + } + + //When + fire().forEach((element) { + Buzz.fire(element); + }); + }); +} diff --git a/test/subtype_checker_test.dart b/test/subtype_checker_test.dart index 6d5a11f..002c6a1 100644 --- a/test/subtype_checker_test.dart +++ b/test/subtype_checker_test.dart @@ -44,59 +44,4 @@ void main() { expect(subChildChecker.isValid(), false); }); }); - group('isSubtype', () { - test('Child isSubtype of Parent', () { - final checker = SubtypeChecker(); - expect(checker.isValidSubtype(ChildA), true); - }); - test('SubChild isSubtype of Child', () { - final checker = SubtypeChecker(); - expect(checker.isValidSubtype(SubChildA), true); - }); - test('SubChild isSubtype of Parent', () { - final checker = SubtypeChecker(); - expect(checker.isValidSubtype(SubChildA), true); - }); - test('Parent isNotSubtype of Child', () { - final checker = SubtypeChecker(); - expect(checker.isValidSubtype(ParentA), false); - }); - test('NonRelatedClass isNotSubtype of Parent, nor Child, nor SubChild', () { - final parentChecker = SubtypeChecker(); - expect(parentChecker.isValidSubtype(NonRelatedClass), false); - - final childChecker = SubtypeChecker(); - expect(childChecker.isValidSubtype(NonRelatedClass), false); - - final subChildChecker = SubtypeChecker(); - expect(subChildChecker.isValidSubtype(NonRelatedClass), false); - }); - }); - - group('isSubtype3', () { - test('', () { - final nonRelatedClass = NonRelatedClass(); - - expect( - isSubtype3(Child()), - true, - ); - expect( - isSubtype3(SubChild()), - true, - ); - expect( - isSubtype3(nonRelatedClass), - false, - ); - }); - }); -} - -bool isSubtype3(dynamic type) { - return type is T; - - final listFromS = List.from([type]).cast(); - print('isSubtype3 - listFromS: $listFromS'); - return listFromS is List; }