Skip to content

Commit 3d95f39

Browse files
committed
WIP dependency-loading
1 parent 7736f5f commit 3d95f39

File tree

4 files changed

+166
-49
lines changed

4 files changed

+166
-49
lines changed

_tests/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Cli
33

44
```bash
5-
node cli.js -f ../functions/datetime/strtotime.js
6-
node cli.js -f ../functions/datetime/date.js
5+
node _tests/cli.js -f sort
6+
node _tests/cli.js -f strtotime
77
```
88

99
# Web
@@ -18,7 +18,7 @@ node cli.js -f ../functions/datetime/date.js
1818
$('#content').append('<pre class="alert-warning alert">' + JSON.stringify(err, undefined, 2) + '</pre>');
1919
}
2020
$('#content').append('<pre>' + JSON.stringify(result, undefined, 2) + '</pre>');
21-
21+
2222
console.log(result);
2323
});
2424
</script>

_tests/cli.js

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,84 @@
11
var cli = require('cli').enable('status', 'help', 'version', 'glob', 'timeout');
22
var FS = require('fs');
3+
var glob = require('glob');
34
var PhpjsUtil = require('./phpjsutil');
45

6+
// Environment-specific file opener. function name needs to
7+
// be translated to code. The difficulty is in finding the
8+
// category.
9+
PhpjsUtil.opener = function (name, cb) {
10+
glob(__dirname + '/../functions/*/' + name + '.js', {}, function (err, files) {
11+
if (err) {
12+
return cb(err);
13+
}
14+
var filepath = files[0];
15+
16+
if (!filepath) {
17+
return cb('could not find' + name);
18+
}
19+
20+
FS.readFile(filepath, 'utf-8', function (err, code) {
21+
if (err) {
22+
return cb(err);
23+
}
24+
return cb(null, code);
25+
});
26+
});
27+
};
28+
529
cli.parse({
6-
function_file: ['f', 'Function to test', 'path'],
30+
name: ['f', 'Function name to test', 'path']
731
});
832

33+
cli.pad = function(str, pad, chr, dir) {
34+
if (!pad) pad = 20;
35+
if (!chr) chr = ' ';
36+
if (!dir) dir = 'left';
37+
38+
if ((str+'').length >= pad) {
39+
return str;
40+
}
41+
42+
if (dir === 'right') {
43+
return String(str + Array(pad).join(chr)).slice(0, pad);
44+
}
45+
46+
return String(Array(pad).join(chr) + str).slice(-pad);
47+
};
48+
49+
var width = 120;
50+
951
cli.main(function(args, options) {
10-
if (!options.function_file) {
11-
this.fatal('Please specify a file to test (-h for help)');
12-
}
52+
if (!options.name) {
53+
this.fatal('Please specify a file to test (-h for help)');
54+
}
1355

1456
// cli.spinner('Working..');
1557
// cli.spinner('Working.. done!', true); //End the spinner
1658

17-
FS.readFile(options.function_file, 'utf-8', function (err, code) {
59+
PhpjsUtil.load(options.name, function (err, params) {
1860
if (err) {
1961
return cli.fatal(err);
2062
}
2163

22-
PhpjsUtil.parse(options.function_file, code, function (err, params) {
64+
console.log(params['headKeys']);
65+
66+
PhpjsUtil.test(params, function(err, test, params) {
67+
var testline = cli.pad(params['name'] + '#' + test['number'], (width * 0.4), ' ', 'right') +
68+
' ' + cli.pad(test['example'], (width * 0.6 -7)) + '\n' +
69+
' ' + cli.pad(test['expected'], width) + '\n' +
70+
' ' + cli.pad(test['result'], width) + '\n' +
71+
' ';
72+
2373
if (err) {
24-
return cli.fatal(err);
74+
cli.error(testline + '');
75+
} else {
76+
cli.ok(' ' + testline + '');
2577
}
26-
// console.log(params['headKeys']);
27-
28-
PhpjsUtil.test(params, function(err, test, params) {
29-
var testline = params['name'] +
30-
'#' + test['number'] +
31-
'\t' + test['example'] +
32-
'\t' + test['expected'] +
33-
'\t' + test['result'] +
34-
'\t';
35-
36-
if (err) {
37-
cli.error(testline + ' test failed :(');
38-
}
39-
cli.ok(testline + ' test passed :)');
40-
});
4178
});
42-
});
79+
});
80+
81+
// PhpjsUtil.load('')
82+
// PhpjsUtil.parse(options.name, code,
83+
4384
});

_tests/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"node": ">= 0.6.0"
99
},
1010
"dependencies": {
11-
"cli": "~0.4.4-2"
11+
"cli": "~0.4.4-2",
12+
"glob": "~3.2.1"
1213
},
1314
"keywords": [
1415
"cli",

_tests/phpjsutil.js

Lines changed: 97 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ PhpjsUtil._commentBlocks = function(code) {
1919
raise = false;
2020
}
2121
if (!commentBlocks[cnt]) {
22-
commentBlocks[cnt] = {clean: [], raw: [], };
22+
commentBlocks[cnt] = {clean: [], raw: []};
2323
}
2424

2525
commentBlocks[cnt].clean.push(comment[2].trim());
@@ -30,7 +30,7 @@ PhpjsUtil._commentBlocks = function(code) {
3030
}
3131

3232
return commentBlocks;
33-
}
33+
};
3434

3535
PhpjsUtil._headKeys = function(headLines) {
3636
var i;
@@ -50,7 +50,7 @@ PhpjsUtil._headKeys = function(headLines) {
5050
if ((dmatch = key.match(/^(\w+)\s+(\d+)$/))) {
5151
// Things like examples and notes can be grouped
5252
key = dmatch[1];
53-
num = dmatch[2]-1;
53+
num = dmatch[2]-1;
5454

5555
if (!keys[key]) {
5656
keys[key] = [];
@@ -68,7 +68,39 @@ PhpjsUtil._headKeys = function(headLines) {
6868
}
6969

7070
return keys;
71-
}
71+
};
72+
73+
PhpjsUtil._loadDependencies = function(headKeys, dependencies, cb) {
74+
var self = this;
75+
76+
if (!headKeys['depends on'] || !headKeys['depends on'].length) {
77+
if (cb) {
78+
cb(null, {});
79+
}
80+
}
81+
82+
var i;
83+
var name;
84+
var loaded = 0;
85+
for (i in headKeys['depends on']) {
86+
name = headKeys['depends on'][i];
87+
88+
self.load(name, function (err, params) {
89+
if (err) {
90+
return cb(err);
91+
}
92+
93+
dependencies[name] = params;
94+
self._loadDependencies(params.headKeys, dependencies);
95+
96+
loaded++;
97+
98+
if (cb && loaded === headKeys['depends on'].length) {
99+
cb(null, dependencies);
100+
}
101+
});
102+
}
103+
};
72104

73105
PhpjsUtil.parse = function(name, code, cb) {
74106
var commentBlocks = this._commentBlocks(code);
@@ -78,31 +110,75 @@ PhpjsUtil.parse = function(name, code, cb) {
78110
var headKeys = this._headKeys(commentBlocks[0].clean);
79111

80112
// @todo(kvz) If we add function signature, we can use
81-
// body to generate CommonJs compatible output
113+
// body to generate CommonJs compatible output
82114
// in the browser.
83-
cb(null, {
84-
headKeys: headKeys,
85-
body: body,
86-
head: head,
87-
name: name,
88-
code: code,
89-
commentBlocks: commentBlocks,
115+
116+
this._loadDependencies(headKeys, {}, function (err, dependencies) {
117+
if (err) {
118+
return cb(err);
119+
}
120+
cb(null, {
121+
headKeys: headKeys,
122+
body: body,
123+
head: head,
124+
name: name,
125+
code: code,
126+
dependencies: dependencies,
127+
commentBlocks: commentBlocks
128+
});
129+
});
130+
};
131+
132+
PhpjsUtil.opener = function(name, cb) {
133+
return cb('Please override with a method that can translate a function-name to code in your environment');
134+
};
135+
136+
PhpjsUtil.load = function(name, cb) {
137+
var self = this;
138+
self.opener(name, function (err, code) {
139+
if (err) {
140+
return cb(err);
141+
}
142+
143+
self.parse(name, code, function (err, params) {
144+
if (err) {
145+
return cb(err);
146+
}
147+
148+
return cb(null, params);
149+
});
90150
});
91151
};
92152

93153
PhpjsUtil.test = function(params, cb) {
94154
var i = 0;
95155
var j = 0;
156+
var d = 0;
157+
var self = this;
96158

97-
// @todo(kvz)L if a function depends, we need to recursively
98-
// add those.. needs to be done with callbacks cause
99-
// getting code in browser / cli is very different..
100-
eval(params['code']);
101-
for (i in params['headKeys']['example']) {
102159

160+
var codes = [];
161+
// Push own code onto stack
162+
codes.push(params['code']);
163+
// Push dependencies onto stack
164+
for (d in params['dependencies']) {
165+
codes.push(params['dependencies'][d]['code']);
166+
}
167+
168+
// Reverse stack so dependencies are loaded first
169+
codes = codes.reverse();
170+
171+
// console.log(codes);
172+
173+
// Load code
174+
eval(codes.join('\n'));
175+
176+
177+
// Run each example
178+
for (i in params['headKeys']['example']) {
103179
var test = {
104180
example: params['headKeys']['example'][i].join('\n'),
105-
number: i,
181+
number: i
106182
};
107183

108184
// Needs an eval so types are cast properly, also, expected may
@@ -114,13 +190,12 @@ PhpjsUtil.test = function(params, cb) {
114190
eval('test.result = ' + params['headKeys']['example'][i][j] + '');
115191
}
116192

117-
118193
if (test.expected !== test.result) {
119-
var msg = 'Expected: ' + JSON.stringify(test.expected, undefined, 2) +
194+
var err = 'Expected: ' + JSON.stringify(test.expected, undefined, 2) +
120195
' but returned: ' + JSON.stringify(test.result, undefined, 2);
121-
cb(msg, test, params);
196+
cb(err, test, params);
122197
} else {
123-
cb(null, test, params)
198+
cb(null, test, params);
124199
}
125200
}
126201
};

0 commit comments

Comments
 (0)