2026-10-11 10:19:01 +08:00
|
|
|
// 图元库测试:内置符号完整性、JSON 往返、用户目录存取与合并覆盖、坐标换算。
|
|
|
|
|
import 'dart:ui' show Path, Rect;
|
|
|
|
|
|
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
2026-11-22 10:02:48 +08:00
|
|
|
import 'package:sequence_diagram/modules/architecture/library/symbol_library.dart';
|
2026-10-11 10:19:01 +08:00
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
group('内置符号', () {
|
|
|
|
|
test('数量 / id 唯一 / 名称分类引脚完整', () {
|
|
|
|
|
expect(SymbolLibrary.builtin.length, greaterThanOrEqualTo(12));
|
|
|
|
|
final Set<String> ids = <String>{
|
|
|
|
|
for (final LibSymbol s in SymbolLibrary.builtin) s.id,
|
|
|
|
|
};
|
|
|
|
|
expect(ids.length, SymbolLibrary.builtin.length);
|
|
|
|
|
for (final LibSymbol s in SymbolLibrary.builtin) {
|
|
|
|
|
expect(s.name.isNotEmpty, isTrue, reason: '${s.id} 名称缺失');
|
|
|
|
|
expect(s.category.isNotEmpty, isTrue, reason: '${s.id} 分类缺失');
|
|
|
|
|
expect(s.prims.isNotEmpty, isTrue, reason: '${s.id} 基元缺失');
|
|
|
|
|
expect(s.pins.isNotEmpty, isTrue, reason: '${s.id} 引脚缺失');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('引脚坐标在归一化画布边界内', () {
|
|
|
|
|
for (final LibSymbol s in SymbolLibrary.builtin) {
|
|
|
|
|
for (final SymPin p in s.pins) {
|
|
|
|
|
expect(p.x, inInclusiveRange(-5, 105), reason: '${s.id}/${p.id}');
|
|
|
|
|
expect(p.y, inInclusiveRange(-5, 105), reason: '${s.id}/${p.id}');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('JSON 往返(含文本基元与引脚)', () {
|
|
|
|
|
final LibSymbol s =
|
|
|
|
|
SymbolLibrary.builtin.firstWhere((LibSymbol x) => x.id == 'km-coil');
|
|
|
|
|
final LibSymbol back = LibSymbol.fromJson(s.toJson());
|
|
|
|
|
expect(back.name, s.name);
|
|
|
|
|
expect(back.prims.length, s.prims.length);
|
|
|
|
|
expect(
|
|
|
|
|
back.prims
|
|
|
|
|
.where((SymPrim p) => p.kind == SymPrimKind.text)
|
|
|
|
|
.single
|
|
|
|
|
.label,
|
|
|
|
|
'KM',
|
|
|
|
|
);
|
|
|
|
|
expect(back.pins.map((SymPin p) => p.id).toList(), <String>['A1', 'A2']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('路径换算:线基元落在目标矩形内', () {
|
|
|
|
|
const Rect box = Rect.fromLTWH(10, 20, 200, 200);
|
|
|
|
|
const SymPrim line =
|
|
|
|
|
SymPrim(kind: SymPrimKind.line, pts: <double>[50, 0, 50, 100]);
|
|
|
|
|
final Path path = symPrimPath(line, box)!;
|
|
|
|
|
final Rect b = path.getBounds();
|
|
|
|
|
expect(b.center.dx, closeTo(10 + 100, 0.01));
|
|
|
|
|
expect(b.top, closeTo(20, 0.01));
|
|
|
|
|
expect(b.bottom, closeTo(220, 0.01));
|
|
|
|
|
});
|
|
|
|
|
}
|