test(logging): add logging tests
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:social_app/core/logging/log_entry.dart';
|
||||
|
||||
void main() {
|
||||
group('LogEntry', () {
|
||||
test('toJson should serialize correctly', () {
|
||||
final entry = LogEntry(
|
||||
timestamp: DateTime(2026, 4, 1, 10, 0, 0),
|
||||
level: LogLevel.error,
|
||||
message: 'Test error',
|
||||
module: 'test.module',
|
||||
funcName: 'testFunc',
|
||||
lineNo: 42,
|
||||
errorType: 'TestError',
|
||||
stackTrace: 'test stack',
|
||||
extra: {'key': 'value'},
|
||||
);
|
||||
|
||||
final json = entry.toJson();
|
||||
|
||||
expect(json['level'], 'error');
|
||||
expect(json['error_type'], 'TestError');
|
||||
expect(json['extra'], {'key': 'value'});
|
||||
});
|
||||
|
||||
test('toConsoleString should format correctly', () {
|
||||
final entry = LogEntry(
|
||||
timestamp: DateTime(2026, 4, 1, 10, 0, 0),
|
||||
level: LogLevel.debug,
|
||||
message: 'Test message',
|
||||
module: 'test.module',
|
||||
funcName: 'testFunc',
|
||||
lineNo: 42,
|
||||
);
|
||||
|
||||
final result = entry.toConsoleString();
|
||||
|
||||
expect(result, contains('DEBUG'));
|
||||
expect(result, contains('test.module'));
|
||||
expect(result, contains('testFunc'));
|
||||
expect(result, contains('@42'));
|
||||
expect(result, contains('Test message'));
|
||||
});
|
||||
|
||||
test('toFileString should format correctly', () {
|
||||
final entry = LogEntry(
|
||||
timestamp: DateTime(2026, 4, 1, 10, 0, 0),
|
||||
level: LogLevel.error,
|
||||
message: 'Test error',
|
||||
module: 'test.module',
|
||||
funcName: 'testFunc',
|
||||
lineNo: 42,
|
||||
errorType: 'TestError',
|
||||
stackTrace: 'test stack trace',
|
||||
);
|
||||
|
||||
final result = entry.toFileString();
|
||||
|
||||
expect(result, contains('[test.module]'));
|
||||
expect(result, contains('at testFunc:42'));
|
||||
expect(result, contains('Test error'));
|
||||
expect(result, contains('Error: TestError'));
|
||||
expect(result, contains('StackTrace:'));
|
||||
expect(result, contains('test stack trace'));
|
||||
});
|
||||
|
||||
test('toJson should omit null fields', () {
|
||||
final entry = LogEntry(
|
||||
timestamp: DateTime(2026, 4, 1, 10, 0, 0),
|
||||
level: LogLevel.info,
|
||||
message: 'Test',
|
||||
module: 'test',
|
||||
);
|
||||
|
||||
final json = entry.toJson();
|
||||
|
||||
expect(json.containsKey('func_name'), false);
|
||||
expect(json.containsKey('line_no'), false);
|
||||
expect(json.containsKey('error_type'), false);
|
||||
expect(json.containsKey('stack_trace'), false);
|
||||
expect(json.containsKey('extra'), false);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user