|
| 1 | +import 'package:apidash_core/utils/http_request_utils.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('Testing RemoveJsonComments', () { |
| 6 | + test('Removes single-line comments', () { |
| 7 | + String input = ''' |
| 8 | + { |
| 9 | + // This is a single-line comment |
| 10 | + "key": "value" |
| 11 | + } |
| 12 | + '''; |
| 13 | + |
| 14 | + String expected = '''{ |
| 15 | + "key": "value" |
| 16 | +}'''; |
| 17 | + expect(removeJsonComments(input), expected); |
| 18 | + }); |
| 19 | + |
| 20 | + test('Removes multi-line comments', () { |
| 21 | + String input = ''' |
| 22 | + { |
| 23 | + /* |
| 24 | + This is a multi-line comment |
| 25 | + */ |
| 26 | + "key": "value" |
| 27 | + } |
| 28 | + '''; |
| 29 | + |
| 30 | + String expected = '''{ |
| 31 | + "key": "value" |
| 32 | +}'''; |
| 33 | + expect(removeJsonComments(input), expected); |
| 34 | + }); |
| 35 | + |
| 36 | + test('Handles valid JSON without comments', () { |
| 37 | + String input = '{"key":"value"}'; |
| 38 | + String expected = '''{ |
| 39 | + "key": "value" |
| 40 | +}'''; |
| 41 | + expect(removeJsonComments(input), expected); |
| 42 | + }); |
| 43 | + |
| 44 | + test('Returns original string if invalid JSON', () { |
| 45 | + String input = '{key: value}'; |
| 46 | + String expected = '{key: value}'; |
| 47 | + expect(removeJsonComments(input), expected); |
| 48 | + }); |
| 49 | + |
| 50 | + test('Removes trailing commas', () { |
| 51 | + String input = ''' |
| 52 | + { |
| 53 | + "key1": "value1", |
| 54 | + "key2": "value2", // trailing comma |
| 55 | + } |
| 56 | + '''; |
| 57 | + |
| 58 | + String expected = '''{ |
| 59 | + "key1": "value1", |
| 60 | + "key2": "value2" |
| 61 | +}'''; |
| 62 | + expect(removeJsonComments(input), expected); |
| 63 | + }); |
| 64 | + |
| 65 | + test('Test blank json', () { |
| 66 | + String input = ''' |
| 67 | + {} |
| 68 | + '''; |
| 69 | + |
| 70 | + String expected = '{}'; |
| 71 | + expect(removeJsonComments(input), expected); |
| 72 | + }); |
| 73 | + }); |
| 74 | +} |
0 commit comments