model.spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { CodeEditor } from '@jupyterlab/codeeditor';
  4. import {
  5. Completer,
  6. CompleterModel,
  7. CompletionHandler
  8. } from '@jupyterlab/completer';
  9. import { toArray } from '@lumino/algorithm';
  10. import { JSONExt } from '@lumino/coreutils';
  11. function makeState(text: string): Completer.ITextState {
  12. return {
  13. column: 0,
  14. lineHeight: 0,
  15. charWidth: 0,
  16. line: 0,
  17. coords: { left: 0, right: 0, top: 0, bottom: 0 } as CodeEditor.ICoordinate,
  18. text
  19. };
  20. }
  21. describe('completer/model', () => {
  22. describe('CompleterModel', () => {
  23. describe('#constructor()', () => {
  24. it('should create a completer model', () => {
  25. const model = new CompleterModel();
  26. expect(model).toBeInstanceOf(CompleterModel);
  27. expect(model.setCompletionItems).toBeDefined();
  28. });
  29. });
  30. describe('#stateChanged', () => {
  31. it('should signal when model options have changed', () => {
  32. const model = new CompleterModel();
  33. let called = 0;
  34. const listener = (sender: any, args: void) => {
  35. called++;
  36. };
  37. model.stateChanged.connect(listener);
  38. expect(called).toBe(0);
  39. model.setOptions(['foo']);
  40. expect(called).toBe(1);
  41. model.setOptions(['foo'], { foo: 'instance' });
  42. expect(called).toBe(2);
  43. });
  44. it('should signal when model items have changed', () => {
  45. let model = new CompleterModel();
  46. let called = 0;
  47. let listener = (sender: any, args: void) => {
  48. called++;
  49. };
  50. model.stateChanged.connect(listener);
  51. expect(called).toBe(0);
  52. model.setCompletionItems!([{ label: 'foo' }]);
  53. expect(called).toBe(1);
  54. model.setCompletionItems!([{ label: 'foo' }]);
  55. model.setCompletionItems!([{ label: 'foo' }, { label: 'bar' }]);
  56. expect(called).toBe(2);
  57. });
  58. it('should not signal when options have not changed', () => {
  59. const model = new CompleterModel();
  60. let called = 0;
  61. const listener = (sender: any, args: void) => {
  62. called++;
  63. };
  64. model.stateChanged.connect(listener);
  65. expect(called).toBe(0);
  66. model.setOptions(['foo']);
  67. model.setOptions(['foo']);
  68. expect(called).toBe(1);
  69. model.setOptions(['foo'], { foo: 'instance' });
  70. model.setOptions(['foo'], { foo: 'instance' });
  71. expect(called).toBe(2);
  72. model.setOptions([], {});
  73. model.setOptions([], {});
  74. expect(called).toBe(3);
  75. });
  76. it('should not signal when items have not changed', () => {
  77. let model = new CompleterModel();
  78. let called = 0;
  79. let listener = (sender: any, args: void) => {
  80. called++;
  81. };
  82. model.stateChanged.connect(listener);
  83. expect(called).toBe(0);
  84. model.setCompletionItems!([{ label: 'foo' }]);
  85. model.setCompletionItems!([{ label: 'foo' }]);
  86. expect(called).toBe(1);
  87. model.setCompletionItems!([{ label: 'foo' }, { label: 'bar' }]);
  88. model.setCompletionItems!([{ label: 'foo' }, { label: 'bar' }]);
  89. expect(called).toBe(2);
  90. model.setCompletionItems!([]);
  91. model.setCompletionItems!([]);
  92. expect(called).toBe(3);
  93. });
  94. it('should signal when original request changes', () => {
  95. const model = new CompleterModel();
  96. let called = 0;
  97. const listener = (sender: any, args: void) => {
  98. called++;
  99. };
  100. model.stateChanged.connect(listener);
  101. expect(called).toBe(0);
  102. model.original = makeState('foo');
  103. expect(called).toBe(1);
  104. model.original = null;
  105. expect(called).toBe(2);
  106. });
  107. it('should not signal when original request has not changed', () => {
  108. const model = new CompleterModel();
  109. let called = 0;
  110. const listener = (sender: any, args: void) => {
  111. called++;
  112. };
  113. model.stateChanged.connect(listener);
  114. expect(called).toBe(0);
  115. model.original = makeState('foo');
  116. model.original = makeState('foo');
  117. expect(called).toBe(1);
  118. model.original = null;
  119. model.original = null;
  120. expect(called).toBe(2);
  121. });
  122. it('should signal when current text changes', () => {
  123. const model = new CompleterModel();
  124. let called = 0;
  125. const currentValue = 'foo';
  126. const newValue = 'foob';
  127. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  128. const request = makeState(currentValue);
  129. const change = makeState(newValue);
  130. const listener = (sender: any, args: void) => {
  131. called++;
  132. };
  133. model.stateChanged.connect(listener);
  134. expect(called).toBe(0);
  135. model.original = request;
  136. expect(called).toBe(1);
  137. model.cursor = cursor;
  138. model.current = change;
  139. expect(called).toBe(2);
  140. model.current = null;
  141. expect(called).toBe(3);
  142. });
  143. it('should not signal when current text is unchanged', () => {
  144. const model = new CompleterModel();
  145. let called = 0;
  146. const currentValue = 'foo';
  147. const newValue = 'foob';
  148. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  149. const request = makeState(currentValue);
  150. const change = makeState(newValue);
  151. const listener = (sender: any, args: void) => {
  152. called++;
  153. };
  154. model.stateChanged.connect(listener);
  155. expect(called).toBe(0);
  156. model.original = request;
  157. expect(called).toBe(1);
  158. model.cursor = cursor;
  159. model.current = change;
  160. model.current = change;
  161. expect(called).toBe(2);
  162. model.current = null;
  163. model.current = null;
  164. expect(called).toBe(3);
  165. });
  166. });
  167. describe('#completionItems()', () => {
  168. it('should default to { items: [] }', () => {
  169. let model = new CompleterModel();
  170. let want: CompletionHandler.ICompletionItems = [];
  171. expect(model.completionItems!()).toEqual(want);
  172. });
  173. it('should return unmarked ICompletionItems if query is blank', () => {
  174. let model = new CompleterModel();
  175. let want: CompletionHandler.ICompletionItems = [
  176. { label: 'foo' },
  177. { label: 'bar' },
  178. { label: 'baz' }
  179. ];
  180. model.setCompletionItems!([
  181. { label: 'foo' },
  182. { label: 'bar' },
  183. { label: 'baz' }
  184. ]);
  185. expect(model.completionItems!()).toEqual(want);
  186. });
  187. it('should return a marked list of items if query is set', () => {
  188. let model = new CompleterModel();
  189. let want = '<mark>f</mark>oo';
  190. model.setCompletionItems!([
  191. { label: 'foo' },
  192. { label: 'bar' },
  193. { label: 'baz' }
  194. ]);
  195. model.query = 'f';
  196. expect(model.completionItems!().length).toEqual(1);
  197. expect(model.completionItems!()[0].label).toEqual(want);
  198. });
  199. it('should order list based on score', () => {
  200. const model = new CompleterModel();
  201. const want: CompletionHandler.ICompletionItems = [
  202. { insertText: 'qux', label: '<mark>qux</mark>' },
  203. { insertText: 'quux', label: '<mark>qu</mark>u<mark>x</mark>' }
  204. ];
  205. model.setCompletionItems!([
  206. { label: 'foo' },
  207. { label: 'bar' },
  208. { label: 'baz' },
  209. { label: 'quux' },
  210. { label: 'qux' }
  211. ]);
  212. model.query = 'qux';
  213. expect(model.completionItems!()).toEqual(want);
  214. });
  215. it('should break ties in score by locale sort', () => {
  216. const model = new CompleterModel();
  217. const want: CompletionHandler.ICompletionItems = [
  218. { insertText: 'quux', label: '<mark>qu</mark>ux' },
  219. { insertText: 'qux', label: '<mark>qu</mark>x' }
  220. ];
  221. model.setCompletionItems!([
  222. { label: 'foo' },
  223. { label: 'bar' },
  224. { label: 'baz' },
  225. { label: 'quux' },
  226. { label: 'qux' }
  227. ]);
  228. model.query = 'qu';
  229. expect(model.completionItems!()).toEqual(want);
  230. });
  231. it('should return { items: [] } if reset', () => {
  232. let model = new CompleterModel();
  233. let want: CompletionHandler.ICompletionItems = [];
  234. model.setCompletionItems!([
  235. { label: 'foo' },
  236. { label: 'bar' },
  237. { label: 'baz' }
  238. ]);
  239. model.reset();
  240. expect(model.completionItems!()).toEqual(want);
  241. });
  242. });
  243. describe('#items()', () => {
  244. it('should return an unfiltered list of items if query is blank', () => {
  245. const model = new CompleterModel();
  246. const want: Completer.IItem[] = [
  247. { raw: 'foo', text: 'foo' },
  248. { raw: 'bar', text: 'bar' },
  249. { raw: 'baz', text: 'baz' }
  250. ];
  251. model.setOptions(['foo', 'bar', 'baz']);
  252. expect(toArray(model.items())).toEqual(want);
  253. });
  254. it('should return a filtered list of items if query is set', () => {
  255. const model = new CompleterModel();
  256. const want: Completer.IItem[] = [
  257. { raw: 'foo', text: '<mark>f</mark>oo' }
  258. ];
  259. model.setOptions(['foo', 'bar', 'baz']);
  260. model.query = 'f';
  261. expect(toArray(model.items())).toEqual(want);
  262. });
  263. it('should order list based on score', () => {
  264. const model = new CompleterModel();
  265. const want: Completer.IItem[] = [
  266. { raw: 'qux', text: '<mark>qux</mark>' },
  267. { raw: 'quux', text: '<mark>qu</mark>u<mark>x</mark>' }
  268. ];
  269. model.setOptions(['foo', 'bar', 'baz', 'quux', 'qux']);
  270. model.query = 'qux';
  271. expect(toArray(model.items())).toEqual(want);
  272. });
  273. it('should break ties in score by locale sort', () => {
  274. const model = new CompleterModel();
  275. const want: Completer.IItem[] = [
  276. { raw: 'quux', text: '<mark>qu</mark>ux' },
  277. { raw: 'qux', text: '<mark>qu</mark>x' }
  278. ];
  279. model.setOptions(['foo', 'bar', 'baz', 'qux', 'quux']);
  280. model.query = 'qu';
  281. expect(toArray(model.items())).toEqual(want);
  282. });
  283. });
  284. describe('#options()', () => {
  285. it('should default to an empty iterator', () => {
  286. const model = new CompleterModel();
  287. expect(model.options().next()).toBeUndefined();
  288. });
  289. it('should return model options', () => {
  290. const model = new CompleterModel();
  291. const options = ['foo'];
  292. model.setOptions(options, {});
  293. expect(toArray(model.options())).not.toBe(options);
  294. expect(toArray(model.options())).toEqual(options);
  295. });
  296. it('should return the typeMap', () => {
  297. const model = new CompleterModel();
  298. const options = ['foo'];
  299. const typeMap = { foo: 'instance' };
  300. model.setOptions(options, typeMap);
  301. expect(JSONExt.deepEqual(model.typeMap(), typeMap)).toBeTruthy();
  302. });
  303. });
  304. describe('#original', () => {
  305. it('should default to null', () => {
  306. const model = new CompleterModel();
  307. expect(model.original).toBeNull();
  308. });
  309. it('should return the original request', () => {
  310. const model = new CompleterModel();
  311. const request = makeState('foo');
  312. model.original = request;
  313. expect(model.original).toBe(request);
  314. });
  315. });
  316. describe('#current', () => {
  317. it('should default to null', () => {
  318. const model = new CompleterModel();
  319. expect(model.current).toBeNull();
  320. });
  321. it('should initially equal the original request', () => {
  322. const model = new CompleterModel();
  323. const request = makeState('foo');
  324. model.original = request;
  325. expect(model.current).toBe(request);
  326. });
  327. it('should not set if original request is nonexistent', () => {
  328. const model = new CompleterModel();
  329. const currentValue = 'foo';
  330. const newValue = 'foob';
  331. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  332. const request = makeState(currentValue);
  333. const change = makeState(newValue);
  334. model.current = change;
  335. expect(model.current).toBeNull();
  336. model.original = request;
  337. model.cursor = cursor;
  338. model.current = change;
  339. expect(model.current).toBe(change);
  340. });
  341. it('should not set if cursor is nonexistent', () => {
  342. const model = new CompleterModel();
  343. const currentValue = 'foo';
  344. const newValue = 'foob';
  345. const request = makeState(currentValue);
  346. const change = makeState(newValue);
  347. model.original = request;
  348. model.cursor = null;
  349. model.current = change;
  350. expect(model.current).not.toBe(change);
  351. });
  352. it('should reset model if change is shorter than original', () => {
  353. const model = new CompleterModel();
  354. const currentValue = 'foo';
  355. const newValue = 'fo';
  356. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  357. const request = makeState(currentValue);
  358. const change = makeState(newValue);
  359. model.original = request;
  360. model.cursor = cursor;
  361. model.current = change;
  362. expect(model.current).toBeNull();
  363. expect(model.original).toBeNull();
  364. expect(model.options().next()).toBeUndefined();
  365. });
  366. });
  367. describe('#cursor', () => {
  368. it('should default to null', () => {
  369. const model = new CompleterModel();
  370. expect(model.cursor).toBeNull();
  371. });
  372. it('should not set if original request is nonexistent', () => {
  373. const model = new CompleterModel();
  374. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  375. const request = makeState('foo');
  376. model.cursor = cursor;
  377. expect(model.cursor).toBeNull();
  378. model.original = request;
  379. model.cursor = cursor;
  380. expect(model.cursor).toBe(cursor);
  381. });
  382. });
  383. describe('#isDisposed', () => {
  384. it('should be true if model has been disposed', () => {
  385. const model = new CompleterModel();
  386. expect(model.isDisposed).toBe(false);
  387. model.dispose();
  388. expect(model.isDisposed).toBe(true);
  389. });
  390. });
  391. describe('#dispose()', () => {
  392. it('should dispose of the model resources', () => {
  393. const model = new CompleterModel();
  394. model.setOptions(['foo'], { foo: 'instance' });
  395. expect(model.isDisposed).toBe(false);
  396. model.dispose();
  397. expect(model.isDisposed).toBe(true);
  398. });
  399. it('should be safe to call multiple times', () => {
  400. const model = new CompleterModel();
  401. expect(model.isDisposed).toBe(false);
  402. model.dispose();
  403. model.dispose();
  404. expect(model.isDisposed).toBe(true);
  405. });
  406. });
  407. describe('#handleTextChange()', () => {
  408. it('should set current change value', () => {
  409. const model = new CompleterModel();
  410. const currentValue = 'foo';
  411. const newValue = 'foob';
  412. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  413. const request = makeState(currentValue);
  414. const change = makeState(newValue);
  415. (change as any).column = 4;
  416. model.original = request;
  417. model.cursor = cursor;
  418. expect(model.current).toBe(request);
  419. model.handleTextChange(change);
  420. expect(model.current).toBe(change);
  421. });
  422. it('should reset if last char is whitespace && column < original', () => {
  423. const model = new CompleterModel();
  424. const currentValue = 'foo';
  425. const newValue = 'foo ';
  426. const request = makeState(currentValue);
  427. (request as any).column = 3;
  428. const change = makeState(newValue);
  429. (change as any).column = 0;
  430. model.original = request;
  431. expect(model.original).toBe(request);
  432. model.handleTextChange(change);
  433. expect(model.original).toBeNull();
  434. });
  435. });
  436. describe('#createPatch()', () => {
  437. it('should return a patch value', () => {
  438. const model = new CompleterModel();
  439. const patch = 'foobar';
  440. const want: Completer.IPatch = {
  441. start: 0,
  442. end: 3,
  443. value: patch
  444. };
  445. const cursor: Completer.ICursorSpan = { start: 0, end: 3 };
  446. model.original = makeState('foo');
  447. model.cursor = cursor;
  448. expect(model.createPatch(patch)).toEqual(want);
  449. });
  450. it('should return undefined if original request or cursor are null', () => {
  451. const model = new CompleterModel();
  452. expect(model.createPatch('foo')).toBeUndefined();
  453. });
  454. it('should handle line breaks in original value', () => {
  455. const model = new CompleterModel();
  456. const currentValue = 'foo\nbar';
  457. const patch = 'barbaz';
  458. const start = currentValue.length;
  459. const end = currentValue.length;
  460. const want: Completer.IPatch = {
  461. start,
  462. end,
  463. value: patch
  464. };
  465. const cursor: Completer.ICursorSpan = { start, end };
  466. model.original = makeState(currentValue);
  467. model.cursor = cursor;
  468. expect(model.createPatch(patch)).toEqual(want);
  469. });
  470. });
  471. });
  472. });