Skip to content

Commit 8668349

Browse files
committed
✅ add Utils.compact tests
1 parent 72c190c commit 8668349

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

test/unit/utils_test.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,5 +1083,81 @@ void main() {
10831083
expect(Utils.interpretNumericEntities('�'), '�');
10841084
});
10851085
});
1086+
1087+
group('compact', () {
1088+
test('removes Undefined from flat map', () {
1089+
final m = <String, dynamic>{
1090+
'a': 1,
1091+
'b': const Undefined(),
1092+
'c': null,
1093+
};
1094+
final out = Utils.compact(m);
1095+
expect(out, equals({'a': 1, 'c': null}));
1096+
// in-place
1097+
expect(identical(out, m), isTrue);
1098+
});
1099+
1100+
test('removes Undefined from nested map/list', () {
1101+
final m = <String, dynamic>{
1102+
'a': [
1103+
1,
1104+
const Undefined(),
1105+
{'x': const Undefined(), 'y': 2},
1106+
[const Undefined(), 3],
1107+
],
1108+
'b': {'k': const Undefined(), 'z': 0},
1109+
};
1110+
final out = Utils.compact(m);
1111+
expect(
1112+
out,
1113+
equals({
1114+
'a': [
1115+
1,
1116+
{'y': 2},
1117+
[3],
1118+
],
1119+
'b': {'z': 0},
1120+
}),
1121+
);
1122+
});
1123+
1124+
test('handles cycles without infinite loop', () {
1125+
final a = <String, dynamic>{};
1126+
final b = <String, dynamic>{'child': a, 'u': const Undefined()};
1127+
a['parent'] = b;
1128+
a['keep'] = 1;
1129+
1130+
final out = Utils.compact(a);
1131+
expect(out['keep'], 1);
1132+
expect((out['parent'] as Map)['child'], same(out)); // cycle preserved
1133+
expect((out['parent'] as Map).containsKey('u'), isFalse);
1134+
});
1135+
1136+
test('preserves order', () {
1137+
final m = <String, dynamic>{
1138+
'first': 1,
1139+
'second': const Undefined(),
1140+
'third': 3,
1141+
};
1142+
final out = Utils.compact(m);
1143+
1144+
// insertion order: first, third
1145+
expect(out.keys.toList(), equals(['first', 'third']));
1146+
});
1147+
1148+
test('shared substructures are visited once', () {
1149+
final shared = <String, dynamic>{'x': const Undefined(), 'y': 1};
1150+
final m = <String, dynamic>{
1151+
'a': shared,
1152+
'b': shared,
1153+
};
1154+
1155+
final out = Utils.compact(m);
1156+
expect((out['a'] as Map).containsKey('x'), isFalse);
1157+
expect((out['a'] as Map)['y'], 1);
1158+
// 'b' points to the same (mutated) object
1159+
expect(identical(out['a'], out['b']), isTrue);
1160+
});
1161+
});
10861162
});
10871163
}

0 commit comments

Comments
 (0)