2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-11 17:23:13 -04:00
|
|
|
// Run this manually with:
|
|
|
|
//
|
|
|
|
// ./node_modules/.bin/ts-node --project tools/ts_library_builder/tsconfig.json tools/ts_library_builder/test.ts
|
|
|
|
|
2018-10-15 12:47:48 -04:00
|
|
|
import { Project, ts } from "ts-simple-ast";
|
2019-01-03 14:55:22 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEqual,
|
2019-01-31 18:11:37 -05:00
|
|
|
runTests,
|
2019-01-03 14:55:22 -05:00
|
|
|
test
|
|
|
|
} from "../../js/deps/https/deno.land/x/std/testing/mod";
|
2018-10-22 01:08:14 -04:00
|
|
|
import { flatten, mergeGlobal } from "./build_library";
|
2018-10-11 17:23:13 -04:00
|
|
|
import { loadDtsFiles } from "./ast_util";
|
|
|
|
|
2018-10-15 12:47:48 -04:00
|
|
|
const { ModuleKind, ModuleResolutionKind, ScriptTarget } = ts;
|
|
|
|
|
2018-10-11 17:23:13 -04:00
|
|
|
/** setups and returns the fixtures for testing */
|
|
|
|
function setupFixtures() {
|
|
|
|
const basePath = process.cwd();
|
|
|
|
const buildPath = `${basePath}/tools/ts_library_builder/testdata`;
|
|
|
|
const outputFile = `${buildPath}/lib.output.d.ts`;
|
|
|
|
const inputProject = new Project({
|
|
|
|
compilerOptions: {
|
|
|
|
baseUrl: basePath,
|
|
|
|
declaration: true,
|
|
|
|
emitDeclarationOnly: true,
|
|
|
|
module: ModuleKind.AMD,
|
|
|
|
moduleResolution: ModuleResolutionKind.NodeJs,
|
|
|
|
strict: true,
|
|
|
|
stripInternal: true,
|
|
|
|
target: ScriptTarget.ESNext
|
|
|
|
}
|
|
|
|
});
|
|
|
|
inputProject.addExistingSourceFiles([
|
|
|
|
`${buildPath}/globals.ts`,
|
|
|
|
`${buildPath}/api.ts`
|
|
|
|
]);
|
|
|
|
const declarationProject = new Project({
|
|
|
|
compilerOptions: {},
|
|
|
|
useVirtualFileSystem: true
|
|
|
|
});
|
|
|
|
loadDtsFiles(declarationProject);
|
|
|
|
for (const { filePath, text } of inputProject.emitToMemory().getFiles()) {
|
|
|
|
declarationProject.createSourceFile(filePath, text);
|
|
|
|
}
|
|
|
|
const outputProject = new Project({
|
|
|
|
compilerOptions: {},
|
|
|
|
useVirtualFileSystem: true
|
|
|
|
});
|
|
|
|
loadDtsFiles(outputProject);
|
|
|
|
const outputSourceFile = outputProject.createSourceFile(outputFile);
|
|
|
|
const debug = true;
|
|
|
|
|
|
|
|
return {
|
|
|
|
basePath,
|
|
|
|
buildPath,
|
|
|
|
inputProject,
|
|
|
|
outputFile,
|
|
|
|
declarationProject,
|
|
|
|
outputProject,
|
|
|
|
outputSourceFile,
|
|
|
|
debug
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test(function buildLibraryFlatten() {
|
|
|
|
const {
|
|
|
|
basePath,
|
|
|
|
buildPath,
|
|
|
|
debug,
|
|
|
|
declarationProject,
|
|
|
|
outputSourceFile: targetSourceFile
|
|
|
|
} = setupFixtures();
|
|
|
|
|
|
|
|
flatten({
|
|
|
|
basePath,
|
|
|
|
customSources: {},
|
|
|
|
debug,
|
|
|
|
declarationProject,
|
|
|
|
filePath: `${buildPath}/api.d.ts`,
|
|
|
|
namespaceName: `"api"`,
|
|
|
|
targetSourceFile
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(targetSourceFile.getNamespace(`"api"`) != null);
|
|
|
|
assertEqual(targetSourceFile.getNamespaces().length, 1);
|
|
|
|
const namespaceApi = targetSourceFile.getNamespaceOrThrow(`"api"`);
|
|
|
|
const functions = namespaceApi.getFunctions();
|
|
|
|
assertEqual(functions[0].getName(), "foo");
|
|
|
|
assertEqual(
|
|
|
|
functions[0]
|
|
|
|
.getJsDocs()
|
|
|
|
.map(jsdoc => jsdoc.getInnerText())
|
|
|
|
.join("\n"),
|
|
|
|
"jsdoc for foo"
|
|
|
|
);
|
|
|
|
assertEqual(functions[1].getName(), "bar");
|
|
|
|
assertEqual(
|
|
|
|
functions[1]
|
|
|
|
.getJsDocs()
|
|
|
|
.map(jsdoc => jsdoc.getInnerText())
|
|
|
|
.join("\n"),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
assertEqual(functions.length, 2);
|
|
|
|
const classes = namespaceApi.getClasses();
|
|
|
|
assertEqual(classes[0].getName(), "Foo");
|
|
|
|
assertEqual(classes.length, 1);
|
|
|
|
const variableDeclarations = namespaceApi.getVariableDeclarations();
|
|
|
|
assertEqual(variableDeclarations[0].getName(), "arr");
|
|
|
|
assertEqual(variableDeclarations.length, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function buildLibraryMerge() {
|
|
|
|
const {
|
|
|
|
basePath,
|
|
|
|
buildPath,
|
|
|
|
declarationProject,
|
|
|
|
debug,
|
|
|
|
inputProject,
|
|
|
|
outputSourceFile: targetSourceFile
|
|
|
|
} = setupFixtures();
|
|
|
|
|
2018-10-22 01:08:14 -04:00
|
|
|
mergeGlobal({
|
2018-10-11 17:23:13 -04:00
|
|
|
basePath,
|
|
|
|
declarationProject,
|
|
|
|
debug,
|
|
|
|
globalVarName: "foobarbaz",
|
|
|
|
filePath: `${buildPath}/globals.ts`,
|
|
|
|
inputProject,
|
|
|
|
interfaceName: "FooBar",
|
|
|
|
targetSourceFile
|
|
|
|
});
|
|
|
|
|
2018-10-22 01:08:14 -04:00
|
|
|
assert(targetSourceFile.getNamespace("moduleC") != null);
|
2018-11-03 19:45:06 -04:00
|
|
|
assert(targetSourceFile.getNamespace("moduleD") != null);
|
|
|
|
assert(targetSourceFile.getNamespace("moduleE") != null);
|
|
|
|
assert(targetSourceFile.getNamespace("moduleF") != null);
|
|
|
|
assertEqual(targetSourceFile.getNamespaces().length, 4);
|
2018-10-22 01:08:14 -04:00
|
|
|
assert(targetSourceFile.getInterface("FooBar") != null);
|
|
|
|
assertEqual(targetSourceFile.getInterfaces().length, 1);
|
|
|
|
const variableDeclarations = targetSourceFile.getVariableDeclarations();
|
|
|
|
assertEqual(variableDeclarations[0].getType().getText(), `FooBar`);
|
2019-01-16 17:57:15 -05:00
|
|
|
assertEqual(variableDeclarations[1].getType().getText(), `FooBar`);
|
|
|
|
assertEqual(variableDeclarations[2].getType().getText(), `moduleC.Bar`);
|
2018-10-11 17:23:13 -04:00
|
|
|
assertEqual(
|
2019-01-16 17:57:15 -05:00
|
|
|
variableDeclarations[3].getType().getText(),
|
2018-10-22 01:08:14 -04:00
|
|
|
`typeof moduleC.qat`
|
2018-10-11 17:23:13 -04:00
|
|
|
);
|
2018-11-03 19:45:06 -04:00
|
|
|
assertEqual(
|
2019-01-16 17:57:15 -05:00
|
|
|
variableDeclarations[4].getType().getText(),
|
2018-11-03 19:45:06 -04:00
|
|
|
`typeof moduleE.process`
|
|
|
|
);
|
|
|
|
assertEqual(
|
2019-01-16 17:57:15 -05:00
|
|
|
variableDeclarations[5].getType().getText(),
|
2018-11-03 19:45:06 -04:00
|
|
|
`typeof moduleD.reprocess`
|
|
|
|
);
|
2018-11-08 19:09:18 -05:00
|
|
|
assertEqual(
|
2019-01-16 17:57:15 -05:00
|
|
|
variableDeclarations[6].getType().getText(),
|
2018-11-08 19:09:18 -05:00
|
|
|
`typeof moduleC.Bar`
|
|
|
|
);
|
2019-01-16 17:57:15 -05:00
|
|
|
assertEqual(variableDeclarations.length, 7);
|
2018-11-08 19:09:18 -05:00
|
|
|
const typeAliases = targetSourceFile.getTypeAliases();
|
|
|
|
assertEqual(typeAliases[0].getName(), "Bar");
|
|
|
|
assertEqual(typeAliases[0].getType().getText(), "moduleC.Bar");
|
|
|
|
assertEqual(typeAliases.length, 1);
|
2018-10-11 17:23:13 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// TODO author unit tests for `ast_util.ts`
|
2019-01-31 18:11:37 -05:00
|
|
|
|
|
|
|
runTests();
|