rob-revisited/ide/ui/js/expr-at.test.mjs

81 lines
3.7 KiB
JavaScript

// The expression under the mouse for the debugger's hover inspector.
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/expr-at.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { expressionAt, isPlainChain } from './expr-at.js';
/** The expression when the mouse is on the first occurrence of `on` in `line` (its `nth` character). */
const at = (line, on, nth = 0) => expressionAt(line, line.indexOf(on) + nth)?.expression ?? null;
test('a variable', () => {
assert.equal(at('$score += 1;', 'score'), '$score');
assert.equal(at('$score += 1;', '$score'), '$score');
assert.equal(at('return $this;', 'this'), '$this');
});
test('a property chain stops at the segment under the mouse', () => {
const line = 'echo $user->profile->name;';
assert.equal(at(line, 'user'), '$user');
assert.equal(at(line, 'profile'), '$user->profile');
assert.equal(at(line, 'name'), '$user->profile->name');
assert.equal(at(line, '->name'), '$user->profile->name', 'the arrow belongs to the segment it leads to');
assert.equal(at('$a?->b', 'b'), '$a?->b');
});
test('array keys: the whole access, strings and numbers', () => {
assert.equal(at("echo $a['x'];", "'x'"), "$a['x']");
assert.equal(at("echo $a['x'];", 'x'), "$a['x']");
assert.equal(at('echo $list[3][4];', '4'), '$list[3][4]');
assert.equal(at('echo $list[3][4];', '3'), '$list[3]');
assert.equal(at('$this->rows["a]b"]->hp', 'hp'), '$this->rows["a]b"]->hp', 'a bracket in a string does not close it');
});
test('a variable inside a key is its own expression', () => {
assert.equal(at('echo $items[$i]->hp;', '$i]'), '$i');
assert.equal(at('echo $items[$i]->hp;', 'items'), '$items');
assert.equal(at('echo $items[$i]->hp;', 'hp'), '$items[$i]->hp');
assert.equal(at('echo $a[$b[$c]];', '$c'), '$c');
assert.equal(at('echo $a[$b[$c]];', '[$c'), '$b[$c]');
});
test('static members and constants', () => {
assert.equal(at('echo self::$count + 1;', 'count'), 'self::$count');
assert.equal(at('echo \\App\\Game::$best;', 'best'), '\\App\\Game::$best');
assert.equal(at('echo $hero::MAX;', 'MAX'), '$hero::MAX');
assert.equal(at('echo $hero::$total->n;', 'n'), '$hero::$total->n');
});
test('a call is never part of the expression (hovering does not run code)', () => {
const line = '$x = $user->getName($id);';
assert.equal(at(line, 'getName'), null);
assert.equal(at(line, 'user'), '$user');
assert.equal(at(line, '$id'), '$id');
assert.equal(at('$a->b->c()->d', 'd'), null, 'after a call the chain is lost');
assert.equal(at('$a->b->c()->d', 'b'), '$a->b');
});
test('not an expression: words, numbers, spaces, comments of nothing', () => {
assert.equal(at('function update(float $dt)', 'update'), null);
assert.equal(at('function update(float $dt)', 'float'), null);
assert.equal(at('$a = 12;', '12'), null);
assert.equal(at('$a = 12;', ' = '), null);
assert.equal(at('foo$bar', 'foo'), null);
assert.equal(expressionAt('$a', 5), null);
assert.equal(expressionAt('$a', -1), null);
assert.equal(at('$a[] = 1;', '[]'), null, 'an append is not readable');
});
test('inside a double-quoted string', () => {
assert.equal(at('echo "Hello $name!";', 'name'), '$name');
assert.equal(at('echo "Hi {$user->name}";', 'name'), '$user->name');
});
test('the range and the plain-chain test', () => {
const r = expressionAt(' echo $user->name;', 12);
assert.deepEqual(r, { expression: '$user->name', start: 7, end: 18 });
assert.equal(isPlainChain('$user->name'), true);
assert.equal(isPlainChain("$a['x']->y"), true);
assert.equal(isPlainChain('count($a)'), false);
assert.equal(isPlainChain('$a + 1'), false);
assert.equal(isPlainChain('$a->b()'), false);
});