swagger_generator_flutter/test/config_prefix_test.dart

78 lines
3.2 KiB
Dart

import 'dart:io';
import 'package:swagger_generator_flutter/core/config.dart';
import 'package:swagger_generator_flutter/core/config_repository.dart';
import 'package:swagger_generator_flutter/utils/string_helper.dart';
import 'package:test/test.dart';
void main() {
group('Model Class Prefix Support', () {
late Directory tempDir;
late File configFile;
setUp(() {
tempDir = Directory.systemTemp.createTempSync('swagger_gen_test_');
configFile = File('${tempDir.path}/generator_config.yaml');
});
tearDown(() {
tempDir.deleteSync(recursive: true);
});
test('should parse class_prefix from config', () {
configFile.writeAsStringSync('''
generator:
name: test
output:
models:
class_prefix: "MyPrefix"
generation:
models:
class_prefix: "MyPrefix"
''');
final config = ConfigRepository.loadSync(configFile.path);
expect(config.modelClassPrefix, equals('MyPrefix'));
});
test('should return null when class_prefix is missing', () {
configFile.writeAsStringSync('''
generator:
name: test
''');
final config = ConfigRepository.loadSync(configFile.path);
expect(config.modelClassPrefix, isNull);
});
// NOTE: Testing StringHelper.generateClassName directly implies checking if it reads from the GLOBAL config.
// However, ConfigRepository.loadSync() creates an instance, but SwaggerConfig accessors call ConfigRepository.loadSync() individually.
// Since ConfigRepository.loadSync() without args looks for default file, we need a way to inject the config or point it to our file.
// The current implementation of SwaggerConfig calls ConfigRepository.loadSync() which defaults to finding a config file.
// Changing the implementation of StringHelper to depend on a reloadable config or global state would be better,
// but without changing that, we rely on how ConfigRepository finds the file.
// Ideally we should test ConfigRepository logic separately from StringHelper if StringHelper uses a static/global config lookup.
// BUT, wait. ConfigRepository.loadSync() does THIS:
// final file = File(configPath ?? PathResolver.findConfigFile() ?? '');
// If we want SwaggerConfig (static) to pick up our test config, we might need to trick PathResolver
// OR we can explicitly pass the config path if the code supported it, but StringHelper uses static SwaggerConfig.modelClassPrefix.
// The current implementation of `SwaggerConfig.modelClassPrefix` is:
// static String? get modelClassPrefix => ConfigRepository.loadSync().modelClassPrefix;
// So every time we call `StringHelper.generateClassName`, it calls `ConfigRepository.loadSync()`.
// `ConfigRepository.loadSync()` calls `PathResolver.findConfigFile()`.
// Since we cannot easily mock `PathResolver`'s static method or filesystem search path in a unit test without dependency injection,
// we might face issues testing `StringHelper` integration end-to-end here unless we run this test in a context where `findConfigFile` returns our temp file.
// However, for verify purposes, verifying `ConfigRepository` parses it is the most critical part we added.
// The `StringHelper` logic is simple string concatenation.
});
}