0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-31 09:14:20 -04:00
denoland-deno/tools/ts_library_builder/test.ts
Kitson Kelly a21a5ad2fa Add Deno global namespace (#1748)
Resolves #1705

This PR adds the Deno APIs as a global namespace named `Deno`. For backwards
compatibility, the ability to `import * from "deno"` is preserved. I have tried
to convert every test and internal code the references the module to use the
namespace instead, but because I didn't break compatibility I am not sure.

On the REPL, `deno` no longer exists, replaced only with `Deno` to align with
the regular runtime.

The runtime type library includes both the namespace and module. This means it
duplicates the whole type information. When we remove the functionality from the
runtime, it will be a one line change to the library generator to remove the
module definition from the type library.

I marked a `TODO` in a couple places where to remove the `"deno"` module, but
there are additional places I know I didn't mark.
2019-02-12 10:08:56 -05:00

220 lines
6.2 KiB
TypeScript

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// Run this manually with:
//
// ./node_modules/.bin/ts-node --project tools/ts_library_builder/tsconfig.json tools/ts_library_builder/test.ts
import { Project, ts } from "ts-simple-ast";
import {
assert,
assertEqual,
runTests,
test
} from "../../js/deps/https/deno.land/x/std/testing/mod";
import { flatten, mergeGlobal } from "./build_library";
import { inlineFiles, loadDtsFiles } from "./ast_util";
const { ModuleKind, ModuleResolutionKind, ScriptTarget } = ts;
/** 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`,
moduleName: `"api"`,
namespaceName: "Api",
targetSourceFile
});
assert(targetSourceFile.getNamespace(`"api"`) != null);
assert(targetSourceFile.getNamespace("Api") != null);
assertEqual(targetSourceFile.getNamespaces().length, 2);
const moduleApi = targetSourceFile.getNamespaceOrThrow(`"api"`);
const functions = moduleApi.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 = moduleApi.getClasses();
assertEqual(classes[0].getName(), "Foo");
assertEqual(classes.length, 1);
const variableDeclarations = moduleApi.getVariableDeclarations();
assertEqual(variableDeclarations[0].getName(), "arr");
assertEqual(variableDeclarations.length, 1);
const namespaceApi = targetSourceFile.getNamespaceOrThrow(`"api"`);
const functionsNs = namespaceApi.getFunctions();
assertEqual(functionsNs[0].getName(), "foo");
assertEqual(
functionsNs[0]
.getJsDocs()
.map(jsdoc => jsdoc.getInnerText())
.join("\n"),
"jsdoc for foo"
);
assertEqual(functionsNs[1].getName(), "bar");
assertEqual(
functionsNs[1]
.getJsDocs()
.map(jsdoc => jsdoc.getInnerText())
.join("\n"),
""
);
assertEqual(functionsNs.length, 2);
const classesNs = namespaceApi.getClasses();
assertEqual(classesNs[0].getName(), "Foo");
assertEqual(classesNs.length, 1);
const variableDeclarationsNs = namespaceApi.getVariableDeclarations();
assertEqual(variableDeclarationsNs[0].getName(), "arr");
assertEqual(variableDeclarationsNs.length, 1);
});
test(function buildLibraryMerge() {
const {
basePath,
buildPath,
declarationProject,
debug,
inputProject,
outputSourceFile: targetSourceFile
} = setupFixtures();
mergeGlobal({
basePath,
declarationProject,
debug,
globalVarName: "foobarbaz",
filePath: `${buildPath}/globals.ts`,
inputProject,
interfaceName: "FooBar",
targetSourceFile
});
assert(targetSourceFile.getNamespace("moduleC") != null);
assert(targetSourceFile.getNamespace("moduleD") != null);
assert(targetSourceFile.getNamespace("moduleE") != null);
assert(targetSourceFile.getNamespace("moduleF") != null);
assertEqual(targetSourceFile.getNamespaces().length, 4);
assert(targetSourceFile.getInterface("FooBar") != null);
assertEqual(targetSourceFile.getInterfaces().length, 1);
const variableDeclarations = targetSourceFile.getVariableDeclarations();
assertEqual(variableDeclarations[0].getType().getText(), `FooBar`);
assertEqual(variableDeclarations[1].getType().getText(), `FooBar`);
assertEqual(variableDeclarations[2].getType().getText(), `moduleC.Bar`);
assertEqual(
variableDeclarations[3].getType().getText(),
`typeof moduleC.qat`
);
assertEqual(
variableDeclarations[4].getType().getText(),
`typeof moduleE.process`
);
assertEqual(
variableDeclarations[5].getType().getText(),
`typeof moduleD.reprocess`
);
assertEqual(
variableDeclarations[6].getType().getText(),
`typeof moduleC.Bar`
);
assertEqual(variableDeclarations.length, 7);
const typeAliases = targetSourceFile.getTypeAliases();
assertEqual(typeAliases[0].getName(), "Bar");
assertEqual(typeAliases[0].getType().getText(), "moduleC.Bar");
assertEqual(typeAliases.length, 1);
});
test(function testInlineFiles() {
const {
basePath,
buildPath,
debug,
outputSourceFile: targetSourceFile
} = setupFixtures();
inlineFiles({
basePath,
debug,
inline: [`${buildPath}/lib.extra.d.ts`],
targetSourceFile
});
assert(targetSourceFile.getNamespace("Qat") != null);
const qatNamespace = targetSourceFile.getNamespaceOrThrow("Qat");
assert(qatNamespace.getClass("Foo") != null);
});
// TODO author unit tests for `ast_util.ts`
runTests();