|
| 1 | +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:pub_semver/pub_semver.dart'; |
| 6 | +import 'package:record_use/record_use_internal.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + test('MapConstant with InstanceConstant keys round-trip', () { |
| 11 | + const instanceKey = InstanceConstant( |
| 12 | + fields: { |
| 13 | + 'id': IntConstant(1), |
| 14 | + 'tag': StringConstant('key'), |
| 15 | + }, |
| 16 | + ); |
| 17 | + |
| 18 | + const mapConstant = MapConstant([ |
| 19 | + MapEntry(instanceKey, StringConstant('value')), |
| 20 | + ]); |
| 21 | + |
| 22 | + const definition = Definition( |
| 23 | + identifier: Identifier( |
| 24 | + importUri: 'package:test/test.dart', |
| 25 | + name: 'testMethod', |
| 26 | + ), |
| 27 | + ); |
| 28 | + |
| 29 | + final recordings = Recordings( |
| 30 | + metadata: Metadata( |
| 31 | + version: Version(1, 0, 0), |
| 32 | + comment: 'Test complex keys', |
| 33 | + ), |
| 34 | + callsForDefinition: { |
| 35 | + definition: [ |
| 36 | + const CallWithArguments( |
| 37 | + positionalArguments: [mapConstant], |
| 38 | + namedArguments: {}, |
| 39 | + loadingUnit: 'main.js', |
| 40 | + location: Location(uri: 'main.dart', line: 1, column: 1), |
| 41 | + ), |
| 42 | + ], |
| 43 | + }, |
| 44 | + instancesForDefinition: {}, |
| 45 | + ); |
| 46 | + |
| 47 | + final json = recordings.toJson(); |
| 48 | + final backAgain = Recordings.fromJson(json); |
| 49 | + |
| 50 | + expect(backAgain, recordings); |
| 51 | + }); |
| 52 | + |
| 53 | + test('toValue() with InstanceConstant keys', () { |
| 54 | + const instanceKey = InstanceConstant( |
| 55 | + fields: { |
| 56 | + 'id': IntConstant(1), |
| 57 | + 'tag': StringConstant('key'), |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + const mapConstant = MapConstant([ |
| 62 | + MapEntry(instanceKey, StringConstant('value')), |
| 63 | + ]); |
| 64 | + |
| 65 | + final mapValue = mapConstant.toValue() as Map; |
| 66 | + |
| 67 | + expect(mapValue.keys.first, { |
| 68 | + 'id': 1, |
| 69 | + 'tag': 'key', |
| 70 | + }); |
| 71 | + expect(mapValue.values.first, 'value'); |
| 72 | + }); |
| 73 | + |
| 74 | + test('Deeply nested MapConstant with complex keys round-trip', () { |
| 75 | + const listKey = ListConstant([IntConstant(1), IntConstant(2)]); |
| 76 | + const mapKey = MapConstant([ |
| 77 | + MapEntry(StringConstant('inner'), IntConstant(3)), |
| 78 | + ]); |
| 79 | + |
| 80 | + const complexMap = MapConstant([ |
| 81 | + MapEntry(listKey, mapKey), |
| 82 | + MapEntry(mapKey, listKey), |
| 83 | + ]); |
| 84 | + |
| 85 | + const definition = Definition( |
| 86 | + identifier: Identifier( |
| 87 | + importUri: 'package:test/test.dart', |
| 88 | + name: 'complexMethod', |
| 89 | + ), |
| 90 | + ); |
| 91 | + |
| 92 | + final recordings = Recordings( |
| 93 | + metadata: Metadata( |
| 94 | + version: Version(1, 0, 0), |
| 95 | + comment: 'Test deeply nested complex keys', |
| 96 | + ), |
| 97 | + callsForDefinition: { |
| 98 | + definition: [ |
| 99 | + const CallWithArguments( |
| 100 | + positionalArguments: [complexMap], |
| 101 | + namedArguments: {}, |
| 102 | + loadingUnit: 'main.js', |
| 103 | + location: Location(uri: 'main.dart', line: 1, column: 1), |
| 104 | + ), |
| 105 | + ], |
| 106 | + }, |
| 107 | + instancesForDefinition: {}, |
| 108 | + ); |
| 109 | + |
| 110 | + final json = recordings.toJson(); |
| 111 | + final backAgain = Recordings.fromJson(json); |
| 112 | + |
| 113 | + expect(backAgain, recordings); |
| 114 | + }); |
| 115 | + |
| 116 | + test('toValue() with deeply nested complex keys', () { |
| 117 | + const listKey = ListConstant([IntConstant(1), IntConstant(2)]); |
| 118 | + const mapKey = MapConstant([ |
| 119 | + MapEntry(StringConstant('inner'), IntConstant(3)), |
| 120 | + ]); |
| 121 | + |
| 122 | + const complexMap = MapConstant([ |
| 123 | + MapEntry(listKey, mapKey), |
| 124 | + MapEntry(mapKey, listKey), |
| 125 | + ]); |
| 126 | + |
| 127 | + final mapValue = complexMap.toValue() as Map; |
| 128 | + expect(mapValue.length, 2); |
| 129 | + final entries = mapValue.entries.toList(); |
| 130 | + expect(entries[0].key, [1, 2]); |
| 131 | + expect(entries[0].value, {'inner': 3}); |
| 132 | + expect(entries[1].key, {'inner': 3}); |
| 133 | + expect(entries[1].value, [1, 2]); |
| 134 | + }); |
| 135 | +} |
0 commit comments