-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
130 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
require: [ | ||
'ts-node/register', | ||
], | ||
extension: ['ts'], | ||
timeout: 10000, | ||
checkLeaks: true, | ||
recursive: true, | ||
diff: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/// <reference types="node" /> | ||
|
||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
import * as assert from 'assert'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import * as ts from 'typescript'; | ||
|
||
import { PropertiesMinifier } from '../src/properties-minifier'; | ||
|
||
interface TestCase { | ||
name: string; | ||
inputFileName: string; | ||
outputFileContent: string; | ||
} | ||
|
||
const testCasesDir = path.resolve(__dirname, 'test-cases'); | ||
|
||
function isDirectory(filePath: string): boolean { | ||
return fs.lstatSync(path.resolve(testCasesDir, filePath)).isDirectory(); | ||
} | ||
|
||
function prepareString(str: string): string { | ||
return str.trim().replace(/\r\n/g, '\n'); | ||
} | ||
|
||
function getTestCases(): TestCase[] { | ||
if (!fs.existsSync(testCasesDir) || !fs.lstatSync(testCasesDir).isDirectory()) { | ||
throw new Error(`${testCasesDir} folder does not exist`); | ||
} | ||
|
||
return fs.readdirSync(testCasesDir) | ||
.filter((filePath: string) => { | ||
return isDirectory(filePath) && path.basename(filePath) !== 'node_modules'; | ||
}) | ||
.map((directoryName: string) => { | ||
const testCaseDir = path.resolve(testCasesDir, directoryName); | ||
const outputFileName = path.resolve(testCaseDir, 'output.js'); | ||
const inputFileName = path.relative(process.cwd(), path.resolve(testCaseDir, 'input.ts')); | ||
|
||
assert(fs.existsSync(inputFileName), `Input file doesn't exist for ${directoryName}`); | ||
assert(fs.existsSync(outputFileName), `Output file doesn't exist for ${directoryName}`); | ||
|
||
const result: TestCase = { | ||
name: directoryName, | ||
inputFileName, | ||
outputFileContent: prepareString(fs.readFileSync(outputFileName, 'utf-8')), | ||
}; | ||
|
||
return result; | ||
}); | ||
} | ||
|
||
const formatDiagnosticsHost: ts.FormatDiagnosticsHost = { | ||
getCanonicalFileName: (fileName: string) => ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(), | ||
getCurrentDirectory: ts.sys.getCurrentDirectory, | ||
getNewLine: () => ts.sys.newLine, | ||
}; | ||
|
||
function checkProgramDiagnosticsErrors(program: ts.Program): void { | ||
checkDiagnosticsErrors(ts.getPreEmitDiagnostics(program)); | ||
checkDiagnosticsErrors(program.getDeclarationDiagnostics()); | ||
} | ||
|
||
function checkDiagnosticsErrors(diagnostics: ReadonlyArray<ts.Diagnostic>): void { | ||
assert.strictEqual(diagnostics.length, 0, ts.formatDiagnostics(diagnostics, formatDiagnosticsHost).trim()); | ||
} | ||
|
||
describe('Functional tests', () => { | ||
for (const testCase of getTestCases()) { | ||
const minifier = new PropertiesMinifier(); | ||
|
||
it(testCase.name, () => { | ||
const program = ts.createProgram({ | ||
rootNames: [testCase.inputFileName], | ||
options: {}, | ||
}); | ||
|
||
checkProgramDiagnosticsErrors(program); | ||
|
||
let output: string | undefined; | ||
|
||
program.emit( | ||
undefined, | ||
(fileName: string, data: string) => { | ||
output = data; | ||
}, | ||
undefined, | ||
false, | ||
{ | ||
before: [ | ||
(context: ts.TransformationContext) => (file: ts.SourceFile) => minifier.visitSourceFile(file, program, context), | ||
], | ||
} | ||
); | ||
|
||
assert.strictEqual(output, testCase.outputFileContent, 'Output should be the same as expected'); | ||
}); | ||
} | ||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"extends": "./tsconfig.base.json", | ||
"include": [ | ||
"src/**/*.ts" | ||
"src/**/*.ts", | ||
"tests/functional-test-cases.ts" | ||
] | ||
} |