Skip to content

Commit d5d9a0e

Browse files
authored
Merge pull request #66 from luttje/feature/updated-wiki-parsers
Updated wiki parsers
2 parents c1341ca + ec0c9a2 commit d5d9a0e

File tree

3 files changed

+356
-12
lines changed

3 files changed

+356
-12
lines changed

__tests__/api-writer/glua-api-writer.spec.ts

Lines changed: 274 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { apiDefinition as structApiDefinition, markup as structMarkup, json as s
66
import { markup as panelMarkup, apiDefinition as panelApiDefinition } from '../test-data/offline-sites/gmod-wiki/panel-slider';
77
import { markup as multiReturnFuncMarkup, apiDefinition as multiReturnFuncApiDefinition } from '../test-data/offline-sites/gmod-wiki/library-function-concommand-gettable';
88
import { markup as varargsFuncMarkup, apiDefinition as varargsFuncApiDefinition } from '../test-data/offline-sites/gmod-wiki/library-function-coroutine-resume';
9-
import { LibraryFunction, WikiPage, WikiPageMarkupScraper } from '../../src/scrapers/wiki-page-markup-scraper';
9+
import { Enum, LibraryFunction, WikiPage, WikiPageMarkupScraper } from '../../src/scrapers/wiki-page-markup-scraper';
1010
import { GluaApiWriter } from '../../src/api-writer/glua-api-writer';
1111
import fetchMock from "jest-fetch-mock";
1212

@@ -149,6 +149,279 @@ describe('GLua API Writer', () => {
149149
expect(api).toMatch(new RegExp(`^${overrideStart}`));
150150
});
151151

152+
it('should create aliasses for global enumerations', () => {
153+
const writer = new GluaApiWriter();
154+
const api = writer.writePage(<Enum>{
155+
type: 'enum',
156+
name: 'MATERIAL_FOG',
157+
address: 'Enums/MATERIAL_FOG',
158+
description: 'The fog mode.',
159+
realm: 'Client',
160+
items: [
161+
{
162+
key: 'MATERIAL_FOG_NONE',
163+
value: '0',
164+
description: 'No fog',
165+
},
166+
{
167+
key: 'MATERIAL_FOG_LINEAR',
168+
value: '1',
169+
description: 'Linear fog',
170+
},
171+
{
172+
key: 'MATERIAL_FOG_LINEAR_BELOW_FOG_Z',
173+
value: '-2147483648', // test large negative number
174+
},
175+
{
176+
// Should be skipped
177+
key: 'MATERIAL_FOG_NEW_FAKE',
178+
value: 'TODO',
179+
}
180+
],
181+
});
182+
183+
expect(api).toEqual(`---@alias MATERIAL_FOG 0|1|-2147483648\n--- No fog\nMATERIAL_FOG_NONE = 0\n--- Linear fog\nMATERIAL_FOG_LINEAR = 1\nMATERIAL_FOG_LINEAR_BELOW_FOG_Z = -2147483648\n\n\n`);
184+
});
185+
186+
it('should create enums for table enumerations', () => {
187+
const writer = new GluaApiWriter();
188+
const api = writer.writePage(<Enum>{
189+
type: 'enum',
190+
name: 'SCREENFADE',
191+
address: 'Enums/SCREENFADE',
192+
description: 'The screen fade mode.',
193+
realm: 'Client',
194+
items: [
195+
{
196+
key: '',
197+
value: '0',
198+
description: 'Instant fade in',
199+
},
200+
{
201+
key: 'SCREENFADE.IN',
202+
value: '1',
203+
description: 'Instant fade in',
204+
},
205+
{
206+
key: 'SCREENFADE.OUT',
207+
value: '2',
208+
description: 'Slowly fade in',
209+
},
210+
{
211+
key: 'SCREENFADE.MODULATE',
212+
value: '4',
213+
},
214+
{
215+
key: 'SCREENFADE.STAYOUT',
216+
value: '8',
217+
description: 'Never disappear',
218+
},
219+
{
220+
key: 'SCREENFADE.PURGE',
221+
value: '16',
222+
description: 'Used to purge all currently active screen fade effects...\nMultiple\nLines',
223+
},
224+
],
225+
});
226+
227+
expect(api).toEqual(`---@enum SCREENFADE\n--- The screen fade mode.\nSCREENFADE = {\n --- Instant fade in\n IN = 1,\n --- Slowly fade in\n OUT = 2,\n MODULATE = 4,\n --- Never disappear\n STAYOUT = 8,\n --- Used to purge all currently active screen fade effects...\n --- Multiple\n --- Lines\n PURGE = 16,\n}\n\n`);
228+
});
229+
230+
it('should convert table<type> to type[]', () => {
231+
const writer = new GluaApiWriter();
232+
const api = writer.writePage(<LibraryFunction>{
233+
name: 'GetBots',
234+
address: 'player.GetBots',
235+
parent: 'player',
236+
dontDefineParent: true,
237+
description: 'Returns a table of all bots on the server.',
238+
realm: 'Shared',
239+
type: 'libraryfunc',
240+
url: 'na',
241+
returns: [
242+
{
243+
type: 'table<Player>',
244+
description: 'A table only containing bots ( AI / non human players )',
245+
},
246+
],
247+
});
248+
249+
expect(api).toEqual(`---[SHARED] Returns a table of all bots on the server.\n---\n---[(View on wiki)](na)\n---@return Player[] # A table only containing bots ( AI / non human players )\nfunction player.GetBots() end\n\n`);
250+
});
251+
252+
it('should not convert table<type,otherType> to type,otherType[]', () => {
253+
const writer = new GluaApiWriter();
254+
const api = writer.writePage(<LibraryFunction>{
255+
name: 'GetBots',
256+
address: 'player.GetBots',
257+
parent: 'player',
258+
dontDefineParent: true,
259+
description: 'Returns a table of all bots on the server.',
260+
realm: 'Shared',
261+
type: 'libraryfunc',
262+
url: 'na',
263+
returns: [
264+
{
265+
type: 'table<number,Player>',
266+
description: 'A table only containing bots ( AI / non human players )',
267+
},
268+
],
269+
});
270+
271+
expect(api).toEqual(`---[SHARED] Returns a table of all bots on the server.\n---\n---[(View on wiki)](na)\n---@return table<number,Player> # A table only containing bots ( AI / non human players )\nfunction player.GetBots() end\n\n`);
272+
});
273+
274+
const testFuncPart = {
275+
name: 'Fake',
276+
address: 'test.Fake',
277+
parent: 'test',
278+
dontDefineParent: true,
279+
description: 'Just for testing.',
280+
realm: 'Shared',
281+
type: 'libraryfunc',
282+
url: 'na',
283+
};
284+
285+
it.each([
286+
// Simple case with an altType (deprecated in wiki)
287+
{
288+
api: <LibraryFunction>{
289+
...testFuncPart,
290+
arguments: [
291+
{
292+
args: [{
293+
name: 'value',
294+
type: 'string',
295+
description: 'The value to fake.',
296+
altType: 'number',
297+
}]
298+
}
299+
],
300+
},
301+
output: `---[SHARED] Just for testing.\n---\n---[(View on wiki)](na)\n---@param value string|number The value to fake.\nfunction test.Fake(value) end\n\n`,
302+
},
303+
// Case with pipes in the type (prefered in wiki)
304+
{
305+
api: <LibraryFunction>{
306+
...testFuncPart,
307+
arguments: [
308+
{
309+
args: [{
310+
name: 'value',
311+
type: 'string|number',
312+
description: 'The value to fake.',
313+
}]
314+
}
315+
],
316+
},
317+
output: `---[SHARED] Just for testing.\n---\n---[(View on wiki)](na)\n---@param value string|number The value to fake.\nfunction test.Fake(value) end\n\n`,
318+
},
319+
// Case with pipes and table<x> conversion
320+
{
321+
api: <LibraryFunction>{
322+
...testFuncPart,
323+
arguments: [
324+
{
325+
args: [{
326+
name: 'value',
327+
type: 'string|table<number,Player>',
328+
description: 'The value to fake.',
329+
}]
330+
}
331+
],
332+
},
333+
output: `---[SHARED] Just for testing.\n---\n---[(View on wiki)](na)\n---@param value string|table<number,Player> The value to fake.\nfunction test.Fake(value) end\n\n`,
334+
},
335+
// Case with table<x> conversion in both altType and type
336+
{
337+
api: <LibraryFunction>{
338+
...testFuncPart,
339+
arguments: [
340+
{
341+
args: [{
342+
name: 'value',
343+
type: 'table<number,Player>',
344+
description: 'The value to fake.',
345+
altType: 'table<Entity,number>',
346+
}]
347+
}
348+
],
349+
},
350+
output: `---[SHARED] Just for testing.\n---\n---[(View on wiki)](na)\n---@param value table<number,Player>|table<Entity,number> The value to fake.\nfunction test.Fake(value) end\n\n`,
351+
},
352+
])('should handle alternate types correctly', async ({ api, output }) => {
353+
const writer = new GluaApiWriter();
354+
const result = writer.writePage(api);
355+
356+
expect(result).toEqual(output);
357+
});
358+
359+
// TODO: Currently unsupported. Remove 'failing' (and change test name) when supporting this. (low priority imo)
360+
it.failing('should currently fail in complicated cases like `table<string|number>|string`.', () => {
361+
const writer = new GluaApiWriter();
362+
const api = writer.writePage(<LibraryFunction>{
363+
...testFuncPart,
364+
arguments: [
365+
{
366+
args: [{
367+
name: 'value',
368+
type: 'table<string|number>|string',
369+
description: 'The value to fake.',
370+
}]
371+
}
372+
],
373+
});
374+
375+
expect(api).toEqual(`---[SHARED] Just for testing.\n---\n---[(View on wiki)](na)\n---@param value table<string|number>|string The value to fake.\nfunction test.Fake(value) end\n\n`);
376+
});
377+
378+
it('should support structure table type', () => {
379+
const writer = new GluaApiWriter();
380+
const api = writer.writePage(<LibraryFunction>{
381+
name: 'ToScreen',
382+
address: 'Vector.ToScreen',
383+
parent: 'Vector',
384+
dontDefineParent: true,
385+
description: 'Returns where on the screen the specified position vector would appear.',
386+
realm: 'Client',
387+
type: 'libraryfunc',
388+
url: 'na',
389+
returns: [
390+
{
391+
type: 'table{ToScreenData}',
392+
description: 'The created Structures/ToScreenData.',
393+
},
394+
],
395+
});
396+
397+
expect(api).toEqual(`---[CLIENT] Returns where on the screen the specified position vector would appear.\n---\n---[(View on wiki)](na)\n---@return ToScreenData # The created Structures/ToScreenData.\nfunction Vector.ToScreen() end\n\n`);
398+
});
399+
400+
// number{ENUM_NAME} -> ENUM_NAME
401+
it('should support enum type', () => {
402+
const writer = new GluaApiWriter();
403+
const api = writer.writePage(<LibraryFunction>{
404+
name: 'FogMode',
405+
address: 'render.FogMode',
406+
parent: 'render',
407+
dontDefineParent: true,
408+
description: 'Sets the fog mode.',
409+
realm: 'Client',
410+
type: 'libraryfunc',
411+
url: 'na',
412+
arguments: [
413+
{
414+
args: [{
415+
name: 'mode',
416+
type: 'number{MATERIAL_FOG}',
417+
description: 'The fog mode.',
418+
}]
419+
}
420+
],
421+
});
422+
423+
expect(api).toEqual(`---[CLIENT] Sets the fog mode.\n---\n---[(View on wiki)](na)\n---@param mode MATERIAL_FOG The fog mode.\nfunction render.FogMode(mode) end\n\n`);
424+
});
152425

153426
// it('should be able to write Annotated API files directly from wiki pages', async () => {
154427
// const baseUrl = 'https://wiki.facepunch.com/gmod/GM:AcceptInput';

0 commit comments

Comments
 (0)