135 lines
4.7 KiB
Dart
135 lines
4.7 KiB
Dart
import 'package:swagger_generator_flutter/utils/string_utils/text_cleaner.dart';
|
||
import 'package:test/test.dart';
|
||
|
||
void main() {
|
||
group('TextCleaner', () {
|
||
group('cleanDescription', () {
|
||
test('removes newlines from text', () {
|
||
const input = '部长新增工作任务指标\n(会删除所有管理的班级任务指标-删除所有管理的学习官的通用任务指标)';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
expect(result, isNot(contains('\n')));
|
||
expect(result, isNot(contains('\r')));
|
||
expect(result, '部长新增工作任务指标 (会删除所有管理的班级任务指标-删除所有管理的学习官的通用任务指标)');
|
||
});
|
||
|
||
test('removes carriage returns from text', () {
|
||
const input = 'Line 1\r\nLine 2\rLine 3';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
expect(result, isNot(contains('\n')));
|
||
expect(result, isNot(contains('\r')));
|
||
expect(result, 'Line 1 Line 2 Line 3');
|
||
});
|
||
|
||
test('replaces multiple spaces with single space', () {
|
||
const input = 'Text with multiple spaces';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
expect(result, 'Text with multiple spaces');
|
||
});
|
||
|
||
test('removes HTML tags', () {
|
||
const input = '<p>Text with <strong>HTML</strong> tags</p>';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
expect(result, 'Text with HTML tags');
|
||
});
|
||
|
||
test('escapes comment end markers', () {
|
||
const input = 'Text with */ comment end';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
expect(result, 'Text with * / comment end');
|
||
});
|
||
|
||
test('trims leading and trailing whitespace', () {
|
||
const input = ' Text with spaces ';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
expect(result, 'Text with spaces');
|
||
});
|
||
|
||
test('handles empty string', () {
|
||
const input = '';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
expect(result, '');
|
||
});
|
||
|
||
test('handles complex Chinese text with newlines', () {
|
||
const input = '获取用户信息\n包含用户的基本信息和扩展信息';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
expect(result, isNot(contains('\n')));
|
||
expect(result, '获取用户信息 包含用户的基本信息和扩展信息');
|
||
});
|
||
|
||
test('handles text with parentheses and newlines', () {
|
||
const input = '部长新增工作任务指标\n(会删除所有管理的班级任务指标-删除所有管理的学习官的通用任务指标)';
|
||
final result = TextCleaner.cleanDescription(input);
|
||
|
||
// Should not contain newlines
|
||
expect(result, isNot(contains('\n')));
|
||
// Should preserve parentheses
|
||
expect(result, contains('('));
|
||
expect(result, contains(')'));
|
||
// Should be on single line
|
||
expect(result, '部长新增工作任务指标 (会删除所有管理的班级任务指标-删除所有管理的学习官的通用任务指标)');
|
||
});
|
||
});
|
||
|
||
group('normalize', () {
|
||
test('normalizes line endings', () {
|
||
const input = 'Line 1\r\nLine 2\rLine 3\nLine 4';
|
||
final result = TextCleaner.normalize(input);
|
||
|
||
expect(result, 'Line 1\nLine 2\nLine 3\nLine 4');
|
||
});
|
||
|
||
test('removes excessive blank lines', () {
|
||
const input = 'Line 1\n\n\n\nLine 2';
|
||
final result = TextCleaner.normalize(input);
|
||
|
||
expect(result, 'Line 1\n\nLine 2');
|
||
});
|
||
|
||
test('trims whitespace', () {
|
||
const input = ' Text ';
|
||
final result = TextCleaner.normalize(input);
|
||
|
||
expect(result, 'Text');
|
||
});
|
||
});
|
||
|
||
group('escapeString', () {
|
||
test('escapes special characters', () {
|
||
const input = "Text with 'quotes' and \"double quotes\" and \n newlines";
|
||
final result = TextCleaner.escapeString(input);
|
||
|
||
expect(result, contains("\\'"));
|
||
expect(result, contains('\\"'));
|
||
expect(result, contains('\\n'));
|
||
});
|
||
});
|
||
|
||
group('truncate', () {
|
||
test('truncates long text', () {
|
||
const input = 'This is a very long text that needs to be truncated';
|
||
final result = TextCleaner.truncate(input, 20);
|
||
|
||
expect(result.length, lessThanOrEqualTo(20));
|
||
expect(result, endsWith('...'));
|
||
});
|
||
|
||
test('does not truncate short text', () {
|
||
const input = 'Short text';
|
||
final result = TextCleaner.truncate(input, 20);
|
||
|
||
expect(result, input);
|
||
});
|
||
});
|
||
});
|
||
}
|
||
|