Best JavaScript code snippet using best
comments.js
Source:comments.js
...3describe('parse comments', () => {4 describe('body', () => {5 test('directives', () => {6 const src = '#comment\n%YAML 1.2 #comment\n---\nstring\n'7 const doc = YAML.parseDocument(src)8 expect(doc.commentBefore).toBe('comment\ncomment')9 expect(String(doc)).toBe('#comment\n#comment\n\n%YAML 1.2\n---\nstring\n')10 })11 test('body start comments', () => {12 const src = source`13 ---14 #comment15 #comment16 string17 `18 const doc = YAML.parseDocument(src)19 expect(doc.contents.commentBefore).toBe('comment\ncomment')20 expect(String(doc)).toBe(src)21 })22 test('body start comments with empty comment line', () => {23 const src = source`24 ---25 #comment26 #27 #comment28 string29 `30 const doc = YAML.parseDocument(src)31 expect(doc.contents.commentBefore).toBe('comment\n \ncomment')32 expect(String(doc)).toBe(source`33 ---34 #comment35 #36 #comment37 string38 `)39 })40 test('body end comments', () => {41 const src = '\nstring\n#comment\n#comment\n'42 const doc = YAML.parseDocument(src)43 expect(doc.comment).toBe('comment\ncomment')44 expect(String(doc)).toBe('string\n\n#comment\n#comment\n')45 })46 })47 describe('top-level scalar comments', () => {48 test('plain', () => {49 const src = '#c0\nvalue #c1\n#c2'50 const doc = YAML.parseDocument(src)51 expect(doc.contents.commentBefore).toBe('c0')52 expect(doc.contents.comment).toBe('c1')53 expect(doc.comment).toBe('c2')54 expect(doc.contents.value).toBe('value')55 expect(doc.contents.range).toMatchObject([4, 9, 14])56 })57 test('"quoted"', () => {58 const src = '#c0\n"value" #c1\n#c2'59 const doc = YAML.parseDocument(src)60 expect(doc.contents.commentBefore).toBe('c0')61 expect(doc.contents.comment).toBe('c1')62 expect(doc.comment).toBe('c2')63 expect(doc.contents.value).toBe('value')64 expect(doc.contents.range).toMatchObject([4, 11, 16])65 })66 test('block', () => {67 const src = '#c0\n>- #c1\n value\n#c2\n'68 const doc = YAML.parseDocument(src)69 expect(doc.contents.commentBefore).toBe('c0')70 expect(doc.contents.comment).toBe('c1')71 expect(doc.comment).toBe('c2')72 expect(doc.contents.value).toBe('value')73 expect(doc.contents.range).toMatchObject([4, 18, 18])74 })75 })76 describe('seq entry comments', () => {77 test('plain', () => {78 const src = source`79 #c080 - value 181 #c182 - value 283 #c284 `85 const doc = YAML.parseDocument(src)86 expect(doc).toMatchObject({87 contents: {88 items: [89 { commentBefore: 'c0', value: 'value 1', comment: 'c1' },90 { value: 'value 2' }91 ],92 range: [4, 31, 31]93 },94 comment: 'c2'95 })96 })97 test('multiline', () => {98 const src = source`99 - value 1100 #c0101 #c1102 #c2103 - value 2104 #c3105 #c4106 `107 const doc = YAML.parseDocument(src)108 expect(doc).toMatchObject({109 contents: {110 items: [{ comment: 'c0' }, { commentBefore: 'c1\n\nc2' }]111 },112 comment: 'c3\nc4'113 })114 })115 })116 describe('map entry comments', () => {117 test('plain', () => {118 const src = source`119 #c0120 key1: value 1121 #c1122 key2: value 2123 #c2124 `125 const doc = YAML.parseDocument(src)126 expect(doc).toMatchObject({127 contents: {128 items: [129 { key: { commentBefore: 'c0' }, value: { comment: 'c1' } },130 { key: {}, value: {} }131 ]132 },133 comment: 'c2'134 })135 })136 test('multiline', () => {137 const src = source`138 key1: value 1139 #c0140 #c1141 #c2142 key2: value 2143 #c3144 #c4145 `146 const doc = YAML.parseDocument(src)147 expect(doc).toMatchObject({148 contents: {149 items: [150 { value: { comment: 'c0' } },151 { key: { commentBefore: 'c1\n\nc2' } }152 ]153 },154 comment: 'c3\nc4'155 })156 })157 })158 describe('map-in-seq comments', () => {159 test('plain', () => {160 const src = source`161 #c0162 - #c1163 k1: v1164 #c2165 k2: v2 #c3166 #c4167 k3: v3168 #c5169 `170 const doc = YAML.parseDocument(src)171 expect(doc).toMatchObject({172 contents: {173 items: [174 {175 commentBefore: 'c0\nc1',176 items: [177 {},178 { key: { commentBefore: 'c2' }, value: { comment: 'c3' } },179 { key: { commentBefore: 'c4' } }180 ]181 }182 ]183 },184 comment: 'c5'185 })186 expect(String(doc)).toBe(source`187 #c0188 #c1189 - k1: v1190 #c2191 k2: v2 #c3192 #c4193 k3: v3194 #c5195 `)196 })197 })198 describe('seq-in-map comments', () => {199 test('plain', () => {200 const src = source`201 #c0202 k1: #c1203 - v1204 #c2205 - v2206 #c3207 k2:208 - v3 #c4209 #c5210 `211 const doc = YAML.parseDocument(src)212 expect(doc).toMatchObject({213 contents: {214 items: [215 {216 key: { commentBefore: 'c0', value: 'k1' },217 value: {218 commentBefore: 'c1',219 items: [{ value: 'v1' }, { commentBefore: 'c2', value: 'v2' }],220 comment: 'c3'221 }222 },223 {224 key: { value: 'k2' },225 value: { items: [{ value: 'v3', comment: 'c4' }] }226 }227 ]228 },229 comment: 'c5'230 })231 expect(String(doc)).toBe(source`232 #c0233 k1:234 #c1235 - v1236 #c2237 - v2238 #c3239 k2:240 - v3 #c4241 #c5242 `)243 })244 })245 describe('flow collection commens', () => {246 test('line comment after , in seq', () => {247 const doc = YAML.parseDocument(source`248 [ a, #c0249 b #c1250 ]`)251 expect(doc.contents.items).toMatchObject([252 { value: 'a', comment: 'c0' },253 { value: 'b', comment: 'c1' }254 ])255 })256 test('line comment after , in map', () => {257 const doc = YAML.parseDocument(source`258 { a, #c0259 b: c, #c1260 d #c2261 }`)262 expect(doc.contents.items).toMatchObject([263 { key: { value: 'a', comment: 'c0' } },264 { key: { value: 'b' }, value: { value: 'c', comment: 'c1' } },265 { key: { value: 'd', comment: 'c2' } }266 ])267 })268 test('multi-line comments', () => {269 const doc = YAML.parseDocument('{ a,\n#c0\n#c1\nb }')270 expect(doc.contents.items).toMatchObject([271 { key: { value: 'a' } },272 { key: { commentBefore: 'c0\nc1', value: 'b' } }273 ])274 })275 })276})277describe('stringify comments', () => {278 describe('single-line comments', () => {279 test('plain', () => {280 const src = 'string'281 const doc = YAML.parseDocument(src)282 doc.contents.comment = 'comment'283 expect(String(doc)).toBe('string #comment\n')284 })285 test('"quoted"', () => {286 const src = '"string\\u0000"'287 const doc = YAML.parseDocument(src)288 doc.contents.comment = 'comment'289 expect(String(doc)).toBe('"string\\0" #comment\n')290 })291 test('block', () => {292 const src = '>\nstring\n'293 const doc = YAML.parseDocument(src)294 doc.contents.comment = 'comment'295 expect(String(doc)).toBe('> #comment\nstring\n')296 })297 })298 describe('multi-line comments', () => {299 test('plain', () => {300 const src = 'string'301 const doc = YAML.parseDocument(src)302 doc.contents.comment = 'comment\nlines'303 expect(String(doc)).toBe('string\n#comment\n#lines\n')304 })305 test('"quoted"', () => {306 const src = '"string\\u0000"'307 const doc = YAML.parseDocument(src)308 doc.contents.comment = 'comment\nlines'309 expect(String(doc)).toBe('"string\\0"\n#comment\n#lines\n')310 })311 test('block', () => {312 const src = '>\nstring\n'313 const doc = YAML.parseDocument(src)314 doc.contents.comment = 'comment\nlines'315 expect(String(doc)).toBe('> #comment lines\nstring\n')316 })317 })318 describe('document comments', () => {319 test('directive', () => {320 const src = source`321 #c0322 ---323 string324 `325 const doc = YAML.parseDocument(src)326 expect(doc.commentBefore).toBe('c0')327 doc.commentBefore += '\nc1'328 expect(String(doc)).toBe(source`329 #c0330 #c1331 ---332 string333 `)334 })335 })336 describe('seq comments', () => {337 test('plain', () => {338 const src = '- value 1\n- value 2\n'339 const doc = YAML.parseDocument(src)340 doc.contents.commentBefore = 'c0'341 doc.contents.items[0].commentBefore = 'c1'342 doc.contents.items[1].commentBefore = 'c2'343 doc.contents.comment = 'c3'344 expect(String(doc)).toBe(source`345 #c0346 #c1347 - value 1348 #c2349 - value 2350 #c3351 `)352 })353 test('multiline', () => {354 const src = '- value 1\n- value 2\n'355 const doc = YAML.parseDocument(src)356 doc.contents.items[0].commentBefore = 'c0\nc1'357 doc.contents.items[1].commentBefore = ' \nc2\n\nc3'358 doc.contents.comment = 'c4\nc5'359 expect(String(doc)).toBe(source`360 #c0361 #c1362 - value 1363 #364 #c2365 #c3366 - value 2367 #c4368 #c5369 `)370 })371 test('seq-in-map', () => {372 const src = 'map:\n - value 1\n - value 2\n'373 const doc = YAML.parseDocument(src)374 doc.contents.items[0].key.commentBefore = 'c0'375 doc.contents.items[0].key.comment = 'c1'376 const seq = doc.contents.items[0].value377 seq.commentBefore = 'c2'378 seq.items[0].commentBefore = 'c3'379 seq.items[1].commentBefore = 'c4'380 seq.comment = 'c5'381 expect(String(doc)).toBe(source`382 #c0383 map: #c1384 #c2385 #c3386 - value 1387 #c4388 - value 2389 #c5390 `)391 })392 test('custom stringifier', () => {393 const doc = YAML.parseDocument('- a\n- b\n')394 doc.contents.commentBefore = 'c0'395 doc.contents.items[0].commentBefore = 'c1'396 doc.contents.items[1].commentBefore = 'c2\nc3'397 const commentString = str => str.replace(/^/gm, '// ')398 expect(doc.toString({ commentString })).toBe(source`399 // c0400 // c1401 - a402 // c2403 // c3404 - b405 `)406 })407 })408 describe('map entry comments', () => {409 test('plain', () => {410 const src = 'key1: value 1\nkey2: value 2\n'411 const doc = YAML.parseDocument(src)412 doc.contents.items[0].key.commentBefore = 'c0'413 doc.contents.items[1].key.commentBefore = 'c1'414 doc.contents.items[1].key.comment = 'c2'415 doc.contents.items[1].value.spaceBefore = true416 doc.contents.comment = 'c3'417 expect(String(doc)).toBe(source`418 #c0419 key1: value 1420 #c1421 key2: #c2422 value 2423 #c3424 `)425 })426 test('multiline', () => {427 const src = 'key1: value 1\nkey2: value 2\n'428 const doc = YAML.parseDocument(src)429 doc.contents.items[0].key.commentBefore = 'c0\nc1'430 doc.contents.items[1].key.commentBefore = ' \nc2\n\nc3'431 doc.contents.items[1].key.comment = 'c4\nc5'432 doc.contents.items[1].value.spaceBefore = true433 doc.contents.items[1].value.commentBefore = 'c6'434 doc.contents.comment = 'c7\nc8'435 expect(String(doc)).toBe(source`436 #c0437 #c1438 key1: value 1439 #440 #c2441 #c3442 key2:443 #c4444 #c5445 #c6446 value 2447 #c7448 #c8449 `)450 })451 test('indented comment on empty value with spaceBefore', () => {452 const src = source`453 key1:454 # comment455 key2: x456 `457 const doc = YAML.parseDocument(src)458 expect(String(doc)).toBe(src)459 })460 test('comment after empty value with spaceBefore', () => {461 const src = source`462 key1:463 # comment464 key2: x465 `466 const doc = YAML.parseDocument(src)467 expect(String(doc)).toBe(src)468 })469 })470 describe('flow collection comments', () => {471 test('line comment after , in seq', () => {472 const doc = YAML.parseDocument(source`473 [ a, #c0474 b #c1475 ]`)476 expect(String(doc)).toBe(source`477 [478 a, #c0479 b #c1480 ]481 `)482 })483 test('line comment after , in map', () => {484 const doc = YAML.parseDocument(source`485 { a, #c0486 b: c, #c1487 d #c2488 }`)489 expect(String(doc)).toBe(source`490 {491 a, #c0492 b: c, #c1493 d #c2494 }495 `)496 })497 })498})499describe('blank lines', () => {500 describe('drop leading blank lines', () => {501 test('content', () => {502 const src = '\n\nstr\n'503 const doc = YAML.parseDocument(src)504 expect(String(doc)).toBe('str\n')505 })506 test('content comment', () => {507 const src = '\n\n#cc\n \nstr\n'508 const doc = YAML.parseDocument(src)509 expect(String(doc)).toBe('#cc\n\nstr\n')510 })511 test('directive', () => {512 const src = '\n\n%YAML 1.2\n---\nstr\n'513 const doc = YAML.parseDocument(src)514 expect(String(doc)).toBe('%YAML 1.2\n---\nstr\n')515 })516 test('directive comment', () => {517 const src = '\n\n#cc\n%YAML 1.2\n---\nstr\n'518 const doc = YAML.parseDocument(src)519 expect(String(doc)).toBe('#cc\n\n%YAML 1.2\n---\nstr\n')520 })521 })522 describe('drop trailing blank lines', () => {523 test('empty contents', () => {524 const src = '\n\n\n'525 const doc = YAML.parseDocument(src)526 expect(String(doc)).toBe('null\n')527 })528 test('scalar contents', () => {529 const src = 'str\n\n\n'530 const doc = YAML.parseDocument(src)531 expect(String(doc)).toBe('str\n')532 })533 test('seq contents', () => {534 const src = '- a\n- b\n\n\n'535 const doc = YAML.parseDocument(src)536 expect(String(doc)).toBe('- a\n- b\n')537 })538 test('empty/comment contents', () => {539 const src = '#cc\n\n\n'540 const doc = YAML.parseDocument(src)541 expect(String(doc)).toBe('#cc\n\nnull\n')542 })543 })544 test('between directive comment & directive', () => {545 const src = '#cc\n\n\n%YAML 1.2\n---\nstr\n'546 const doc = YAML.parseDocument(src)547 expect(String(doc)).toBe('#cc\n\n%YAML 1.2\n---\nstr\n')548 })549 test('after leading comment', () => {550 const src = '#cc\n\n\nstr\n'551 const doc = YAML.parseDocument(src)552 expect(String(doc)).toBe('#cc\n\nstr\n')553 })554 test('before first node in document with directives', () => {555 const doc = YAML.parseDocument('str\n')556 doc.contents.spaceBefore = true557 expect(doc.toString({ directives: true })).toBe('---\n\nstr\n')558 })559 test('between seq items', () => {560 const src = '- a\n\n- b\n\n\n- c\n'561 const doc = YAML.parseDocument(src)562 expect(String(doc)).toBe('- a\n\n- b\n\n- c\n')563 })564 test('between seq items with leading comments', () => {565 const src = source`566 #A567 - a568 #B569 - b570 #C571 - c572 `573 const doc = YAML.parseDocument(src)574 expect(String(doc)).toBe(source`575 #A576 - a577 #B578 - b579 #C580 - c581 `)582 })583 describe('not after block scalar with keep chomping', () => {584 const cases = [585 { name: 'in seq', src: '- |+\n a\n\n- b\n' },586 { name: 'in map', src: 'a: |+\n A\n\nb: B\n' },587 { name: 'in seq in map', src: 'a:\n - |+\n A\n\nb: B\n' }588 ]589 for (const { name, src } of cases) {590 test(name, () => {591 const doc = YAML.parseDocument(src)592 expect(String(doc)).toBe(src)593 let it = doc.contents.items[1]594 if (it.key) it = it.key595 expect(it).not.toHaveProperty('spaceBefore', true)596 it.spaceBefore = true597 expect(String(doc)).toBe(src)598 it.commentBefore = '\n\n'599 expect(String(doc)).toBe(src)600 })601 }602 test('as contents', () => {603 const src = '|+\n a\n\n#c\n'604 const doc = YAML.parseDocument(src)605 expect(doc).toMatchObject({606 comment: 'c',607 contents: { value: 'a\n\n' }608 })609 expect(String(doc)).toBe(src)610 doc.comment = '\n\nc'611 expect(String(doc)).toBe(src)612 })613 })614 test('before block map values', () => {615 const src = 'a:\n\n 1\nb:\n\n #c\n 2\n'616 const doc = YAML.parseDocument(src)617 expect(doc.contents).toMatchObject({618 items: [619 {620 key: { value: 'a' },621 value: { value: 1, spaceBefore: true }622 },623 {624 key: { value: 'b' },625 value: { value: 2, commentBefore: 'c', spaceBefore: true }626 }627 ]628 })629 expect(String(doc)).toBe(src)630 })631 describe('after block value', () => {632 test('in seq', () => {633 const src = '- |\n a\n\n- >-\n b\n\n- |+\n c\n\n- d\n'634 const doc = YAML.parseDocument(src)635 expect(String(doc)).toBe('- |\n a\n\n- >-\n b\n\n- |+\n c\n\n- d\n')636 })637 test('in map', () => {638 const src = 'A: |\n a\n\nB: >-\n b\n\nC: |+\n c\n\nD: d\n'639 const doc = YAML.parseDocument(src)640 expect(String(doc)).toBe(641 'A: |\n a\n\nB: >-\n b\n\nC: |+\n c\n\nD: d\n'642 )643 })644 })645 describe('flow collections', () => {646 test('flow seq', () => {647 const src = '[1,\n\n2,\n3,\n\n4\n\n]'648 const doc = YAML.parseDocument(src)649 expect(doc.contents).toMatchObject({650 items: [651 { value: 1 },652 { value: 2, spaceBefore: true },653 { value: 3 },654 { value: 4, spaceBefore: true }655 ]656 })657 expect(String(doc)).toBe('[\n 1,\n\n 2,\n 3,\n\n 4\n]\n')658 })659 test('flow map', () => {660 const src = '{\n\na: 1,\n\nb: 2 }'661 const doc = YAML.parseDocument(src)662 expect(doc.contents).toMatchObject({663 items: [664 { key: { value: 'a', spaceBefore: true }, value: { value: 1 } },665 { key: { value: 'b', spaceBefore: true }, value: { value: 2 } }666 ]667 })668 })669 test('flow map value comments & spaces', () => {670 const src = '{\n a:\n #c\n 1,\n b:\n\n #d\n 2\n}\n'671 const doc = YAML.parseDocument(src)672 expect(doc.contents).toMatchObject({673 items: [674 {675 key: { value: 'a' },676 value: { value: 1, commentBefore: 'c' }677 },678 {679 key: { value: 'b' },680 value: { value: 2, commentBefore: 'd', spaceBefore: true }681 }682 ]683 })684 expect(String(doc)).toBe(src)685 })686 })687 test('blank line after less-indented comment (eemeli/yaml#91)', () => {688 const src = `689map:690 foo0:691 key2: value2692# foo1:693# key0: value0694# key1: value1695 foo2:696 key3: value3`697 const doc = YAML.parseDocument(src)698 expect(doc.errors).toHaveLength(0)699 expect(doc.toJS()).toMatchObject({700 map: { foo0: { key2: 'value2' }, foo2: { key3: 'value3' } }701 })702 })703 describe('commented empty lines', () => {704 test('document comments with #', () => {705 const src = source`706 # c1707 #708 # c2709 value710 # c3711 #712 # c4713 `714 const doc = YAML.parseDocument(src)715 expect(doc).toMatchObject({716 commentBefore: ' c1\n \n c2',717 comment: ' c3\n \n c4'718 })719 expect(doc.toString()).toBe(source`720 # c1721 #722 # c2723 value724 # c3725 #726 # c4727 `)728 })729 test('document comments without #', () => {730 const src = source`731 # c1732 # c2733 value734 # c3735 # c4736 `737 const doc = YAML.parseDocument(src)738 expect(doc).toMatchObject({739 commentBefore: ' c1\n\n c2',740 comment: ' c3\n\n c4'741 })742 expect(doc.toString()).toBe(src)743 })744 test('map comments with #', () => {745 const src = source`746 key:747 # c1748 #749 # c2750 value751 # c3752 #753 # c4754 `755 const doc = YAML.parseDocument(src)756 expect(doc.contents).toMatchObject({757 items: [758 { value: { commentBefore: ' c1\n \n c2\n', comment: ' c3\n \n c4' } }759 ]760 })761 expect(doc.toString()).toBe(source`762 key:763 # c1764 #765 # c2766 value767 # c3768 #769 # c4770 `)771 })772 test('map comments without #', () => {773 const src = source`774 key:775 # c1776 # c2777 value778 # c3779 # c4780 `781 const doc = YAML.parseDocument(src)782 expect(doc.contents).toMatchObject({783 items: [784 { value: { commentBefore: ' c1\n\n c2\n', comment: ' c3\n\n c4' } }785 ]786 })787 expect(doc.toString()).toBe(source`788 key:789 # c1790 # c2791 value792 # c3793 # c4794 `)795 })796 test('seq comments with #', () => {797 const src = source`798 # c1799 #800 # c2801 - v1802 # c3803 #804 # c4805 - v2806 `807 const doc = YAML.parseDocument(src)808 expect(doc.contents).toMatchObject({809 items: [810 { commentBefore: ' c1\n \n c2', comment: ' c3\n ' },811 { commentBefore: ' c4' }812 ]813 })814 expect(doc.toString()).toBe(source`815 # c1816 #817 # c2818 - v1819 # c3820 #821 # c4822 - v2823 `)824 })825 test('seq comments without #', () => {826 const src = source`827 # c1828 # c2829 - v1830 # c3831 # c4832 - v2833 `834 const doc = YAML.parseDocument(src)835 expect(doc.contents).toMatchObject({836 items: [837 { commentBefore: ' c1\n\n c2', comment: ' c3' },838 { commentBefore: ' c4', spaceBefore: true }839 ]840 })841 expect(doc.toString()).toBe(source`842 # c1843 # c2844 - v1 # c3845 # c4846 - v2847 `)848 })849 })850 describe('newlines as comments', () => {851 test('seq', () => {852 const doc = YAML.parseDocument('- v1\n- v2\n')853 const [v1, v2] = doc.contents.items854 v1.commentBefore = '\n'855 v1.comment = '\n'856 v2.commentBefore = '\n'857 v2.comment = '\n'858 expect(doc.toString()).toBe(source`859 - v1860 - v2861 `)862 })863 test('map', () => {864 const doc = YAML.parseDocument('k1: v1\nk2: v2')865 const [p1, p2] = doc.contents.items866 p1.key.commentBefore = '\n'867 p1.value.commentBefore = '\n'868 p1.value.comment = '\n'869 p2.key.commentBefore = '\n'870 p2.value.commentBefore = '\n'871 p2.value.comment = '\n'872 expect(doc.toString()).toBe(source`873 k1:874 v1875 k2:876 v2877 `)878 })879 })880 test('eemeli/yaml#277', () => {881 const src = source`882 environment:883 ### SESSION START ###884 A: "true"885 B: "true"886 ### SESSION END ###887 ### ANOTHER SESSION START ###888 C: "true"889 D: "true"890 ### ANOTHER SESSION END ###891 `892 const doc = YAML.parseDocument(src)893 expect(doc.toString()).toBe(src)894 })895})896describe('eemeli/yaml#18', () => {897 test('reported', () => {898 const src = `test1:899 foo:900 #123901 bar: 1\n`902 const doc = YAML.parseDocument(src)903 expect(String(doc)).toBe(src)904 })905 test('minimal', () => {906 const src = `foo:\n #123\n bar: baz\n`907 const doc = YAML.parseDocument(src)908 expect(String(doc)).toBe(src)909 })910})911describe('eemeli/yaml#28', () => {912 test('reported', () => {913 const src = `# This comment is ok914entryA:915 - foo916entryB:917 - bar # bar comment918# Ending comment919# Ending comment 2\n`920 const doc = YAML.parseDocument(src)921 expect(String(doc)).toBe(`# This comment is ok922entryA:923 - foo924entryB:925 - bar # bar comment926# Ending comment927# Ending comment 2\n`)928 })929 test('collection end comment', () => {930 const src = `a: b #c\n#d\n`931 const doc = YAML.parseDocument(src)932 expect(String(doc)).toBe(`a: b #c\n\n#d\n`)933 })934 test('blank line after seq in map', () => {935 const src = `a:936 - aa937b:938 - bb939c: cc\n`940 const doc = YAML.parseDocument(src)941 expect(String(doc)).toBe(src)942 })943 test('blank line after map in seq', () => {944 const src = `- a: aa945- b: bb946 c: cc947- d: dd\n`948 const doc = YAML.parseDocument(src)949 expect(String(doc)).toBe(src)950 })951})952describe('collection end comments', () => {953 test('seq in seq', () => {954 const src = source`955 #0956 - - a957 - b958 #1959 #2960 - d961 `962 const doc = YAML.parseDocument(src)963 expect(doc.contents).toMatchObject({964 items: [965 { items: [{ value: 'a' }, { value: 'b' }], comment: '1\n\n2' },966 { value: 'd' }967 ]968 })969 expect(String(doc)).toBe(source`970 #0971 - - a972 - b973 #1974 #2975 - d976 `)977 })978 test('map in seq', () => {979 const src = source`980 #0981 - a: 1982 b: 2983 #1984 #2985 - d986 `987 const doc = YAML.parseDocument(src)988 expect(doc.contents).toMatchObject({989 items: [990 {991 items: [992 { key: { value: 'a' }, value: { value: 1 } },993 { key: { value: 'b' }, value: { value: 2 } }994 ],995 comment: '1\n\n2'996 },997 { value: 'd' }998 ]999 })1000 expect(String(doc)).toBe(source`1001 #01002 - a: 11003 b: 21004 #11005 #21006 - d1007 `)1008 })1009 test('seq in map', () => {1010 const src = source`1011 #01012 a:1013 - b1014 - c1015 #11016 #21017 d: 11018 `1019 const doc = YAML.parseDocument(src)1020 expect(doc.contents).toMatchObject({1021 items: [1022 {1023 key: { value: 'a' },1024 value: { items: [{ value: 'b' }, { value: 'c' }], comment: '1\n\n2' }1025 },1026 { key: { value: 'd' }, value: { value: 1 } }1027 ]1028 })1029 expect(String(doc)).toBe(source`1030 #01031 a:1032 - b1033 - c1034 #11035 #21036 d: 11037 `)1038 })1039 test('map in map', () => {1040 const src = source`1041 #01042 a:1043 b: 11044 c: 21045 #11046 #21047 d: 11048 `1049 const doc = YAML.parseDocument(src)1050 expect(doc.contents).toMatchObject({1051 items: [1052 {1053 key: { value: 'a' },1054 value: {1055 items: [1056 { key: { value: 'b' }, value: { value: 1 } },1057 { key: { value: 'c' }, value: { value: 2 } }1058 ],1059 comment: '1\n\n2'1060 }1061 },1062 { key: { value: 'd' }, value: { value: 1 } }1063 ]1064 })1065 expect(String(doc)).toBe(source`1066 #01067 a:1068 b: 11069 c: 21070 #11071 #21072 d: 11073 `)1074 })1075 test('indented seq in map in seq', () => {1076 const src = `#01077a:1078 #11079 - b:1080 - c1081 #21082 - e\n`1083 const doc = YAML.parseDocument(src)1084 expect(doc.contents).toMatchObject({1085 items: [1086 {1087 key: { value: 'a' },1088 value: {1089 commentBefore: '1',1090 items: [1091 {1092 items: [1093 {1094 key: { value: 'b' },1095 value: { items: [{ value: 'c' }] }1096 }1097 ]...
document.js
Source:document.js
1const faunadb = require('faunadb');2const q = faunadb.query;3function ParseDocument(document){4 return q.Do(5 q.Let(6 {7 "document": document,8 "data": q.Select(["data"], q.Var("document")),9 "_ts": q.Select("ts", q.Var("document")),10 "_id": q.Select("id",q.Select("ref", q.Var("document"))),11 },12 q.Merge( 13 {14 _ts: q.Var("_ts"),15 _id: q.Var("_id"),16 },17 [18 q.Var("data"), 19 ]20 ) 21 )22 )23}24module.exports =(main, collection)=> {25 return {26 create(data) {27 if (data.id) {28 let id = data.id29 delete data.id30 main.query = ParseDocument(q.Create(q.Ref(q.Collection(collection), id), { data:data }))31 }else{32 main.query = ParseDocument(q.Create(q.Collection(collection), { data: data }));33 } 34 return main;35 },36 37 createMany(documents,ttl=null) {38 main.query = q.Map(39 q.Map(40 documents, 41 q.Lambda('item', 42 q.Create(43 q.Collection("Comment"), {ttl, data: q.Var('item') }44 )45 )46 ),47 Lambda("item",ParseDocument(q.Var("item")))48 ) ;49 return main;50 },51 allRef(options) {52 const config = { 53 size: (options.size) ? options.size : null,54 after: (options.after) ? options.after : null,55 before: (options.before) ? options.before : null,56 ts: (options.ts) ? options.ts : null,57 events: (options.events) ? options.events : null,58 sources: (options.sources) ? options.sources : null,59 };60 main.query = ParseDocument(q.Paginate(q.Documents(q.Collection(collection)), config));61 return main;62 },63 all(options) {64 const config = { 65 size: (options.size) ? options.size : null,66 after: (options.after) ? options.after : null,67 before: (options.before) ? options.before : null,68 ts: (options.ts) ? options.ts : null,69 events: (options.events) ? options.events : null,70 sources: (options.sources) ? options.sources : null,71 };72 main.query = q.Map(73 q.Paginate(q.Documents(q.Collection(collection)), config),74 q.Lambda("collectionRef", 75 ParseDocument(q.Get(q.Var("collectionRef"))) 76 )77 )78 return main;79 },80 81 get(id) {82 main.query = ParseDocument(q.Get(q.Ref(q.Collection(collection), id)));83 return main;84 },85 86 update(id, data, ttl=null) {87 delete data.id88 let update = q.Update(q.Ref(q.Collection(collection), id), { ttl, data: { ...data } })89 let existCheck = q.Let(90 {91 item: q.Ref(q.Collection(collection), id), 92 },93 q.If( q.Exists(q.Var("item"), q.Now()),94 update,95 q.Abort(`Document with the id of ${id} does not Exist on colection ${collection}`)96 )97 ) 98 99 main.query = ParseDocument(existCheck);100 return main;101 },102 upsert(id, data, ttl=null) {103 delete data.id104 let create = q.Create(q.Collection(collection), {ttl, data: data })105 let update = q.Update(q.Ref(q.Collection(collection), id), { ttl, data: { ...data } })106 let existCheck = q.Let(107 {108 item: q.Ref(q.Collection(collection), id), 109 },110 q.If( q.Exists(q.Var("item"), q.Now()),111 create,112 update113 )114 ) 115 116 main.query = ParseDocument(existCheck);117 return main;118 },119 120 replace(id, data, ttl=null) {121 delete data.id122 main.query = ParseDocument(q.Replace(q.Ref(q.Collection(collection), id), { ttl, data: { ...data } }));123 return main;124 },125 126 delete(id) {127 main.query = ParseDocument(q.Delete(q.Ref(q.Collection(collection), id)));128 return main;129 },130 setTTL(ttl) {131 main.query = ParseDocument(q.Update(q.Ref(q.Collection(collection), id), { ttl }));132 return main;133 }134 }...
DomHandler.test.ts
Source:DomHandler.test.ts
...6}7describe('DOMParser', () => {8 it('should parse', () => {9 const html = '<div><span>Text</span><div></div></div>';10 expectRendersHtml(parseDocument(html), html);11 });12 describe('ignoreTags option', () => {13 it('should support ignoredTags', () => {14 const doc = parseDocument('<div><span></span></div>', {15 ignoredTags: ['span']16 });17 expectRendersHtml(doc, '<div></div>');18 });19 it('should ignore children of ignoredTags', () => {20 const doc = parseDocument(21 '<div><span><strong><em></em><mark></mark></strong>Text node!</span></div>',22 {23 ignoredTags: ['span']24 }25 );26 expectRendersHtml(doc, '<div></div>');27 });28 it('should include siblings of ignoredTags', () => {29 const doc = parseDocument(30 '<div><span></span>Text<strong></strong></div>',31 {32 ignoredTags: ['span']33 }34 );35 expectRendersHtml(doc, '<div>Text<strong></strong></div>');36 });37 });38 describe('ignoreNode option', () => {39 it('should ignore element nodes', () => {40 const doc = parseDocument('<div><span></span></div>', {41 ignoreNode: (node) => (node as Element).name === 'span'42 });43 expectRendersHtml(doc, '<div></div>');44 });45 it('should ignore text nodes', () => {46 const doc = parseDocument('<div>Text!</div>', {47 ignoreNode: (node) => node.type === 'text'48 });49 expectRendersHtml(doc, '<div></div>');50 });51 it('should ignore children elements of ignored nodes', () => {52 const doc = parseDocument(53 '<div><span><strong><em></em><mark></mark></strong>Text node!</span></div>',54 {55 ignoreNode: (node) => (node as Element).name === 'span'56 }57 );58 expectRendersHtml(doc, '<div></div>');59 });60 it('should provide parent in ignoreNode', () => {61 const html = '<p><a></a></p>';62 const ignoreNode = jest.fn((node, parent) => {63 expect(parent).not.toBeNull();64 return false;65 });66 parseDocument(html, {67 ignoreNode68 });69 });70 it('should retain sibling text nodes of ignored nodes', () => {71 const doc = parseDocument(72 '<div><a href="">you a noisy one</a>Can you see the anchor? It has been ignored!</div>',73 {74 ignoreNode: (node) => (node as Element).name === 'a'75 }76 );77 expectRendersHtml(78 doc,79 '<div>Can you see the anchor? It has been ignored!</div>'80 );81 });82 it('should retain next siblings elements of ignored nodes', () => {83 const doc = parseDocument(84 '<div><span></span>Text<strong></strong></div>',85 {86 ignoreNode: (node) => (node as Element).name === 'span'87 }88 );89 expectRendersHtml(doc, '<div>Text<strong></strong></div>');90 });91 it('should retain previous siblings elements of ignored nodes', () => {92 const html = '<div> <a>A</a>B</div>';93 const doc = parseDocument(html, {94 ignoreNode: (node) => (node as Element).name === 'a'95 });96 expectRendersHtml(doc, '<div> B</div>');97 });98 });99 describe('visitors option', () => {100 it('should support visitors.onElement', () => {101 const onElement = jest.fn();102 parseDocument('<div><span></span>Text<strong></strong></div>', {103 visitors: {104 onElement105 }106 });107 expect(onElement).toHaveBeenCalledTimes(3);108 });109 it('should support visitors.onText', () => {110 const onText = jest.fn();111 parseDocument('<div><span></span>Text<strong></strong></div>', {112 visitors: { onText }113 });114 expect(onText).toHaveBeenCalledTimes(1);115 });116 it('should support visitors.onDocument', () => {117 const onDocument = jest.fn();118 parseDocument('<div></div>', {119 visitors: { onDocument }120 });121 expect(onDocument).toHaveBeenCalledTimes(1);122 });123 it('should call visitor.onElement when the element children have been parsed', () => {124 parseDocument('<div><span></span><strong></strong>Text</div>', {125 visitors: {126 onElement(element) {127 if (element.tagName === 'div') {128 expect(element.children).toHaveLength(3);129 }130 }131 }132 });133 });134 it('should call visitor.onDocument when the document children have been parsed', () => {135 parseDocument('<span></span><strong></strong>Text', {136 visitors: {137 onDocument(document) {138 expect(document.children).toHaveLength(3);139 }140 }141 });142 });143 });...
Using AI Code Generation
1var BestParser = require('bestparser');2var fs = require('fs');3var parser = new BestParser();4var doc = parser.parseDocument(fs.readFileSync('test.html'));5var BestParser = require('bestparser');6var parser = new BestParser();7var doc = parser.parse('<html><head></head><body></body></html>');8var BestParser = require('bestparser');9var parser = new BestParser();10var doc = parser.parse('<html><head></head><body></body></html>');11var BestParser = require('bestparser');12var parser = new BestParser();13var doc = parser.parse('<html><head></head><body></body></html>');14var BestParser = require('bestparser');15var parser = new BestParser();16var doc = parser.parse('<html><head></head><body></body></html>');17var BestParser = require('bestparser');18var parser = new BestParser();19var doc = parser.parse('<html><head></head><body></body></html>');20var BestParser = require('bestparser');21var parser = new BestParser();22var doc = parser.parse('<html><head></head><body></body></html>');23var BestParser = require('bestparser');24var parser = new BestParser();25var doc = parser.parse('<html><head></head><body></body></html>');26var BestParser = require('bestparser');27var parser = new BestParser();28var doc = parser.parse('<html><head></head><body></body></html>');29var BestParser = require('bestparser');30var parser = new BestParser();31var doc = parser.parse('<html><head></head><body></body></html>');32var BestParser = require('bestparser');33var parser = new BestParser();34var doc = parser.parse('<html><head></head><body></body></html>');35var BestParser = require('bestparser');
Using AI Code Generation
1var BestPractices = require('best-practices');2var bestPractices = new BestPractices();3var doc = bestPractices.parseDocument('test.html');4var BestPractices = require('best-practices');5var bestPractices = new BestPractices();6var doc = bestPractices.parseDocument('test.html');7var BestPractices = require('best-practices');8var bestPractices = new BestPractices();9var doc = bestPractices.parseDocument('test.html');10var BestPractices = require('best-practices');11var bestPractices = new BestPractices();12var doc = bestPractices.parseDocument('test.html');13var BestPractices = require('best-practices');14var bestPractices = new BestPractices();15var doc = bestPractices.parseDocument('test.html');16var BestPractices = require('best-practices');17var bestPractices = new BestPractices();18var doc = bestPractices.parseDocument('test.html');19var BestPractices = require('best-practices');20var bestPractices = new BestPractices();21var doc = bestPractices.parseDocument('test.html');22var BestPractices = require('best-practices');23var bestPractices = new BestPractices();24var doc = bestPractices.parseDocument('test.html');25var BestPractices = require('best-practices');26var bestPractices = new BestPractices();27var doc = bestPractices.parseDocument('test.html');28var BestPractices = require('best-practices');29var bestPractices = new BestPractices();30var doc = bestPractices.parseDocument('test.html');31var BestPractices = require('best-practices');32var bestPractices = new BestPractices();33var doc = bestPractices.parseDocument('test.html');
Using AI Code Generation
1var BestMatchFinder = require('bestmatchfinder').BestMatchFinder;2var bestMatchFinder = new BestMatchFinder();3var document = "This is a test document";4var bestMatch = bestMatchFinder.parseDocument(document);5console.log(bestMatch);6exports.BestMatchFinder = function() {7 var self = this;8 var document = '';9 var bestMatch = '';10 var documentWords = [];11 var bestMatchWords = [];12 var bestMatchWordsCount = 0;13 self.parseDocument = function(doc) {14 document = doc;15 documentWords = document.split(" ");16 bestMatch = documentWords[0];17 for (var i = 1; i < documentWords.length; i++) {18 bestMatchWords = bestMatch.split(" ");19 bestMatchWordsCount = bestMatchWords.length;20 if (documentWords[i].length > bestMatchWords[bestMatchWordsCount - 1].length) {21 bestMatch = bestMatch + " " + documentWords[i];22 }23 }24 return bestMatch;25 }26}
Using AI Code Generation
1var bestBuyAPI = require('./BestBuyAPI.js');2var fs = require('fs');3var html = fs.readFileSync('bestbuy.html', 'utf8');4var bestBuy = new bestBuyAPI();5var result = bestBuy.parseDocument(html);6console.log(result);
Using AI Code Generation
1var bestParser = require('bestparser');2var parser = new bestParser();3var doc = parser.parseDocument('Hello World');4console.log(doc);5var bestParser = require('bestparser');6var parser = new bestParser();7var doc = parser.parseDocument('Hello World');8console.log(doc);9var bestParser = require('bestparser');10var parser = new bestParser();11var doc = parser.parseDocument('Hello World');12console.log(doc);13var bestParser = require('bestparser');14var parser = new bestParser();15var doc = parser.parseDocument('Hello World');16console.log(doc);17var bestParser = require('bestparser');18var parser = new bestParser();19var doc = parser.parseDocument('Hello World');20console.log(doc);21var bestParser = require('bestparser');22var parser = new bestParser();23var doc = parser.parseDocument('Hello World');24console.log(doc);25var bestParser = require('bestparser');26var parser = new bestParser();27var doc = parser.parseDocument('Hello World');28console.log(doc);29var bestParser = require('bestparser');30var parser = new bestParser();31var doc = parser.parseDocument('Hello World');32console.log(doc);33var bestParser = require('bestparser');34var parser = new bestParser();35var doc = parser.parseDocument('Hello World');36console.log(doc);
Using AI Code Generation
1var bestPractices = require('best-practices');2var document = bestPractices.parseDocument('path/to/file');3console.log(document);4var bestPractices = require('best-practices');5var document = bestPractices.parseDocument('path/to/file');6console.log(document);7var bestPractices = require('best-practices');8var document = bestPractices.parseDocument('path/to/file');9console.log(document);10var bestPractices = require('best-practices');11var document = bestPractices.parseDocument('path/to/file');12console.log(document);13var bestPractices = require('best-practices');14var document = bestPractices.parseDocument('path/to/file');15console.log(document);16var bestPractices = require('best-practices');17var document = bestPractices.parseDocument('path/to/file');18console.log(document);19var bestPractices = require('best-practices');20var document = bestPractices.parseDocument('path/to/file');21console.log(document);22var bestPractices = require('best-practices');23var document = bestPractices.parseDocument('path/to/file');24console.log(document);25var bestPractices = require('best-practices');26var document = bestPractices.parseDocument('path/to/file');27console.log(document);28var bestPractices = require('best-practices');29var document = bestPractices.parseDocument('path/to/file');30console.log(document);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!