Skip to content

Commit

Permalink
テストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaru committed Feb 2, 2013
1 parent 518511b commit 04eb345
Show file tree
Hide file tree
Showing 32 changed files with 481 additions and 90 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
test/tmp
/.idea
109 changes: 76 additions & 33 deletions grunt.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,80 @@
module.exports = function(grunt) {

grunt.initConfig({
test: {
files: ['test/**/*.js']
},
lint: {
files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
},
watch: {
files: '<config:lint.files>',
tasks: 'default'
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
es5: true
},
globals: {}
}
});
module.exports = function (grunt) {
"use strict";

grunt.loadTasks('tasks');
grunt.initConfig({
clean:{
test:[
"test/fixtures/*.js",
"test/fixtures/*.js.map",
"test/fixtures/*.d.ts",
"test/temp/**/*.*",
"test/temp"
]
},
typescript:{
simple:{
src:"test/fixtures/simple.ts"
},
declaration:{
src:"test/fixtures/declaration.ts",
options:{
declaration:true
}
},
sourcemap:{
src:"test/fixtures/sourcemap.ts",
options:{
sourcemap:true
}
},
es5:{
src:"test/fixtures/es5.ts",
options:{
target:"ES5"
}
},
"no-module":{
src:"test/fixtures/no-module.ts"
},
amd:{
src:"test/fixtures/amd.ts",
options:{
module:"amd"
}
},
commonjs:{
src:"test/fixtures/commonjs.ts",
options:{
module:"commonjs"
}
},
single:{
src:"test/fixtures/single/**/*.ts",
dest: "test/temp/single.js"
},
multi:{
src:"test/fixtures/multi/**/*.ts",
dest:"test/temp/multi"
},
basePath:{
src:"test/fixtures/multi/**/*.ts",
dest:"test/temp/basePath",
options: {
base_path: "test/fixtures/multi"
}
}

grunt.registerTask('default', 'lint test');
},
nodeunit:{
tasks:["test/test.js"]
}
});

grunt.loadTasks("tasks");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.renameTask("test", "nodeunit");
grunt.registerTask("test", "clean typescript nodeunit");

grunt.registerTask("default", "test");

};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
},
"dependencies": {
"grunt": "~0.3.9",
"typescript": "0.8.1",
"typescript": "0.8.2",
"grunt-lib-contrib": "~0.3.1"
},
"devDependencies": {
"grunt": "~0.3.9",
"typescript": "0.8.1"
"typescript": "0.8.2",
"grunt-contrib-clean": "~0.3.0"
},
"optionalDependencies": {},
"keywords": [
"gruntplugin"
]
}
}
110 changes: 56 additions & 54 deletions tasks/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function (grunt) {
var path = require('path'),
fs = require('fs'),
vm = require('vm'),
gruntIO = function (currentPath, destPath, basePath, compSetting) {
gruntIO = function (currentPath, destPath, basePath, compSetting, outputOne) {
var createdFiles = [];
basePath = basePath || ".";

Expand All @@ -26,7 +26,6 @@ module.exports = function (grunt) {

createFile:function (writeFile, useUTF8) {
var source = "";

return {
Write:function (str) {
source += str;
Expand All @@ -35,11 +34,11 @@ module.exports = function (grunt) {
source += str + grunt.util.linefeed;
},
Close:function () {

if (source.trim().length < 1) {
return;
}
if(compSetting.outputMany){

if(!outputOne){
var g = path.join(currentPath, basePath);
writeFile = writeFile.substr(g.length);
writeFile = path.join(currentPath, destPath, writeFile);
Expand All @@ -50,36 +49,43 @@ module.exports = function (grunt) {
}
}
},
findFile: function (rootPath, partialFilePath) {

var file = path.join(rootPath, partialFilePath);
while(true) {
if(fs.existsSync(file)) {
try {
var content = grunt.file.read(file);
return {
content: content,
path: file
};
} catch (err) {
}
} else {
var parentPath = path.resolve(rootPath, "..");
if(rootPath === parentPath) {
return null;
} else {
rootPath = parentPath;
file = path.resolve(rootPath, partialFilePath);
}
}
}
// findFile: function (rootPath, partialFilePath) {
// var file = path.join(rootPath, partialFilePath);
// while(true) {
// if(fs.existsSync(file)) {
// try {
// var content = grunt.file.read(file);
// return {
// content: content,
// path: file
// };
// } catch (err) {
// }
// } else {
// var parentPath = path.resolve(rootPath, "..");
// if(rootPath === parentPath) {
// return null;
// } else {
// rootPath = parentPath;
// file = path.resolve(rootPath, partialFilePath);
// }
// }
// }
// },
directoryExists: function (path) {
return fs.existsSync(path) && fs.lstatSync(path).isDirectory();
},
fileExists: function (path) {
return fs.existsSync(path);
},
stderr: {
Write: function (str) {
grunt.fail.warn(str);
console.log(str);
//grunt.fail.warn(str);
},
WriteLine: function (str) {
grunt.fail.warn(str + '\n');
console.log(str);
//grunt.fail.warn(str + '\n');
},
Close: function () {
}
Expand Down Expand Up @@ -115,29 +121,27 @@ module.exports = function (grunt) {
});

compile(files, dest, grunt.util._.clone(options), extension);
if (grunt.task.current.errorCount) {
return false;
} else {
return true;
}

return !grunt.task.current.errorCount;
});

var compile = function (srces, destPath, options, extension) {
var currentPath = path.resolve("."),
var currentPath = path.resolve("."),
basePath = options.base_path,
typeScriptBinPath = resolveTypeScriptBinPath(currentPath, 0),
typeScriptPath = path.resolve(typeScriptBinPath, "typescript.js"),
libDPath = path.resolve(typeScriptBinPath, "lib.d.ts");
libDPath = path.resolve(typeScriptBinPath, "lib.d.ts"),
outputOne = !!destPath && path.extname(destPath) === ".js";

if(!typeScriptBinPath){
grunt.fail.warn("typescript.js not found. please 'npm install typescript'.");
return false;
}

var code = grunt.file.read(typeScriptPath);
vm.runInThisContext(code, typeScriptPath);

var setting = new TypeScript.CompilationSettings();
var io = gruntIO(currentPath, destPath, basePath, setting);
var io = gruntIO(currentPath, destPath, basePath, setting, outputOne);
var env = new TypeScript.CompilationEnvironment(setting, io);
var resolver = new TypeScript.CodeResolver(env);

Expand Down Expand Up @@ -170,19 +174,22 @@ module.exports = function (grunt) {
//grunt.log.writeln("'declaration_file' option now obsolate. use 'declaration' option");
}
}
if(options.comment){
//setting.emitComments = true;
}
}

if(path.extname(destPath) === ".js"){
var originalDestPath = destPath;
if(outputOne){
destPath = path.resolve(currentPath, destPath);
setting.outputOne(destPath);
setting.outputOption = destPath;
}

// if(destPath){
// destPath = path.resolve(currentPath, destPath);
// setting.outputOption = destPath;
// }
var units = [{
fileName: libDPath,
code: grunt.file.read(libDPath)
}];

var resolutionDispatcher = {
postResolutionError : function (errorFile, errorMessage) {
grunt.fail.warn(errorFile + " : " + errorMessage);
Expand All @@ -193,11 +200,9 @@ module.exports = function (grunt) {
}
}
};

srces.forEach(function(src){
resolver.resolveCode(path.resolve(currentPath, src), "", false, resolutionDispatcher);
});

var compiler = new TypeScript.TypeScriptCompiler(io.stderr, new TypeScript.NullLogger(), setting);
compiler.setErrorOutput(io.stderr);

Expand All @@ -207,10 +212,9 @@ module.exports = function (grunt) {
}
compiler.addUnit(unit.code, unit.fileName, false);
});

compiler.typeCheck();
compiler.emit(io.createFile);
compiler.emitDeclarationFile(io.createFile);
compiler.emit(io);
compiler.emitDeclarations();

var result = {js: [], m: [], d: [], other: []};
io.getCreatedFiles().forEach(function(item){
Expand All @@ -220,17 +224,15 @@ module.exports = function (grunt) {
else if(/\.d\.ts$/.test(file)) result.d.push(file);
else result.other.push(file);
});
var resultMessage = "js:" + result.js.length + " files, map:" +
var resultMessage = "js:" + result.js.length + " files, map:" +
result.m.length + " files, declaration:" +
result.d.length + " files";

if(!setting.outputMany){
if(outputOne){
grunt.log.writeln("File " + (result.js[0]).cyan + " created.");
grunt.log.writeln(resultMessage);
}else{
grunt.log.writeln(io.getCreatedFiles().length + " Files created. " + resultMessage);
grunt.log.writeln((io.getCreatedFiles().length + " Files").cyan + " created. " + resultMessage);
}

return true;
};
};
9 changes: 9 additions & 0 deletions test/expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(["require", "exports"], function(require, exports) {
(function (Foo) {
function bar() {
return "foobar!";
}
Foo.bar = bar;
})(exports.Foo || (exports.Foo = {}));
var Foo = exports.Foo;
})
7 changes: 7 additions & 0 deletions test/expected/commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(function (Foo) {
function bar() {
return "foobar!";
}
Foo.bar = bar;
})(exports.Foo || (exports.Foo = {}));
var Foo = exports.Foo;
3 changes: 3 additions & 0 deletions test/expected/declaration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Simple2 {
function main(): string;
}
8 changes: 8 additions & 0 deletions test/expected/declaration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var Simple2;
(function (Simple2) {
function main() {
return "hello simple2";
}
Simple2.main = main;
})(Simple2 || (Simple2 = {}));
Simple2.main();

0 comments on commit 04eb345

Please sign in to comment.