Files
clide/test/terminal/utils/utils_test.dart
T
jpmschweitzerandClaude 6008b4914c 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>
2026-05-07 08:13:07 +02:00

438 lines
14 KiB
Dart

/// 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');
});
});
}