Skip to content

Commit

Permalink
feat: add green test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Jul 10, 2022
1 parent cb8f12d commit bc3632a
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 131 deletions.
22 changes: 14 additions & 8 deletions lib/buzz_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:buzz/buzz.dart';
import 'package:buzz/feedbacks.dart';

import 'infra/registries.dart';
import 'utils.dart';

IBuzzBase? _buzz;

Expand Down Expand Up @@ -32,7 +31,7 @@ abstract class IBuzzBase {
List<ModuleBuzzRegistries>? moduleRegistries,
});

void fire<X extends SupportedTyped>(X event);
void fire(dynamic message);
void destroy();
}

Expand Down Expand Up @@ -71,12 +70,15 @@ class BuzzBase implements IBuzzBase {
}

@override
void fire<X extends SupportedTyped>(X event) {
print('fire $event');
try {
EventBusHolder.ofType<X>().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);
}
}

Expand All @@ -102,3 +104,7 @@ class BuzzBase implements IBuzzBase {
});
}
}

class UnsupportedBuzzMessage extends BuzzError {
UnsupportedBuzzMessage(dynamic message) : super('$message is not supported');
}
10 changes: 5 additions & 5 deletions lib/event_bus_holder_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class EventBusHolderImpl implements IEventBusHolder {
}

@override
TypedEventBus ofType<T extends SupportedTyped>() {
TypedEventBus forType<T>() {
TypedEventBus? eventBus;

for (var value in _allStreamBuses.values) {
print('$value - $T - ${T.runtimeType}');
bool isSupported = value.isTypeSupported<T>();
for (var bus in _allStreamBuses.values) {
print('\n$bus - ${T}');
bool isSupported = bus.isTypeSupported<T>();
if (isSupported) {
eventBus = value;
eventBus = bus;
}

if (eventBus != null) {
Expand Down
2 changes: 1 addition & 1 deletion lib/infra/event_bus_holder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class IEventBusHolder {
X of<X extends TypedEventBus>();
void addEventBus<X extends SupportedTyped>(TypedEventBus<X> eventBus);
void destroy();
TypedEventBus ofType<T extends SupportedTyped>();
TypedEventBus forType<T>();
}

class BusNotFound extends BuzzError {
Expand Down
4 changes: 2 additions & 2 deletions lib/infra/typed_event_bus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../utils.dart';

abstract class SupportedTyped {}

abstract class TypedEventBus<T extends SupportedTyped> {
abstract class TypedEventBus<T> {
final _eventBus = EventBus();

bool isTypeSupported<X>() {
Expand All @@ -17,7 +17,7 @@ abstract class TypedEventBus<T extends SupportedTyped> {

dynamic get supportedType => T;

void fire<X extends T>(X event) {
void fire(T event) {
buzzLog('$runtimeType - $event');
_eventBus.fire(event);
}
Expand Down
28 changes: 3 additions & 25 deletions lib/utils/subtype_checker.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
class SubtypeChecker<Child, Parent> {
bool isValidSubtype(dynamic type) {
return isSubtype2<Child, Parent>(type);
bool isValid() {
return isSubtype1<Child, Parent>();
}

bool isValid({dynamic type = null}) {
return isSubtype1<Child, Parent>(type);
//return isSubtype1<Child, Parent>(type);
}

bool isSubtype1<S, T>(dynamic type) {
bool isSubtype1<S, T>() {
final isSASubtypeOfT = <S>[] is List<T>;
print('isSubtype1 - is $S ASubtypeOf $T: $isSASubtypeOfT');

Expand All @@ -17,21 +12,4 @@ class SubtypeChecker<Child, Parent> {

return isSASubtypeOfT && !isTASubtypeOfS;
}

bool isSubtype2<S, T>(dynamic type) {
var isSubtype = false;
try {
isSubtype = isSubtype3<T>(type);
} catch (e) {
print(e);
}
print('isSubtype2 - isSubtype: $isSubtype');
return isSubtype;
}

bool isSubtype3<T>(dynamic type) {
final listFromS = List.from([type]);
print('isSubtype3 - listFromS: $listFromS');
return listFromS is List<T>;
}
}
59 changes: 26 additions & 33 deletions test/buzz_test.dart
Original file line number Diff line number Diff line change
@@ -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<AppEvent1>(),
isA<AppEvent2>(),
]),
);

expectLater(
Buzz.uiEvents.on(),
emitsInAnyOrder([
isA<UiEvent1>(),
isA<UiEvent2>(),
]),
);
buzzTest(
'Buzz emits only one AppEvent',
expectAppEvents: () => [
isA<AppEvent1>(),
],
fire: () => [
AppEvent1(),
],
);

//When
Buzz
..fire(AppEvent1())
..fire(UiEvent1())
..fire(AppEvent2())
..fire(UiEvent2());
});
buzzTest(
'Buzz alternately emits two AppEvents and two UiEvents',
expectAppEvents: () => [
isA<AppEvent1>(),
isA<AppEvent2>(),
],
expectUiEvents: () => [
isA<UiEvent1>(),
isA<UiEvent2>(),
],
fire: () => [
UiEvent1(),
AppEvent1(),
UiEvent2(),
AppEvent2(),
],
);
}
4 changes: 2 additions & 2 deletions test/event_bus_holder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
test('', () {
final eventBusHolder = EventBusHolderImpl();
expect(
() => eventBusHolder.ofType<BaseAppEvent>(),
() => eventBusHolder.forType<BaseAppEvent>(),
throwsA(isA<BusNotFound>()),
);
});
Expand All @@ -30,7 +30,7 @@ void main() {
final eventBusHolder = EventBusHolderImpl()..addEventBus(AppEventBus());

expect(
eventBusHolder.ofType<BaseAppEvent>(),
eventBusHolder.forType<BaseAppEvent>(),
isA<AppEventBus>(),
);
});
Expand Down
46 changes: 46 additions & 0 deletions test/helpers/test_events.dart
Original file line number Diff line number Diff line change
@@ -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 {}

Expand All @@ -24,3 +26,47 @@ class UiEventHandler extends Mock implements TypedEventHandler<UiEvent> {
print(event.toString());
}
}

class MockNavigator extends Mock implements Navigator {}

void buzzTest(
String message, {
List<TypeMatcher> Function()? expectUiEvents,
List<TypeMatcher> Function()? expectAppEvents,
List<TypeMatcher> Function()? expectCommands,
required List<SupportedTyped> 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);
});
});
}
55 changes: 0 additions & 55 deletions test/subtype_checker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,59 +44,4 @@ void main() {
expect(subChildChecker.isValid(), false);
});
});
group('isSubtype', () {
test('Child isSubtype of Parent', () {
final checker = SubtypeChecker<Child, Parent>();
expect(checker.isValidSubtype(ChildA), true);
});
test('SubChild isSubtype of Child', () {
final checker = SubtypeChecker<SubChild, Child>();
expect(checker.isValidSubtype(SubChildA), true);
});
test('SubChild isSubtype of Parent', () {
final checker = SubtypeChecker<SubChild, Parent>();
expect(checker.isValidSubtype(SubChildA), true);
});
test('Parent isNotSubtype of Child', () {
final checker = SubtypeChecker<Parent, Child>();
expect(checker.isValidSubtype(ParentA), false);
});
test('NonRelatedClass isNotSubtype of Parent, nor Child, nor SubChild', () {
final parentChecker = SubtypeChecker<NonRelatedClass, Parent>();
expect(parentChecker.isValidSubtype(NonRelatedClass), false);

final childChecker = SubtypeChecker<NonRelatedClass, Child>();
expect(childChecker.isValidSubtype(NonRelatedClass), false);

final subChildChecker = SubtypeChecker<NonRelatedClass, SubChild>();
expect(subChildChecker.isValidSubtype(NonRelatedClass), false);
});
});

group('isSubtype3', () {
test('', () {
final nonRelatedClass = NonRelatedClass();

expect(
isSubtype3<Parent>(Child()),
true,
);
expect(
isSubtype3<Parent>(SubChild()),
true,
);
expect(
isSubtype3<Parent>(nonRelatedClass),
false,
);
});
});
}

bool isSubtype3<T>(dynamic type) {
return type is T;

final listFromS = List.from([type]).cast();
print('isSubtype3 - listFromS: $listFromS');
return listFromS is List<T>;
}

0 comments on commit bc3632a

Please sign in to comment.