Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spec-name-tokenizer.spec.ts 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. import nameTokenizer from '../../src/parser/tokenizers/name';
  2. import { seedTokens, seedSpec } from '../../src/util';
  3. const tokenize = nameTokenizer();
  4. test('single word', () => {
  5. expect(
  6. tokenize(
  7. seedSpec({
  8. source: [
  9. {
  10. number: 1,
  11. source: '...',
  12. tokens: seedTokens({ description: 'param param description 0' }),
  13. },
  14. ],
  15. })
  16. )
  17. ).toEqual(
  18. seedSpec({
  19. name: 'param',
  20. source: [
  21. {
  22. number: 1,
  23. source: '...',
  24. tokens: seedTokens({
  25. name: 'param',
  26. postName: ' ',
  27. description: 'param description 0',
  28. }),
  29. },
  30. ],
  31. })
  32. );
  33. });
  34. test('dash-delimitered', () => {
  35. expect(
  36. tokenize(
  37. seedSpec({
  38. source: [
  39. {
  40. number: 1,
  41. source: '...',
  42. tokens: seedTokens({ description: 'param-param description 0' }),
  43. },
  44. ],
  45. })
  46. )
  47. ).toEqual(
  48. seedSpec({
  49. name: 'param-param',
  50. source: [
  51. {
  52. number: 1,
  53. source: '...',
  54. tokens: seedTokens({
  55. name: 'param-param',
  56. postName: ' ',
  57. description: 'description 0',
  58. }),
  59. },
  60. ],
  61. })
  62. );
  63. });
  64. test('quoted', () => {
  65. expect(
  66. tokenize(
  67. seedSpec({
  68. source: [
  69. {
  70. number: 1,
  71. source: '...',
  72. tokens: seedTokens({ description: '"param param" description 0' }),
  73. },
  74. ],
  75. })
  76. )
  77. ).toEqual(
  78. seedSpec({
  79. name: 'param param',
  80. source: [
  81. {
  82. number: 1,
  83. source: '...',
  84. tokens: seedTokens({
  85. name: '"param param"',
  86. postName: ' ',
  87. description: 'description 0',
  88. }),
  89. },
  90. ],
  91. })
  92. );
  93. });
  94. test('inconsistent quotes', () => {
  95. expect(
  96. tokenize(
  97. seedSpec({
  98. source: [
  99. {
  100. number: 1,
  101. source: '...',
  102. tokens: seedTokens({ description: '"param param description 0' }),
  103. },
  104. ],
  105. })
  106. )
  107. ).toEqual(
  108. seedSpec({
  109. name: '"param',
  110. source: [
  111. {
  112. number: 1,
  113. source: '...',
  114. tokens: seedTokens({
  115. name: '"param',
  116. postName: ' ',
  117. description: 'param description 0',
  118. }),
  119. },
  120. ],
  121. })
  122. );
  123. });
  124. test('optional', () => {
  125. expect(
  126. tokenize(
  127. seedSpec({
  128. source: [
  129. {
  130. number: 1,
  131. source: '...',
  132. tokens: seedTokens({ description: '[param] param description' }),
  133. },
  134. ],
  135. })
  136. )
  137. ).toEqual(
  138. seedSpec({
  139. name: 'param',
  140. optional: true,
  141. source: [
  142. {
  143. number: 1,
  144. source: '...',
  145. tokens: seedTokens({
  146. name: '[param]',
  147. postName: ' ',
  148. description: 'param description',
  149. }),
  150. },
  151. ],
  152. })
  153. );
  154. });
  155. test('optional with default', () => {
  156. expect(
  157. tokenize(
  158. seedSpec({
  159. source: [
  160. {
  161. number: 1,
  162. source: '...',
  163. tokens: seedTokens({
  164. description: '[param=value] param description',
  165. }),
  166. },
  167. ],
  168. })
  169. )
  170. ).toEqual(
  171. seedSpec({
  172. name: 'param',
  173. optional: true,
  174. default: 'value',
  175. source: [
  176. {
  177. number: 1,
  178. source: '...',
  179. tokens: seedTokens({
  180. name: '[param=value]',
  181. postName: ' ',
  182. description: 'param description',
  183. }),
  184. },
  185. ],
  186. })
  187. );
  188. });
  189. test('quoted default', () => {
  190. expect(
  191. tokenize(
  192. seedSpec({
  193. source: [
  194. {
  195. number: 1,
  196. source: '...',
  197. tokens: seedTokens({
  198. description: '[param="value"] param description',
  199. }),
  200. },
  201. ],
  202. })
  203. )
  204. ).toEqual(
  205. seedSpec({
  206. name: 'param',
  207. optional: true,
  208. default: '"value"',
  209. source: [
  210. {
  211. number: 1,
  212. source: '...',
  213. tokens: seedTokens({
  214. name: '[param="value"]',
  215. postName: ' ',
  216. description: 'param description',
  217. }),
  218. },
  219. ],
  220. })
  221. );
  222. });
  223. test('loosely quoted default', () => {
  224. expect(
  225. tokenize(
  226. seedSpec({
  227. source: [
  228. {
  229. number: 1,
  230. source: '...',
  231. tokens: seedTokens({
  232. description: '[param="value] param description',
  233. }),
  234. },
  235. ],
  236. })
  237. )
  238. ).toEqual(
  239. seedSpec({
  240. name: 'param',
  241. optional: true,
  242. default: '"value',
  243. source: [
  244. {
  245. number: 1,
  246. source: '...',
  247. tokens: seedTokens({
  248. name: '[param="value]',
  249. postName: ' ',
  250. description: 'param description',
  251. }),
  252. },
  253. ],
  254. })
  255. );
  256. });
  257. test('quoted default with =', () => {
  258. expect(
  259. tokenize(
  260. seedSpec({
  261. source: [
  262. {
  263. number: 1,
  264. source: '...',
  265. tokens: seedTokens({
  266. description: '[param="value=1"] param description',
  267. }),
  268. },
  269. ],
  270. })
  271. )
  272. ).toEqual(
  273. seedSpec({
  274. name: 'param',
  275. optional: true,
  276. default: '"value=1"',
  277. source: [
  278. {
  279. number: 1,
  280. source: '...',
  281. tokens: seedTokens({
  282. name: '[param="value=1"]',
  283. postName: ' ',
  284. description: 'param description',
  285. }),
  286. },
  287. ],
  288. })
  289. );
  290. });
  291. test('non-alphanumeric', () => {
  292. expect(
  293. tokenize(
  294. seedSpec({
  295. source: [
  296. {
  297. number: 1,
  298. source: '...',
  299. tokens: seedTokens({
  300. description: '$param description',
  301. }),
  302. },
  303. ],
  304. })
  305. )
  306. ).toEqual(
  307. seedSpec({
  308. name: '$param',
  309. source: [
  310. {
  311. number: 1,
  312. source: '...',
  313. tokens: seedTokens({
  314. name: '$param',
  315. postName: ' ',
  316. description: 'description',
  317. }),
  318. },
  319. ],
  320. })
  321. );
  322. });
  323. test('spread notation', () => {
  324. expect(
  325. tokenize(
  326. seedSpec({
  327. source: [
  328. {
  329. number: 1,
  330. source: '...',
  331. tokens: seedTokens({
  332. description: '...params description',
  333. }),
  334. },
  335. ],
  336. })
  337. )
  338. ).toEqual(
  339. seedSpec({
  340. name: '...params',
  341. source: [
  342. {
  343. number: 1,
  344. source: '...',
  345. tokens: seedTokens({
  346. name: '...params',
  347. postName: ' ',
  348. description: 'description',
  349. }),
  350. },
  351. ],
  352. })
  353. );
  354. });
  355. test('optionsl spread notation', () => {
  356. expect(
  357. tokenize(
  358. seedSpec({
  359. source: [
  360. {
  361. number: 1,
  362. source: '...',
  363. tokens: seedTokens({
  364. description: '[...params] description',
  365. }),
  366. },
  367. ],
  368. })
  369. )
  370. ).toEqual(
  371. seedSpec({
  372. name: '...params',
  373. optional: true,
  374. source: [
  375. {
  376. number: 1,
  377. source: '...',
  378. tokens: seedTokens({
  379. name: '[...params]',
  380. postName: ' ',
  381. description: 'description',
  382. }),
  383. },
  384. ],
  385. })
  386. );
  387. });
  388. test('optional multiple words', () => {
  389. expect(
  390. tokenize(
  391. seedSpec({
  392. source: [
  393. {
  394. number: 1,
  395. source: '...',
  396. tokens: seedTokens({
  397. description: '[param name] param description',
  398. }),
  399. },
  400. ],
  401. })
  402. )
  403. ).toEqual(
  404. seedSpec({
  405. name: 'param name',
  406. optional: true,
  407. source: [
  408. {
  409. number: 1,
  410. source: '...',
  411. tokens: seedTokens({
  412. name: '[param name]',
  413. postName: ' ',
  414. description: 'param description',
  415. }),
  416. },
  417. ],
  418. })
  419. );
  420. });
  421. test('name spacing', () => {
  422. expect(
  423. tokenize(
  424. seedSpec({
  425. source: [
  426. {
  427. number: 1,
  428. source: '...',
  429. tokens: seedTokens({
  430. description: '[ param = value ] param description',
  431. }),
  432. },
  433. ],
  434. })
  435. )
  436. ).toEqual(
  437. seedSpec({
  438. name: 'param',
  439. optional: true,
  440. default: 'value',
  441. source: [
  442. {
  443. number: 1,
  444. source: '...',
  445. tokens: seedTokens({
  446. name: '[ param = value ]',
  447. postName: ' ',
  448. description: 'param description',
  449. }),
  450. },
  451. ],
  452. })
  453. );
  454. });
  455. test('inconsistent brackets', () => {
  456. expect(
  457. tokenize(
  458. seedSpec({
  459. source: [
  460. {
  461. number: 1,
  462. source: '...',
  463. tokens: seedTokens({
  464. description: '[param param description',
  465. }),
  466. },
  467. ],
  468. })
  469. )
  470. ).toEqual(
  471. seedSpec({
  472. problems: [
  473. {
  474. code: 'spec:name:unpaired-brackets',
  475. line: 1,
  476. critical: true,
  477. message: 'unpaired brackets',
  478. },
  479. ],
  480. source: [
  481. {
  482. number: 1,
  483. source: '...',
  484. tokens: seedTokens({
  485. description: '[param param description',
  486. }),
  487. },
  488. ],
  489. })
  490. );
  491. });
  492. test('empty name', () => {
  493. expect(
  494. tokenize(
  495. seedSpec({
  496. source: [
  497. {
  498. number: 1,
  499. source: '...',
  500. tokens: seedTokens({
  501. description: '[=value] param description',
  502. }),
  503. },
  504. ],
  505. })
  506. )
  507. ).toEqual(
  508. seedSpec({
  509. problems: [
  510. {
  511. code: 'spec:name:empty-name',
  512. line: 1,
  513. critical: true,
  514. message: 'empty name',
  515. },
  516. ],
  517. source: [
  518. {
  519. number: 1,
  520. source: '...',
  521. tokens: seedTokens({
  522. description: '[=value] param description',
  523. }),
  524. },
  525. ],
  526. })
  527. );
  528. });
  529. test('empty default value', () => {
  530. expect(
  531. tokenize(
  532. seedSpec({
  533. source: [
  534. {
  535. number: 1,
  536. source: '...',
  537. tokens: seedTokens({
  538. description: '[param=] param description',
  539. }),
  540. },
  541. ],
  542. })
  543. )
  544. ).toEqual(
  545. seedSpec({
  546. problems: [
  547. {
  548. code: 'spec:name:empty-default',
  549. line: 1,
  550. critical: true,
  551. message: 'empty default value',
  552. },
  553. ],
  554. source: [
  555. {
  556. number: 1,
  557. source: '...',
  558. tokens: seedTokens({
  559. description: '[param=] param description',
  560. }),
  561. },
  562. ],
  563. })
  564. );
  565. });
  566. test('empty', () => {
  567. expect(
  568. tokenize(
  569. seedSpec({
  570. source: [
  571. {
  572. number: 1,
  573. source: '...',
  574. tokens: seedTokens({
  575. description: '[] param description',
  576. }),
  577. },
  578. ],
  579. })
  580. )
  581. ).toEqual(
  582. seedSpec({
  583. problems: [
  584. {
  585. code: 'spec:name:empty-name',
  586. line: 1,
  587. critical: true,
  588. message: 'empty name',
  589. },
  590. ],
  591. source: [
  592. {
  593. number: 1,
  594. source: '...',
  595. tokens: seedTokens({
  596. description: '[] param description',
  597. }),
  598. },
  599. ],
  600. })
  601. );
  602. });
  603. test('default value syntax', () => {
  604. expect(
  605. tokenize(
  606. seedSpec({
  607. source: [
  608. {
  609. number: 1,
  610. source: '...',
  611. tokens: seedTokens({
  612. description: '[param=value=value] param description',
  613. }),
  614. },
  615. ],
  616. })
  617. )
  618. ).toEqual(
  619. seedSpec({
  620. problems: [
  621. {
  622. code: 'spec:name:invalid-default',
  623. line: 1,
  624. critical: true,
  625. message: 'invalid default value syntax',
  626. },
  627. ],
  628. source: [
  629. {
  630. number: 1,
  631. source: '...',
  632. tokens: seedTokens({
  633. description: '[param=value=value] param description',
  634. }),
  635. },
  636. ],
  637. })
  638. );
  639. });
  640. test('default with arrow', () => {
  641. expect(
  642. tokenize(
  643. seedSpec({
  644. source: [
  645. {
  646. number: 1,
  647. source: '...',
  648. tokens: seedTokens({
  649. description: '[param = value => value] param description',
  650. }),
  651. },
  652. ],
  653. })
  654. )
  655. ).toEqual(
  656. seedSpec({
  657. name: 'param',
  658. optional: true,
  659. default: 'value => value',
  660. source: [
  661. {
  662. number: 1,
  663. source: '...',
  664. tokens: seedTokens({
  665. name: '[param = value => value]',
  666. postName: ' ',
  667. description: 'param description',
  668. }),
  669. },
  670. ],
  671. })
  672. );
  673. });
  674. test('after multiline {type}', () => {
  675. const sourceIn = [
  676. {
  677. number: 0,
  678. source: '...',
  679. tokens: seedTokens({
  680. tag: '@aram',
  681. postTag: ' ',
  682. type: '{function(',
  683. }),
  684. },
  685. {
  686. number: 1,
  687. source: '...',
  688. tokens: seedTokens({ type: ' number' }),
  689. },
  690. {
  691. number: 2,
  692. source: '...',
  693. tokens: seedTokens({
  694. type: ')}',
  695. postType: ' ',
  696. description: 'paramname description text',
  697. }),
  698. },
  699. ];
  700. const sourceOut = JSON.parse(JSON.stringify(sourceIn));
  701. Object.assign(sourceOut[2].tokens, {
  702. name: 'paramname',
  703. postName: ' ',
  704. description: 'description text',
  705. });
  706. expect(tokenize(seedSpec({ source: sourceIn }))).toEqual(
  707. seedSpec({
  708. name: 'paramname',
  709. source: sourceOut,
  710. })
  711. );
  712. });