summaryrefslogtreecommitdiff
path: root/tests/testing.js
blob: 6fef6263f020e47438dadc228c53cd18b8cfa48c (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
var path = require("path");
var fs = require("fs");
var q = require("q");
var _ = require("underscore");

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


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

function testData(testDataPath) {
    var fullPath = path.join(__dirname, "test-data", testDataPath);
    return q.nfcall(fs.readFile, fullPath, "utf-8");
}

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