test sweep: cover utils/ + core/mouse/ (T-91)
Adds two test files closing the last two pure-Dart sub-areas in `lib/src/terminal/src/`: `test/terminal/utils/utils_test.dart` — 51 tests covering: - Ascii.isNonPrintable (control chars + DEL). - hashValues (every optional arg slot, 2..20 inclusive) + hashList (including the documented zero-on-empty short-circuit). - ByteConsumer (single-block + cross-block consume, peek's consume-rollback path, rollback within block + across consumed blocks, rollbackTo, unrefConsumedBlocks, reset). - IndexAwareCircularBuffer (push/trim, pushAll, pop, [] / []=, clear, forEach, remove with count clamp / no-op, insert in the middle / at end / at full ring, insertAll, trimStart, replaceWith with truncate-from-head, swap, maxLength setter incl. error + no-op cases, debugDump, IndexedItem mixin attach/detach/index). - UnicodeV11.wcwidth (control chars, printable ASCII, DEL+C1, combining marks BMP+high-plane, wide chars BMP+high-plane, unmapped high-plane default, version field). - The push wrap branch where _startIndex resets to 0 after a full revolution (last circular_buffer line). `test/terminal/mouse/mouse_test.dart` — 21 tests covering: - TerminalMouseButton ids + isWheel for the 7 enum values. - MouseReporter for all four MouseReportMode shapes (normal with >223 null-byte clamp, utf with the 2015-limit clamp, sgr's M/m pair, urxvt's button+32 / 3-on-up encoding). - TerminalMouseEvent constructor. - CascadeMouseHandler first-non-null semantics. - ClickMouseHandler — only fires on clickOnly + down + button id < 3; null otherwise. UpDownMouseHandler — fires on every upDownScroll* mode; drops wheel-up; passes wheel-down. Default defaultMouseHandler routes through both. Coverage delta: - utils/ascii.dart: 0/2 → 2/2. - utils/byte_consumer.dart: 28/42 → 42/42. - utils/circular_buffer.dart: 71/130 → 130/130. - utils/hash_values.dart: 16/36 → 36/36. - utils/unicode_v11.dart: 16/27 → 27/27. - core/mouse/handler.dart: 3/34 → 34/34. - core/mouse/reporter.dart: 0/17 → 17/17. - (utils/char_code.dart, utils/lookup_table.dart already at 100% from earlier reflow + parser work; mouse/button.dart, mouse/button_state.dart, mouse/mode.dart are pure enums with no executable lines.) - Total project: 54.62% → 56.40%; coverage_floor bumped 54 → 56. Two pre-existing `// ignore_for_file: constant_identifier_names` suppressions that lacked documented reasons get inline justifications: - `lib/src/terminal/src/utils/ascii.dart` — RFC 20 / ISO 646 control-character names; lowerCamelCase would diverge from every spec/man-page reference. - `lib/src/terminal/src/utils/unicode_v11.dart` — Unicode 11 wcwidth tables vendored as-is; future re-vendoring stays a verbatim paste. Both fall under the same "FFI / spec-shaped names" pattern as the libc.dart suppression (D-66 era). Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,304 @@
|
||||
/// Pure-Dart tests for `lib/src/terminal/src/core/mouse/`.
|
||||
library;
|
||||
|
||||
import 'package:clide/src/terminal/src/core/buffer/cell_offset.dart';
|
||||
import 'package:clide/src/terminal/src/core/cursor.dart';
|
||||
import 'package:clide/src/terminal/src/core/mouse/button.dart';
|
||||
import 'package:clide/src/terminal/src/core/mouse/button_state.dart';
|
||||
import 'package:clide/src/terminal/src/core/mouse/handler.dart';
|
||||
import 'package:clide/src/terminal/src/core/mouse/mode.dart';
|
||||
import 'package:clide/src/terminal/src/core/mouse/reporter.dart';
|
||||
import 'package:clide/src/terminal/src/core/platform.dart';
|
||||
import 'package:clide/src/terminal/src/core/state.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
class _State implements TerminalState {
|
||||
_State({
|
||||
this.mouseMode = MouseMode.none,
|
||||
this.mouseReportMode = MouseReportMode.normal,
|
||||
});
|
||||
|
||||
@override
|
||||
MouseMode mouseMode;
|
||||
@override
|
||||
MouseReportMode mouseReportMode;
|
||||
|
||||
@override
|
||||
int get viewWidth => 80;
|
||||
@override
|
||||
int get viewHeight => 24;
|
||||
@override
|
||||
CursorStyle get cursor => CursorStyle();
|
||||
@override
|
||||
bool get reflowEnabled => false;
|
||||
@override
|
||||
bool get insertMode => false;
|
||||
@override
|
||||
bool get lineFeedMode => false;
|
||||
@override
|
||||
bool get cursorKeysMode => false;
|
||||
@override
|
||||
bool get reverseDisplayMode => false;
|
||||
@override
|
||||
bool get originMode => false;
|
||||
@override
|
||||
bool get autoWrapMode => true;
|
||||
@override
|
||||
bool get cursorBlinkMode => true;
|
||||
@override
|
||||
bool get cursorVisibleMode => true;
|
||||
@override
|
||||
bool get appKeypadMode => false;
|
||||
@override
|
||||
bool get reportFocusMode => false;
|
||||
@override
|
||||
bool get altBufferMouseScrollMode => false;
|
||||
@override
|
||||
bool get bracketedPasteMode => false;
|
||||
}
|
||||
|
||||
TerminalMouseEvent _evt(
|
||||
TerminalMouseButton button,
|
||||
TerminalMouseButtonState state, {
|
||||
int x = 0,
|
||||
int y = 0,
|
||||
MouseMode mode = MouseMode.none,
|
||||
MouseReportMode reportMode = MouseReportMode.normal,
|
||||
TerminalTargetPlatform platform = TerminalTargetPlatform.linux,
|
||||
}) {
|
||||
return TerminalMouseEvent(
|
||||
button: button,
|
||||
buttonState: state,
|
||||
position: CellOffset(x, y),
|
||||
state: _State(mouseMode: mode, mouseReportMode: reportMode),
|
||||
platform: platform,
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('TerminalMouseButton', () {
|
||||
test('left/middle/right have ids 0/1/2 and isWheel=false', () {
|
||||
expect(TerminalMouseButton.left.id, 0);
|
||||
expect(TerminalMouseButton.middle.id, 1);
|
||||
expect(TerminalMouseButton.right.id, 2);
|
||||
expect(TerminalMouseButton.left.isWheel, isFalse);
|
||||
expect(TerminalMouseButton.middle.isWheel, isFalse);
|
||||
expect(TerminalMouseButton.right.isWheel, isFalse);
|
||||
});
|
||||
|
||||
test('wheel buttons are flagged isWheel and use the 64+N transposed ids', () {
|
||||
expect(TerminalMouseButton.wheelUp.id, 64 + 4);
|
||||
expect(TerminalMouseButton.wheelDown.id, 64 + 5);
|
||||
expect(TerminalMouseButton.wheelLeft.id, 64 + 6);
|
||||
expect(TerminalMouseButton.wheelRight.id, 64 + 7);
|
||||
for (final b in [
|
||||
TerminalMouseButton.wheelUp,
|
||||
TerminalMouseButton.wheelDown,
|
||||
TerminalMouseButton.wheelLeft,
|
||||
TerminalMouseButton.wheelRight,
|
||||
]) {
|
||||
expect(b.isWheel, isTrue, reason: '$b');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('MouseReporter (normal mode)', () {
|
||||
String r(TerminalMouseButton b, TerminalMouseButtonState s, {int x = 0, int y = 0}) => MouseReporter.report(b, s, CellOffset(x, y), MouseReportMode.normal);
|
||||
|
||||
test('press encodes button id + 1-based coordinates', () {
|
||||
// Position (0,0) → button code 32+0=32 (' '), col 32+1=33 ('!'),
|
||||
// row 32+1+1=34 ('"').
|
||||
expect(r(TerminalMouseButton.left, TerminalMouseButtonState.down), '\x1b[M !"');
|
||||
});
|
||||
|
||||
test('release uses button id 3 regardless of which button was up', () {
|
||||
expect(r(TerminalMouseButton.right, TerminalMouseButtonState.up), '\x1b[M#!"'); // 32+3='#' for the up code
|
||||
});
|
||||
|
||||
test('coordinates beyond 223 (8-bit limit) emit a null byte', () {
|
||||
final out = r(TerminalMouseButton.left, TerminalMouseButtonState.down, x: 300, y: 0);
|
||||
expect(out, '\x1b[M \x00"'); // null in the column slot
|
||||
});
|
||||
});
|
||||
|
||||
group('MouseReporter (utf mode)', () {
|
||||
test('uses the 2015 limit for the null-byte clamp', () {
|
||||
// x=300 fits into utf mode (limit 2015) — should emit a real char.
|
||||
final out = MouseReporter.report(TerminalMouseButton.left, TerminalMouseButtonState.down, const CellOffset(300, 0), MouseReportMode.utf);
|
||||
expect(out.contains('\x00'), isFalse);
|
||||
// x=3000 trips the utf limit.
|
||||
final outBig = MouseReporter.report(TerminalMouseButton.left, TerminalMouseButtonState.down, const CellOffset(3000, 3000), MouseReportMode.utf);
|
||||
expect(outBig.contains('\x00'), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('MouseReporter (sgr mode)', () {
|
||||
test('M for press, m for release, with raw 1-based coords', () {
|
||||
expect(
|
||||
MouseReporter.report(TerminalMouseButton.middle, TerminalMouseButtonState.down, const CellOffset(10, 20), MouseReportMode.sgr),
|
||||
'\x1b[<1;11;21M',
|
||||
);
|
||||
expect(
|
||||
MouseReporter.report(TerminalMouseButton.middle, TerminalMouseButtonState.up, const CellOffset(10, 20), MouseReportMode.sgr),
|
||||
'\x1b[<1;11;21m',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('MouseReporter (urxvt mode)', () {
|
||||
test('always M, button id +32 (3 for up, real for down)', () {
|
||||
expect(
|
||||
MouseReporter.report(TerminalMouseButton.left, TerminalMouseButtonState.down, const CellOffset(5, 7), MouseReportMode.urxvt),
|
||||
'\x1b[32;6;8M', // 32+0 = 32 for left-button down
|
||||
);
|
||||
expect(
|
||||
MouseReporter.report(TerminalMouseButton.left, TerminalMouseButtonState.up, const CellOffset(5, 7), MouseReportMode.urxvt),
|
||||
'\x1b[35;6;8M', // 32+3 = 35 for any up
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('TerminalMouseEvent', () {
|
||||
test('constructor exposes every field directly', () {
|
||||
final e = _evt(TerminalMouseButton.left, TerminalMouseButtonState.down, x: 3, y: 5);
|
||||
expect(e.button, TerminalMouseButton.left);
|
||||
expect(e.buttonState, TerminalMouseButtonState.down);
|
||||
expect(e.position, const CellOffset(3, 5));
|
||||
expect(e.platform, TerminalTargetPlatform.linux);
|
||||
expect(e.state, isA<TerminalState>());
|
||||
});
|
||||
});
|
||||
|
||||
group('CascadeMouseHandler', () {
|
||||
test('returns the first non-null result; null when all return null', () {
|
||||
const cascade = CascadeMouseHandler([
|
||||
_NullHandler(),
|
||||
_ConstHandler('first'),
|
||||
_ConstHandler('second'),
|
||||
]);
|
||||
expect(
|
||||
cascade(_evt(TerminalMouseButton.left, TerminalMouseButtonState.down)),
|
||||
'first',
|
||||
);
|
||||
|
||||
const allNull = CascadeMouseHandler([_NullHandler(), _NullHandler()]);
|
||||
expect(
|
||||
allNull(_evt(TerminalMouseButton.left, TerminalMouseButtonState.down)),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('ClickMouseHandler', () {
|
||||
const h = ClickMouseHandler();
|
||||
|
||||
test('clickOnly mode + down + button id < 3 → reports', () {
|
||||
final out = h(_evt(TerminalMouseButton.middle, TerminalMouseButtonState.down, mode: MouseMode.clickOnly));
|
||||
expect(out, isNotNull);
|
||||
});
|
||||
|
||||
test('clickOnly mode + up → null (only down events report)', () {
|
||||
expect(
|
||||
h(_evt(TerminalMouseButton.middle, TerminalMouseButtonState.up, mode: MouseMode.clickOnly)),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('clickOnly mode + button id >= 3 (wheel) → null', () {
|
||||
expect(
|
||||
h(_evt(TerminalMouseButton.wheelUp, TerminalMouseButtonState.down, mode: MouseMode.clickOnly)),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('non-clickOnly modes always return null', () {
|
||||
for (final m in [
|
||||
MouseMode.none,
|
||||
MouseMode.upDownScroll,
|
||||
MouseMode.upDownScrollDrag,
|
||||
MouseMode.upDownScrollMove,
|
||||
]) {
|
||||
expect(
|
||||
h(_evt(TerminalMouseButton.left, TerminalMouseButtonState.down, mode: m)),
|
||||
isNull,
|
||||
reason: '$m',
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('UpDownMouseHandler', () {
|
||||
const h = UpDownMouseHandler();
|
||||
|
||||
test('none / clickOnly modes return null', () {
|
||||
for (final m in [MouseMode.none, MouseMode.clickOnly]) {
|
||||
expect(
|
||||
h(_evt(TerminalMouseButton.left, TerminalMouseButtonState.down, mode: m)),
|
||||
isNull,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('upDownScroll modes report regular button events', () {
|
||||
for (final m in [
|
||||
MouseMode.upDownScroll,
|
||||
MouseMode.upDownScrollDrag,
|
||||
MouseMode.upDownScrollMove,
|
||||
]) {
|
||||
expect(
|
||||
h(_evt(TerminalMouseButton.left, TerminalMouseButtonState.down, mode: m)),
|
||||
isNotNull,
|
||||
reason: '$m',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('wheel up events are silently dropped (no report on wheel release)', () {
|
||||
expect(
|
||||
h(_evt(TerminalMouseButton.wheelUp, TerminalMouseButtonState.up, mode: MouseMode.upDownScroll)),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('wheel down events do report (one click per scroll tick)', () {
|
||||
expect(
|
||||
h(_evt(TerminalMouseButton.wheelDown, TerminalMouseButtonState.down, mode: MouseMode.upDownScroll)),
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('defaultMouseHandler', () {
|
||||
test('routes clickOnly through ClickMouseHandler', () {
|
||||
final out = defaultMouseHandler(_evt(TerminalMouseButton.left, TerminalMouseButtonState.down, mode: MouseMode.clickOnly));
|
||||
expect(out, isNotNull);
|
||||
});
|
||||
|
||||
test('routes upDownScroll through UpDownMouseHandler', () {
|
||||
final out = defaultMouseHandler(_evt(TerminalMouseButton.middle, TerminalMouseButtonState.up, mode: MouseMode.upDownScrollDrag));
|
||||
expect(out, isNotNull);
|
||||
});
|
||||
|
||||
test('mode=none produces null (no handler reports)', () {
|
||||
expect(
|
||||
defaultMouseHandler(
|
||||
_evt(TerminalMouseButton.left, TerminalMouseButtonState.down, mode: MouseMode.none),
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class _NullHandler extends TerminalMouseHandler {
|
||||
const _NullHandler();
|
||||
@override
|
||||
String? call(TerminalMouseEvent event) => null;
|
||||
}
|
||||
|
||||
class _ConstHandler extends TerminalMouseHandler {
|
||||
const _ConstHandler(this._value);
|
||||
final String _value;
|
||||
@override
|
||||
String? call(TerminalMouseEvent event) => _value;
|
||||
}
|
||||
@@ -0,0 +1,437 @@
|
||||
/// Pure-Dart tests for `lib/src/terminal/src/utils/`.
|
||||
library;
|
||||
|
||||
import 'package:clide/src/terminal/src/utils/ascii.dart';
|
||||
import 'package:clide/src/terminal/src/utils/byte_consumer.dart';
|
||||
import 'package:clide/src/terminal/src/utils/circular_buffer.dart';
|
||||
import 'package:clide/src/terminal/src/utils/hash_values.dart';
|
||||
import 'package:clide/src/terminal/src/utils/unicode_v11.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
class _Item with IndexedItem {
|
||||
_Item(this.value);
|
||||
final int value;
|
||||
@override
|
||||
String toString() => 'Item($value)';
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('Ascii.isNonPrintable', () {
|
||||
test('returns true for control chars (< 32) and DEL (127)', () {
|
||||
expect(Ascii.isNonPrintable(0), isTrue);
|
||||
expect(Ascii.isNonPrintable(31), isTrue);
|
||||
expect(Ascii.isNonPrintable(127), isTrue);
|
||||
});
|
||||
|
||||
test('returns false for printable range [32, 126]', () {
|
||||
for (var i = 32; i < 127; i++) {
|
||||
expect(Ascii.isNonPrintable(i), isFalse, reason: 'codepoint $i');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('hashValues', () {
|
||||
test('combines two args deterministically', () {
|
||||
expect(hashValues(1, 2), hashValues(1, 2));
|
||||
expect(hashValues(1, 2), isNot(hashValues(2, 1)));
|
||||
});
|
||||
|
||||
test('extra args change the hash (covers each optional slot)', () {
|
||||
// Walk through 3..20 inclusive — the cascade of nested ifs in
|
||||
// hashValues is one branch per arg.
|
||||
final base = hashValues(1, 2);
|
||||
// Wrap up to 20 args; assert each adds entropy.
|
||||
var prev = base;
|
||||
final args = <Object?>[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
|
||||
for (var i = 0; i < args.length; i++) {
|
||||
final next = Function.apply(hashValues, [1, 2, ...args.sublist(0, i + 1)]) as int;
|
||||
expect(next, isNot(prev), reason: 'arg slot ${i + 3}');
|
||||
prev = next;
|
||||
}
|
||||
});
|
||||
|
||||
test('hashList sums an iterable to a single hash', () {
|
||||
expect(hashList([1, 2, 3]), hashList([1, 2, 3]));
|
||||
expect(hashList([1, 2, 3]), isNot(hashList([3, 2, 1])));
|
||||
// Empty iterable short-circuits — Jenkins.finish(0) == 0; document
|
||||
// the contract rather than fight it.
|
||||
expect(hashList(<Object>[]), 0);
|
||||
});
|
||||
});
|
||||
|
||||
group('ByteConsumer', () {
|
||||
test('add + consume — basic single-block sequence', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('abc');
|
||||
expect(c.length, 3);
|
||||
expect(c.isEmpty, isFalse);
|
||||
expect(c.isNotEmpty, isTrue);
|
||||
expect(c.consume(), 'a'.codeUnitAt(0));
|
||||
expect(c.consume(), 'b'.codeUnitAt(0));
|
||||
expect(c.consume(), 'c'.codeUnitAt(0));
|
||||
expect(c.length, 0);
|
||||
expect(c.totalConsumed, 3);
|
||||
expect(c.isEmpty, isTrue);
|
||||
});
|
||||
|
||||
test('add empty string is a no-op', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('');
|
||||
expect(c.length, 0);
|
||||
});
|
||||
|
||||
test('consume across block boundaries (recursive consume path)', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('ab');
|
||||
c.add('cd');
|
||||
expect(c.consume(), 'a'.codeUnitAt(0));
|
||||
expect(c.consume(), 'b'.codeUnitAt(0));
|
||||
// Crossing boundary: consume() recurses after _queue.removeFirst().
|
||||
expect(c.consume(), 'c'.codeUnitAt(0));
|
||||
expect(c.consume(), 'd'.codeUnitAt(0));
|
||||
expect(c.length, 0);
|
||||
});
|
||||
|
||||
test('peek returns the current head without consuming', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('ab');
|
||||
expect(c.peek(), 'a'.codeUnitAt(0));
|
||||
expect(c.length, 2); // unchanged
|
||||
});
|
||||
|
||||
test('peek across a block boundary uses consume+rollback', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('ab');
|
||||
c.add('cd');
|
||||
c.consume();
|
||||
c.consume();
|
||||
// _currentOffset == 2, equal to first block length → peek takes the
|
||||
// consume + rollback path.
|
||||
expect(c.peek(), 'c'.codeUnitAt(0));
|
||||
expect(c.length, 2);
|
||||
});
|
||||
|
||||
test('rollback by 1 within current block', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('abc');
|
||||
c.consume();
|
||||
c.consume();
|
||||
c.rollback();
|
||||
expect(c.length, 2);
|
||||
expect(c.consume(), 'b'.codeUnitAt(0));
|
||||
});
|
||||
|
||||
test('rollback across consumed blocks restores them', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('ab');
|
||||
c.add('cd');
|
||||
c.consume();
|
||||
c.consume();
|
||||
c.consume(); // crosses boundary, moves first block to consumed
|
||||
c.rollback(2); // span back across the boundary
|
||||
expect(c.length, 3);
|
||||
expect(c.consume(), 'b'.codeUnitAt(0));
|
||||
});
|
||||
|
||||
test('rollbackTo restores the consumer to a previous length', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('abcd');
|
||||
c.consume();
|
||||
c.consume();
|
||||
c.rollbackTo(4); // back to the start
|
||||
expect(c.length, 4);
|
||||
});
|
||||
|
||||
test('unrefConsumedBlocks empties the consumed history', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('ab');
|
||||
c.add('cd');
|
||||
c.consume();
|
||||
c.consume();
|
||||
c.consume(); // 'c' from second block; first block now in _consumed
|
||||
c.unrefConsumedBlocks();
|
||||
// Subsequent rollback within current block still works.
|
||||
c.rollback();
|
||||
expect(c.length, 2);
|
||||
});
|
||||
|
||||
test('reset wipes everything', () {
|
||||
final c = ByteConsumer();
|
||||
c.add('abc');
|
||||
c.consume();
|
||||
c.reset();
|
||||
expect(c.length, 0);
|
||||
expect(c.totalConsumed, 0);
|
||||
c.add('x');
|
||||
expect(c.consume(), 'x'.codeUnitAt(0));
|
||||
});
|
||||
});
|
||||
|
||||
group('IndexAwareCircularBuffer', () {
|
||||
test('push grows the list while length < maxLength', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.push(_Item(1));
|
||||
r.push(_Item(2));
|
||||
expect(r.length, 2);
|
||||
expect(r[0].value, 1);
|
||||
expect(r[1].value, 2);
|
||||
expect(r.isFull, isFalse);
|
||||
});
|
||||
|
||||
test('push past capacity trims the first element (FIFO ring)', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(2);
|
||||
r.push(_Item(1));
|
||||
r.push(_Item(2));
|
||||
r.push(_Item(3)); // trims item 1
|
||||
expect(r.length, 2);
|
||||
expect(r[0].value, 2);
|
||||
expect(r[1].value, 3);
|
||||
expect(r.isFull, isTrue);
|
||||
});
|
||||
|
||||
test('push wraps _startIndex back to 0 after a full revolution', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(2);
|
||||
r.push(_Item(1));
|
||||
r.push(_Item(2));
|
||||
r.push(_Item(3)); // _startIndex 0 → 1
|
||||
r.push(_Item(4)); // _startIndex 1 → 2 → wraps to 0
|
||||
// Visible state: ring contains the most recent two items.
|
||||
expect(r[0].value, 3);
|
||||
expect(r[1].value, 4);
|
||||
});
|
||||
|
||||
test('pushAll forwards each item through push', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2), _Item(3)]);
|
||||
expect(r.toList().map((i) => i.value), [1, 2, 3]);
|
||||
});
|
||||
|
||||
test('pop returns and removes the last item', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
expect(r.pop().value, 2);
|
||||
expect(r.length, 1);
|
||||
});
|
||||
|
||||
test('[]= replaces an element in place; [] reads it', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
r[0] = _Item(99);
|
||||
expect(r[0].value, 99);
|
||||
});
|
||||
|
||||
test('clear removes every element', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
r.clear();
|
||||
expect(r.length, 0);
|
||||
});
|
||||
|
||||
test('forEach iterates in order', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2), _Item(3)]);
|
||||
final out = <int>[];
|
||||
r.forEach((it) => out.add(it.value));
|
||||
expect(out, [1, 2, 3]);
|
||||
});
|
||||
|
||||
test('remove deletes a contiguous range and shifts trailing items left', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(8);
|
||||
r.pushAll([_Item(1), _Item(2), _Item(3), _Item(4), _Item(5)]);
|
||||
r.remove(1, 2); // drops items 2 + 3
|
||||
expect(r.toList().map((i) => i.value), [1, 4, 5]);
|
||||
});
|
||||
|
||||
test('remove with count past end clamps to remaining length', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(8);
|
||||
r.pushAll([_Item(1), _Item(2), _Item(3)]);
|
||||
r.remove(1, 99);
|
||||
expect(r.toList().map((i) => i.value), [1]);
|
||||
});
|
||||
|
||||
test('remove with count=0 is a no-op', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(8);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
r.remove(0, 0);
|
||||
expect(r.length, 2);
|
||||
});
|
||||
|
||||
test('insert at length delegates to push', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
r.insert(2, _Item(3));
|
||||
expect(r.toList().map((i) => i.value), [1, 2, 3]);
|
||||
});
|
||||
|
||||
test('insert in the middle shifts trailing items right', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
r.insert(1, _Item(99));
|
||||
expect(r.toList().map((i) => i.value), [1, 99, 2]);
|
||||
});
|
||||
|
||||
test('insert at index 0 of a full ring drops the new value', () {
|
||||
// Documented contract: when ring is full, inserting at 0 immediately
|
||||
// trims that same value, so the visible state is unchanged.
|
||||
final r = IndexAwareCircularBuffer<_Item>(2);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
r.insert(0, _Item(99));
|
||||
expect(r.toList().map((i) => i.value), [1, 2]);
|
||||
});
|
||||
|
||||
test('insert in the middle of a full ring trims the head', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(3);
|
||||
r.pushAll([_Item(1), _Item(2), _Item(3)]);
|
||||
r.insert(1, _Item(99)); // ring full → head trimmed
|
||||
expect(r.length, 3);
|
||||
});
|
||||
|
||||
test('insertAll preserves order', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(8);
|
||||
r.pushAll([_Item(1), _Item(4)]);
|
||||
r.insertAll(1, [_Item(2), _Item(3)]);
|
||||
expect(r.toList().map((i) => i.value), [1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
test('insertAll truncates when target is full from the head', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(2);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
r.insertAll(0, [_Item(91), _Item(92), _Item(93)]);
|
||||
expect(r.length, 2);
|
||||
});
|
||||
|
||||
test('trimStart drops the leading [count] items in O(1)', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(8);
|
||||
r.pushAll([_Item(1), _Item(2), _Item(3), _Item(4)]);
|
||||
r.trimStart(2);
|
||||
expect(r.toList().map((i) => i.value), [3, 4]);
|
||||
});
|
||||
|
||||
test('trimStart clamps to the current length', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1)]);
|
||||
r.trimStart(10);
|
||||
expect(r.length, 0);
|
||||
});
|
||||
|
||||
test('replaceWith swaps the contents with a new list', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(8);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
r.replaceWith([_Item(10), _Item(20), _Item(30)]);
|
||||
expect(r.toList().map((i) => i.value), [10, 20, 30]);
|
||||
});
|
||||
|
||||
test('replaceWith truncates inputs longer than maxLength', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(2);
|
||||
r.replaceWith([_Item(1), _Item(2), _Item(3), _Item(4)]);
|
||||
expect(r.toList().map((i) => i.value), [3, 4]); // last two kept
|
||||
});
|
||||
|
||||
test('swap replaces the element and returns the old value', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
final old = r.swap(0, _Item(99));
|
||||
expect(old.value, 1);
|
||||
expect(r[0].value, 99);
|
||||
});
|
||||
|
||||
test('maxLength setter rejects non-positive values', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
expect(() => r.maxLength = 0, throwsArgumentError);
|
||||
expect(() => r.maxLength = -1, throwsArgumentError);
|
||||
});
|
||||
|
||||
test('maxLength setter same value is a no-op', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1)]);
|
||||
r.maxLength = 4;
|
||||
expect(r.length, 1);
|
||||
});
|
||||
|
||||
test('maxLength setter rebuilds the array, preserving order', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2), _Item(3)]);
|
||||
r.maxLength = 8;
|
||||
expect(r.maxLength, 8);
|
||||
expect(r.toList().map((i) => i.value), [1, 2, 3]);
|
||||
});
|
||||
|
||||
test('debugDump returns a multi-line string with each item', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
r.pushAll([_Item(1), _Item(2)]);
|
||||
final s = r.debugDump();
|
||||
expect(s, contains('CircularList:'));
|
||||
expect(s, contains('Item(1)'));
|
||||
expect(s, contains('Item(2)'));
|
||||
});
|
||||
|
||||
test('IndexedItem mixin: index reflects ring position; attached toggles', () {
|
||||
final r = IndexAwareCircularBuffer<_Item>(4);
|
||||
final a = _Item(1);
|
||||
expect(a.attached, isFalse);
|
||||
r.push(a);
|
||||
expect(a.attached, isTrue);
|
||||
expect(a.index, 0);
|
||||
r.push(_Item(2));
|
||||
// After a.push, index stays absolute-relative; the buffer's index
|
||||
// exposes regular 0-based.
|
||||
r.pop(); // removes the newer item, leaves a in place at index 0
|
||||
expect(a.attached, isTrue);
|
||||
expect(a.index, 0);
|
||||
r.pop(); // removes a
|
||||
expect(a.attached, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('UnicodeV11.wcwidth', () {
|
||||
final w = unicodeV11.wcwidth;
|
||||
|
||||
test('control chars (<32) are zero-width', () {
|
||||
for (var c = 0; c < 32; c++) {
|
||||
expect(w(c), 0, reason: 'codepoint $c');
|
||||
}
|
||||
});
|
||||
|
||||
test('printable ASCII is width 1', () {
|
||||
for (var c = 32; c < 127; c++) {
|
||||
expect(w(c), 1, reason: 'codepoint $c');
|
||||
}
|
||||
});
|
||||
|
||||
test('DEL + C1 controls are width 0 (table)', () {
|
||||
expect(w(0x7f), 0); // DEL
|
||||
for (var c = 0x80; c < 0xa0; c++) {
|
||||
expect(w(c), 0, reason: 'C1 control $c');
|
||||
}
|
||||
});
|
||||
|
||||
test('combining marks (BMP_COMBINING) are width 0', () {
|
||||
expect(w(0x0301), 0); // combining acute accent
|
||||
expect(w(0x05BD), 0); // hebrew point meteg
|
||||
});
|
||||
|
||||
test('CJK ideographs (BMP_WIDE) are width 2', () {
|
||||
expect(w(0x4E2D), 2); // 中
|
||||
expect(w(0x4F60), 2); // 你
|
||||
});
|
||||
|
||||
test('high-plane combining marks return 0', () {
|
||||
// Pull a known HIGH_COMBINING entry — variation-selector range.
|
||||
expect(w(0xE0100), 0); // VARIATION SELECTOR-17
|
||||
});
|
||||
|
||||
test('high-plane wide chars return 2', () {
|
||||
// Emoji.
|
||||
expect(w(0x1F600), 2); // GRINNING FACE
|
||||
});
|
||||
|
||||
test('unmapped high codepoint defaults to width 1', () {
|
||||
// 0x100000 is in PUA-B; not in HIGH_WIDE / HIGH_COMBINING.
|
||||
expect(w(0x100000), 1);
|
||||
});
|
||||
|
||||
test('UnicodeV11.version is the expected unicode version', () {
|
||||
expect(UnicodeV11().version, '11');
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user