Skip to content

Commit

Permalink
#130: fixed normal enum test
Browse files Browse the repository at this point in the history
  • Loading branch information
jorre127 committed Sep 4, 2023
1 parent 07ae502 commit f17dbdd
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/model/model/enum_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 {
Expand Down Expand Up @@ -56,5 +57,5 @@ class EnumProperty {
EnumProperty({
required this.value,
required this.name,
}) : type = IntegerType();
}) : type = StringType();
}
5 changes: 3 additions & 2 deletions lib/writer/enum_model_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ class EnumModelWriter {
jsonModel.fields?.forEach((key) {
final keyProperty = key.enumProperties.firstWhereOrNull((element) => element.name.toLowerCase() == jsonModel.keyProperty);
final jsonValue = keyProperty?.value ?? key.serializedName;
final propertyType = keyProperty?.type;

if (keyProperty?.type is StringType) {
if (propertyType is StringType || propertyType == null) {
sb.writeln(' @JsonValue(\'$jsonValue\')');
} else if (keyProperty?.type is DoubleType) {
} else if (propertyType is DoubleType) {
final doubleValue = double.tryParse(jsonValue);
sb.writeln(' @JsonValue($doubleValue)');
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/writer/enum_model_writer/normal/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MyEnumModel:
path: test/enum/
type: enum
properties:
MY_VALUE_1:
value: '1'
MY_VALUE_2:
value: '2'
1 change: 0 additions & 1 deletion test/writer/enum_model_writer/normal/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import 'package:json_annotation/json_annotation.dart';

enum MyEnumModel {
///A good description of this field
@JsonValue('MY_VALUE_1')
MY_VALUE_1,
@JsonValue('MY_VALUE_2')
Expand Down
2 changes: 1 addition & 1 deletion test/writer/enum_model_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'writer_helper.dart';

void main() {
void testEnumModelWriter(String path) {
final result = WriterHelper.prepareYmlConfig(
final result = WriterHelper.prepareWriterTest(
path: path,
pubspecPath: 'test/writer/enum_model_writer',
);
Expand Down
2 changes: 1 addition & 1 deletion test/writer/object_model_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'writer_helper.dart';

void main() {
void testObjectModelWriter(String path) {
final result = WriterHelper.prepareYmlConfig(path: path);
final result = WriterHelper.prepareWriterTest(path: path);
final jsonModel = result.config.models.first;
if (jsonModel is! ObjectModel) {
throw Exception('The first model in the config file must be an object model and will be validated. The model is ${jsonModel.runtimeType}');
Expand Down
2 changes: 1 addition & 1 deletion test/writer/writer_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WriterHelper {
String expected,
YmlGeneratorConfig config,
PubspecConfig pubspecConfig,
}) prepareYmlConfig({
}) prepareWriterTest({
required String path,
String? pubspecPath,
}) {
Expand Down

0 comments on commit f17dbdd

Please sign in to comment.