Skip to content

Commit

Permalink
[ package:vm_service ] Deduplicate expression evaluation testing helper
Browse files Browse the repository at this point in the history
Change-Id: I371ef13859bf73a44e89ffbf0f5f8c042a112e0b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405362
Reviewed-by: Derek Xu <[email protected]>
Commit-Queue: Jens Johansen <[email protected]>
Reviewed-by: Ben Konyi <[email protected]>
  • Loading branch information
jensjoha authored and Commit Queue committed Jan 24, 2025
1 parent 60d98b8 commit 663c55b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 122 deletions.
38 changes: 38 additions & 0 deletions pkg/vm_service/test/common/service_test_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -889,3 +889,41 @@ IsolateTest expectUnhandledExceptionWithFrames({
}
};
}

/// This helper does 3 things:
/// 1) makes sure it's at the expected frame ([topFrameName]).
/// 2) checks that the expected variables are available (by name)
/// ([availableVariables]).
/// 3) Evaluates the given expression(s) and checks their (valueAsString) result
/// ([evaluations]).
IsolateTest testExpressionEvaluationAndAvailableVariables(
String topFrameName,
List<String> availableVariables,
List<(String evaluate, String evaluationResult)> evaluations,
) {
return (VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
final stack = await service.getStack(isolateId);

// Make sure we are in the right place.
expect(stack.frames!.length, greaterThanOrEqualTo(1));
expect(stack.frames![0].function!.name, topFrameName);

// Check variables.
expect(
(stack.frames![0].vars ?? []).map((v) => v.name).toList(),
equals(availableVariables),
);

// Evaluate.
for (final (expression, expectedResult) in evaluations) {
final result = await service.evaluateInFrame(
isolateId,
/* frame = */ 0,
expression,
) as InstanceRef;
print(result.valueAsString);
expect(result.valueAsString, equals(expectedResult));
}
};
}
42 changes: 4 additions & 38 deletions pkg/vm_service/test/issue_56911_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:developer';
import 'package:test/test.dart';
import 'package:vm_service/vm_service.dart';
import 'common/service_test_common.dart';
import 'common/test_helper.dart';

Expand All @@ -14,8 +12,8 @@ import 'common/test_helper.dart';
//
// dart pkg/vm_service/test/update_line_numbers.dart pkg/vm_service/test/issue_56911_test.dart
//
const LINE_A = 28;
const LINE_B = 32;
const LINE_A = 26;
const LINE_B = 30;
// AUTOGENERATED END

extension type ExtensionType._(String s) {
Expand All @@ -34,42 +32,10 @@ void code() {
});
}

Future<void> Function(VmService, IsolateRef) test(
String topFrameName,
List<String> availableVariables,
List<(String evaluate, String evaluationResult)> evaluations,
) {
return (VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
final stack = await service.getStack(isolateId);

// Make sure we are in the right place.
expect(stack.frames!.length, greaterThanOrEqualTo(1));
expect(stack.frames![0].function!.name, topFrameName);

// Check variables.
expect(
(stack.frames![0].vars ?? []).map((v) => v.name).toList(),
equals(availableVariables),
);

// Evaluate.
for (final (expression, expectedResult) in evaluations) {
final result = await service.evaluateInFrame(
isolateId,
/* frame = */ 0,
expression,
) as InstanceRef;
print(result.valueAsString);
expect(result.valueAsString, equals(expectedResult));
}
};
}

final tests = <IsolateTest>[
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_A),
test('code', [
testExpressionEvaluationAndAvailableVariables('code', [
'list',
], [
('() { return list.single.value; }()', '48'),
Expand All @@ -78,7 +44,7 @@ final tests = <IsolateTest>[
resumeIsolate,
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_B),
test('<anonymous closure>', [
testExpressionEvaluationAndAvailableVariables('<anonymous closure>', [
'input',
], [
('() { return input.value; }()', '48'),
Expand Down
42 changes: 4 additions & 38 deletions pkg/vm_service/test/issue_57040_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:developer';
import 'package:test/test.dart';
import 'package:vm_service/vm_service.dart';
import 'common/service_test_common.dart';
import 'common/test_helper.dart';

Expand All @@ -14,8 +12,8 @@ import 'common/test_helper.dart';
//
// dart pkg/vm_service/test/update_line_numbers.dart pkg/vm_service/test/issue_57040_test.dart
//
const LINE_A = 30;
const LINE_B = 33;
const LINE_A = 28;
const LINE_B = 31;
// AUTOGENERATED END

extension on String? {
Expand All @@ -34,42 +32,10 @@ void code() {
print(str.isNullOrEmpty);
}

Future<void> Function(VmService, IsolateRef) test(
String topFrameName,
List<String> availableVariables,
List<(String evaluate, String evaluationResult)> evaluations,
) {
return (VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
final stack = await service.getStack(isolateId);

// Make sure we are in the right place.
expect(stack.frames!.length, greaterThanOrEqualTo(1));
expect(stack.frames![0].function!.name, topFrameName);

// Check variables.
expect(
(stack.frames![0].vars ?? []).map((v) => v.name).toList(),
equals(availableVariables),
);

// Evaluate.
for (final (expression, expectedResult) in evaluations) {
final result = await service.evaluateInFrame(
isolateId,
/* frame = */ 0,
expression,
) as InstanceRef;
print(result.valueAsString);
expect(result.valueAsString, equals(expectedResult));
}
};
}

final tests = <IsolateTest>[
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_A),
test('code', [
testExpressionEvaluationAndAvailableVariables('code', [
'str',
], [
('() { return str.isNullOrEmpty; }()', 'false'),
Expand All @@ -78,7 +44,7 @@ final tests = <IsolateTest>[
resumeIsolate,
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_B),
test('code', [
testExpressionEvaluationAndAvailableVariables('code', [
'str',
], [
('() { return str.isNullOrEmpty; }()', 'true'),
Expand Down
78 changes: 32 additions & 46 deletions pkg/vm_service/test/issue_59661_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:developer';
import 'package:test/test.dart';
import 'package:vm_service/vm_service.dart';
import 'common/service_test_common.dart';
import 'common/test_helper.dart';

Expand All @@ -14,12 +12,12 @@ import 'common/test_helper.dart';
//
// dart pkg/vm_service/test/update_line_numbers.dart pkg/vm_service/test/issue_59661_test.dart
//
const LINE_CLASS_A = 29;
const LINE_CLASS_A_NAMED = 34;
const LINE_CLASS_A_NAMED2_BREAK_1 = 40;
const LINE_CLASS_A_NAMED2_BREAK_2 = 43;
const LINE_CLASS_A_NAMED2_BREAK_3 = 46;
const LINE_CLASS_B = 55;
const LINE_CLASS_A = 27;
const LINE_CLASS_A_NAMED = 32;
const LINE_CLASS_A_NAMED2_BREAK_1 = 38;
const LINE_CLASS_A_NAMED2_BREAK_2 = 41;
const LINE_CLASS_A_NAMED2_BREAK_3 = 44;
const LINE_CLASS_B = 53;
// AUTOGENERATED END

class A {
Expand Down Expand Up @@ -64,50 +62,26 @@ void code() {
B([1, 2]);
}

Future<void> Function(VmService, IsolateRef) test(
String topFrameName,
List<String> availableVariables,
List<(String evaluate, String evaluationResult)> evaluations,
) {
return (VmService service, IsolateRef isolateRef) async {
final isolateId = isolateRef.id!;
final stack = await service.getStack(isolateId);

// Make sure we are in the right place.
expect(stack.frames!.length, greaterThanOrEqualTo(1));
expect(stack.frames![0].function!.name, topFrameName);

// Check variables.
expect(
(stack.frames![0].vars ?? []).map((v) => v.name).toList(),
equals(availableVariables),
);

// Evaluate.
for (final (expression, expectedResult) in evaluations) {
final dynamic result = await service.evaluateInFrame(
isolateId,
/* frame = */ 0,
expression,
);
print(result.valueAsString);
expect(result.valueAsString, equals(expectedResult));
}
};
}

final tests = <IsolateTest>[
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_CLASS_A),
test('A', ['this'], [('list.toString()', '[3]')]),
testExpressionEvaluationAndAvailableVariables(
'A',
['this'],
[('list.toString()', '[3]')],
),
resumeIsolate,
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_CLASS_A_NAMED),
test('A.named', ['this'], [('list.toString()', '[4]')]),
testExpressionEvaluationAndAvailableVariables(
'A.named',
['this'],
[('list.toString()', '[4]')],
),
resumeIsolate,
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_CLASS_A_NAMED2_BREAK_1),
test(
testExpressionEvaluationAndAvailableVariables(
'A.named2',
['this', 'list'],
[
Expand All @@ -118,15 +92,27 @@ final tests = <IsolateTest>[
resumeIsolate,
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_CLASS_A_NAMED2_BREAK_2),
test('A.named2', ['this'], [('list.toString()', '[1, 2]')]),
testExpressionEvaluationAndAvailableVariables(
'A.named2',
['this'],
[('list.toString()', '[1, 2]')],
),
resumeIsolate,
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_CLASS_A_NAMED2_BREAK_3),
test('A.named2', ['this'], [('list.toString()', '[6]')]),
testExpressionEvaluationAndAvailableVariables(
'A.named2',
['this'],
[('list.toString()', '[6]')],
),
resumeIsolate,
hasStoppedAtBreakpoint,
stoppedAtLine(LINE_CLASS_B),
test('B', ['this'], [('list.toString()', '[7]')]),
testExpressionEvaluationAndAvailableVariables(
'B',
['this'],
[('list.toString()', '[7]')],
),
];

void main([args = const <String>[]]) => runIsolateTests(
Expand Down

0 comments on commit 663c55b

Please sign in to comment.