summaryrefslogtreecommitdiff
path: root/tests/testing.js
blob: b8757c955ffc01be7e792d2e039366b047f7e7f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var path = require("path");
var fs = require("fs");
var promises = require("../lib/promises");
var _ = require("underscore");

exports.test = test;
exports.testPath = testPath;
exports.testData = testData;
exports.createFakeDocxFile = createFakeDocxFile;


function test(name, func) {
    it(name, function(done) {
        var result = func();
        promises.when(result).then(function() {
            done();
        }).done();
    });
}

function testPath(filename) {
    return path.join(__dirname, "test-data", filename);
}

function testData(testDataPath) {
    var fullPath = testPath(testDataPath);
    return promises.nfcall(fs.readFile, fullPath, "utf-8");
}

function createFakeDocxFile(files) {
    function read(path, encoding) {
        return promises.when(files[path], function(buffer) {
            if (_.isString(buffer)) {
                buffer = new Buffer(buffer);
            }
            
            if (!Buffer.isBuffer(buffer)) {
                return promises.reject(new Error("file was not a buffer"));
            } else if (encoding) {
                return promises.when(buffer.toString(encoding));
            } else {
                return promises.when(buffer);
            }
        });
    }
    
    function exists(path) {
        return !!files[path];
    }
    
    return {
        read: read,
        exists: exists
    };
}