Skip to content

Commit

Permalink
#1 add test and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
susatthi committed May 1, 2022
1 parent 95c50ac commit b437672
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
9 changes: 6 additions & 3 deletions lib/src/printers/single_pretty_printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ class SinglePrettyPrinter extends LogPrinter {
line.startsWith(selfPath);
}

String _getCurrentTime() {
@visibleForTesting
String getCurrentTime({
DateTime? dateTime,
}) {
String _threeDigits(int n) {
if (n >= 100) {
return '$n';
Expand All @@ -223,7 +226,7 @@ class SinglePrettyPrinter extends LogPrinter {
return '0$n';
}

final now = DateTime.now();
final now = dateTime ?? DateTime.now();
final h = _twoDigits(now.hour);
final min = _twoDigits(now.minute);
final sec = _twoDigits(now.second);
Expand Down Expand Up @@ -287,7 +290,7 @@ class SinglePrettyPrinter extends LogPrinter {
buffer.add(levelLabels[level]!);
}
if (printTime) {
buffer.add(_getCurrentTime());
buffer.add(getCurrentTime());
}
if (printCaller) {
final caller = getCaller();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/roggle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Roggle {
_output = output ?? ConsoleOutput() {
_filter.init();
// ignore: cascade_invocations
_filter.level = level ?? Logger.level;
_filter.level = level ?? Roggle.level;
_printer.init();
_output.init();
}
Expand Down
27 changes: 27 additions & 0 deletions test/printers/single_pretty_printer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,22 @@ packages/flutter_sample_custom_logger/main.dart 5:10 main
);
});
});
group('getCurrentTime()', () {
test('getCurrentTime()', () {
_wrapCurrentTimeTest(
DateTime(2022, 1, 1, 0, 0, 0, 0),
'00:00:00.000',
);
_wrapCurrentTimeTest(
DateTime(2022, 1, 1, 10, 10, 10, 10),
'10:10:10.010',
);
_wrapCurrentTimeTest(
DateTime(2022, 1, 1, 20, 20, 20, 100),
'20:20:20.100',
);
});
});
}

String _readMessage(List<String> log) {
Expand Down Expand Up @@ -593,3 +609,14 @@ void _wrapCallerTest(
);
expect(actualCaller, expectCaller);
}

void _wrapCurrentTimeTest(
DateTime dateTime,
String? expectTime,
) {
final printer = SinglePrettyPrinter();
final actualTime = printer.getCurrentTime(
dateTime: dateTime,
);
expect(actualTime, expectTime);
}
62 changes: 57 additions & 5 deletions test/roggle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ void main() {
printedStackTrace = null;
});

test('Constructor', () {
Roggle(filter: _NeverFilter(), printer: callbackPrinter)
.log(Level.debug, 'Some message');

expect(printedMessage, null);
group('Constructor', () {
test('default', () {
Roggle().d('some message');
});
test('filter', () {
Roggle(filter: _NeverFilter(), printer: callbackPrinter)
.log(Level.debug, 'Some message');

expect(printedMessage, null);
});
});

test('Roggle.log', () {
Expand Down Expand Up @@ -177,4 +182,51 @@ void main() {
logger.w('This is');
expect(printedMessage, 'This is');
});

test('setting static log level above log level of message', () {
printedMessage = null;
Roggle.level = Level.warning;
final logger = Roggle(
filter: ProductionFilter(),
printer: callbackPrinter,
)..d('This isn\'t logged');
expect(printedMessage, isNull);

logger.w('This is');
expect(printedMessage, 'This is');

Roggle.level = Level.verbose;
});

test('get filter', () {
final filter = ProductionFilter();
final logger = Roggle(
filter: filter,
);
expect(logger.filter.hashCode, filter.hashCode);
});

test('get printer', () {
final printer = SinglePrettyPrinter();
final logger = Roggle(
printer: printer,
);
expect(logger.printer.hashCode, printer.hashCode);
});

test('get output', () {
final output = ConsoleOutput();
final logger = Roggle(
output: output,
);
expect(logger.output.hashCode, output.hashCode);
});

test('get active', () {
final logger = Roggle();
expect(logger.active, true);

logger.close();
expect(logger.active, false);
});
}

0 comments on commit b437672

Please sign in to comment.