|
| 1 | +import finalfunction from '../../corsscripts/ascii/extractors/finalfunction.js'; |
| 2 | + |
| 3 | +describe('finalfunction extractor', () => { |
| 4 | + |
| 5 | + // ── Block-mode tests ────────────────────────────────────────────────────── |
| 6 | + |
| 7 | + describe('with blocks', () => { |
| 8 | + test('returns expression from a matching code_inline block', () => { |
| 9 | + const blocks = [{ type: 'code_inline', raw: 'f(x) = x^2' }]; |
| 10 | + expect(finalfunction('', blocks)).toBe('x^2'); |
| 11 | + }); |
| 12 | + |
| 13 | + test('strips leading/trailing whitespace from code_inline before matching', () => { |
| 14 | + const blocks = [{ type: 'code_inline', raw: ' f(x) = sin(x) ' }]; |
| 15 | + expect(finalfunction('', blocks)).toBe('sin(x)'); |
| 16 | + }); |
| 17 | + |
| 18 | + test('returns last matching line from an asciimath_block', () => { |
| 19 | + const blocks = [{ |
| 20 | + type: 'asciimath_block', |
| 21 | + raw: 'y = 3\nf(x) = x + 1' |
| 22 | + }]; |
| 23 | + expect(finalfunction('', blocks)).toBe('x + 1'); |
| 24 | + }); |
| 25 | + |
| 26 | + test('scans asciimath_block lines bottom-up and returns last match', () => { |
| 27 | + const blocks = [{ |
| 28 | + type: 'asciimath_block', |
| 29 | + raw: 'f(x) = x\nf(x) = x^2\nsome other line' |
| 30 | + }]; |
| 31 | + expect(finalfunction('', blocks)).toBe('x^2'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('scans blocks bottom-up, returning last block with a match', () => { |
| 35 | + const blocks = [ |
| 36 | + { type: 'code_inline', raw: 'f(x) = first' }, |
| 37 | + { type: 'code_inline', raw: 'f(x) = last' } |
| 38 | + ]; |
| 39 | + expect(finalfunction('', blocks)).toBe('last'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('skips non-matching code_inline blocks and finds earlier match', () => { |
| 43 | + const blocks = [ |
| 44 | + { type: 'code_inline', raw: 'f(x) = found' }, |
| 45 | + { type: 'code_inline', raw: 'y = x' } |
| 46 | + ]; |
| 47 | + expect(finalfunction('', blocks)).toBe('found'); |
| 48 | + }); |
| 49 | + |
| 50 | + test('returns ERROR when no block line matches f(x) = pattern', () => { |
| 51 | + const blocks = [ |
| 52 | + { type: 'code_inline', raw: 'y = x' }, |
| 53 | + { type: 'asciimath_block', raw: 'a = 1\nb = 2' } |
| 54 | + ]; |
| 55 | + expect(finalfunction('', blocks)).toBe('ERROR'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('ignores blocks that are not code_inline or asciimath_block', () => { |
| 59 | + const blocks = [ |
| 60 | + { type: 'paragraph', raw: 'f(x) = ignored' }, |
| 61 | + { type: 'code_inline', raw: 'f(x) = found' }, |
| 62 | + { type: 'paragraph', raw: 'f(x) = ignored' }, |
| 63 | + ]; |
| 64 | + expect(finalfunction('', blocks)).toBe('found'); |
| 65 | + }); |
| 66 | + |
| 67 | + test('returns ERROR for an empty blocks array (falls back to raw, no match)', () => { |
| 68 | + expect(finalfunction('no match here', [])).toBe('ERROR'); |
| 69 | + }); |
| 70 | + |
| 71 | + test('handles windows-style line endings in asciimath_block', () => { |
| 72 | + const blocks = [{ |
| 73 | + type: 'asciimath_block', |
| 74 | + raw: 'y = 1\r\nf(x) = x^3' |
| 75 | + }]; |
| 76 | + expect(finalfunction('', blocks)).toBe('x^3'); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + // ── Raw-fallback tests ──────────────────────────────────────────────────── |
| 81 | + |
| 82 | + describe('without blocks (raw fallback)', () => { |
| 83 | + test('returns expression from a plain raw line', () => { |
| 84 | + expect(finalfunction('f(x) = x + 2', null)).toBe('x + 2'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('strips backtick delimiters from raw input', () => { |
| 88 | + expect(finalfunction('`f(x) = x^2`', null)).toBe('x^2'); |
| 89 | + }); |
| 90 | + |
| 91 | + test('scans raw lines bottom-up and returns last match', () => { |
| 92 | + const raw = 'f(x) = first\nsome text\nf(x) = last'; |
| 93 | + expect(finalfunction(raw, null)).toBe('last'); |
| 94 | + }); |
| 95 | + |
| 96 | + test('ignores non-matching lines in raw', () => { |
| 97 | + const raw = 'a = 1\nb = 2\nf(x) = expr\nb = 2'; |
| 98 | + expect(finalfunction(raw, null)).toBe('expr'); |
| 99 | + }); |
| 100 | + |
| 101 | + test('returns ERROR when no raw line matches', () => { |
| 102 | + expect(finalfunction('a = 1\nb = 2', null)).toBe('ERROR'); |
| 103 | + }); |
| 104 | + |
| 105 | + test('returns ERROR for empty raw string with null blocks', () => { |
| 106 | + expect(finalfunction('', null)).toBe('ERROR'); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments