|
| 1 | +import 'package:logger/logger.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + String _readMessage(List<String> log) { |
| 6 | + return log.reduce((acc, val) => acc + val); |
| 7 | + } |
| 8 | + |
| 9 | + final prettyPrinter = PrettyPrinter(printEmojis: false); |
| 10 | + test('should print an emoji when option is enabled', () { |
| 11 | + final expectedMessage = 'some message with an emoji'; |
| 12 | + final emojiPrettyPrinter = PrettyPrinter(printEmojis: true); |
| 13 | + |
| 14 | + final event = LogEvent( |
| 15 | + Level.debug, |
| 16 | + expectedMessage, |
| 17 | + 'some error', |
| 18 | + StackTrace.current, |
| 19 | + ); |
| 20 | + |
| 21 | + final actualLog = emojiPrettyPrinter.log(event); |
| 22 | + final actualLogString = _readMessage(actualLog); |
| 23 | + expect(actualLogString, contains(PrettyPrinter.levelEmojis[Level.debug])); |
| 24 | + expect(actualLogString, contains(expectedMessage)); |
| 25 | + }); |
| 26 | + |
| 27 | + test('deal with string type message', () { |
| 28 | + final expectedMessage = 'normally computed message'; |
| 29 | + final withFunction = LogEvent( |
| 30 | + Level.debug, |
| 31 | + expectedMessage, |
| 32 | + 'some error', |
| 33 | + StackTrace.current, |
| 34 | + ); |
| 35 | + |
| 36 | + final actualLog = prettyPrinter.log(withFunction); |
| 37 | + final actualLogString = _readMessage(actualLog); |
| 38 | + |
| 39 | + expect( |
| 40 | + actualLogString, |
| 41 | + contains(expectedMessage), |
| 42 | + ); |
| 43 | + }); |
| 44 | + test('deal with Map type message', () { |
| 45 | + final expectedMsgMap = {'foo': 123, 1: 2, true: 'false'}; |
| 46 | + var withMap = LogEvent( |
| 47 | + Level.debug, |
| 48 | + expectedMsgMap, |
| 49 | + 'some error', |
| 50 | + StackTrace.current, |
| 51 | + ); |
| 52 | + |
| 53 | + final actualLog = prettyPrinter.log(withMap); |
| 54 | + final actualLogString = _readMessage(actualLog); |
| 55 | + for (var expectedMsg in expectedMsgMap.entries) { |
| 56 | + expect( |
| 57 | + actualLogString, |
| 58 | + contains('${expectedMsg.key}: ${expectedMsg.value}'), |
| 59 | + ); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + test('deal with Iterable type message', () { |
| 64 | + final expectedMsgItems = ['first', 'second', 'third', 'last']; |
| 65 | + var withIterable = LogEvent( |
| 66 | + Level.debug, |
| 67 | + ['first', 'second', 'third', 'last'], |
| 68 | + 'some error', |
| 69 | + StackTrace.current, |
| 70 | + ); |
| 71 | + final actualLog = prettyPrinter.log(withIterable); |
| 72 | + final actualLogString = _readMessage(actualLog); |
| 73 | + for (var expectedMsg in expectedMsgItems) { |
| 74 | + expect( |
| 75 | + actualLogString, |
| 76 | + contains(expectedMsg), |
| 77 | + ); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + test('deal with Function type message', () { |
| 82 | + final expectedMessage = 'heavily computed very pretty Message'; |
| 83 | + final withFunction = LogEvent( |
| 84 | + Level.debug, |
| 85 | + () => expectedMessage, |
| 86 | + 'some error', |
| 87 | + StackTrace.current, |
| 88 | + ); |
| 89 | + |
| 90 | + final actualLog = prettyPrinter.log(withFunction); |
| 91 | + final actualLogString = _readMessage(actualLog); |
| 92 | + |
| 93 | + expect( |
| 94 | + actualLogString, |
| 95 | + contains(expectedMessage), |
| 96 | + ); |
| 97 | + }); |
| 98 | +} |
0 commit comments