|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { annotateOverloads } from '../overloads.mjs'; |
| 5 | + |
| 6 | +const entry = (name, type, slug, depth = 2, text = `${name}(...)`) => ({ |
| 7 | + heading: { depth, data: { name, type, slug, text } }, |
| 8 | +}); |
| 9 | + |
| 10 | +describe('annotateOverloads', () => { |
| 11 | + it('flags every overload after the first heading of a run', () => { |
| 12 | + const entries = [ |
| 13 | + entry('fs.read', 'method', 'fsreadfd'), |
| 14 | + entry('fs.read', 'method', 'fsreadbuffer'), |
| 15 | + entry('fs.read', 'method', 'fsreadoptions'), |
| 16 | + ]; |
| 17 | + |
| 18 | + annotateOverloads(entries); |
| 19 | + |
| 20 | + // The first (most stable) heading is left as-is... |
| 21 | + assert.ok(!entries[0].heading.data.isOverload); |
| 22 | + // ...and the rest are flagged so the ToC can drop them. |
| 23 | + assert.ok(entries[1].heading.data.isOverload); |
| 24 | + assert.ok(entries[2].heading.data.isOverload); |
| 25 | + }); |
| 26 | + |
| 27 | + it('leaves a single-signature function untouched', () => { |
| 28 | + const entries = [entry('fs.access', 'method', 'fsaccess')]; |
| 29 | + |
| 30 | + annotateOverloads(entries); |
| 31 | + |
| 32 | + assert.ok(!entries[0].heading.data.isOverload); |
| 33 | + }); |
| 34 | + |
| 35 | + it('groups constructors by name', () => { |
| 36 | + const entries = [ |
| 37 | + entry('Buffer', 'ctor', 'new-bufferarray'), |
| 38 | + entry('Buffer', 'ctor', 'new-buffersize'), |
| 39 | + ]; |
| 40 | + |
| 41 | + annotateOverloads(entries); |
| 42 | + |
| 43 | + assert.ok(!entries[0].heading.data.isOverload); |
| 44 | + assert.ok(entries[1].heading.data.isOverload); |
| 45 | + }); |
| 46 | + |
| 47 | + it('does not group headings of different types or depths', () => { |
| 48 | + const entries = [ |
| 49 | + entry('Buffer', 'class', 'class-buffer'), |
| 50 | + entry('Buffer', 'ctor', 'new-bufferarray'), |
| 51 | + entry('Buffer', 'ctor', 'new-buffersize', 3), |
| 52 | + ]; |
| 53 | + |
| 54 | + annotateOverloads(entries); |
| 55 | + |
| 56 | + // class is not overloadable; the two ctors differ in depth, so none group. |
| 57 | + assert.ok(entries.every(e => !e.heading.data.isOverload)); |
| 58 | + }); |
| 59 | + |
| 60 | + it('starts a fresh group when a different function interrupts a run', () => { |
| 61 | + const entries = [ |
| 62 | + entry('fs.read', 'method', 'fsreadfd'), |
| 63 | + entry('fs.read', 'method', 'fsreadbuffer'), |
| 64 | + entry('fs.write', 'method', 'fswritefd'), |
| 65 | + entry('fs.write', 'method', 'fswritebuffer'), |
| 66 | + ]; |
| 67 | + |
| 68 | + annotateOverloads(entries); |
| 69 | + |
| 70 | + assert.deepEqual( |
| 71 | + entries.map(e => !!e.heading.data.isOverload), |
| 72 | + [false, true, false, true] |
| 73 | + ); |
| 74 | + }); |
| 75 | +}); |
0 commit comments