dh_demo

DreamHanks demo project
git clone git://git.lair.cx/dh_demo
Log | Files | Refs | README

parse.test.ts (3768B)


      1 import { Token } from './token'
      2 import { parse } from './parse'
      3 
      4 const parseTests: Array<{ label: string; src: string; expected: Token[] }> = [{
      5   label: 'empty',
      6   src: '',
      7   expected: [],
      8 }, {
      9   label: 'paragraph',
     10   src: `Hello,
     11 World!
     12 
     13 This is a test.`,
     14   expected: [{
     15     key: 'paragraph', children: [{
     16       key: 'text', text: 'Hello,',
     17     }, {
     18       key: 'linebreak',
     19     }, {
     20       key: 'text', text: 'World!',
     21     }],
     22   }, {
     23     key: 'paragraph', children: [{
     24       key: 'text', text: 'This is a test.',
     25     }],
     26   }],
     27 }, {
     28   label: 'heading',
     29   src: `# Hello, World!
     30   
     31 ## This is a test.`,
     32   expected: [{
     33     key: 'heading', level: 1, children: [{
     34       key: 'text', text: 'Hello, World!',
     35     }],
     36   }, {
     37     key: 'heading', level: 2, children: [{
     38       key: 'text', text: 'This is a test.',
     39     }],
     40   }],
     41 }, {
     42   label: 'horizontal rule',
     43   src: `Hello, World!
     44   
     45 ----
     46   
     47 This is a test.`,
     48   expected: [{
     49     key: 'paragraph', children: [{
     50       key: 'text', text: 'Hello, World!',
     51     }],
     52   }, {
     53     key: 'horizontal-rule',
     54   }, {
     55     key: 'paragraph', children: [{
     56       key: 'text', text: 'This is a test.',
     57     }],
     58   }],
     59 }, {
     60   label: 'list',
     61   src: `Hello, World!
     62   
     63 - This is a test.
     64   - This is a test.
     65     Multiple lines.
     66 - List supports any block syntax.
     67   
     68   # For Example,
     69   Heading.
     70 - This is a test.
     71 
     72 This is a test.`,
     73   expected: [{
     74     key: 'paragraph', children: [{ key: 'text', text: 'Hello, World!' }],
     75   }, {
     76     key: 'list', listType: 'unordered', children: [{
     77       key: 'list-item', children: [{
     78         key: 'text', children: [{
     79           key: 'text', text: 'This is a test.',
     80         }],
     81       }, {
     82         key: 'list', listType: 'unordered', children: [{
     83           key: 'list-item', children: [{
     84             key: 'text', children: [{
     85               key: 'text', text: 'This is a test.',
     86             }],
     87           }, {
     88             key: 'text', children: [{
     89               key: 'text', text: 'Multiple lines.',
     90             }],
     91           }],
     92         }],
     93       }],
     94     }, {
     95       key: 'list-item', children: [{
     96         key: 'text', children: [{
     97           key: 'text', text: 'List supports any block syntax.',
     98         }],
     99       }, {
    100         key: 'heading', level: 1, children: [{
    101           key: 'text', text: 'For Example,',
    102         }],
    103       }, {
    104         key: 'text', children: [{
    105           key: 'text', text: 'Heading.',
    106         }],
    107       }],
    108     }, {
    109       key: 'list-item', children: [{
    110         key: 'text', children: [{
    111           key: 'text', text: 'This is a test.',
    112         }],
    113       }],
    114     }],
    115   }, {
    116     key: 'paragraph', children: [{
    117       key: 'text', text: 'This is a test.',
    118     }],
    119   }],
    120 }, {
    121   label: 'blockquote',
    122   src: `Hello, World!
    123   
    124 > This is a test.
    125 >
    126 > Blockquote supports any block syntax.
    127 >
    128 > > This is a test.
    129 > >
    130 > > > This is a test.
    131 
    132 This is a test.`,
    133   expected: [{
    134     key: 'paragraph', children: [{
    135       key: 'text', text: 'Hello, World!',
    136     }],
    137   }, {
    138     key: 'blockquote', children: [{
    139       key: 'paragraph', children: [{
    140         key: 'text', text: 'This is a test.',
    141       }],
    142     }, {
    143       key: 'paragraph', children: [{
    144         key: 'text', text: 'Blockquote supports any block syntax.',
    145       }],
    146     }, {
    147       key: 'blockquote', children: [{
    148         key: 'paragraph', children: [{
    149           key: 'text', text: 'This is a test.',
    150         }],
    151       }, {
    152         key: 'blockquote', children: [{
    153           key: 'paragraph', children: [{
    154             key: 'text', text: 'This is a test.',
    155           }],
    156         }],
    157       }],
    158     }],
    159   }, {
    160     key: 'paragraph', children: [{
    161       key: 'text', text: 'This is a test.',
    162     }],
    163   }],
    164 }]
    165 
    166 describe('Parse', () => {
    167   for (const { label, src, expected } of parseTests) {
    168     it(label, () => {
    169       expect(parse(src)).toEqual(expected)
    170     })
    171   }
    172 })