|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:path/path.dart' as p; |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +import 'package:openapi_spec/openapi_spec.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + final tmp = Directory( |
| 9 | + p.join('test', 'tmp', 'unions'), |
| 10 | + ); |
| 11 | + |
| 12 | + final truthJson = p.join('test', 'unions', 'unions.json'); |
| 13 | + final testJson = p.join(tmp.path, 'unions.json'); |
| 14 | + |
| 15 | + final genSchemaDir = p.join(tmp.path, 'gen_schema'); |
| 16 | + final genSchemaOptionsDir = p.join(tmp.path, 'gen_schema_options'); |
| 17 | + final genSchemaSingleDir = p.join(tmp.path, 'gen_schema_single'); |
| 18 | + |
| 19 | + late OpenApi spec; |
| 20 | + |
| 21 | + group('Unions', () { |
| 22 | + setUp(() { |
| 23 | + spec = OpenApi.fromFile(source: truthJson); |
| 24 | + if (!tmp.existsSync()) { |
| 25 | + tmp.createSync(recursive: true); |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + /// Test JSON to Dart [OpenApi] object parsing |
| 30 | + test('JSON -> Dart', () { |
| 31 | + // Ensure generated file can be read back into Dart |
| 32 | + final spec = OpenApi.fromFile( |
| 33 | + source: truthJson, |
| 34 | + ); |
| 35 | + |
| 36 | + // Write the Dart representation back to JSON |
| 37 | + spec.toJsonFile(destination: testJson); |
| 38 | + }); |
| 39 | + |
| 40 | + /// Test schema generation (multi-file) |
| 41 | + test('Generate Schema Code', () async { |
| 42 | + await spec.generate( |
| 43 | + package: 'unions', |
| 44 | + quiet: true, |
| 45 | + destination: genSchemaDir, |
| 46 | + ); |
| 47 | + final expectedFiles = [ |
| 48 | + 'alpha_training.dart', |
| 49 | + 'beta_training.dart', |
| 50 | + 'alpha_transform.dart', |
| 51 | + 'beta_transform.dart', |
| 52 | + 'gamma_transform.dart', |
| 53 | + 'group_transform.dart', |
| 54 | + 'schema.dart', |
| 55 | + 'training_setup.dart', |
| 56 | + 'union_training.dart', |
| 57 | + 'union_transform.dart', |
| 58 | + 'union_transform2.dart', |
| 59 | + ]; |
| 60 | + final genFiles = Directory(p.join(genSchemaDir, 'schema')) |
| 61 | + .listSync() |
| 62 | + .map((e) => p.basename(e.path)); |
| 63 | + for (final file in expectedFiles) { |
| 64 | + expect(genFiles, contains(file)); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + /// Test schema generation (single file) |
| 69 | + test('Generate Schema Code', () async { |
| 70 | + await spec.generate( |
| 71 | + package: 'unions', |
| 72 | + quiet: true, |
| 73 | + destination: genSchemaSingleDir, |
| 74 | + schemaOptions: SchemaGeneratorOptions( |
| 75 | + singleFile: true, |
| 76 | + ), |
| 77 | + ); |
| 78 | + final genFiles = |
| 79 | + Directory(p.join(genSchemaSingleDir, 'schema')).listSync(); |
| 80 | + expect(genFiles.length, 1); |
| 81 | + }); |
| 82 | + |
| 83 | + /// Test schema generation with union customizations |
| 84 | + test('Generate Schema Code (Options)', () async { |
| 85 | + await spec.generate( |
| 86 | + package: 'unions', |
| 87 | + quiet: true, |
| 88 | + destination: genSchemaOptionsDir, |
| 89 | + schemaOptions: SchemaGeneratorOptions( |
| 90 | + onSchemaUnionName: (name, unions) { |
| 91 | + if (name == 'UnionTransform' && unions.length == 3) { |
| 92 | + return 'UnionTransformMember'; |
| 93 | + } |
| 94 | + return name; |
| 95 | + }, |
| 96 | + ), |
| 97 | + ); |
| 98 | + final expectedFiles = [ |
| 99 | + 'alpha_training.dart', |
| 100 | + 'beta_training.dart', |
| 101 | + 'alpha_transform.dart', |
| 102 | + 'beta_transform.dart', |
| 103 | + 'gamma_transform.dart', |
| 104 | + 'group_transform.dart', |
| 105 | + 'schema.dart', |
| 106 | + 'training_setup.dart', |
| 107 | + 'union_training.dart', |
| 108 | + 'union_transform.dart', |
| 109 | + // This file should be renamed from default "union_transform2.dart" |
| 110 | + 'union_transform_member.dart', |
| 111 | + ]; |
| 112 | + final genFiles = Directory(p.join(genSchemaOptionsDir, 'schema')) |
| 113 | + .listSync() |
| 114 | + .map((e) => p.basename(e.path)); |
| 115 | + for (final file in expectedFiles) { |
| 116 | + expect(genFiles, contains(file)); |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
| 120 | +} |
0 commit comments