model.spec.ts 16 KB

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