| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| import 'dart:convert'; |
| |
| import 'package:dart_model/dart_model.dart'; |
| import 'package:test/test.dart'; |
| |
| void main() { |
| group(Model, () { |
| final model = Model(uris: { |
| 'package:dart_model/dart_model.dart': Library(scopes: { |
| 'JsonData': Interface( |
| properties: Properties(isClass: true), |
| metadataAnnotations: [ |
| MetadataAnnotation( |
| type: QualifiedName( |
| 'package:dart_model/dart_model.dart#SomeAnnotation')) |
| ], |
| members: { |
| '_root': Member( |
| properties: Properties(isField: true), |
| ) |
| }) |
| }) |
| }); |
| |
| final expected = { |
| 'uris': { |
| 'package:dart_model/dart_model.dart': { |
| 'scopes': { |
| 'JsonData': { |
| 'metadataAnnotations': [ |
| {'type': 'package:dart_model/dart_model.dart#SomeAnnotation'} |
| ], |
| 'members': { |
| '_root': { |
| 'properties': {'isField': true} |
| } |
| }, |
| 'properties': {'isClass': true} |
| } |
| } |
| } |
| } |
| }; |
| |
| test('maps to JSON', () { |
| expect(model as Map, expected); |
| }); |
| |
| test('maps to JSON after deserialization', () { |
| final deserializedModel = Model.fromJson( |
| json.decode(json.encode(model as Map)) as Map<String, Object?>); |
| expect(deserializedModel as Map, expected); |
| }); |
| |
| test('maps can be accessed as fields', () { |
| expect(model.uris['package:dart_model/dart_model.dart'], |
| expected['uris']!['package:dart_model/dart_model.dart']); |
| expect( |
| model.uris['package:dart_model/dart_model.dart']!.scopes['JsonData'], |
| expected['uris']!['package:dart_model/dart_model.dart']!['scopes']![ |
| 'JsonData']); |
| expect( |
| model.uris['package:dart_model/dart_model.dart']!.scopes['JsonData']! |
| .properties, |
| expected['uris']!['package:dart_model/dart_model.dart']!['scopes']![ |
| 'JsonData']!['properties']); |
| }); |
| |
| test('maps can be accessed as fields after deserialization', () { |
| final deserializedModel = Model.fromJson( |
| json.decode(json.encode(model as Map)) as Map<String, Object?>); |
| |
| expect( |
| deserializedModel.uris['package:dart_model/dart_model.dart']! |
| .scopes['JsonData']!.properties, |
| expected['uris']!['package:dart_model/dart_model.dart']!['scopes']![ |
| 'JsonData']!['properties']); |
| }); |
| |
| test('lists can be accessed as fields', () { |
| expect( |
| model.uris['package:dart_model/dart_model.dart']!.scopes['JsonData']! |
| .members, |
| expected['uris']!['package:dart_model/dart_model.dart']!['scopes']![ |
| 'JsonData']!['members']); |
| }); |
| |
| test('lists can be accessed as fields after deserialization', () { |
| final deserializedModel = Model.fromJson( |
| json.decode(json.encode(model as Map)) as Map<String, Object?>); |
| |
| expect( |
| deserializedModel.uris['package:dart_model/dart_model.dart']! |
| .scopes['JsonData']!.members, |
| expected['uris']!['package:dart_model/dart_model.dart']!['scopes']![ |
| 'JsonData']!['members']); |
| }); |
| }); |
| } |