model.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { toArray } from '@lumino/algorithm';
  4. import { JSONExt } from '@lumino/coreutils';
  5. import { CodeEditor } from '@jupyterlab/codeeditor';
  6. import {
  7. CompleterModel,
  8. Completer,
  9. CompletionHandler
  10. } from '@jupyterlab/completer';
  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 return { items: [] } if reset', () => {
  200. let model = new CompleterModel();
  201. let want: CompletionHandler.ICompletionItems = [];
  202. model.setCompletionItems!([
  203. { label: 'foo' },
  204. { label: 'bar' },
  205. { label: 'baz' }
  206. ]);
  207. model.reset();
  208. expect(model.completionItems!()).toEqual(want);
  209. });
  210. });
  211. describe('#items()', () => {
  212. it('should return an unfiltered list of items if query is blank', () => {
  213. const model = new CompleterModel();
  214. const want: Completer.IItem[] = [
  215. { raw: 'foo', text: 'foo' },
  216. { raw: 'bar', text: 'bar' },
  217. { raw: 'baz', text: 'baz' }
  218. ];
  219. model.setOptions(['foo', 'bar', 'baz']);
  220. expect(toArray(model.items())).toEqual(want);
  221. });
  222. it('should return a filtered list of items if query is set', () => {
  223. const model = new CompleterModel();
  224. const want: Completer.IItem[] = [
  225. { raw: 'foo', text: '<mark>f</mark>oo' }
  226. ];
  227. model.setOptions(['foo', 'bar', 'baz']);
  228. model.query = 'f';
  229. expect(toArray(model.items())).toEqual(want);
  230. });
  231. it('should order list based on score', () => {
  232. const model = new CompleterModel();
  233. const want: Completer.IItem[] = [
  234. { raw: 'qux', text: '<mark>qux</mark>' },
  235. { raw: 'quux', text: '<mark>qu</mark>u<mark>x</mark>' }
  236. ];
  237. model.setOptions(['foo', 'bar', 'baz', 'quux', 'qux']);
  238. model.query = 'qux';
  239. expect(toArray(model.items())).toEqual(want);
  240. });
  241. it('should break ties in score by locale sort', () => {
  242. const model = new CompleterModel();
  243. const want: Completer.IItem[] = [
  244. { raw: 'quux', text: '<mark>qu</mark>ux' },
  245. { raw: 'qux', text: '<mark>qu</mark>x' }
  246. ];
  247. model.setOptions(['foo', 'bar', 'baz', 'qux', 'quux']);
  248. model.query = 'qu';
  249. expect(toArray(model.items())).toEqual(want);
  250. });
  251. });
  252. describe('#options()', () => {
  253. it('should default to an empty iterator', () => {
  254. const model = new CompleterModel();
  255. expect(model.options().next()).toBeUndefined();
  256. });
  257. it('should return model options', () => {
  258. const model = new CompleterModel();
  259. const options = ['foo'];
  260. model.setOptions(options, {});
  261. expect(toArray(model.options())).not.toBe(options);
  262. expect(toArray(model.options())).toEqual(options);
  263. });
  264. it('should return the typeMap', () => {
  265. const model = new CompleterModel();
  266. const options = ['foo'];
  267. const typeMap = { foo: 'instance' };
  268. model.setOptions(options, typeMap);
  269. expect(JSONExt.deepEqual(model.typeMap(), typeMap)).toBeTruthy();
  270. });
  271. });
  272. describe('#original', () => {
  273. it('should default to null', () => {
  274. const model = new CompleterModel();
  275. expect(model.original).toBeNull();
  276. });
  277. it('should return the original request', () => {
  278. const model = new CompleterModel();
  279. const request = makeState('foo');
  280. model.original = request;
  281. expect(model.original).toBe(request);
  282. });
  283. });
  284. describe('#current', () => {
  285. it('should default to null', () => {
  286. const model = new CompleterModel();
  287. expect(model.current).toBeNull();
  288. });
  289. it('should initially equal the original request', () => {
  290. const model = new CompleterModel();
  291. const request = makeState('foo');
  292. model.original = request;
  293. expect(model.current).toBe(request);
  294. });
  295. it('should not set if original request is nonexistent', () => {
  296. const model = new CompleterModel();
  297. const currentValue = 'foo';
  298. const newValue = 'foob';
  299. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  300. const request = makeState(currentValue);
  301. const change = makeState(newValue);
  302. model.current = change;
  303. expect(model.current).toBeNull();
  304. model.original = request;
  305. model.cursor = cursor;
  306. model.current = change;
  307. expect(model.current).toBe(change);
  308. });
  309. it('should not set if cursor is nonexistent', () => {
  310. const model = new CompleterModel();
  311. const currentValue = 'foo';
  312. const newValue = 'foob';
  313. const request = makeState(currentValue);
  314. const change = makeState(newValue);
  315. model.original = request;
  316. model.cursor = null;
  317. model.current = change;
  318. expect(model.current).not.toBe(change);
  319. });
  320. it('should reset model if change is shorter than original', () => {
  321. const model = new CompleterModel();
  322. const currentValue = 'foo';
  323. const newValue = 'fo';
  324. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  325. const request = makeState(currentValue);
  326. const change = makeState(newValue);
  327. model.original = request;
  328. model.cursor = cursor;
  329. model.current = change;
  330. expect(model.current).toBeNull();
  331. expect(model.original).toBeNull();
  332. expect(model.options().next()).toBeUndefined();
  333. });
  334. });
  335. describe('#cursor', () => {
  336. it('should default to null', () => {
  337. const model = new CompleterModel();
  338. expect(model.cursor).toBeNull();
  339. });
  340. it('should not set if original request is nonexistent', () => {
  341. const model = new CompleterModel();
  342. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  343. const request = makeState('foo');
  344. model.cursor = cursor;
  345. expect(model.cursor).toBeNull();
  346. model.original = request;
  347. model.cursor = cursor;
  348. expect(model.cursor).toBe(cursor);
  349. });
  350. });
  351. describe('#isDisposed', () => {
  352. it('should be true if model has been disposed', () => {
  353. const model = new CompleterModel();
  354. expect(model.isDisposed).toBe(false);
  355. model.dispose();
  356. expect(model.isDisposed).toBe(true);
  357. });
  358. });
  359. describe('#dispose()', () => {
  360. it('should dispose of the model resources', () => {
  361. const model = new CompleterModel();
  362. model.setOptions(['foo'], { foo: 'instance' });
  363. expect(model.isDisposed).toBe(false);
  364. model.dispose();
  365. expect(model.isDisposed).toBe(true);
  366. });
  367. it('should be safe to call multiple times', () => {
  368. const model = new CompleterModel();
  369. expect(model.isDisposed).toBe(false);
  370. model.dispose();
  371. model.dispose();
  372. expect(model.isDisposed).toBe(true);
  373. });
  374. });
  375. describe('#handleTextChange()', () => {
  376. it('should set current change value', () => {
  377. const model = new CompleterModel();
  378. const currentValue = 'foo';
  379. const newValue = 'foob';
  380. const cursor: Completer.ICursorSpan = { start: 0, end: 0 };
  381. const request = makeState(currentValue);
  382. const change = makeState(newValue);
  383. (change as any).column = 4;
  384. model.original = request;
  385. model.cursor = cursor;
  386. expect(model.current).toBe(request);
  387. model.handleTextChange(change);
  388. expect(model.current).toBe(change);
  389. });
  390. it('should reset if last char is whitespace && column < original', () => {
  391. const model = new CompleterModel();
  392. const currentValue = 'foo';
  393. const newValue = 'foo ';
  394. const request = makeState(currentValue);
  395. (request as any).column = 3;
  396. const change = makeState(newValue);
  397. (change as any).column = 0;
  398. model.original = request;
  399. expect(model.original).toBe(request);
  400. model.handleTextChange(change);
  401. expect(model.original).toBeNull();
  402. });
  403. });
  404. describe('#createPatch()', () => {
  405. it('should return a patch value', () => {
  406. const model = new CompleterModel();
  407. const patch = 'foobar';
  408. const want: Completer.IPatch = {
  409. start: 0,
  410. end: 3,
  411. value: patch
  412. };
  413. const cursor: Completer.ICursorSpan = { start: 0, end: 3 };
  414. model.original = makeState('foo');
  415. model.cursor = cursor;
  416. expect(model.createPatch(patch)).toEqual(want);
  417. });
  418. it('should return undefined if original request or cursor are null', () => {
  419. const model = new CompleterModel();
  420. expect(model.createPatch('foo')).toBeUndefined();
  421. });
  422. it('should handle line breaks in original value', () => {
  423. const model = new CompleterModel();
  424. const currentValue = 'foo\nbar';
  425. const patch = 'barbaz';
  426. const start = currentValue.length;
  427. const end = currentValue.length;
  428. const want: Completer.IPatch = {
  429. start,
  430. end,
  431. value: patch
  432. };
  433. const cursor: Completer.ICursorSpan = { start, end };
  434. model.original = makeState(currentValue);
  435. model.cursor = cursor;
  436. expect(model.createPatch(patch)).toEqual(want);
  437. });
  438. });
  439. });
  440. });