From 710c9536d8bc9a1c941eef718df6a3587e19a8c9 Mon Sep 17 00:00:00 2001 From: Javier Ciberman Mora Date: Fri, 2 Mar 2018 05:56:41 -0300 Subject: [PATCH] Refactor GMS2ProjectLoader --- src/cli/CliGenerateFacade.ts | 4 +- src/config/OutputConfig.ts | 3 - src/gm_project/ProjectLoader.ts | 35 ++-- ...ProjectFactory.ts => GMS1ProjectLoader.ts} | 33 +--- src/gm_project/gms2/GMS2ProjectFactory.ts | 159 +++++++----------- src/gm_project/gms2/GMS2ProjectLoader.ts | 52 ++++++ src/gm_project/gms2/GMS2Script.ts | 5 +- ...jectFactory.d.ts => IGMProjectLoader.d.ts} | 4 +- tests/gm_project/ProjectLoader.spec.ts | 12 +- ...tory.spec.ts => GMS1ProjectLoader.spec.ts} | 18 +- ...tory.spec.ts => GMS2ProjectLoader.spec.ts} | 26 +-- tests/gm_project/gms2/GMS2Script.spec.ts | 39 ++--- 12 files changed, 184 insertions(+), 206 deletions(-) rename src/gm_project/gms1/{GMS1ProjectFactory.ts => GMS1ProjectLoader.ts} (74%) create mode 100644 src/gm_project/gms2/GMS2ProjectLoader.ts rename src/gm_project/interfaces/{IGMProjectFactory.d.ts => IGMProjectLoader.d.ts} (54%) rename tests/gm_project/gms1/{GMS1ProjectFactory.spec.ts => GMS1ProjectLoader.spec.ts} (80%) rename tests/gm_project/gms2/{GMS2ProjectFactory.spec.ts => GMS2ProjectLoader.spec.ts} (82%) diff --git a/src/cli/CliGenerateFacade.ts b/src/cli/CliGenerateFacade.ts index 2ff9128..9e62255 100644 --- a/src/cli/CliGenerateFacade.ts +++ b/src/cli/CliGenerateFacade.ts @@ -59,8 +59,8 @@ export default class CliGenerateFacade implements ICliGenerateFacade { this._reporter.info("Loading Project..."); - const loader = new ProjectLoader(projectPath); - const project = await loader.load(); + const loader = new ProjectLoader(); + const project = await loader.load(projectPath); this._reporter.info("Loading project configuration..."); diff --git a/src/config/OutputConfig.ts b/src/config/OutputConfig.ts index b2130a8..d24672b 100644 --- a/src/config/OutputConfig.ts +++ b/src/config/OutputConfig.ts @@ -1,11 +1,8 @@ -import { injectable } from "inversify"; - import IOutputConfig from "./interfaces/IOutputConfig"; /** * This class has all the configuration for the DocsGM Output */ -@injectable() export default class OutputConfig implements IOutputConfig { /** * The design name. If empty, it will use the first design in the designs list. diff --git a/src/gm_project/ProjectLoader.ts b/src/gm_project/ProjectLoader.ts index b320e40..7a54c58 100644 --- a/src/gm_project/ProjectLoader.ts +++ b/src/gm_project/ProjectLoader.ts @@ -1,37 +1,24 @@ import * as globby from "globby"; import * as path from "path"; -import GMS1ProjectFactory from "../gm_project/gms1/GMS1ProjectFactory"; -import GMS2ProjectFactory from "../gm_project/gms2/GMS2ProjectFactory"; import IGMProject from "../gm_project/interfaces/IGMProject"; -import IGMProjectFactory from "../gm_project/interfaces/IGMProjectFactory"; +import GMS1ProjectLoader from "./gms1/GMS1ProjectLoader"; +import GMS2ProjectLoader from "./gms2/GMS2ProjectLoader"; +import IGMProjectLoader from "./interfaces/IGMProjectLoader"; /** * This Factory class loads a GMS1 or GMS2 project and returns a GMProject object */ -export default class ProjectLoader { - - /** - * The project path - */ - private readonly _gmProjectPath: string; - - /** - * Creates a new Project loader - * @param gmProjectPath The path of the project to load - */ - public constructor(gmProjectPath: string) { - this._gmProjectPath = gmProjectPath; - } +export default class ProjectLoader implements IGMProjectLoader { /** * Loads a specified GMS1 or GMS2 Project - * @param GMProjectPath The project path to load + * @param gmProjectPath The project path to load * @return A promise with the loaded project */ - public async load(): Promise { + public async load(gmProjectPath: string): Promise { - const files = await globby(this._gmProjectPath + "/*.{yyp,gmx}"); + const files = await globby(gmProjectPath + "/*.{yyp,gmx}"); if (files.length === 0) { throw new Error("Unrecognized GM project. No *.yyp or *.gmx file found"); @@ -41,12 +28,12 @@ export default class ProjectLoader { const ext = extArr[extArr.length - 1]; - let factory: IGMProjectFactory; + let loader: IGMProjectLoader; if (ext === "gmx") { - factory = new GMS1ProjectFactory(files[0]); + loader = new GMS1ProjectLoader(); } else { - factory = new GMS2ProjectFactory(files[0]); + loader = new GMS2ProjectLoader(); } - return factory.load(); + return loader.load(files[0]); } } diff --git a/src/gm_project/gms1/GMS1ProjectFactory.ts b/src/gm_project/gms1/GMS1ProjectLoader.ts similarity index 74% rename from src/gm_project/gms1/GMS1ProjectFactory.ts rename to src/gm_project/gms1/GMS1ProjectLoader.ts index 1cd77ad..192fc36 100644 --- a/src/gm_project/gms1/GMS1ProjectFactory.ts +++ b/src/gm_project/gms1/GMS1ProjectLoader.ts @@ -5,41 +5,24 @@ import * as xml2js from "xml2js"; import GMFolder from "../GMFolder"; import GMProject from "../GMProject"; import IGMProject from "../interfaces/IGMProject"; -import IGMProjectFactory from "../interfaces/IGMProjectFactory"; +import IGMProjectLoader from "../interfaces/IGMProjectLoader"; import GMS1Script from "./GMS1Script"; import { IGMS1DescriptorFolder, IGMS1DescriptorRoot } from "./IGMS1Descriptor"; /** * Loads a GMS1 project file and creates a GMS1Project */ -export default class GMS1ProjectFactory implements IGMProjectFactory { - - /** - * The project file - */ - private readonly _file: string; - - /** - * The project to create - */ - private readonly _project: IGMProject; - - /** - * Creates a new GMS1Project Factory - * @param file The project file - */ - constructor(file: string) { - this._file = file; - this._project = new GMProject(path.dirname(this._file)); - } +export default class GMS1ProjectLoader implements IGMProjectLoader { /** * Loads the specified GMS1 project * @param file The file path of the project to load * @returns A Promise with the GMS1Project */ - public async load(): Promise { - const str = await fse.readFile(this._file, "utf8"); + public async load(file: string): Promise { + const project = new GMProject(path.dirname(file)); + + const str = await fse.readFile(file, "utf8"); // "assets" is the root XML node of the document, but we call Root at the content of that node const data: IGMS1DescriptorRoot = await this._xmlParse(str); @@ -47,10 +30,10 @@ export default class GMS1ProjectFactory implements IGMProjectFactory { // Creates the root folder if (assets.scripts) { const scriptsFolder = this._createFolder(assets.scripts[0]); - this._project.addChild(scriptsFolder); + project.addChild(scriptsFolder); } - return this._project; + return project; } /** diff --git a/src/gm_project/gms2/GMS2ProjectFactory.ts b/src/gm_project/gms2/GMS2ProjectFactory.ts index 3459289..fdf9321 100644 --- a/src/gm_project/gms2/GMS2ProjectFactory.ts +++ b/src/gm_project/gms2/GMS2ProjectFactory.ts @@ -1,153 +1,114 @@ -import * as fse from "fs-extra"; -import * as path from "path"; - import GMFolder from "../GMFolder"; import GMProject from "../GMProject"; import GMResource from "../GMResource"; import IGMFolder from "../interfaces/IGMFolder"; import IGMProject from "../interfaces/IGMProject"; -import IGMProjectFactory from "../interfaces/IGMProjectFactory"; import IGMResource from "../interfaces/IGMResource"; -import GMS2ResourceType from "./GMS2ResourceType"; import GMS2Script from "./GMS2Script"; -import { IFolder, IProject, IResource, IResourceInfo, IScript } from "./IGMS2Descriptor"; /** - * Creates a new GameMakerStudio 2 Project Factory that loads a project - * file and creates a new GMProject + * Factory class to create GMS2Project instances */ -export default class GMS2ProjectFactory implements IGMProjectFactory { - - /** - * A map with each key is the GMS2 parent folder and each value is an - * array with the keys of the childrens of that folder. - */ - private _folderChildsKeys: Map = new Map(); +export default class GMS2ProjectFactory { /** - * A map with the resources by key. + * The created GMProject */ - private _resourcesByKey: Map = new Map(); + private _project: IGMProject; /** - * The project file + * A map with each GMFolder, and an array with the children key ids of each folder */ - private readonly _file: string; + private _folderChildrenKeys: Map = new Map(); /** - * The GMS2Project to create + * A map with the key ids and the associated resource */ - private readonly _project: IGMProject; + private _resourcesByKey: Map = new Map(); /** - * Creates GMS2ProjectFactory - * @param file The project file + * Creates an instance of GMS2ProjectFactory. + * @param {string} gmProjectFolder The project folder + * @memberof GMS2ProjectFactory */ - constructor(file: string) { - this._file = file; - this._project = new GMProject(path.dirname(this._file)); + public constructor(gmProjectFolder: string) { + this._project = new GMProject(gmProjectFolder); } /** - * Loads the specified GMS2 project - * @param file The file path of the project to load - * @returns A Promise with the created GMS2Project + * Builds the GMS2Project instance and returns it */ - public async load(): Promise { - const str = await fse.readFile(this._file, "utf8"); - const data: IProject = JSON.parse(str); - - for (const item of data.resources) { - await this._addResource(item.Key, item.Value); - } + public build() { for (const folder of this._project.children) { - this._buildSubTree(folder); + this._buildSubtree(folder); } return this._project; } /** - * Build subtre for the specified folder - * @param folder The folder to find the childrens + * Adds a new GMS2Folder to the project + * + * @param {string} key The resource key id + * @param {string} folderName The folder name + * @param {string} localizedFolderName The localizedFolderName + * @param {string[]} children An array with the children keys id of the folder + * @memberof GMS2ProjectFactory */ - private _buildSubTree(folder: IGMFolder) { - const children = this._folderChildsKeys.get(folder) as string[]; - for (const childKey of children) { - const child = this._resourcesByKey.get(childKey); - if (child) { - folder.addChild(child); - if (this._isFolder(child)) { - this._buildSubTree(child); - } - } + public addFolder(key: string, folderName: string, localizedFolderName: string, children: string[]): void { + const topLevelName = localizedFolderName.split("ResourceTree_").join(""); + const folder = new GMFolder(folderName); + this._folderChildrenKeys.set(folder, children); + if (topLevelName !== "") { + this._project.addChild(folder); } + this._resourcesByKey.set(key, folder); } /** - * Determines if the resource is a folder (ducktyping) - * @param res The resource + * Adds a new GMS2Script + * + * @param {string} key The resource key id + * @param {string} name The script name + * @param {boolean} isCompatibility Is a compatibility script? + * @memberof GMS2ProjectFactory */ - private _isFolder(res: IGMResource): res is IGMFolder { - return ("addChild" in res); + public addScript(key: string, name: string, isCompatibility: boolean): void { + const script = new GMS2Script(name, isCompatibility); + this._resourcesByKey.set(key, script); } /** - * Adds and loads a resource - * @param key Resource Key - * @param resource ResourceInfo + * Adds a new Resource to the GMS2Project + * @param key The resource key id + * @param name The resource name */ - private async _addResource(key: string, resource: IResourceInfo) { - const data = await this._loadResourceData(resource.resourcePath); - const res = this._createFromData(data); + public addResource(key: string, name: string) { + const res = new GMResource(name); this._resourcesByKey.set(key, res); } /** - * Creates a GMS2Resource from a YOYO model data - * @param modelData The YOYO model data to create the GMS2Resource from + * Build subtree for the specified folder + * @param folder The folder to find the children */ - private _createFromData(modelData: IResource): GMResource { - switch (modelData.modelName) { - case GMS2ResourceType.GMFolder: - return this._createFolder(modelData as IFolder); - case GMS2ResourceType.GMScript: - return this._createScript(modelData as IScript); - default: - return new GMResource(modelData.name); - } - } - - /** - * Load a resource JSON data from a file - * @param filename The relative path to load the data from - */ - private async _loadResourceData(filename: string): Promise { - const file = path.resolve(this._project.path, filename); - const str = await fse.readFile(file, "utf8"); - return JSON.parse(str); - } - - /** - * Creates a new GMS2Folder - * @param data The folder data - */ - private _createFolder(data: IFolder): GMFolder { - const topLevelName = data.localisedFolderName.split("ResourceTree_").join(""); - const folder = new GMFolder(data.folderName); - this._folderChildsKeys.set(folder, data.children); - if (topLevelName !== "") { - this._project.addChild(folder); + private _buildSubtree(folder: IGMFolder) { + const children = this._folderChildrenKeys.get(folder) as string[]; + for (const childKey of children) { + const child = this._resourcesByKey.get(childKey); + if (child) { + folder.addChild(child); + if (this._isFolder(child)) { + this._buildSubtree(child); + } + } } - return folder; } /** - * Creates a new GMS2Script - * @param data The Script data + * Determines if the resource is a folder (ducktyping) + * @param res The resource */ - private _createScript(data: IScript): GMS2Script { - const script = new GMS2Script(data.name); - script.isCompatibility = data.IsCompatibility; - return script; + private _isFolder(res: IGMResource): res is IGMFolder { + return ("addChild" in res); } } diff --git a/src/gm_project/gms2/GMS2ProjectLoader.ts b/src/gm_project/gms2/GMS2ProjectLoader.ts new file mode 100644 index 0000000..4084eab --- /dev/null +++ b/src/gm_project/gms2/GMS2ProjectLoader.ts @@ -0,0 +1,52 @@ +import * as fse from "fs-extra"; +import * as path from "path"; + +import IGMProject from "../interfaces/IGMProject"; +import IGMProjectLoader from "../interfaces/IGMProjectLoader"; +import GMS2ProjectFactory from "./GMS2ProjectFactory"; +import GMS2ResourceType from "./GMS2ResourceType"; +import { IFolder, IProject, IResource, IScript } from "./IGMS2Descriptor"; + +/** + * Creates a new GameMakerStudio 2 Project Factory that loads a project + * file and creates a new GMProject + */ +export default class GMS2ProjectLoader implements IGMProjectLoader { + /** + * Loads the specified GMS2 project + * @param file The file path of the project to load + * @returns A Promise with the created GMS2Project + */ + public async load(projectFile: string): Promise { + const projectFolder = path.dirname(projectFile); + const factory = new GMS2ProjectFactory(projectFolder); + + const data: IProject = await fse.readJson(projectFile); + + for (const item of data.resources) { + const resourceJsonFile = path.resolve(projectFolder, item.Value.resourcePath); + const resourceData: IResource = await fse.readJson(resourceJsonFile); + this._createFromData(factory, item.Key, resourceData); + } + + return factory.build(); + } + + /** + * Creates a GMS2Resource from a YOYO model data + * @param modelData The YOYO model data to create the GMS2Resource from + */ + private _createFromData(factory: GMS2ProjectFactory, key: string, modelData: IResource): void { + switch (modelData.modelName) { + case GMS2ResourceType.GMFolder: + const folderData = modelData as IFolder; + return factory.addFolder(key, folderData.name, folderData.localisedFolderName, folderData.children); + case GMS2ResourceType.GMScript: + const scriptData = modelData as IScript; + return factory.addScript(key, scriptData.name, scriptData.IsCompatibility); + default: + return factory.addResource(key, modelData.name); + } + } + +} diff --git a/src/gm_project/gms2/GMS2Script.ts b/src/gm_project/gms2/GMS2Script.ts index f21c0f8..ba2eb58 100644 --- a/src/gm_project/gms2/GMS2Script.ts +++ b/src/gm_project/gms2/GMS2Script.ts @@ -8,7 +8,7 @@ export default class GMS2Script extends GMScript { /** * Is a compatibility script? */ - public isCompatibility: boolean; + public readonly isCompatibility: boolean; /** * The GML content @@ -19,8 +19,9 @@ export default class GMS2Script extends GMScript { * Creates a new Script * @param data The yoyo model data for the script */ - constructor(name: string) { + constructor(name: string, isCompatibility: boolean) { super(name); + this.isCompatibility = isCompatibility; } /** diff --git a/src/gm_project/interfaces/IGMProjectFactory.d.ts b/src/gm_project/interfaces/IGMProjectLoader.d.ts similarity index 54% rename from src/gm_project/interfaces/IGMProjectFactory.d.ts rename to src/gm_project/interfaces/IGMProjectLoader.d.ts index eecbf4a..bcdd966 100644 --- a/src/gm_project/interfaces/IGMProjectFactory.d.ts +++ b/src/gm_project/interfaces/IGMProjectLoader.d.ts @@ -1,8 +1,8 @@ import IGMProject from "./IGMProject"; -export default interface IGMProjectFactory { +export default interface IGMProjectLoader { /** * Loads a project file and returns a GMProject */ - load(): Promise + load(file: string): Promise } \ No newline at end of file diff --git a/tests/gm_project/ProjectLoader.spec.ts b/tests/gm_project/ProjectLoader.spec.ts index d2cbaa3..b4146ef 100644 --- a/tests/gm_project/ProjectLoader.spec.ts +++ b/tests/gm_project/ProjectLoader.spec.ts @@ -46,22 +46,22 @@ export class DocsGMFixture { @AsyncTest("load should load a GMS1 Project") public async load_GMS1() { - const loader = new ProjectLoader(this.projectGMS1.dir); - const p = await loader.load(); + const loader = new ProjectLoader(); + const p = await loader.load(this.projectGMS1.dir); Expect(p).toBeDefined(); } @AsyncTest("load should load a GMS2 Project") public async load_GMS2() { - const loader = new ProjectLoader(this.projectGMS2.dir); - const p = await loader.load(); + const loader = new ProjectLoader(); + const p = await loader.load(this.projectGMS2.dir); Expect(p).toBeDefined(); } @AsyncTest("load should thrown when loading an invalid project") public async load_Invalid() { - const loader = new ProjectLoader(this.noProject.dir); - await loader.load().then(() => { + const loader = new ProjectLoader(); + await loader.load(this.noProject.dir).then(() => { throw new Error("load() did not throw on invalid project"); }).catch((e) => { Expect(e).toBeDefined(); diff --git a/tests/gm_project/gms1/GMS1ProjectFactory.spec.ts b/tests/gm_project/gms1/GMS1ProjectLoader.spec.ts similarity index 80% rename from tests/gm_project/gms1/GMS1ProjectFactory.spec.ts rename to tests/gm_project/gms1/GMS1ProjectLoader.spec.ts index 86045ff..7ab4dab 100644 --- a/tests/gm_project/gms1/GMS1ProjectFactory.spec.ts +++ b/tests/gm_project/gms1/GMS1ProjectLoader.spec.ts @@ -9,15 +9,15 @@ import { import * as xml2js from "xml2js"; import GMFolder from "../../../src/gm_project/GMFolder"; import GMProject from "../../../src/gm_project/GMProject"; -import GMS1ProjectFactory from "../../../src/gm_project/gms1/GMS1ProjectFactory"; +import GMS1ProjectLoader from "../../../src/gm_project/gms1/GMS1ProjectLoader"; import { IGMS1DescriptorRoot } from "../../../src/gm_project/gms1/IGMS1Descriptor"; import IGMProject from "../../../src/gm_project/interfaces/IGMProject"; import { TempDir } from "../../_testing_helpers/TempDir.help"; /* tslint:disable:max-classes-per-file completed-docs */ -@TestFixture("GMScript") -export class GMS1ProjectFactoryFixture { +@TestFixture("GMS1ProjectLoader") +export class GMS1ProjectLoaderFixture { public folder: TempDir; @@ -58,8 +58,8 @@ export class GMS1ProjectFactoryFixture { @AsyncTest("should load a normal project") public async load_normal() { - const factory = new GMS1ProjectFactory(this.folder.join("my-project-normal.xml")); - const proj: IGMProject = await factory.load(); + const factory = new GMS1ProjectLoader(); + const proj: IGMProject = await factory.load(this.folder.join("my-project-normal.xml")); const ls = this._ls(proj as GMProject); Expect(ls).toEqual([ @@ -75,8 +75,8 @@ export class GMS1ProjectFactoryFixture { @AsyncTest("should load an empty project") public async load_empty() { - const factory = new GMS1ProjectFactory(this.folder.join("my-project-empty.xml")); - const proj: IGMProject = await factory.load(); + const loader = new GMS1ProjectLoader(); + const proj: IGMProject = await loader.load(this.folder.join("my-project-empty.xml")); const ls = this._ls(proj as GMProject); Expect(ls).toEqual([ @@ -86,8 +86,8 @@ export class GMS1ProjectFactoryFixture { @AsyncTest("should thrown on invalid XML project") public async load_invalid() { - const factory = new GMS1ProjectFactory(this.folder.join("my-project-invalid.xml")); - return factory.load().then(() => { + const loader = new GMS1ProjectLoader(); + return loader.load(this.folder.join("my-project-invalid.xml")).then(() => { throw new Error("load() did not throw on invalid xml"); }).catch((e) => { Expect(e).toBeDefined(); diff --git a/tests/gm_project/gms2/GMS2ProjectFactory.spec.ts b/tests/gm_project/gms2/GMS2ProjectLoader.spec.ts similarity index 82% rename from tests/gm_project/gms2/GMS2ProjectFactory.spec.ts rename to tests/gm_project/gms2/GMS2ProjectLoader.spec.ts index bee80dc..2bf5ab3 100644 --- a/tests/gm_project/gms2/GMS2ProjectFactory.spec.ts +++ b/tests/gm_project/gms2/GMS2ProjectLoader.spec.ts @@ -7,10 +7,10 @@ import { } from "alsatian"; import { TempDir } from "../../_testing_helpers/TempDir.help"; -import GMS2ProjectFactory from "../../../src/gm_project/gms2/GMS2ProjectFactory"; import IGMFolder from "../../../src/gm_project/interfaces/IGMFolder"; import IGMProject from "../../../src/gm_project/interfaces/IGMProject"; +import GMS2ProjectLoader from "../../../src/gm_project/gms2/GMS2ProjectLoader"; import projectBad from "./__mock__/ProjectBad.mock"; import projectEmpty from "./__mock__/ProjectEmpty.mock"; import projectFolderFolderRoom from "./__mock__/ProjectFolderFolderRoom.mock"; @@ -19,8 +19,8 @@ import projectSingleFolder from "./__mock__/ProjectSingleFolder.mock"; /* tslint:disable:max-classes-per-file completed-docs */ -@TestFixture("GMScript") -export class GMS1ProjectFactoryFixture { +@TestFixture("GMS2ProjectLoader") +export class GMS2ProjectLoaderFixture { public projectFolderScript: TempDir; @@ -71,8 +71,8 @@ export class GMS1ProjectFactoryFixture { @AsyncTest("should load an empty project") public async load_empty() { - const factory = new GMS2ProjectFactory(this.projectEmpty.join("project.json")); - const proj: IGMProject = await factory.load(); + const factory = new GMS2ProjectLoader(); + const proj: IGMProject = await factory.load(this.projectEmpty.join("project.json")); Expect(Array.from(proj.children).length).toBe(0); Expect(proj.path).toBe(this.projectEmpty.dir); @@ -81,8 +81,8 @@ export class GMS1ProjectFactoryFixture { @AsyncTest("should load a single folder project") public async load_singleFolder() { - const factory = new GMS2ProjectFactory(this.projectSingleFolder.join("project.json")); - const proj: IGMProject = await factory.load(); + const factory = new GMS2ProjectLoader(); + const proj: IGMProject = await factory.load(this.projectSingleFolder.join("project.json")); const arr = Array.from(proj.children); Expect(arr.length).toBe(1); @@ -91,8 +91,8 @@ export class GMS1ProjectFactoryFixture { @AsyncTest("should load a folder/script project") public async load_folderScript() { - const factory = new GMS2ProjectFactory(this.projectFolderScript.join("project.json")); - const proj: IGMProject = await factory.load(); + const factory = new GMS2ProjectLoader(); + const proj: IGMProject = await factory.load(this.projectFolderScript.join("project.json")); const arr = Array.from(proj.children); Expect(arr.length).toBe(1); @@ -105,8 +105,8 @@ export class GMS1ProjectFactoryFixture { @AsyncTest("should load a folder/folder/room project") public async load_folder_folder_script() { - const factory = new GMS2ProjectFactory(this.projectFolderFolderScript.join("project.json")); - const proj: IGMProject = await factory.load(); + const factory = new GMS2ProjectLoader(); + const proj: IGMProject = await factory.load(this.projectFolderFolderScript.join("project.json")); const arr = Array.from(proj.children); Expect(arr.length).toBe(1); @@ -124,8 +124,8 @@ export class GMS1ProjectFactoryFixture { @AsyncTest("should load a project with bad keys references") public async load_bad() { - const factory = new GMS2ProjectFactory(this.projectBad.join("project.json")); - const proj: IGMProject = await factory.load(); + const factory = new GMS2ProjectLoader(); + const proj: IGMProject = await factory.load(this.projectBad.join("project.json")); const arr = Array.from(proj.children); Expect(arr.length).toBe(1); diff --git a/tests/gm_project/gms2/GMS2Script.spec.ts b/tests/gm_project/gms2/GMS2Script.spec.ts index 90d049e..3f1da7a 100644 --- a/tests/gm_project/gms2/GMS2Script.spec.ts +++ b/tests/gm_project/gms2/GMS2Script.spec.ts @@ -1,6 +1,5 @@ import { Expect, - Setup, Test, TestFixture, } from "alsatian"; @@ -12,17 +11,11 @@ import GMS2Script from "../../../src/gm_project/gms2/GMS2Script"; @TestFixture("GMS2Script") export class GMS2ScriptFixture { - public script: GMS2Script; - - @Setup - public setup() { - this.script = new GMS2Script("my-name"); - } - @Test("should load a script and get the script content and name through an iterator") public subScripts() { - this.script.loadFromString("my-gml-content"); - const arr = Array.from(this.script.subScripts()); + const script = new GMS2Script("my-name", false); + script.loadFromString("my-gml-content"); + const arr = Array.from(script.subScripts()); Expect(arr.length).toBe(1); Expect(arr[0][0]).toBe("my-name"); Expect(arr[0][1]).toBe("my-gml-content"); @@ -30,8 +23,9 @@ export class GMS2ScriptFixture { @Test("should convert triple slash comments into JSdoc comments") public subScripts_triple_slash() { - this.script.loadFromString("/// Hi"); - const arr = Array.from(this.script.subScripts()); + const script = new GMS2Script("my-name", false); + script.loadFromString("/// Hi"); + const arr = Array.from(script.subScripts()); Expect(arr.length).toBe(1); Expect(arr[0][0]).toBe("my-name"); Expect(arr[0][1]).toBe("/**\n * Hi\n */\n"); @@ -39,8 +33,9 @@ export class GMS2ScriptFixture { @Test("should convert triple slash comments into JSdoc comments and strip initial spaces") public subScripts_triple_slash_spaces() { - this.script.loadFromString("/// Hi"); - const arr = Array.from(this.script.subScripts()); + const script = new GMS2Script("my-name", false); + script.loadFromString("/// Hi"); + const arr = Array.from(script.subScripts()); Expect(arr.length).toBe(1); Expect(arr[0][0]).toBe("my-name"); Expect(arr[0][1]).toBe("/**\n * Hi\n */\n"); @@ -48,8 +43,9 @@ export class GMS2ScriptFixture { @Test("should NOT convert triple slash comments into JSdoc comments") public subScripts_not_four_slash() { - this.script.loadFromString("//// Hi"); - const arr = Array.from(this.script.subScripts()); + const script = new GMS2Script("my-name", false); + script.loadFromString("//// Hi"); + const arr = Array.from(script.subScripts()); Expect(arr.length).toBe(1); Expect(arr[0][0]).toBe("my-name"); Expect(arr[0][1]).toBe("//// Hi"); @@ -57,20 +53,21 @@ export class GMS2ScriptFixture { @Test("should get the filepath of the gml file of a non-compatibility script") public filepath_NonCompatibility() { - this.script.isCompatibility = false; - Expect(this.script.filepath).toBe("scripts/my-name/my-name.gml"); + const script = new GMS2Script("my-name", false); + Expect(script.filepath).toBe("scripts/my-name/my-name.gml"); } @Test("should get the filepath of the gml file of a compatibility script") public filepath_Compatibility() { - this.script.isCompatibility = true; - Expect(this.script.filepath).toBe("scripts/@my-name/my-name.gml"); + const script = new GMS2Script("my-name", true); + Expect(script.filepath).toBe("scripts/@my-name/my-name.gml"); } @Test("should throw when calling subScripts() without calling loadFromString") public subScripts_Throw() { + const script = new GMS2Script("my-name", false); Expect(() => { - this.script.subScripts().next(); + script.subScripts().next(); }).toThrow(); }