222 lines
8.2 KiB
Dart
222 lines
8.2 KiB
Dart
import 'package:test/test.dart';
|
|
|
|
import '../lib/utils/string_utils.dart';
|
|
|
|
void main() {
|
|
group('StringUtils', () {
|
|
group('toDartPropertyName', () {
|
|
test('converts snake_case to camelCase', () {
|
|
expect(StringUtils.toDartPropertyName('user_id'), 'userId');
|
|
expect(StringUtils.toDartPropertyName('user-id'), 'userId');
|
|
expect(StringUtils.toDartPropertyName('1st_field'), 'n1stField');
|
|
expect(StringUtils.toDartPropertyName(''), 'property');
|
|
});
|
|
|
|
test('handles special characters', () {
|
|
expect(StringUtils.toDartPropertyName('field@name'), 'fieldName');
|
|
expect(StringUtils.toDartPropertyName('with space'), 'withSpace');
|
|
expect(StringUtils.toDartPropertyName('with.dot'), 'withDot');
|
|
expect(StringUtils.toDartPropertyName('with/slash'), 'withSlash');
|
|
});
|
|
|
|
test('handles numbers at start', () {
|
|
expect(StringUtils.toDartPropertyName('1field'), 'n1field');
|
|
expect(StringUtils.toDartPropertyName('123test'), 'n123test');
|
|
});
|
|
});
|
|
|
|
group('toCamelCase', () {
|
|
test('converts snake_case to camelCase', () {
|
|
expect(StringUtils.toCamelCase('user_id'), 'userId');
|
|
expect(StringUtils.toCamelCase('first_name'), 'firstName');
|
|
expect(StringUtils.toCamelCase('api_version'), 'apiVersion');
|
|
});
|
|
|
|
test('converts PascalCase to camelCase', () {
|
|
expect(StringUtils.toCamelCase('GetClassesTaskChecklistUsers'),
|
|
'getClassesTaskChecklistUsers');
|
|
expect(StringUtils.toCamelCase('GetUserInfo'), 'getUserInfo');
|
|
expect(StringUtils.toCamelCase('CreateTask'), 'createTask');
|
|
expect(
|
|
StringUtils.toCamelCase('UpdateUserProfile'), 'updateUserProfile');
|
|
expect(StringUtils.toCamelCase('DeleteTaskById'), 'deleteTaskById');
|
|
});
|
|
|
|
test('preserves existing camelCase', () {
|
|
expect(StringUtils.toCamelCase('getClassesTaskChecklistUsers'),
|
|
'getClassesTaskChecklistUsers');
|
|
expect(StringUtils.toCamelCase('getUserInfo'), 'getUserInfo');
|
|
expect(StringUtils.toCamelCase('createTask'), 'createTask');
|
|
});
|
|
|
|
test('handles single word', () {
|
|
expect(StringUtils.toCamelCase('user'), 'user');
|
|
expect(StringUtils.toCamelCase(''), '');
|
|
});
|
|
|
|
test('handles multiple underscores', () {
|
|
expect(StringUtils.toCamelCase('user__id'), 'userId');
|
|
expect(StringUtils.toCamelCase('_user_id_'), 'userId');
|
|
});
|
|
});
|
|
|
|
group('toPascalCase', () {
|
|
test('converts snake_case to PascalCase', () {
|
|
expect(StringUtils.toPascalCase('user_id'), 'UserId');
|
|
expect(StringUtils.toPascalCase('first_name'), 'FirstName');
|
|
expect(StringUtils.toPascalCase('api_version'), 'ApiVersion');
|
|
});
|
|
|
|
test('handles already PascalCase', () {
|
|
expect(StringUtils.toPascalCase('User'), 'User');
|
|
expect(StringUtils.toPascalCase('UserID'), 'UserID');
|
|
});
|
|
|
|
test('handles camelCase input', () {
|
|
expect(StringUtils.toPascalCase('userId'), 'UserId');
|
|
expect(StringUtils.toPascalCase('firstName'), 'FirstName');
|
|
});
|
|
|
|
test('handles empty and single character', () {
|
|
expect(StringUtils.toPascalCase(''), '');
|
|
expect(StringUtils.toPascalCase('a'), 'A');
|
|
});
|
|
});
|
|
|
|
group('toSnakeCase', () {
|
|
test('converts camelCase to snake_case', () {
|
|
expect(StringUtils.toSnakeCase('userId'), 'user_id');
|
|
expect(StringUtils.toSnakeCase('firstName'), 'first_name');
|
|
expect(StringUtils.toSnakeCase('apiVersion'), 'api_version');
|
|
});
|
|
|
|
test('converts PascalCase to snake_case', () {
|
|
expect(StringUtils.toSnakeCase('UserID'), 'user_id');
|
|
expect(StringUtils.toSnakeCase('FirstName'), 'first_name');
|
|
});
|
|
|
|
test('handles acronyms', () {
|
|
expect(StringUtils.toSnakeCase('API'), 'api');
|
|
expect(StringUtils.toSnakeCase('URL'), 'url');
|
|
});
|
|
|
|
test('handles empty and single character', () {
|
|
expect(StringUtils.toSnakeCase(''), '');
|
|
expect(StringUtils.toSnakeCase('a'), 'a');
|
|
});
|
|
});
|
|
|
|
group('generateClassName', () {
|
|
test('generates valid class names', () {
|
|
expect(StringUtils.generateClassName('user'), 'User');
|
|
expect(StringUtils.generateClassName('user_id'), 'UserId');
|
|
expect(StringUtils.generateClassName('api-version'), 'ApiVersion');
|
|
});
|
|
|
|
test('handles special characters', () {
|
|
expect(StringUtils.generateClassName('user@name'), 'UserName');
|
|
expect(StringUtils.generateClassName('user.name'), 'UserName');
|
|
});
|
|
});
|
|
|
|
group('generateFileName', () {
|
|
test('generates valid file names', () {
|
|
expect(StringUtils.generateFileName('User'), 'user.dart');
|
|
expect(StringUtils.generateFileName('UserID'), 'user_id.dart');
|
|
expect(StringUtils.generateFileName('ApiVersion'), 'api_version.dart');
|
|
});
|
|
});
|
|
|
|
group('isValidDartIdentifier', () {
|
|
test('valid identifiers', () {
|
|
expect(StringUtils.isValidDartIdentifier('user'), true);
|
|
expect(StringUtils.isValidDartIdentifier('user_id'), true);
|
|
expect(StringUtils.isValidDartIdentifier('_private'), true);
|
|
expect(StringUtils.isValidDartIdentifier('user123'), true);
|
|
});
|
|
|
|
test('invalid identifiers', () {
|
|
expect(StringUtils.isValidDartIdentifier(''), false);
|
|
expect(StringUtils.isValidDartIdentifier('123user'), false);
|
|
expect(StringUtils.isValidDartIdentifier('user-name'), false);
|
|
expect(StringUtils.isValidDartIdentifier('user@name'), false);
|
|
});
|
|
});
|
|
|
|
group('generateEnumValueName', () {
|
|
test('generates valid enum names from strings', () {
|
|
expect(StringUtils.generateEnumValueName('active', 0), 'active');
|
|
expect(
|
|
StringUtils.generateEnumValueName('user_status', 1), 'userStatus');
|
|
});
|
|
|
|
test('handles invalid strings', () {
|
|
expect(StringUtils.generateEnumValueName('', 0), 'value1');
|
|
expect(StringUtils.generateEnumValueName('123', 1), 'value2');
|
|
});
|
|
|
|
test('handles non-string values', () {
|
|
expect(StringUtils.generateEnumValueName(123, 0), 'value1');
|
|
expect(StringUtils.generateEnumValueName('', 1), 'value2');
|
|
});
|
|
});
|
|
|
|
group('cleanDescription', () {
|
|
test('cleans basic descriptions', () {
|
|
expect(StringUtils.cleanDescription(' test description '),
|
|
'test description');
|
|
expect(StringUtils.cleanDescription('line1\nline2'), 'line1 line2');
|
|
});
|
|
|
|
test('removes special characters', () {
|
|
expect(StringUtils.cleanDescription('test@#\$%'), 'test');
|
|
expect(StringUtils.cleanDescription('test[description]'),
|
|
'testdescription');
|
|
});
|
|
|
|
test('truncates long descriptions', () {
|
|
final longDesc = 'a' * 300;
|
|
final result = StringUtils.cleanDescription(longDesc);
|
|
expect(result.length, lessThanOrEqualTo(203)); // 200 + '...'
|
|
expect(result.endsWith('...'), true);
|
|
});
|
|
|
|
test('handles empty and null', () {
|
|
expect(StringUtils.cleanDescription(''), '');
|
|
});
|
|
});
|
|
|
|
group('generateComment', () {
|
|
test('generates single line comments', () {
|
|
expect(StringUtils.generateComment('test'), '/// test');
|
|
expect(StringUtils.generateComment(''), '');
|
|
});
|
|
|
|
test('generates block comments', () {
|
|
final result = StringUtils.generateComment('test', isBlock: true);
|
|
expect(result, contains('/**'));
|
|
expect(result, contains('test'));
|
|
expect(result, contains('*/'));
|
|
});
|
|
});
|
|
|
|
group('formatBytes', () {
|
|
test('formats bytes correctly', () {
|
|
expect(StringUtils.formatBytes(1023), '1023B');
|
|
expect(StringUtils.formatBytes(1024), '1.0KB');
|
|
expect(StringUtils.formatBytes(1024 * 1024), '1.0MB');
|
|
expect(StringUtils.formatBytes(1024 * 1024 * 1024), '1.0GB');
|
|
});
|
|
});
|
|
|
|
group('formatDuration', () {
|
|
test('formats duration correctly', () {
|
|
expect(
|
|
StringUtils.formatDuration(Duration(milliseconds: 500)), '500毫秒');
|
|
expect(StringUtils.formatDuration(Duration(seconds: 1)), '1.00秒');
|
|
expect(StringUtils.formatDuration(Duration(seconds: 2)), '2.00秒');
|
|
});
|
|
});
|
|
});
|
|
}
|