|
| 1 | +// Copyright (c) 2025, 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:dwds/src/services/expression_compiler.dart' show ModuleFormat; |
| 6 | +import 'package:path/path.dart' show basename; |
| 7 | +import 'package:test/test.dart'; |
| 8 | +import 'package:test_common/logging.dart'; |
| 9 | +import 'package:test_common/test_sdk_configuration.dart'; |
| 10 | +import 'package:vm_service/vm_service.dart'; |
| 11 | + |
| 12 | +import '../../fixtures/context.dart'; |
| 13 | +import '../../fixtures/project.dart'; |
| 14 | +import '../../fixtures/utilities.dart'; |
| 15 | +import 'test_inspector.dart'; |
| 16 | + |
| 17 | +void runTests({ |
| 18 | + required TestSdkConfigurationProvider provider, |
| 19 | + required CompilationMode compilationMode, |
| 20 | + required bool canaryFeatures, |
| 21 | + required bool debug, |
| 22 | +}) { |
| 23 | + final context = TestContext(TestProject.testExperiment, provider); |
| 24 | + final testInspector = TestInspector(context); |
| 25 | + |
| 26 | + late VmService service; |
| 27 | + late Stream<Event> stream; |
| 28 | + late String isolateId; |
| 29 | + late ScriptRef mainScript; |
| 30 | + |
| 31 | + Future<void> onBreakpoint( |
| 32 | + String breakPointId, |
| 33 | + Future<void> Function(Event) body, |
| 34 | + ) => testInspector.onBreakPoint( |
| 35 | + stream, |
| 36 | + isolateId, |
| 37 | + mainScript, |
| 38 | + breakPointId, |
| 39 | + body, |
| 40 | + ); |
| 41 | + |
| 42 | + Future<InstanceRef> getInstanceRef(frame, expression) => |
| 43 | + testInspector.getInstanceRef(isolateId, frame, expression); |
| 44 | + |
| 45 | + group('$compilationMode | dot shorthands:', () { |
| 46 | + setUp(() async { |
| 47 | + setCurrentLogWriter(debug: debug); |
| 48 | + await context.setUp( |
| 49 | + testSettings: TestSettings( |
| 50 | + compilationMode: compilationMode, |
| 51 | + enableExpressionEvaluation: true, |
| 52 | + verboseCompiler: debug, |
| 53 | + experiments: ['dot-shorthands'], |
| 54 | + canaryFeatures: canaryFeatures, |
| 55 | + moduleFormat: provider.ddcModuleFormat, |
| 56 | + ), |
| 57 | + ); |
| 58 | + service = context.debugConnection.vmService; |
| 59 | + |
| 60 | + final vm = await service.getVM(); |
| 61 | + isolateId = vm.isolates!.first.id!; |
| 62 | + final scripts = await service.getScripts(isolateId); |
| 63 | + |
| 64 | + await service.streamListen(EventStreams.kDebug); |
| 65 | + stream = service.onDebugEvent; |
| 66 | + |
| 67 | + mainScript = scripts.scripts!.firstWhere( |
| 68 | + (each) => each.uri!.contains('main.dart'), |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + tearDown(() async { |
| 73 | + await context.tearDown(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('expression evaluation and single-stepping', () async { |
| 77 | + await onBreakpoint('testDotShorthands', (event) async { |
| 78 | + final frame = event.topFrame!.index!; |
| 79 | + |
| 80 | + var instanceRef = await getInstanceRef(frame, '(c = .two).value'); |
| 81 | + expect(instanceRef.valueAsString, '2'); |
| 82 | + |
| 83 | + instanceRef = await getInstanceRef(frame, '(c = .three).value'); |
| 84 | + expect(instanceRef.valueAsString, '3'); |
| 85 | + |
| 86 | + instanceRef = await getInstanceRef(frame, '(c = .four()).value'); |
| 87 | + expect(instanceRef.valueAsString, '4'); |
| 88 | + |
| 89 | + final scriptBasename = basename(mainScript.uri!); |
| 90 | + |
| 91 | + const lineA = 116; |
| 92 | + const lineB = 118; |
| 93 | + const lineC = 119; |
| 94 | + const lineD = 120; |
| 95 | + const lineE = 127; |
| 96 | + const lineF = 129; |
| 97 | + const lineG = 131; |
| 98 | + const lineH = 132; |
| 99 | + |
| 100 | + final expected = [ |
| 101 | + '$scriptBasename:$lineE:3', // on 'c' |
| 102 | + // TODO(2638): Investigate why this conditional exclusion is needed. |
| 103 | + if (provider.ddcModuleFormat == ModuleFormat.ddc) |
| 104 | + '$scriptBasename:$lineB:20', // on '2' |
| 105 | + '$scriptBasename:$lineF:3', // on 'c' |
| 106 | + '$scriptBasename:$lineC:25', // on 'C' |
| 107 | + '$scriptBasename:$lineA:10', // on 'v' of 'value' |
| 108 | + '$scriptBasename:$lineA:16', // on ';' |
| 109 | + '$scriptBasename:$lineC:27', // on '3' |
| 110 | + '$scriptBasename:$lineG:3', // on 'c' |
| 111 | + '$scriptBasename:$lineD:22', // on 'C' |
| 112 | + '$scriptBasename:$lineA:10', // on 'v' of 'value' |
| 113 | + '$scriptBasename:$lineA:16', // on ';' |
| 114 | + '$scriptBasename:$lineD:24', // on '4' |
| 115 | + '$scriptBasename:$lineH:3', // on 'p' of 'print' |
| 116 | + ]; |
| 117 | + |
| 118 | + final stops = <String>[]; |
| 119 | + await testInspector.runStepIntoThroughProgramRecordingStops( |
| 120 | + isolateId, |
| 121 | + stops, |
| 122 | + provider.ddcModuleFormat == ModuleFormat.ddc ? 13 : 12, |
| 123 | + ); |
| 124 | + |
| 125 | + expect(stops, expected); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | +} |
0 commit comments