aboutsummaryrefslogtreecommitdiff
path: root/test/testing.js
blob: ef21ba9969f95c142805603e7ac3568bd3311098 (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
56
var path = require("path");
var _ = require("underscore");

var fs = require("../lib/fs");
var promises = require("../lib/promises");

exports.testPath = testPath;
exports.testData = testData;
exports.createFakeDocxFile = createFakeDocxFile;
exports.createFakeFiles = createFakeFiles;


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

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

function createFakeDocxFile(files) {
    function exists(path) {
        return !!files[path];
    }

    return {
        read: createRead(files),
        exists: exists
    };
}

function createFakeFiles(files) {
    return {
        read: createRead(files)
    };
}

function createRead(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.buffer);
            }
        });
    }
    return read;
}