dart-migrate-to-checks-package
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMigrating Dart Tests to Package Checks
将Dart测试迁移至Package Checks
Contents
目录
Dependency Management
依赖管理
Manage dependencies using the Dart Tooling MCP Server tool or standard CLI commands.
pub- Add as a
package:checksusingdev_dependency.dart pub add dev:checks - Remove if it is explicitly listed in the
package:matcher(note: it is often transitively included bypubspec.yaml, which is fine).package:test - Import in all test files undergoing migration.
package:checks/checks.dart
使用Dart Tooling MCP Server的工具或标准CLI命令管理依赖。
pub- 通过将
dart pub add dev:checks添加为开发依赖。package:checks - 如果中显式列出了
pubspec.yaml,则将其移除(注意:它通常会被package:matcher间接引入,这种情况无需处理)。package:test - 在所有待迁移的测试文件中导入。
package:checks/checks.dart
Syntax Migration Guidelines
语法迁移指南
Transition test assertions from the syntax to the literate API provided by .
package:matcherpackage:checks- Basic Equality: Replace or
expect(actual, equals(expected))withexpect(actual, expected).check(actual).equals(expected) - Type Checking: Replace with
expect(actual, isA<Type>()).check(actual).isA<Type>() - Property Extraction: Replace with
expect(actual.property, expected).check(actual).has((a) => a.property, 'property name').equals(expected) - Cascades for Multiple Checks: Use Dart's cascade operator () to chain multiple expectations on a single subject.
.. - Asynchronous Expectations:
- If checking a ,
Futuretheawaitcall:check.await check(someFuture).completes((r) => r.equals(expected)); - If checking a , wrap it in a
Streamfor multiple checks, or useStreamQueuefor single/broadcast checks..withQueue
- If checking a
将测试断言从语法转换为提供的易读API。
package:matcherpackage:checks- 基础相等性检查: 将或
expect(actual, equals(expected))替换为expect(actual, expected)。check(actual).equals(expected) - 类型检查: 将替换为
expect(actual, isA<Type>())。check(actual).isA<Type>() - 属性提取: 将替换为
expect(actual.property, expected)。check(actual).has((a) => a.property, 'property name').equals(expected) - 多检查链式调用: 使用Dart的级联运算符()对同一对象链式调用多个断言。
.. - 异步断言:
- 如果检查,需
Future对应的await调用:check。await check(someFuture).completes((r) => r.equals(expected)); - 如果检查,可将其包装在
Stream中进行多检查,或使用StreamQueue进行单次/广播流检查。.withQueue
- 如果检查
Utilizing Dart MCP Tools
使用Dart MCP工具
Leverage the Dart MCP Server tools to automate and validate the migration process.
- Use to run
pubordart pub get.dart pub add - Use to run static analysis on the project or specific paths.
analyze_files - Use to execute Dart or Flutter tests with an agent-centric UX. ALWAYS use this instead of shell commands like
run_tests.dart test - Use to apply automated fixes if applicable.
dart_fix
借助Dart MCP Server工具自动化并验证迁移流程。
- 使用执行
pub或dart pub get命令。dart pub add - 使用对项目或指定路径进行静态分析。
analyze_files - 使用以Agent为中心的用户体验执行Dart或Flutter测试。请务必使用该工具替代
run_tests等shell命令。dart test - 若适用,使用应用自动修复。
dart_fix
Migration Workflow
迁移工作流
Copy and use the following checklist to track progress when migrating a test suite:
- Task Progress
- Add as a dev dependency.
package:checks - Identify all test files using (
package:matchercalls).expect - Import in target test files.
package:checks/checks.dart - Rewrite all statements to
expect(...)statements.check(...) - Run static analyzer ().
analyze_files - Run tests ().
run_tests
- Add
复制并使用以下清单跟踪测试套件的迁移进度:
- 任务进度
- 将添加为开发依赖。
package:checks - 识别所有使用(含
package:matcher调用)的测试文件。expect - 在目标测试文件中导入。
package:checks/checks.dart - 将所有语句改写为
expect(...)语句。check(...) - 运行静态分析器()。
analyze_files - 运行测试()。
run_tests
- 将
Feedback Loop: Static Analysis
反馈循环:静态分析
- Run the tool on the modified test directories.
analyze_files - Review any static analysis warnings or errors (e.g., missing imports, incorrect generic types on , unawaited futures).
isA - Fix the warnings.
- Repeat until the analyzer returns zero issues.
- 在修改后的测试目录上运行工具。
analyze_files - 查看所有静态分析警告或错误(例如:缺失导入、上的泛型类型错误、未等待的Future)。
isA - 修复警告。
- 重复操作直至分析器返回零问题。
Feedback Loop: Test Validation
反馈循环:测试验证
- Run the tool.
run_tests - If tests fail, review the failure output. provides detailed context (e.g.,
package:checks).Which: has length of <2> - Adjust the expectations or the underlying code to resolve the failure.
check() - Repeat until all tests pass.
- 运行工具。
run_tests - 如果测试失败,查看失败输出。会提供详细上下文(例如:
package:checks)。Which: has length of <2> - 调整断言或底层代码以解决失败问题。
check() - 重复操作直至所有测试通过。
Examples
示例
Basic Assertions
基础断言
Input ():
matcherdart
expect(someList.length, 1);
expect(someString, startsWith('a'));
expect(someObject, isA<Map>());Output ():
checksdart
check(someList).length.equals(1);
check(someString).startsWith('a');
check(someObject).isA<Map>();输入():
matcherdart
expect(someList.length, 1);
expect(someString, startsWith('a'));
expect(someObject, isA<Map>());输出():
checksdart
check(someList).length.equals(1);
check(someString).startsWith('a');
check(someObject).isA<Map>();Composed Expectations
组合断言
Input ():
matcherdart
expect('foo,bar,baz', allOf([
contains('foo'),
isNot(startsWith('bar')),
endsWith('baz')
]));Output ():
checksdart
check('foo,bar,baz')
..contains('foo')
..not((s) => s.startsWith('bar'))
..endsWith('baz');输入():
matcherdart
expect('foo,bar,baz', allOf([
contains('foo'),
isNot(startsWith('bar')),
endsWith('baz')
]));输出():
checksdart
check('foo,bar,baz')
..contains('foo')
..not((s) => s.startsWith('bar'))
..endsWith('baz');Asynchronous Futures
异步Future
Input ():
matcherdart
expect(Future.value(10), completion(equals(10)));
expect(Future.error('oh no'), throwsA(equals('oh no')));Output ():
checksdart
await check(Future.value(10)).completes((it) => it.equals(10));
await check(Future.error('oh no')).throws<String>().equals('oh no');输入():
matcherdart
expect(Future.value(10), completion(equals(10)));
expect(Future.error('oh no'), throwsA(equals('oh no')));输出():
checksdart
await check(Future.value(10)).completes((it) => it.equals(10));
await check(Future.error('oh no')).throws<String>().equals('oh no');Asynchronous Streams
异步Stream
Input ():
matcherdart
var stdout = StreamQueue(Stream.fromIterable(['Ready', 'Go']));
await expectLater(stdout, emitsThrough('Ready'));Output ():
checksdart
var stdout = StreamQueue(Stream.fromIterable(['Ready', 'Go']));
await check(stdout).emitsThrough((it) => it.equals('Ready'));输入():
matcherdart
var stdout = StreamQueue(Stream.fromIterable(['Ready', 'Go']));
await expectLater(stdout, emitsThrough('Ready'));输出():
checksdart
var stdout = StreamQueue(Stream.fromIterable(['Ready', 'Go']));
await check(stdout).emitsThrough((it) => it.equals('Ready'));