mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 18:57:05 +08:00
tests: add tests for dashbot utils(cv: 100)
This commit is contained in:
37
test/dashbot/utils/safe_parse_json_message_test.dart
Normal file
37
test/dashbot/utils/safe_parse_json_message_test.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:apidash/dashbot/core/utils/safe_parse_json_message.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('MessageJson.safeParse', () {
|
||||
test('parses valid JSON object', () {
|
||||
final m = MessageJson.safeParse('{"a":1,"b":"x"}');
|
||||
expect(m, containsPair('a', 1));
|
||||
expect(m['b'], 'x');
|
||||
});
|
||||
|
||||
test('returns empty map for non-object top-level JSON', () {
|
||||
final m = MessageJson.safeParse('[1,2,3]');
|
||||
expect(m, isEmpty);
|
||||
});
|
||||
|
||||
test('extracts object from markdown fenced code block', () {
|
||||
const input =
|
||||
'''Here is your result:\n```json\n{\n "ok": true,\n "count": 2\n}\n```\nThanks''';
|
||||
final m = MessageJson.safeParse(input);
|
||||
expect(m['ok'], true);
|
||||
expect(m['count'], 2);
|
||||
});
|
||||
|
||||
test('throws FormatException on invalid JSON with no braces slice', () {
|
||||
expect(() => MessageJson.safeParse('totally invalid'),
|
||||
throwsFormatException);
|
||||
});
|
||||
|
||||
test('falls back to slice between first { and last }', () {
|
||||
const input = 'prefix {"z":42, "k":"v"} suffix';
|
||||
final m = MessageJson.safeParse(input);
|
||||
expect(m['z'], 42);
|
||||
expect(m['k'], 'v');
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user