| 1 | // Test for a test, huh? |
| 2 | |
| 3 | import { LLParse } from 'llparse'; |
| 4 | import * as path from 'path'; |
| 5 | |
| 6 | import { Fixture } from '../src/fixture'; |
| 7 | |
| 8 | const TMP_DIR = path.join(__dirname, 'tmp'); |
| 9 | const EXTRA_CODE = path.join(__dirname, 'fixtures', 'extra.c'); |
| 10 | const EXTRA_JS = path.join(__dirname, 'fixtures', 'extra.js'); |
| 11 | |
| 12 | describe('llparse-test-fixture', function() { |
| 13 | const timeoutMS = (process.env.CI ? 60 : 10) * 1000; |
| 14 | this.timeout(timeoutMS); |
| 15 | |
| 16 | let fixture: Fixture; |
| 17 | let p: LLParse; |
| 18 | |
| 19 | beforeEach(() => { |
| 20 | p = new LLParse(); |
| 21 | |
| 22 | fixture = new Fixture({ |
| 23 | buildDir: TMP_DIR, |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | it('should build with extra files', async () => { |
| 28 | const start = p.node('start'); |
| 29 | const invoke = p.invoke(p.code.match('llparse__print_off')); |
| 30 | |
| 31 | start |
| 32 | .match('a', start) |
| 33 | .match('b', invoke) |
| 34 | .otherwise(p.error(1, 'error')); |
| 35 | |
| 36 | invoke |
| 37 | .otherwise(start); |
| 38 | |
| 39 | const build = await fixture.build(p.build(start, { |
| 40 | c: { header: 'extra' }, |
| 41 | }), 'extra', { |
| 42 | extra: [ EXTRA_CODE ], |
| 43 | }); |
| 44 | |
| 45 | await build.check('abaaba', 'off=2\noff=5\n'); |
| 46 | }); |
| 47 | |
| 48 | it('should normalize spans', async () => { |
| 49 | const start = p.node('start'); |
| 50 | const sub = p.node('sub'); |
| 51 | const span = p.span(p.code.span('llparse__on_span')); |
| 52 | |
| 53 | start |
| 54 | .match('a', start) |
| 55 | .peek('b', span.start(sub)) |
| 56 | .otherwise(p.error(1, 'error')); |
| 57 | |
| 58 | sub |
| 59 | .match('b', sub) |
| 60 | .otherwise(span.end(start)); |
| 61 | |
| 62 | const build = await fixture.build(p.build(start, { |
| 63 | c: { header: 'span' }, |
| 64 | }), 'span', { |
| 65 | extra: [ EXTRA_CODE ], |
| 66 | }); |
| 67 | |
| 68 | await build.check( |
| 69 | 'abbbaabba', |
| 70 | 'off=1 len=3 span[span]="bbb"\n' + |
| 71 | 'off=6 len=2 span[span]="bb"\n'); |
| 72 | }); |
| 73 | |
| 74 | it('should print lf spans', async () => { |
| 75 | const start = p.node('start'); |
| 76 | const sub = p.node('sub'); |
| 77 | const span = p.span(p.code.span('llparse__on_span')); |
| 78 | |
| 79 | start |
| 80 | .match('a', start) |
| 81 | .peek('b', span.start(sub)) |
| 82 | .otherwise(p.error(1, 'error')); |
| 83 | |
| 84 | sub |
| 85 | .match([ 'b', '\r', '\n' ], sub) |
| 86 | .otherwise(span.end(start)); |
| 87 | |
| 88 | const build = await fixture.build(p.build(start, { |
| 89 | c: { header: 'span-lf' }, |
| 90 | }), 'span-lf', { |
| 91 | extra: [ EXTRA_CODE ], |
| 92 | }); |
| 93 | |
| 94 | await build.check('abbb\nb\r\nbbbaabba', [ |
| 95 | 'off=1 len=3 span[span]="bbb"', |
| 96 | 'off=4 len=1 span[span]=lf', |
| 97 | 'off=5 len=1 span[span]="b"', |
| 98 | 'off=6 len=1 span[span]=cr', |
| 99 | 'off=7 len=1 span[span]=lf', |
| 100 | 'off=8 len=3 span[span]="bbb"', |
| 101 | 'off=13 len=2 span[span]="bb"', |
| 102 | ]); |
| 103 | }); |
| 104 | |
| 105 | it('should print errors', async () => { |
| 106 | const start = p.node('start'); |
| 107 | |
| 108 | start |
| 109 | .match('a', start) |
| 110 | .otherwise(p.error(1, 'some reason')); |
| 111 | |
| 112 | const build = await fixture.build(p.build(start, { |
| 113 | c: { header: 'error' }, |
| 114 | }), 'error', { |
| 115 | extra: [ EXTRA_CODE ], |
| 116 | }); |
| 117 | |
| 118 | await build.check( |
| 119 | 'aaab', |
| 120 | 'off=3 error code=1 reason="some reason"\n'); |
| 121 | }); |
| 122 | |
| 123 | it('should check against regexp', async () => { |
| 124 | const start = p.node('start'); |
| 125 | |
| 126 | start |
| 127 | .match('a', start) |
| 128 | .otherwise(p.error(1, 'some reason')); |
| 129 | |
| 130 | const build = await fixture.build(p.build(start, { |
| 131 | c: { header: 'error-re' }, |
| 132 | }), 'error-re', { |
| 133 | extra: [ EXTRA_CODE ], |
| 134 | }); |
| 135 | |
| 136 | await build.check( |
| 137 | 'aaab', |
| 138 | /off=\d+ error code=1 reason="some reason"/g); |
| 139 | }); |
| 140 | |
| 141 | it('should check against array of mixed strings/regexps', async () => { |
| 142 | const start = p.node('start'); |
| 143 | const invoke = p.invoke(p.code.match('llparse__print_off')); |
| 144 | |
| 145 | start |
| 146 | .skipTo(invoke.otherwise(start)); |
| 147 | |
| 148 | const build = await fixture.build(p.build(start, { |
| 149 | c: { header: 'mixed' }, |
| 150 | }), 'mixed', { |
| 151 | extra: [ EXTRA_CODE ], |
| 152 | }); |
| 153 | |
| 154 | await build.check('aaab', [ |
| 155 | 'off=1', |
| 156 | 'off=2', |
| 157 | /off=\d/, |
| 158 | 'off=4', |
| 159 | ]); |
| 160 | }); |
| 161 | }); |