| // Copyright (c) 2026, 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 'package:record_use/record_use.dart'; |
| import 'package:test/test.dart'; |
| |
| void main() { |
| const classDefinition = Class( |
| 'MyClass', |
| Library('package:test/test.dart'), |
| ); |
| |
| test('MapConstant with InstanceConstant keys round-trip', () { |
| const instanceKey = InstanceConstant( |
| definition: classDefinition, |
| fields: { |
| 'id': IntConstant(1), |
| 'tag': StringConstant('key'), |
| }, |
| ); |
| |
| const mapConstant = MapConstant([ |
| MapEntry(instanceKey, StringConstant('value')), |
| ]); |
| |
| const definition = Method( |
| 'testMethod', |
| Library('package:test/test.dart'), |
| ); |
| |
| final recordings = Recordings( |
| calls: { |
| definition: [ |
| const CallWithArguments( |
| positionalArguments: [mapConstant], |
| namedArguments: {}, |
| loadingUnit: LoadingUnit('main.js'), |
| ), |
| ], |
| }, |
| instances: {}, |
| ); |
| |
| final json = recordings.toJson(); |
| final backAgain = Recordings.fromJson(json); |
| |
| expect(backAgain, recordings); |
| }); |
| |
| test('MapConstant equality with InstanceConstant keys', () { |
| const instanceKey = InstanceConstant( |
| definition: classDefinition, |
| fields: { |
| 'id': IntConstant(1), |
| 'tag': StringConstant('key'), |
| }, |
| ); |
| |
| const mapConstant = MapConstant([ |
| MapEntry(instanceKey, StringConstant('value')), |
| ]); |
| |
| expect( |
| mapConstant.entries.first.key, |
| const InstanceConstant( |
| definition: classDefinition, |
| fields: { |
| 'id': IntConstant(1), |
| 'tag': StringConstant('key'), |
| }, |
| ), |
| ); |
| expect(mapConstant.entries.first.value, const StringConstant('value')); |
| }); |
| |
| test('Deeply nested MapConstant with complex keys round-trip', () { |
| const listKey = ListConstant([IntConstant(1), IntConstant(2)]); |
| const mapKey = MapConstant([ |
| MapEntry(StringConstant('inner'), IntConstant(3)), |
| ]); |
| const recordKey = RecordConstant( |
| positional: [IntConstant(4)], |
| named: {'a': StringConstant('b')}, |
| ); |
| |
| const complexMap = MapConstant([ |
| MapEntry(listKey, mapKey), |
| MapEntry(mapKey, listKey), |
| MapEntry(recordKey, IntConstant(5)), |
| ]); |
| |
| const definition = Method( |
| 'complexMethod', |
| Library('package:test/test.dart'), |
| ); |
| |
| final recordings = Recordings( |
| calls: { |
| definition: [ |
| const CallWithArguments( |
| positionalArguments: [complexMap], |
| namedArguments: {}, |
| loadingUnit: LoadingUnit('main.js'), |
| ), |
| ], |
| }, |
| instances: {}, |
| ); |
| |
| final json = recordings.toJson(); |
| final backAgain = Recordings.fromJson(json); |
| |
| expect(backAgain, recordings); |
| }); |
| |
| test('Deeply nested complex keys structure', () { |
| const listKey = ListConstant([IntConstant(1), IntConstant(2)]); |
| const mapKey = MapConstant([ |
| MapEntry(StringConstant('inner'), IntConstant(3)), |
| ]); |
| const recordKey = RecordConstant( |
| positional: [IntConstant(4)], |
| named: {'a': StringConstant('b')}, |
| ); |
| |
| const complexMap = MapConstant([ |
| MapEntry(listKey, mapKey), |
| MapEntry(mapKey, listKey), |
| MapEntry(recordKey, IntConstant(5)), |
| ]); |
| |
| expect(complexMap.entries, hasLength(3)); |
| final entries = complexMap.entries; |
| expect( |
| entries[0].key, |
| const ListConstant([IntConstant(1), IntConstant(2)]), |
| ); |
| expect( |
| entries[0].value, |
| const MapConstant([ |
| MapEntry(StringConstant('inner'), IntConstant(3)), |
| ]), |
| ); |
| expect( |
| entries[1].key, |
| const MapConstant([ |
| MapEntry(StringConstant('inner'), IntConstant(3)), |
| ]), |
| ); |
| expect( |
| entries[1].value, |
| const ListConstant([IntConstant(1), IntConstant(2)]), |
| ); |
| expect( |
| entries[2].key, |
| const RecordConstant( |
| positional: [IntConstant(4)], |
| named: {'a': StringConstant('b')}, |
| ), |
| ); |
| expect(entries[2].value, const IntConstant(5)); |
| }); |
| } |