Skip to content

Commit

Permalink
Fix output bug in writeLibraryGlobalFallback when no description. Fix…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
luttje committed Feb 16, 2024
1 parent b0c5aac commit 9ef97ac
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 25 deletions.
23 changes: 16 additions & 7 deletions __tests__/api-writer/glua-api-writer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import { LibraryFunction, WikiPage, WikiPageMarkupScraper } from '../../src/scra
import { GluaApiWriter } from '../../src/api-writer/glua-api-writer';
import fetchMock from "jest-fetch-mock";

const mockFilePath = '<irrelevant for this test>';

describe('GLua API Writer', () => {
beforeEach(() => {
fetchMock.resetMocks();
});

it('should be able to write Lua API definitions directly from wiki json data', async () => {
const writer = new GluaApiWriter();
const api = writer.writePages([<WikiPage>hookJson]);
writer.writePages([<WikiPage>hookJson], mockFilePath);
const api = writer.makeApiFromPages(writer.getPages(mockFilePath));

expect(api).toEqual(hookApiDefinition);
});
Expand All @@ -34,7 +37,8 @@ describe('GLua API Writer', () => {
const scrapeCallback = new WikiPageMarkupScraper(responseMock.url).getScrapeCallback();
const structs = await scrapeCallback(responseMock, structMarkup);
expect(structs).toHaveLength(1);
const api = writer.writePages(structs);
writer.writePages(structs, mockFilePath);
const api = writer.makeApiFromPages(writer.getPages(mockFilePath));
expect(api).toEqual(structApiDefinition);
});

Expand All @@ -50,7 +54,8 @@ describe('GLua API Writer', () => {
const scrapeCallback = new WikiPageMarkupScraper(responseMock.url).getScrapeCallback();
const panel = await scrapeCallback(responseMock, panelMarkup);
expect(panel).toHaveLength(1);
const api = writer.writePages(panel);
writer.writePages(panel, mockFilePath);
const api = writer.makeApiFromPages(writer.getPages(mockFilePath));
expect(api).toEqual(panelApiDefinition);
});

Expand All @@ -66,7 +71,8 @@ describe('GLua API Writer', () => {
const scrapeCallback = new WikiPageMarkupScraper(responseMock.url).getScrapeCallback();
const panel = await scrapeCallback(responseMock, multiReturnFuncMarkup);
expect(panel).toHaveLength(1);
const api = writer.writePages(panel);
writer.writePages(panel, mockFilePath);
const api = writer.makeApiFromPages(writer.getPages(mockFilePath));
expect(api).toEqual(multiReturnFuncApiDefinition);
});

Expand All @@ -82,7 +88,8 @@ describe('GLua API Writer', () => {
const scrapeCallback = new WikiPageMarkupScraper(responseMock.url).getScrapeCallback();
const panel = await scrapeCallback(responseMock, varargsFuncMarkup);
expect(panel).toHaveLength(1);
const api = writer.writePages(panel);
writer.writePages(panel, mockFilePath);
const api = writer.makeApiFromPages(writer.getPages(mockFilePath));
expect(api).toEqual(varargsFuncApiDefinition);
});

Expand Down Expand Up @@ -122,7 +129,8 @@ describe('GLua API Writer', () => {
const override = Math.random().toString(36).substring(7);
writer.addOverride(hookJson.address, override);

const api = writer.writePages([<WikiPage>hookJson]);
writer.writePages([<WikiPage>hookJson], mockFilePath);
const api = writer.makeApiFromPages(writer.getPages(mockFilePath));

expect(api).toEqual(`${override}\n\n`);
});
Expand All @@ -133,7 +141,8 @@ describe('GLua API Writer', () => {
const override = `${overrideStart}\nlocal Custom_Entity_Fields = {}`;
writer.addOverride('class.Custom_Entity_Fields', override);

const api = writer.writePages([<WikiPage>structJson]);
writer.writePages([<WikiPage>structJson], mockFilePath);
const api = writer.makeApiFromPages(writer.getPages(mockFilePath));

expect(api).toMatch(new RegExp(`^${overrideStart}`));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ end )
};

export const apiDefinition = `---@class GM
local GM = {}
GM = {}
---[SERVER] Called when the player spawns for the first time.
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const markup = `<function name="GetTable" parent="concommand" type="libra
</function>`;

export const apiDefinition =
`concommand = {}
`--- Missing description.
concommand = {}
---[SHARED AND MENU] Returns the tables of all console command callbacks, and autocomplete functions, that were added to the game with concommand.Add.
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const markup = `<function name="resume" parent="coroutine" type="libraryf
</function>`;

export const apiDefinition =
`coroutine = {}
`--- Missing description.
coroutine = {}
---[SHARED AND MENU] Resumes the given coroutine and passes the given vararg to either the function arguments or the coroutine.yield that is inside that function and returns whatever yield is called with the next time or by the final return in the function.
---
Expand Down
7 changes: 6 additions & 1 deletion __tests__/test-data/offline-sites/gmod-wiki/panel-slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ end
</example>`;

export const apiDefinition =
`---@deprecated Panel:SetActionFunction and Panel:PostMessage. Use DNumSlider instead.
`---
---
---
--- A simple slider featuring an numeric display.
--- Creates a slider atop a DFrame which prints its value as it's being dragged.
---@deprecated Panel:SetActionFunction and Panel:PostMessage. Use DNumSlider instead.
---@class Slider : Panel
local Slider = {}\n\n`;
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ This can also be set from map, see <page>Sandbox Specific Mapping</page></item>
</structure>`;

export const apiDefinition =
`---@class Custom_Entity_Fields
`---
--- Information about custom fields **all** entities can have.
---
--- See also Structures/ENT
---
---@class Custom_Entity_Fields
local Custom_Entity_Fields = {}
---\`Serverside\`, Sandbox and Sandbox derived only. Called by the Drive property to override the default drive type, which is \`drive_sandbox\`.
Expand Down
35 changes: 23 additions & 12 deletions src/api-writer/glua-api-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class GluaApiWriter {
if (!func.dontDefineParent && !this.writtenLibraryGlobals.has(func.parent)) {
let api = '';

api += `---Missing description.`;
api += `--- Missing description.\n`;
api += `${func.parent} = {}\n\n`;

this.writtenLibraryGlobals.add(func.parent);
Expand All @@ -154,7 +154,7 @@ export class GluaApiWriter {
let api = '';

api += page.description ? `${putCommentBeforeEachLine(page.description, false)}\n` : '';

if (page.deprecated)
api += `---@deprecated ${removeNewlines(page.deprecated)}\n`;

Expand All @@ -167,7 +167,7 @@ export class GluaApiWriter {

return '';
}

private writeClassGlobal(page: TypePage) {
return this.writeClassStart(page.name, page.parent, page.deprecated, page.description);
}
Expand Down Expand Up @@ -279,17 +279,28 @@ export class GluaApiWriter {
this.files.get(filePath)!.push(...pages);
}

public getPages(filePath: string) {
return this.files.get(filePath) ?? [];
}

public makeApiFromPages(pages: WikiPage[]) {
let api = "";

// First we write the "header" types
for (const page of pages.filter(x => isClass(x) || isLibrary(x))) {
api += this.writePage(page);
}

for (const page of pages.filter(x => !isClass(x) && !isLibrary(x))) {
api += this.writePage(page);
}

return api;
}

public writeToDisk() {
this.files.forEach((pages: WikiPage[], filePath: string) => {
let api = "";

// First we write the "header" types
for (const page of pages.filter(x => isClass(x) || isLibrary(x))) {
api += this.writePage(page);
}
for (const page of pages.filter(x => !isClass(x) && !isLibrary(x))) {
api += this.writePage(page);
}
let api = this.makeApiFromPages(pages);

if (api.length > 0) {
fs.appendFileSync(filePath, "---@meta\n\n" + api);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export function putCommentBeforeEachLine(text: string, skipLineOne: boolean = tr
if (index === 0 && skipLineOne)
return line;

line = line.trimEnd();
let space = line.length > 0 ? ' ' : '';
return `---${space}${line.trimEnd()}`;
return `---${space}${line}`;
}).join('\n');
}

Expand Down

0 comments on commit 9ef97ac

Please sign in to comment.