From cc1a73a5a8f7862beefff63ef6c751cee31499b3 Mon Sep 17 00:00:00 2001 From: Jordy de Jonghe Date: Mon, 4 Sep 2023 17:06:44 +0200 Subject: [PATCH] #130: removed unused code --- example/lib/model/user/person/gender.dart | 22 +- lib/config/yml_generator_config.dart | 121 +++----- lib/model/model/enum_model.dart | 33 +- lib/writer/enum_model_writer.dart | 80 +---- .../yml_generator_config/enum-normal.txt | 8 + test/config/yml_generator_config_test.dart | 27 +- test/model/model/enum_model_test.dart | 48 ++- .../output.txt} | 0 .../output.txt} | 0 .../output.txt} | 0 .../{no-fields.txt => no-fields/output.txt} | 0 .../output.txt} | 0 .../{normal.txt => normal/output.txt} | 0 .../output.txt} | 0 .../output.txt} | 0 .../output.txt} | 0 .../output.txt} | 0 .../output.txt} | 0 .../output.txt} | 0 .../{null-value.txt => null-value/output.txt} | 0 test/writer/enum_model_writer_test.dart | 293 +----------------- test/writer/writer_test_helper.dart | 16 - 22 files changed, 140 insertions(+), 508 deletions(-) rename test/writer/enum_model_writer/{custom-value-map-extension.txt => custom-value-map-extension/output.txt} (100%) rename test/writer/enum_model_writer/{custom-value-map.txt => custom-value-map/output.txt} (100%) rename test/writer/enum_model_writer/{custom-value.txt => custom-value/output.txt} (100%) rename test/writer/enum_model_writer/{no-fields.txt => no-fields/output.txt} (100%) rename test/writer/enum_model_writer/{normal-description.txt => normal-description/output.txt} (100%) rename test/writer/enum_model_writer/{normal.txt => normal/output.txt} (100%) rename test/writer/enum_model_writer/{normal_with_double_type.txt => normal_with_double_type/output.txt} (100%) rename test/writer/enum_model_writer/{normal_with_double_type_map.txt => normal_with_double_type_map/output.txt} (100%) rename test/writer/enum_model_writer/{normal_with_double_type_map_extension.txt => normal_with_double_type_map_extension/output.txt} (100%) rename test/writer/enum_model_writer/{normal_with_int_type.txt => normal_with_int_type/output.txt} (100%) rename test/writer/enum_model_writer/{normal_with_int_type_map.txt => normal_with_int_type_map/output.txt} (100%) rename test/writer/enum_model_writer/{normal_with_int_type_map_extension.txt => normal_with_int_type_map_extension/output.txt} (100%) rename test/writer/enum_model_writer/{null-value.txt => null-value/output.txt} (100%) delete mode 100644 test/writer/writer_test_helper.dart diff --git a/example/lib/model/user/person/gender.dart b/example/lib/model/user/person/gender.dart index 687ae82..a82ca2d 100644 --- a/example/lib/model/user/person/gender.dart +++ b/example/lib/model/user/person/gender.dart @@ -4,21 +4,25 @@ import 'package:json_annotation/json_annotation.dart'; enum Gender { @JsonValue('_mAl3') - MALE, + MALE(name: 'brian'), @JsonValue('femAle') - FEMALE, + FEMALE(name: 'brian'), @JsonValue('X') - X, + X(name: 'brian'), @JsonValue('GENDER_X') - GENDER_X, + GENDER_X(name: 'brian'), @JsonValue('null') - GENDER_Y, + GENDER_Y(name: 'brian'), @JsonValue('gender_z') - GENDER_Z, + GENDER_Z(name: 'brian'), @JsonValue('GENDER_abC') - GENDER_ABC, + GENDER_ABC(name: 'brian'), @JsonValue('null') - GENDER_DEF, + GENDER_DEF(name: 'brian'), @JsonValue('GENDER_lap') - GENDER_LAP, + GENDER_LAP(name: 'brian'); + + final String name; + + const Gender({required this.name}); } diff --git a/lib/config/yml_generator_config.dart b/lib/config/yml_generator_config.dart index 3a9e3e6..07988ab 100644 --- a/lib/config/yml_generator_config.dart +++ b/lib/config/yml_generator_config.dart @@ -27,20 +27,16 @@ class YmlGeneratorConfig { List get models => _models; - YmlGeneratorConfig( - PubspecConfig pubspecConfig, String configContent, this.fileName) { + YmlGeneratorConfig(PubspecConfig pubspecConfig, String configContent, this.fileName) { final yamlContent = loadYaml(configContent); if (yamlContent == null) return; // Ignore empty file yamlContent.forEach((key, value) { - final String baseDirectory = - value['base_directory'] ?? pubspecConfig.baseDirectory; + final String baseDirectory = value['base_directory'] ?? pubspecConfig.baseDirectory; final String? path = value['path']; final String? extendsModel = value['extends']; - final bool generateForGenerics = - value['generate_for_generics'] ?? pubspecConfig.generateForGenerics; + final bool generateForGenerics = value['generate_for_generics'] ?? pubspecConfig.generateForGenerics; - final extraImports = - value.containsKey('extra_imports') ? [] : null; + final extraImports = value.containsKey('extra_imports') ? [] : null; final extraImportsVal = value['extra_imports']; extraImportsVal?.forEach((e) { if (e != null) { @@ -48,8 +44,7 @@ class YmlGeneratorConfig { } }); - final extraAnnotations = - value.containsKey('extra_annotations') ? [] : null; + final extraAnnotations = value.containsKey('extra_annotations') ? [] : null; final extraAnnotationsVal = value['extra_annotations']; extraAnnotationsVal?.forEach((e) { if (e != null) { @@ -93,69 +88,54 @@ class YmlGeneratorConfig { throw Exception('Properties can not be null. model: $key'); } if (properties is! YamlMap) { - throw Exception( - 'Properties should be a map, right now you are using a ${properties.runtimeType}. model: $key'); + throw Exception('Properties should be a map, right now you are using a ${properties.runtimeType}. model: $key'); } if (type == 'enum') { - final uppercaseEnums = - (value['uppercase_enums'] ?? pubspecConfig.uppercaseEnums) == true; - final itemType = value['item_type'] == null - ? const StringType() - : _parseSimpleType(value['item_type']); - - if (itemType is! StringType && - itemType is! IntegerType && - itemType is! DoubleType) { - throw Exception( - 'item_type should be a string or integer. model: $key'); - } + final uppercaseEnums = (value['uppercase_enums'] ?? pubspecConfig.uppercaseEnums) == true; final fields = []; properties.forEach((propertyKey, propertyValue) { if (propertyValue != null && propertyValue is! YamlMap) { throw Exception('$propertyKey should be an object'); } + final propertiesYaml = propertyValue as YamlMap; + final properties = []; + propertiesYaml.forEach((propertyKey, propertyValue) { + properties.add(EnumProperty( + value: propertyValue, + name: propertyKey, + )); + }); fields.add(EnumField( name: uppercaseEnums ? propertyKey.toUpperCase() : propertyKey, rawName: propertyKey, - value: propertyValue == null - ? null - : propertyValue['value'].toString(), - description: - propertyValue == null ? null : propertyValue['description'], + enumProperties: properties, )); }); models.add(EnumModel( name: key, path: path, - generateMap: value['generate_map'] == true, - generateExtensions: value['generate_extensions'] == true, + keyProperty: value['key_property'], baseDirectory: baseDirectory, fields: fields, - itemType: itemType, extraImports: extraImports, extraAnnotations: extraAnnotations, description: description, )); } else { final staticCreate = (value['static_create'] ?? false) == true; - final disallowNullForDefaults = - value.containsKey('disallow_null_for_defaults') - ? (value['disallow_null_for_defaults'] == true) - : pubspecConfig.disallowNullForDefaults; + final disallowNullForDefaults = value.containsKey('disallow_null_for_defaults') ? (value['disallow_null_for_defaults'] == true) : pubspecConfig.disallowNullForDefaults; final fields = []; properties.forEach((propertyKey, propertyValue) { if (propertyValue is YamlMap) { - fields.add(getField(propertyKey, propertyValue, - disallowNullForDefaults: disallowNullForDefaults)); + fields.add(getField(propertyKey, propertyValue, disallowNullForDefaults: disallowNullForDefaults)); } else if (propertyValue is String) { fields.add(getSimpleField(name: propertyKey, value: propertyValue)); } else { throw Exception('$propertyKey should be an object'); } }); - final mappedConverters = - converters?.map((element) => element.toString()).toList(); + final mappedConverters = converters?.map((element) => element.toString()).toList(); models.add(ObjectModel( name: key, path: path, @@ -177,36 +157,25 @@ class YmlGeneratorConfig { }); } - Field getField(String name, YamlMap property, - {required bool disallowNullForDefaults}) { + Field getField(String name, YamlMap property, {required bool disallowNullForDefaults}) { try { if (property.containsKey('required')) { - throw ArgumentError( - 'required is removed, follow the migration to version 7.0.0'); + throw ArgumentError('required is removed, follow the migration to version 7.0.0'); } - final ignored = - property.containsKey('ignore') && property['ignore'] == true; - final includeFromJson = !property.containsKey('includeFromJson') || - property['includeFromJson'] == true; - final includeToJson = !property.containsKey('includeToJson') || - property['includeToJson'] == true; - final nonFinal = ignored || - property.containsKey('non_final') && property['non_final'] == true; - final includeIfNull = property.containsKey('include_if_null') && - property['include_if_null'] == true; + final ignored = property.containsKey('ignore') && property['ignore'] == true; + final includeFromJson = !property.containsKey('includeFromJson') || property['includeFromJson'] == true; + final includeToJson = !property.containsKey('includeToJson') || property['includeToJson'] == true; + final nonFinal = ignored || property.containsKey('non_final') && property['non_final'] == true; + final includeIfNull = property.containsKey('include_if_null') && property['include_if_null'] == true; final unknownEnumValue = property['unknown_enum_value']; final jsonKey = property['jsonKey'] ?? property['jsonkey']; final fromJson = property['fromJson']; final toJson = property['toJson']; - final description = property.containsKey('description') - ? property['description']!.toString() - : null; + final description = property.containsKey('description') ? property['description']!.toString() : null; final type = property['type'] as String?; final skipEquality = property['ignore_equality'] == true; final defaultValue = property['default_value']?.toString(); - final disallowNull = property.containsKey('disallow_null') - ? (property['disallow_null'] == true) - : disallowNullForDefaults; + final disallowNull = property.containsKey('disallow_null') ? (property['disallow_null'] == true) : disallowNullForDefaults; ItemType itemType; if (type == null) { @@ -272,9 +241,7 @@ class YmlGeneratorConfig { return 'DateTime'; } else if (lowerType == 'int' || lowerType == 'integer') { return 'int'; - } else if (lowerType == 'object' || - lowerType == 'dynamic' || - lowerType == 'any') { + } else if (lowerType == 'object' || lowerType == 'dynamic' || lowerType == 'any') { return 'dynamic'; } else { return typeName; @@ -289,8 +256,7 @@ class YmlGeneratorConfig { //Maybe a generic final dartType = DartType(name); if (dartType.generics.isEmpty) { - throw Exception( - 'getPathForName is null: because `$name` was not added to the config file'); + throw Exception('getPathForName is null: because `$name` was not added to the config file'); } final paths = {}; for (final element in dartType.generics) { @@ -298,8 +264,7 @@ class YmlGeneratorConfig { } return paths; } else { - final baseDirectory = - foundModel.baseDirectory ?? pubspecConfig.baseDirectory; + final baseDirectory = foundModel.baseDirectory ?? pubspecConfig.baseDirectory; final path = foundModel.path; if (path == null) { return [baseDirectory]; @@ -353,26 +318,22 @@ class YmlGeneratorConfig { Model? getModelByName(ItemType itemType) { if (itemType is! ObjectType) return null; - final model = - models.firstWhereOrNull((model) => model.name == itemType.name); + final model = models.firstWhereOrNull((model) => model.name == itemType.name); if (model == null) { - throw Exception( - 'getModelByName is null: because `${itemType.name}` was not added to the config file'); + throw Exception('getModelByName is null: because `${itemType.name}` was not added to the config file'); } return model; } void checkTypesKnown(final Set names, String type) { if (!TypeChecker.isKnownDartType(type) && !names.contains(type)) { - throw Exception( - 'Could not generate all models. `$type` is not added to the config file, but is extended. These types are known: ${names.join(',')}'); + throw Exception('Could not generate all models. `$type` is not added to the config file, but is extended. These types are known: ${names.join(',')}'); } } ItemType _parseSimpleType(String type) { final listRegex = RegExp(r'^\s*[Ll]ist<\s*([a-zA-Z_0-9<>]*)\s*>\s*$'); - final mapRegex = - RegExp(r'^\s*[Mm]ap<([a-zA-Z_0-9<>]*)\s*,\s*([a-zA-Z_0-9<>]*)\s*>\s*$'); + final mapRegex = RegExp(r'^\s*[Mm]ap<([a-zA-Z_0-9<>]*)\s*,\s*([a-zA-Z_0-9<>]*)\s*>\s*$'); final lowerType = type.toLowerCase(); @@ -393,21 +354,17 @@ class YmlGeneratorConfig { return ArrayType(_makeGenericName(arrayType)); } else if (mapRegex.hasMatch(type)) { final match = mapRegex.firstMatch(type)!; - return MapType( - key: _makeGenericName(match.group(1)!), - valueName: _makeGenericName(match.group(2)!)); + return MapType(key: _makeGenericName(match.group(1)!), valueName: _makeGenericName(match.group(2)!)); } return ObjectType(type); } - YmlGeneratorConfig.merge(Iterable configs, String dirName) - : fileName = dirName { + YmlGeneratorConfig.merge(Iterable configs, String dirName) : fileName = dirName { final names = {}; for (final config in configs) { for (final model in config.models) { if (names.containsKey(model.name)) { - throw Exception( - 'Model with same name ${model.name} found in multiple files: ${names[model.name]!.fileName} and ${config.fileName}'); + throw Exception('Model with same name ${model.name} found in multiple files: ${names[model.name]!.fileName} and ${config.fileName}'); } names[model.name] = config; } diff --git a/lib/model/model/enum_model.dart b/lib/model/model/enum_model.dart index ebea3c1..1f9a099 100644 --- a/lib/model/model/enum_model.dart +++ b/lib/model/model/enum_model.dart @@ -1,23 +1,19 @@ +import 'package:model_generator/model/item_type/integer_type.dart'; import 'package:model_generator/model/item_type/item_type.dart'; -import 'package:model_generator/model/item_type/string_type.dart'; import 'package:model_generator/model/model/model.dart'; class EnumModel extends Model { final List? fields; - final ItemType itemType; - final bool generateMap; - final bool generateExtensions; + final String keyProperty; EnumModel({ required String name, + required this.keyProperty, String? path, String? baseDirectory, this.fields, - this.itemType = const StringType(), List? extraImports, List? extraAnnotations, - this.generateMap = false, - this.generateExtensions = false, String? description, }) : super( name: name, @@ -31,27 +27,34 @@ class EnumModel extends Model { class EnumField { final String name; + final List enumProperties; final String serializedName; - final String? value; - final String? description; EnumField._({ required this.name, required this.serializedName, - required this.value, - required this.description, + required this.enumProperties, }); factory EnumField({ required String name, + required List enumProperties, required String rawName, - String? value, - String? description, }) => EnumField._( name: name, + enumProperties: enumProperties, serializedName: rawName, - value: value, - description: description, ); } + +class EnumProperty { + final String value; + final String name; + final ItemType type; + + EnumProperty({ + required this.value, + required this.name, + }) : type = IntegerType(); +} diff --git a/lib/writer/enum_model_writer.dart b/lib/writer/enum_model_writer.dart index f90f2d2..62d42e4 100644 --- a/lib/writer/enum_model_writer.dart +++ b/lib/writer/enum_model_writer.dart @@ -2,6 +2,7 @@ import 'package:model_generator/model/item_type/double_type.dart'; import 'package:model_generator/model/item_type/string_type.dart'; import 'package:model_generator/model/model/enum_model.dart'; import 'package:model_generator/util/case_util.dart'; +import 'package:model_generator/util/list_extensions.dart'; import 'package:model_generator/writer/object_model_writer.dart'; class EnumModelWriter { @@ -22,20 +23,15 @@ class EnumModelWriter { } final jsonModelName = CaseUtil(jsonModel.name); - final itemTypeName = CaseUtil(jsonModel.itemType.name); sb.writeln('enum ${jsonModelName.pascalCase} {'); jsonModel.fields?.forEach((key) { - final jsonValue = key.value == null || key.value?.isEmpty == null - ? key.serializedName - : key.value; - final description = key.description; - if (description != null) { - sb.writeln(' ///$description'); - } - if (jsonModel.itemType is StringType) { + final keyProperty = key.enumProperties.firstWhereOrNull((element) => element.name.toLowerCase() == jsonModel.keyProperty); + final jsonValue = keyProperty?.value ?? key.serializedName; + + if (keyProperty?.type is StringType) { sb.writeln(' @JsonValue(\'$jsonValue\')'); - } else if (jsonModel.itemType is DoubleType && jsonValue != null) { + } else if (keyProperty?.type is DoubleType) { final doubleValue = double.tryParse(jsonValue); sb.writeln(' @JsonValue($doubleValue)'); } else { @@ -45,70 +41,6 @@ class EnumModelWriter { }); sb.writeln('}'); - if (jsonModel.generateMap) { - sb - ..writeln() - ..writeln('const ${jsonModelName.camelCase}Mapping = {'); - - jsonModel.fields?.forEach((key) { - final jsonValue = key.value == null || key.value?.isEmpty == null - ? key.serializedName - : key.value; - sb.write(' ${jsonModelName.pascalCase}.${key.name}: '); - if (jsonModel.itemType is StringType) { - sb.writeln('\'$jsonValue\','); - } else if (jsonModel.itemType is DoubleType && jsonValue != null) { - final doubleValue = double.tryParse(jsonValue); - sb.writeln('$doubleValue,'); - } else { - sb.writeln('$jsonValue,'); - } - }); - - sb - ..writeln('};') - ..writeln(); - - if (jsonModel.itemType is DoubleType) { - sb.writeln('final reverse${jsonModelName.pascalCase}Mapping = {'); - } else { - sb.writeln('const reverse${jsonModelName.pascalCase}Mapping = {'); - } - - jsonModel.fields?.forEach((key) { - final jsonValue = key.value == null || key.value?.isEmpty == null - ? key.serializedName - : key.value; - if (jsonModel.itemType is StringType) { - sb.write(' \'$jsonValue\': '); - } else if (jsonModel.itemType is DoubleType && jsonValue != null) { - final doubleValue = double.tryParse(jsonValue); - sb.write(' $doubleValue: '); - } else { - sb.write(' $jsonValue: '); - } - sb.writeln('${jsonModelName.pascalCase}.${key.name},'); - }); - - sb.writeln('};'); - - if (jsonModel.generateExtensions) { - sb - ..writeln() - ..writeln( - 'extension ${jsonModelName.pascalCase}Extension on ${jsonModelName.pascalCase} {') - ..writeln( - ' ${itemTypeName.originalText} get ${itemTypeName.camelCase}Value => ${jsonModelName.camelCase}Mapping[this]!;') - ..writeln('}') - ..writeln() - ..writeln( - 'extension ${jsonModelName.pascalCase}${itemTypeName.pascalCase}Extension on ${itemTypeName.originalText} {') - ..writeln( - ' ${jsonModelName.pascalCase}? get as${jsonModelName.pascalCase} => reverse${jsonModelName.pascalCase}Mapping[this];') - ..writeln('}'); - } - } - return sb.toString(); } } diff --git a/test/config/yml_generator_config/enum-normal.txt b/test/config/yml_generator_config/enum-normal.txt index d453524..f92c07b 100644 --- a/test/config/yml_generator_config/enum-normal.txt +++ b/test/config/yml_generator_config/enum-normal.txt @@ -6,19 +6,27 @@ User: Gender: type: enum + key_property: value properties: MALE: + value: male FEMALE: value: femAle other: + value: other X: + value: x Vehicles: type: enum + key_property: value uppercase_enums: false properties: male: + value: male female: value: femAle other: + value: other X: + value: x diff --git a/test/config/yml_generator_config_test.dart b/test/config/yml_generator_config_test.dart index fca5c3b..d9a204b 100644 --- a/test/config/yml_generator_config_test.dart +++ b/test/config/yml_generator_config_test.dart @@ -178,33 +178,44 @@ void main() { final enumModel2 = ymlConfig.models[2] as EnumModel; // ignore: avoid_as expect(enumModel.fields, isNotNull); expect(enumModel.fields!.length, 4); + expect(enumModel.keyProperty, 'value'); expect(enumModel.fields![0].name, 'MALE'); expect(enumModel.fields![0].serializedName, 'MALE'); - expect(enumModel.fields![0].value, null); + expect(enumModel.fields![0].enumProperties[0].value, 'male'); + expect(enumModel.fields![0].enumProperties[0].name, 'value'); expect(enumModel.fields![1].name, 'FEMALE'); expect(enumModel.fields![1].serializedName, 'FEMALE'); - expect(enumModel.fields![1].value, 'femAle'); + expect(enumModel.fields![1].enumProperties[0].value, 'femAle'); + expect(enumModel.fields![1].enumProperties[0].name, 'value'); expect(enumModel.fields![2].name, 'OTHER'); expect(enumModel.fields![2].serializedName, 'other'); - expect(enumModel.fields![2].value, null); + expect(enumModel.fields![2].enumProperties[0].value, 'other'); + expect(enumModel.fields![2].enumProperties[0].name, 'value'); expect(enumModel.fields![3].name, 'X'); expect(enumModel.fields![3].serializedName, 'X'); - expect(enumModel.fields![3].value, null); + expect(enumModel.fields![3].enumProperties[0].value, 'x'); + expect(enumModel.fields![3].enumProperties[0].name, 'value'); + expect(enumModel2.fields, isNotNull); expect(enumModel2.fields!.length, 4); + expect(enumModel.keyProperty, 'value'); expect(enumModel2.fields![0].name, 'male'); expect(enumModel2.fields![0].serializedName, 'male'); - expect(enumModel2.fields![0].value, null); + expect(enumModel.fields![0].enumProperties[0].value, 'male'); + expect(enumModel.fields![0].enumProperties[0].name, 'value'); expect(enumModel2.fields![1].name, 'female'); expect(enumModel2.fields![1].serializedName, 'female'); - expect(enumModel2.fields![1].value, 'femAle'); + expect(enumModel.fields![1].enumProperties[0].value, 'femAle'); + expect(enumModel.fields![1].enumProperties[0].name, 'value'); expect(enumModel2.fields![2].name, 'other'); expect(enumModel2.fields![2].serializedName, 'other'); - expect(enumModel2.fields![2].value, null); + expect(enumModel.fields![2].enumProperties[0].value, 'other'); + expect(enumModel.fields![2].enumProperties[0].name, 'value'); expect(enumModel2.fields![3].name, 'X'); expect(enumModel2.fields![3].serializedName, 'X'); - expect(enumModel2.fields![3].value, null); + expect(enumModel.fields![3].enumProperties[0].value, 'x'); + expect(enumModel.fields![3].enumProperties[0].name, 'value'); }); test('Error Enum no properties map', () { diff --git a/test/model/model/enum_model_test.dart b/test/model/model/enum_model_test.dart index 0ec0709..4ae4e98 100644 --- a/test/model/model/enum_model_test.dart +++ b/test/model/model/enum_model_test.dart @@ -6,6 +6,7 @@ void main() { group('Default', () { test('Normal EnumModel', () { final model = EnumModel( + keyProperty: 'keyProperty', name: 'MyEnumModel', path: 'path_to_my_model', baseDirectory: 'base_dir', @@ -18,6 +19,7 @@ void main() { group('Custom Path', () { test('Normal Custom Path', () { final model = EnumModel( + keyProperty: 'keyProperty', name: 'MyEnumModel', path: 'path_to_my_model/', baseDirectory: 'base_dir', @@ -29,6 +31,7 @@ void main() { test('Normal Custom Base Dir', () { final model = EnumModel( + keyProperty: 'keyProperty', name: 'MyEnumModel', path: 'path_to_my_model', baseDirectory: 'base_dir/', @@ -44,32 +47,53 @@ void main() { group('Default', () { test('Normal EnumField', () { final field = EnumField( - name: 'MY_ENUM_VALUE', - rawName: 'MY_ENUM_VALUE', - value: 'MY_ENUM_VALUE'); + name: 'MY_ENUM_VALUE', + rawName: 'MY_ENUM_VALUE', + enumProperties: [ + EnumProperty( + value: 'MY_ENUM_VALUE', + name: 'MY_ENUM_NAME', + ), + ], + ); expect(field.name, 'MY_ENUM_VALUE'); expect(field.serializedName, 'MY_ENUM_VALUE'); - expect(field.value, 'MY_ENUM_VALUE'); + expect(field.enumProperties.first.value, 'MY_ENUM_VALUE'); + expect(field.enumProperties.first.name, 'MY_ENUM_NAME'); }); test('Normal EnumField, lowercased', () { final field = EnumField( - name: 'my_enum_value', - rawName: 'MY_ENUM_VALUE', - value: 'MY_ENUM_VALUE'); + name: 'my_enum_value', + rawName: 'MY_ENUM_VALUE', + enumProperties: [ + EnumProperty( + value: 'MY_ENUM_VALUE', + name: 'MY_ENUM_NAME', + ), + ], + ); expect(field.name, 'my_enum_value'); expect(field.serializedName, 'MY_ENUM_VALUE'); - expect(field.value, 'MY_ENUM_VALUE'); + expect(field.enumProperties.first.value, 'MY_ENUM_VALUE'); + expect(field.enumProperties.first.name, 'MY_ENUM_NAME'); }); }); group('Custom serializedName', () { test('Normal Custom Base Dir', () { final field = EnumField( - name: 'MY_ENUM_VALUE', - rawName: 'my_enum_value', - value: 'my_enum_value'); + name: 'MY_ENUM_VALUE', + rawName: 'my_enum_value', + enumProperties: [ + EnumProperty( + value: 'MY_ENUM_VALUE', + name: 'MY_ENUM_NAME', + ), + ], + ); expect(field.name, 'MY_ENUM_VALUE'); expect(field.serializedName, 'my_enum_value'); - expect(field.value, 'my_enum_value'); + expect(field.enumProperties.first.value, 'MY_ENUM_VALUE'); + expect(field.enumProperties.first.name, 'MY_ENUM_NAME'); }); }); }); diff --git a/test/writer/enum_model_writer/custom-value-map-extension.txt b/test/writer/enum_model_writer/custom-value-map-extension/output.txt similarity index 100% rename from test/writer/enum_model_writer/custom-value-map-extension.txt rename to test/writer/enum_model_writer/custom-value-map-extension/output.txt diff --git a/test/writer/enum_model_writer/custom-value-map.txt b/test/writer/enum_model_writer/custom-value-map/output.txt similarity index 100% rename from test/writer/enum_model_writer/custom-value-map.txt rename to test/writer/enum_model_writer/custom-value-map/output.txt diff --git a/test/writer/enum_model_writer/custom-value.txt b/test/writer/enum_model_writer/custom-value/output.txt similarity index 100% rename from test/writer/enum_model_writer/custom-value.txt rename to test/writer/enum_model_writer/custom-value/output.txt diff --git a/test/writer/enum_model_writer/no-fields.txt b/test/writer/enum_model_writer/no-fields/output.txt similarity index 100% rename from test/writer/enum_model_writer/no-fields.txt rename to test/writer/enum_model_writer/no-fields/output.txt diff --git a/test/writer/enum_model_writer/normal-description.txt b/test/writer/enum_model_writer/normal-description/output.txt similarity index 100% rename from test/writer/enum_model_writer/normal-description.txt rename to test/writer/enum_model_writer/normal-description/output.txt diff --git a/test/writer/enum_model_writer/normal.txt b/test/writer/enum_model_writer/normal/output.txt similarity index 100% rename from test/writer/enum_model_writer/normal.txt rename to test/writer/enum_model_writer/normal/output.txt diff --git a/test/writer/enum_model_writer/normal_with_double_type.txt b/test/writer/enum_model_writer/normal_with_double_type/output.txt similarity index 100% rename from test/writer/enum_model_writer/normal_with_double_type.txt rename to test/writer/enum_model_writer/normal_with_double_type/output.txt diff --git a/test/writer/enum_model_writer/normal_with_double_type_map.txt b/test/writer/enum_model_writer/normal_with_double_type_map/output.txt similarity index 100% rename from test/writer/enum_model_writer/normal_with_double_type_map.txt rename to test/writer/enum_model_writer/normal_with_double_type_map/output.txt diff --git a/test/writer/enum_model_writer/normal_with_double_type_map_extension.txt b/test/writer/enum_model_writer/normal_with_double_type_map_extension/output.txt similarity index 100% rename from test/writer/enum_model_writer/normal_with_double_type_map_extension.txt rename to test/writer/enum_model_writer/normal_with_double_type_map_extension/output.txt diff --git a/test/writer/enum_model_writer/normal_with_int_type.txt b/test/writer/enum_model_writer/normal_with_int_type/output.txt similarity index 100% rename from test/writer/enum_model_writer/normal_with_int_type.txt rename to test/writer/enum_model_writer/normal_with_int_type/output.txt diff --git a/test/writer/enum_model_writer/normal_with_int_type_map.txt b/test/writer/enum_model_writer/normal_with_int_type_map/output.txt similarity index 100% rename from test/writer/enum_model_writer/normal_with_int_type_map.txt rename to test/writer/enum_model_writer/normal_with_int_type_map/output.txt diff --git a/test/writer/enum_model_writer/normal_with_int_type_map_extension.txt b/test/writer/enum_model_writer/normal_with_int_type_map_extension/output.txt similarity index 100% rename from test/writer/enum_model_writer/normal_with_int_type_map_extension.txt rename to test/writer/enum_model_writer/normal_with_int_type_map_extension/output.txt diff --git a/test/writer/enum_model_writer/null-value.txt b/test/writer/enum_model_writer/null-value/output.txt similarity index 100% rename from test/writer/enum_model_writer/null-value.txt rename to test/writer/enum_model_writer/null-value/output.txt diff --git a/test/writer/enum_model_writer_test.dart b/test/writer/enum_model_writer_test.dart index 98044bc..48e5569 100644 --- a/test/writer/enum_model_writer_test.dart +++ b/test/writer/enum_model_writer_test.dart @@ -1,296 +1,5 @@ -import 'package:model_generator/model/item_type/double_type.dart'; -import 'package:model_generator/model/item_type/integer_type.dart'; import 'package:test/test.dart'; -import 'package:model_generator/model/model/enum_model.dart'; - -import 'writer_test_helper.dart'; void main() { - group('EnumModel', () { - test('Normal EnumModel', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: 'MY_VALUE_1', - description: 'A good description of this field', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: 'MY_VALUE_2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter(model, 'normal'); - }); - - test('Normal EnumModel with int type', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - itemType: IntegerType(), - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: '1', - description: 'A good description of this field', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: '2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter(model, 'normal_with_int_type'); - }); - - test('Normal EnumModel with int type generate map', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - itemType: IntegerType(), - generateMap: true, - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: '1', - description: 'A good description of this field', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: '2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter(model, 'normal_with_int_type_map'); - }); - - test('Normal EnumModel with int type generate map extension', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - itemType: IntegerType(), - generateMap: true, - generateExtensions: true, - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: '1', - description: 'A good description of this field', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: '2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter( - model, 'normal_with_int_type_map_extension'); - }); - - test('Normal EnumModel with double type', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - itemType: DoubleType(), - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: '1.0', - description: 'A good description of this field', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: '2.2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter(model, 'normal_with_double_type'); - }); - - test('Normal EnumModel with double type generate map', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - itemType: DoubleType(), - generateMap: true, - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: '1.0', - description: 'A good description of this field', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: '2.2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter( - model, 'normal_with_double_type_map'); - }); - - test('Normal EnumModel with double type generate map extension', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - itemType: DoubleType(), - generateMap: true, - generateExtensions: true, - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: '1.0', - description: 'A good description of this field', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: '2.2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter( - model, 'normal_with_double_type_map_extension'); - }); - - test('Normal EnumModel custom value', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: 'MY_VALUE_1', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: 'custom_value_2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter(model, 'custom-value'); - }); - - test('Enum model with null enumfield.value ', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: 'MY_VALUE_1', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter(model, 'null-value'); - }); - - test('EnumModel with no fields ', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - fields: [], - ); - WriterTestHelper.testEnumModelWriter(model, 'no-fields'); - }); - - test('Custom EnumModel generate map', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - generateMap: true, - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: 'customValue', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter(model, 'custom-value-map'); - }); - - test('Custom EnumModel generate map extension', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - generateMap: true, - generateExtensions: true, - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: 'customValue', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - ), - ], - ); - WriterTestHelper.testEnumModelWriter(model, 'custom-value-map-extension'); - }); - - test('Normal EnumModel with description', () { - final model = EnumModel( - name: 'MyEnumModel', - path: 'path_to_my_model', - baseDirectory: 'base_dir', - fields: [ - EnumField( - name: 'MY_VALUE_1', - rawName: 'MY_VALUE_1', - value: 'MY_VALUE_1', - description: 'A good description of this field', - ), - EnumField( - name: 'MY_VALUE_2', - rawName: 'MY_VALUE_2', - value: 'MY_VALUE_2', - ), - ], - description: 'A good description of this enum', - ); - WriterTestHelper.testEnumModelWriter(model, 'normal-description'); - }); - }); + group('EnumModel', () {}); } diff --git a/test/writer/writer_test_helper.dart b/test/writer/writer_test_helper.dart deleted file mode 100644 index 55fb148..0000000 --- a/test/writer/writer_test_helper.dart +++ /dev/null @@ -1,16 +0,0 @@ -import 'dart:io'; - -import 'package:test/test.dart'; -import 'package:model_generator/model/model/enum_model.dart'; -import 'package:model_generator/writer/enum_model_writer.dart'; - -class WriterTestHelper { - static testEnumModelWriter(EnumModel model, String resultFileName) { - print(Directory.current); - final file = File('test/writer/enum_model_writer/$resultFileName.txt'); - final output = file.readAsStringSync(); - final actual = EnumModelWriter(model).write(); - // print(actual); - expect(actual, output); - } -}