|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { |
| 5 | + extractTypesAndDescription, |
| 6 | + processListItemsIntoProperties, |
| 7 | + generateSignatures, |
| 8 | + getFullName, |
| 9 | +} from '../createSignatureElements.mjs'; |
| 10 | + |
| 11 | +describe('createSignatureElements', () => { |
| 12 | + describe('extractTypesAndDescription', () => { |
| 13 | + it('should extract types and description from content nodes', () => { |
| 14 | + const contentNodes = [ |
| 15 | + { type: 'link', children: [{ type: 'inlineCode', value: '<string>' }] }, |
| 16 | + { type: 'text', value: ' | ' }, |
| 17 | + { type: 'link', children: [{ type: 'inlineCode', value: '<number>' }] }, |
| 18 | + { type: 'text', value: ' - This is a description' }, |
| 19 | + ]; |
| 20 | + |
| 21 | + const result = extractTypesAndDescription(contentNodes); |
| 22 | + |
| 23 | + assert.equal(result.types.length, 3); |
| 24 | + assert.equal(result.description.length, 1); |
| 25 | + assert.equal(result.description[0].value, ' - This is a description'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should handle generic types correctly', () => { |
| 29 | + const contentNodes = [ |
| 30 | + { type: 'link', children: [{ type: 'inlineCode', value: '<T>' }] }, |
| 31 | + { type: 'text', value: ' - Description of generic' }, |
| 32 | + ]; |
| 33 | + |
| 34 | + const result = extractTypesAndDescription(contentNodes); |
| 35 | + |
| 36 | + assert.equal(result.types.length, 1); |
| 37 | + assert.equal(result.types[0].children[0].value, '<T>'); |
| 38 | + assert.equal(result.description.length, 1); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should handle content with no types', () => { |
| 42 | + const contentNodes = [{ type: 'text', value: 'Only description' }]; |
| 43 | + |
| 44 | + const result = extractTypesAndDescription(contentNodes); |
| 45 | + |
| 46 | + assert.equal(result.types.length, 0); |
| 47 | + assert.equal(result.description.length, 1); |
| 48 | + assert.equal(result.description[0].value, 'Only description'); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('processListItemsIntoProperties', () => { |
| 53 | + it('should convert list items into property data objects', () => { |
| 54 | + const listItems = [ |
| 55 | + { |
| 56 | + children: [ |
| 57 | + { |
| 58 | + type: 'paragraph', |
| 59 | + children: [ |
| 60 | + { type: 'text', value: 'propName' }, |
| 61 | + { |
| 62 | + type: 'link', |
| 63 | + children: [{ type: 'inlineCode', value: '<string>' }], |
| 64 | + }, |
| 65 | + { type: 'text', value: ' - Description text' }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | + ]; |
| 71 | + |
| 72 | + const result = processListItemsIntoProperties(listItems); |
| 73 | + |
| 74 | + assert.equal(result.length, 1); |
| 75 | + assert.ok(result[0].hasTypes); |
| 76 | + assert.ok(result[0].hasDescription); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('generateSignatures', () => { |
| 81 | + it('should generate a single signature when no optional parameters', () => { |
| 82 | + const functionName = 'testFunction'; |
| 83 | + const signature = { |
| 84 | + params: [ |
| 85 | + { name: 'param1', optional: false }, |
| 86 | + { name: 'param2', optional: false }, |
| 87 | + ], |
| 88 | + return: { type: 'string' }, |
| 89 | + }; |
| 90 | + |
| 91 | + const result = generateSignatures(functionName, signature); |
| 92 | + |
| 93 | + assert.equal(result.length, 1); |
| 94 | + assert.equal(result[0], 'testFunction(param1, param2): string'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should generate multiple signatures for optional parameters', () => { |
| 98 | + const functionName = 'testFunction'; |
| 99 | + const signature = { |
| 100 | + params: [ |
| 101 | + { name: 'required', optional: false }, |
| 102 | + { name: 'optional1', optional: true }, |
| 103 | + { name: 'optional2', optional: true }, |
| 104 | + ], |
| 105 | + return: { type: 'number' }, |
| 106 | + }; |
| 107 | + |
| 108 | + const result = generateSignatures(functionName, signature); |
| 109 | + |
| 110 | + assert.equal(result.length, 4); |
| 111 | + assert.ok(result.includes('testFunction(required): number')); |
| 112 | + assert.ok(result.includes('testFunction(required, optional1?): number')); |
| 113 | + assert.ok(result.includes('testFunction(required, optional2?): number')); |
| 114 | + assert.ok( |
| 115 | + result.includes( |
| 116 | + 'testFunction(required, optional1?, optional2?): number' |
| 117 | + ) |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle functions without return type', () => { |
| 122 | + const functionName = 'voidFunction'; |
| 123 | + const signature = { |
| 124 | + params: [{ name: 'param', optional: false }], |
| 125 | + return: null, |
| 126 | + }; |
| 127 | + |
| 128 | + const result = generateSignatures(functionName, signature); |
| 129 | + |
| 130 | + assert.equal(result[0], 'voidFunction(param)'); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('getFullName', () => { |
| 135 | + it('should extract function name from code in text', () => { |
| 136 | + const heading = { |
| 137 | + name: 'functionName', |
| 138 | + text: 'The `obj.functionName` function', |
| 139 | + }; |
| 140 | + |
| 141 | + const result = getFullName(heading); |
| 142 | + |
| 143 | + assert.equal(result, 'obj.functionName'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should return just the name when no code is found', () => { |
| 147 | + const heading = { |
| 148 | + name: 'functionName', |
| 149 | + text: 'The functionName function', |
| 150 | + }; |
| 151 | + |
| 152 | + const result = getFullName(heading); |
| 153 | + |
| 154 | + assert.equal(result, 'functionName'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should handle complex function names', () => { |
| 158 | + const heading = { |
| 159 | + name: 'add', |
| 160 | + text: 'Using `math.add` to add numbers', |
| 161 | + }; |
| 162 | + |
| 163 | + const result = getFullName(heading); |
| 164 | + |
| 165 | + assert.equal(result, 'math.add'); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments