273 lines
8.1 KiB
Dart
273 lines
8.1 KiB
Dart
import 'package:swagger_generator_flutter/core/config_repository.dart';
|
||
import 'package:swagger_generator_flutter/core/models.dart';
|
||
import 'package:swagger_generator_flutter/pipeline/generate/apis.dart';
|
||
import 'package:test/test.dart';
|
||
|
||
void main() {
|
||
group('CancelToken Support Tests', () {
|
||
late SwaggerDocument simpleDocument;
|
||
|
||
setUp(() {
|
||
// 清除缓存以确保每个测试使用新配置
|
||
ConfigRepository.setCachedConfig(ConfigRepository({}));
|
||
|
||
simpleDocument = SwaggerDocument(
|
||
title: 'Test API',
|
||
version: '1.0.0',
|
||
description: 'A test API for request cancellation',
|
||
servers: [
|
||
const ApiServer(
|
||
url: 'https://api.example.com',
|
||
description: 'Test server',
|
||
),
|
||
],
|
||
paths: {
|
||
const ApiPathKey('/users', HttpMethod.get): const ApiPath(
|
||
path: '/users',
|
||
method: HttpMethod.get,
|
||
summary: 'Get users',
|
||
description: 'Get all users',
|
||
operationId: 'getUsers',
|
||
tags: ['users'],
|
||
parameters: [
|
||
ApiParameter(
|
||
name: 'page',
|
||
location: ParameterLocation.query,
|
||
required: false,
|
||
type: PropertyType.integer,
|
||
description: 'Page number',
|
||
),
|
||
],
|
||
responses: {
|
||
'200': ApiResponse(
|
||
code: '200',
|
||
description: 'Success',
|
||
),
|
||
},
|
||
),
|
||
const ApiPathKey('/users/{id}', HttpMethod.get): const ApiPath(
|
||
path: '/users/{id}',
|
||
method: HttpMethod.get,
|
||
summary: 'Get user by ID',
|
||
description: 'Get a specific user',
|
||
operationId: 'getUserById',
|
||
tags: ['users'],
|
||
parameters: [
|
||
ApiParameter(
|
||
name: 'id',
|
||
location: ParameterLocation.path,
|
||
required: true,
|
||
type: PropertyType.integer,
|
||
description: 'User ID',
|
||
),
|
||
],
|
||
responses: {
|
||
'200': ApiResponse(
|
||
code: '200',
|
||
description: 'User found',
|
||
),
|
||
},
|
||
),
|
||
const ApiPathKey('/users', HttpMethod.post): const ApiPath(
|
||
path: '/users',
|
||
method: HttpMethod.post,
|
||
summary: 'Create user',
|
||
description: 'Create a new user',
|
||
operationId: 'createUser',
|
||
tags: ['users'],
|
||
parameters: [],
|
||
requestBody: ApiRequestBody(
|
||
description: 'User data',
|
||
required: true,
|
||
content: {
|
||
'application/json': ApiMediaType(
|
||
schema: {'type': 'object'},
|
||
),
|
||
},
|
||
),
|
||
responses: {
|
||
'201': ApiResponse(
|
||
code: '201',
|
||
description: 'User created',
|
||
),
|
||
},
|
||
),
|
||
},
|
||
models: {},
|
||
controllers: {},
|
||
);
|
||
});
|
||
|
||
tearDown(() {
|
||
// 清除缓存
|
||
ConfigRepository.setCachedConfig(ConfigRepository({}));
|
||
});
|
||
|
||
group('CancelToken 参数生成测试', () {
|
||
test('当 cancelTokenSupport 启用时,生成 CancelToken 参数', () {
|
||
// 设置配置启用 CancelToken
|
||
ConfigRepository.setCachedConfig(
|
||
ConfigRepository({
|
||
'generation': {
|
||
'api': {
|
||
'cancel_token_support': true,
|
||
},
|
||
},
|
||
}),
|
||
);
|
||
|
||
final generator = RetrofitApiGenerator(
|
||
className: 'TestApi',
|
||
splitByTags: false,
|
||
);
|
||
|
||
final result = generator.generateFromDocument(simpleDocument);
|
||
|
||
// 验证生成的代码包含 CancelToken 参数(使用更精确的匹配)
|
||
expect(result, contains('CancelToken? cancelToken'));
|
||
expect(result, contains('@CancelRequest()'));
|
||
});
|
||
|
||
test('当 cancelTokenSupport 禁用时,不生成 CancelToken 参数', () {
|
||
// 设置配置禁用 CancelToken
|
||
ConfigRepository.setCachedConfig(
|
||
ConfigRepository({
|
||
'generation': {
|
||
'api': {
|
||
'cancel_token_support': false,
|
||
},
|
||
},
|
||
}),
|
||
);
|
||
|
||
final generator = RetrofitApiGenerator(
|
||
className: 'TestApi',
|
||
splitByTags: false,
|
||
);
|
||
|
||
final result = generator.generateFromDocument(simpleDocument);
|
||
|
||
// 验证生成的代码不包含 CancelToken 参数(使用更精确的匹配)
|
||
expect(result, isNot(contains('CancelToken? cancelToken')));
|
||
expect(result, isNot(contains('@CancelRequest()')));
|
||
});
|
||
|
||
test('默认情况下不生成 CancelToken 参数', () {
|
||
// 使用默认配置(空配置)
|
||
ConfigRepository.setCachedConfig(ConfigRepository({}));
|
||
|
||
final generator = RetrofitApiGenerator(
|
||
className: 'TestApi',
|
||
splitByTags: false,
|
||
);
|
||
|
||
final result = generator.generateFromDocument(simpleDocument);
|
||
|
||
// 验证默认情况下不生成 CancelToken 参数
|
||
expect(result, isNot(contains('CancelToken? cancelToken')));
|
||
expect(result, isNot(contains('@CancelRequest()')));
|
||
});
|
||
|
||
test('CancelToken 参数应该是可选的', () {
|
||
ConfigRepository.setCachedConfig(
|
||
ConfigRepository({
|
||
'generation': {
|
||
'api': {
|
||
'cancel_token_support': true,
|
||
},
|
||
},
|
||
}),
|
||
);
|
||
|
||
final generator = RetrofitApiGenerator(
|
||
className: 'TestApi',
|
||
splitByTags: false,
|
||
);
|
||
|
||
final result = generator.generateFromDocument(simpleDocument);
|
||
|
||
// 验证 CancelToken 参数是可选的(使用 ? 表示)
|
||
expect(result, contains('CancelToken?'));
|
||
// 验证不是必选参数
|
||
expect(result, isNot(contains('required CancelToken')));
|
||
});
|
||
|
||
test('所有 API 方法都应包含 CancelToken 参数', () {
|
||
ConfigRepository.setCachedConfig(
|
||
ConfigRepository({
|
||
'generation': {
|
||
'api': {
|
||
'cancel_token_support': true,
|
||
},
|
||
},
|
||
}),
|
||
);
|
||
|
||
final generator = RetrofitApiGenerator(
|
||
className: 'TestApi',
|
||
splitByTags: false,
|
||
);
|
||
|
||
final result = generator.generateFromDocument(simpleDocument);
|
||
|
||
// 统计 CancelToken 参数出现的次数(应该等于 API 方法数量)
|
||
final cancelTokenCount =
|
||
'CancelToken? cancelToken'.allMatches(result).length;
|
||
|
||
// 我们有 3 个 API 方法(getUsers, getUserById, createUser)
|
||
expect(cancelTokenCount, equals(3));
|
||
});
|
||
});
|
||
|
||
group('ConfigRepository CancelToken 配置测试', () {
|
||
test('cancelTokenSupport getter 正确解析 true', () {
|
||
final config = ConfigRepository({
|
||
'generation': {
|
||
'api': {
|
||
'cancel_token_support': true,
|
||
},
|
||
},
|
||
});
|
||
|
||
expect(config.cancelTokenSupport, isTrue);
|
||
});
|
||
|
||
test('cancelTokenSupport getter 正确解析 false', () {
|
||
final config = ConfigRepository({
|
||
'generation': {
|
||
'api': {
|
||
'cancel_token_support': false,
|
||
},
|
||
},
|
||
});
|
||
|
||
expect(config.cancelTokenSupport, isFalse);
|
||
});
|
||
|
||
test('cancelTokenSupport getter 默认为 false', () {
|
||
final config = ConfigRepository({});
|
||
|
||
expect(config.cancelTokenSupport, isFalse);
|
||
});
|
||
|
||
test('cancelTokenSupport getter 处理部分配置', () {
|
||
final config = ConfigRepository(<String, dynamic>{
|
||
'generation': <String, dynamic>{
|
||
'api': <String, dynamic>{},
|
||
},
|
||
});
|
||
|
||
expect(config.cancelTokenSupport, isFalse);
|
||
});
|
||
|
||
test('cancelTokenSupport getter 处理缺少 api 配置', () {
|
||
final config = ConfigRepository(<String, dynamic>{
|
||
'generation': <String, dynamic>{},
|
||
});
|
||
|
||
expect(config.cancelTokenSupport, isFalse);
|
||
});
|
||
});
|
||
});
|
||
}
|