This repository has been archived by the owner on Apr 30, 2019. It is now read-only.
/
helper.ts
225 lines (189 loc) · 7.02 KB
/
helper.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/// <reference path="_ref.d.ts" />
/// <reference path="globals.ts" />
/// <reference path="assert/xm/_all.ts" />
/// <reference path="assert/Map.ts" />
/// <reference path="../src/xm/data/PackageJSON.ts" />
module helper {
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var Q:typeof Q = require('q');
var FS:typeof QioFS = require('q-io/fs');
var Reader:Qio.BufferReader = require('q-io/reader');
var assert:Chai.Assert = require('chai').assert;
var childProcess = require('child_process');
var bufferEqual = require('buffer-equal');
var shaRegExp = /^[0-9a-f]{40}$/;
var md5RegExp = /^[0-9a-f]{32}$/;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
export function getProjectRoot():string {
return path.dirname(xm.PackageJSON.find());
}
export function getDirNameFixtures():string {
return path.resolve(__dirname, '..', 'fixtures');
}
export function getDirNameTmp():string {
return path.resolve(__dirname, '..', 'tmp');
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// helper to get a readable debug message (useful when comparing things absed on 2 paths)
// can be improved freely (as required as it is for visualisation only)
export function getPathMessage(pathA:string, pathB:string, message:string):string {
// make absolute
pathA = path.resolve(pathA);
pathB = path.resolve(pathB);
var elemsA = pathA.split(path.sep);
var elemsB = pathB.split(path.sep);
// remove identical parts
while (elemsA.length > 0 && elemsB.length > 0 && elemsA[0] === elemsB[0]) {
elemsA.shift();
elemsB.shift();
}
// same paths?
if (elemsA.length === 0 && elemsA.length === elemsB.length) {
return message + ': \'' + path.basename(pathA) + '\'';
}
// different, print remains
return message + ': ' + '\'' + elemsA.join(path.sep) + '\' vs \'' + elemsB.join(path.sep) + '\'';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
export function longAssert(actual:string, expected:string, msg?:string):void {
if (actual !== expected) {
throw new chai.AssertionError((msg ? msg + ': ' : '') + 'long string', {
actual: actual,
expected: expected
}, helper.longAssert);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
export function dump(object:any, message?:string, depth:number = 6, showHidden:boolean = false):any {
message = xm.isUndefined(message) ? '' : message + ': ';
xm.log(message + util.inspect(object, showHidden, depth, true));
}
export function dumpJSON(object:any, message?:string):any {
message = xm.isUndefined(message) ? '' : message + ': ';
xm.log(message + JSON.stringify(object, null, 4));
}
export function assertFormatSHA1(value:string, msg?:string):void {
assert.isString(value, msg);
assert.match(value, shaRegExp, msg);
}
export function assertFormatMD5(value:string, msg?:string):void {
assert.isString(value, msg);
assert.match(value, md5RegExp, msg);
}
export function propStrictEqual(actual:Object, expected:Object, prop:string, message:string):void {
assert.property(actual, prop, message + '.' + prop + ' actual');
assert.property(expected, prop, message + '.' + prop + ' expected');
assert.strictEqual(actual[prop], expected[prop], message + '.' + prop + ' equal');
}
export function assertBufferEqual(act:NodeBuffer, exp:NodeBuffer, msg?:string):void {
assert.instanceOf(act, Buffer, msg + ': ' + act);
assert.instanceOf(exp, Buffer, msg + ': ' + exp);
assert(bufferEqual(act, exp), msg + ': bufferEqual');
}
export function assertBufferUTFEqual(act:NodeBuffer, exp:NodeBuffer, msg?:string):void {
assert.instanceOf(act, Buffer, msg + ': ' + act);
assert.instanceOf(exp, Buffer, msg + ': ' + exp);
assert.strictEqual(act.toString('utf8'), exp.toString('utf8'), msg + ': bufferEqual');
}
export function assertObjectValues(actual:Object, expected:Object, msg?:string):void {
assert.isObject(actual, msg + ': actual');
assert.isObject(expected, msg + ': expected');
var test:any = {};
Object.keys(expected).forEach((prop:string) => {
if (typeof actual[prop] !== 'undefined') {
test[prop] = actual[prop];
}
else {
test[prop] = 0;
}
});
assert.deepEqual(test, expected, msg);
}
export function assertNotes(actual:Object[], expected:Object[], msg:string):void {
assert.isArray(actual, msg + ': actual');
assert.isArray(expected, msg + ': expected');
actual = actual.slice(0);
expected = expected.slice(0);
var next:any;
while (expected.length > 0) {
next = expected.shift();
while (actual.length > 0) {
var act:any = actual.shift();
var codeOK = (typeof next.code !== 'undefined') && (next.code === act.code);
var messageOK = (typeof next.message !== 'undefined') && (next.message.test(act.message));
if (codeOK && messageOK) {
next = null;
break;
}
}
if (actual.length === 0) {
break;
}
}
// report
if (next) {
expected.unshift(next);
}
if (expected.length > 0) {
expected.forEach((item:any) => {
item.message = String(item.message);
});
actual.forEach((item:any) => {
item.message = String(item.message);
});
assert.fail(actual, expected, 'expected more notes');
}
}
// hackish to get more ingot then assert.throws()
export function assertError(exec:() => void, expected:any, msg?:string):void {
msg = (msg ? msg + ': ' : '');
try {
exec();
}
catch (e) {
var errorMsg:any = e.message.toString().match(/.*/m);
if (errorMsg) {
errorMsg = errorMsg[0];
if (xm.isRegExp(expected)) {
if (!expected.test(errorMsg)) {
assert.strictEqual(errorMsg, '', msg + 'expected to match RegExp: ' + expected);
}
}
else {
assert.strictEqual(errorMsg, expected, msg + 'match message');
}
return;
}
}
assert.strictEqual('', expected.toString(), msg + 'expected to throw and match ' + expected);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// for safety
function promiseDoneMistake():void {
throw new Error('don\'t use a done() callback when using it.eventually()');
}
// monkey patch
it.eventually = function eventually(expectation:string, assertion?:(call:() => void) => void):void {
it(expectation, (done) => {
Q(assertion(promiseDoneMistake)).done(() => {
done();
}, (err:Error) => {
done(err);
});
});
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
export interface AssertCB<T> {
(actual:T, expected:T, message?:string):void;
}
export interface AssertCBA<T> {
(actual:T[], expected:T[], message?:string):void;
}
export interface IsLikeCB<T> {
(actual:T, expected:T):boolean;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}