Skip to content

Commit

Permalink
refactor: Simplify log entry addition in SpinifyLogBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Jul 17, 2024
1 parent bb280b7 commit 9375157
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/src/model/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ extension type const SpinifyLogLevel._(int level) implements int {

/// If log level is warning or higher
bool get isError => level > 3;

/// Values of log level
static const List<SpinifyLogLevel> values = <SpinifyLogLevel>[
SpinifyLogLevel.debug(),
SpinifyLogLevel.transport(),
SpinifyLogLevel.config(),
SpinifyLogLevel.info(),
SpinifyLogLevel.warning(),
SpinifyLogLevel.error(),
SpinifyLogLevel.critical(),
];
}

/// Logger function to use for logging.
Expand Down
43 changes: 43 additions & 0 deletions test/unit/spinify_logs_test.dart → test/unit/logs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,43 @@ void main() {
context = const <String, Object?>{}}) =>
buffer.add(level, event, message, context);

test('LogLevel', () async {
for (final v in SpinifyLogLevel.values) {
expect(v, isNotNull);
expect(v.isError, v.level >= const SpinifyLogLevel.warning().level);
{
final level = v.map(
debug: () => 0,
transport: () => 1,
config: () => 2,
info: () => 3,
warning: () => 4,
error: () => 5,
critical: () => 6,
);
expect(level, equals(v.level));
}
{
final isError = v.maybeMap(
orElse: () => false,
warning: () => true,
error: () => true,
critical: () => true,
);
expect(isError, equals(v.isError));
}
{
final isError = v.mapOrNull(
warning: () => true,
error: () => true,
critical: () => true,
) ??
false;
expect(isError, equals(v.isError));
}
}
});

test('LogBuffer', () async {
final buffer = SpinifyLogBuffer(size: 10);
expect(buffer.logs, isEmpty);
Expand All @@ -40,6 +77,12 @@ void main() {
expect(buffer.logs, hasLength(buffer.size));
expect(buffer.isFull, isTrue);
expect(buffer.isEmpty, isFalse);
for (var i = 0; i < buffer.size * 2; i++) {
addFake();
}
expect(buffer.logs, hasLength(buffer.size));
expect(buffer.isFull, isTrue);
expect(buffer.isEmpty, isFalse);
});
});
}
2 changes: 2 additions & 0 deletions test/unit_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'package:test/test.dart';

import 'unit/logs_test.dart' as logs_test;
import 'unit/server_subscription_test.dart' as server_subscription_test;
import 'unit/spinify_test.dart' as spinify_test;

void main() {
group('Unit', () {
logs_test.main();
spinify_test.main();
server_subscription_test.main();
});
Expand Down

0 comments on commit 9375157

Please sign in to comment.