var assert = require('power-assert');
var fs = require('fs');
var lib = require('../src/lib');
function collectFileName(dirName) {
    var fileName = [];
    fs.readdirSync(dirName).forEach(function (name) {
        var newName = dirName + '/' + name;
        var stats = fs.statSync(newName);
        if (stats.isDirectory()) {
            fileName = fileName.concat(collectFileName(newName));
        } else if (stats.isFile()) {
            fileName.push(newName);
        }
    });
    return fileName;
}
describe('tsfmt test', function () {
    var fixtureDir = './tests/fixture';
    var expectedDir = './tests/expected';
    describe('processFiles function', function () {
        var fileNames = collectFileName(fixtureDir);
        fileNames.filter(function (fileName) {
            return /\.ts$/.test(fileName);
        }).forEach(function (fileName) {
            it(fileName, function () {
                var resultMap = lib.processFiles({ dryRun: true }, [fileName]);
                var result = resultMap[fileName];
                assert(assert._expr(assert._capt(assert._capt(result, 'ident', {
                    start: {
                        line: 33,
                        column: 23
                    }
                }) !== null, 'binary', {
                    start: {
                        line: 33,
                        column: 30
                    }
                }), {
                    start: {
                        line: 33,
                        column: 23
                    },
                    path: '/Users/vvakame/Dropbox/work/typescript-formatter/tests/mainTest.js'
                }, '                assert(result !== null);'));
                var expectedTsFileName = fileName.replace(fixtureDir, expectedDir);
                if (!fs.existsSync(expectedTsFileName)) {
                    fs.writeFileSync(expectedTsFileName, result.dest);
                }
                var expected = fs.readFileSync(expectedTsFileName, 'utf-8');
                assert(assert._expr(assert._capt(assert._capt(expected, 'ident', {
                    start: {
                        line: 42,
                        column: 23
                    }
                }) === assert._capt(assert._capt(result, 'ident', {
                    start: {
                        line: 42,
                        column: 36
                    }
                }).dest, 'ident', {
                    start: {
                        line: 42,
                        column: 43
                    }
                }), 'binary', {
                    start: {
                        line: 42,
                        column: 32
                    }
                }), {
                    start: {
                        line: 42,
                        column: 23
                    },
                    path: '/Users/vvakame/Dropbox/work/typescript-formatter/tests/mainTest.js'
                }, '                assert(expected === result.dest);'));
                var expectedOptionsFileName = expectedTsFileName.replace(/\.ts$/, '.json');
                if (!fs.existsSync(expectedOptionsFileName)) {
                    fs.writeFileSync(expectedOptionsFileName, JSON.stringify(result.options, null, 2));
                }
                var expectedOptions = JSON.parse(fs.readFileSync(expectedOptionsFileName, 'utf-8'));
                assert.deepEqual(assert._expr(assert._capt(expectedOptions, 'ident', {
                    start: {
                        line: 51,
                        column: 33
                    }
                }), {
                    start: {
                        line: 51,
                        column: 33
                    },
                    path: '/Users/vvakame/Dropbox/work/typescript-formatter/tests/mainTest.js'
                }, '                assert.deepEqual(expectedOptions, result.options);'), assert._expr(assert._capt(assert._capt(result, 'ident', {
                    start: {
                        line: 51,
                        column: 50
                    }
                }).options, 'ident', {
                    start: {
                        line: 51,
                        column: 57
                    }
                }), {
                    start: {
                        line: 51,
                        column: 50
                    },
                    path: '/Users/vvakame/Dropbox/work/typescript-formatter/tests/mainTest.js'
                }, '                assert.deepEqual(expectedOptions, result.options);'));
            });
        });
    });
});