-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
85 lines (70 loc) · 3.33 KB
/
test.js
File metadata and controls
85 lines (70 loc) · 3.33 KB
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
assert = require('assert');
eval(require('fs').readFileSync('codehint.js').toString())
_ = require('./underscore-min');
// Simple testing code.
function test() {
var two = 2;
var s = 'live';
var exp = function(x, y) { if (typeof x !== 'number' || typeof y !== 'number') throw 'Must give a number.'; else return Math.pow(x, y); };
var person = { firstName: "John", lastName: "Doe", age: 42, live: function(x) { if (typeof(x) == 'number') { this.age += x; return this.age; } else throw 'Must give a number.' }, answer: function () { return 42; } };
var a = [1, 2, 3];
var numSpec = function (rv) { return typeof rv == 'number'; };
var startTime = (new Date()).getTime();
var results = CodeHint.synthesize({two: two, s: s, person: person, a: a, exp: exp, n: null, u: undefined}, numSpec);
var endTime = (new Date()).getTime();
console.log('Found ' + results.length + ' results in ' + ((endTime - startTime) / 1000) + 's.');
var resultStrs = _.map(results, function (e) { return e.str; });
assert(resultStrs.length === _.uniq(resultStrs).length, 'Contains duplicate results');
var resultsMap = _.object(_.map(results, function (e) { return [e.str, e]; }));
function checkEquals(str, val) {
assert.strictEqual(resultsMap[str].value, val, str + ' has value ' + resultsMap[str].value + ' but should be ' + val);
}
function checkDepth(str, depth) {
assert.strictEqual(resultsMap[str].depth, depth, 'Depth of ' + str + ' is ' + resultsMap[str].depth + ' but should be ' + depth);
}
function checkContainsStr(str, shouldHave) {
var has = str in resultsMap;
if (shouldHave)
assert(has, 'Does not contain ' + str);
else
assert(!has, 'Contains ' + str);
}
// Check values
checkEquals('two', 2);
checkEquals('-two', -2);
assert.throws(function() { checkEquals('two', 42) });
checkEquals('a.length', 3);
checkEquals('person.answer()', 42);
checkEquals('a[two]', 3);
checkEquals('(s + s).length', 8);
checkEquals('person.firstName.length', 4);
checkEquals('exp(two, two)', 4);
// Check depths
checkDepth('two', 0);
checkDepth('exp(two, two)', 1);
checkDepth('person.live(two)', 2);
assert.throws(function() { checkDepth('person.live(two)', 1); });
// Ensure we can access properties with . and [].
checkContainsStr('person.live(two)', true);
assert.throws(function() { checkContainsStr('person[s](two)', false); });
checkContainsStr('person[s](two)', true);
checkContainsStr('person.age', true);
checkContainsStr('person[\'age\']', false);
// Ensure we do not have certain infix ops.
checkContainsStr('two - two', false);
assert.throws(function() { checkContainsStr('two - two', true); });
checkContainsStr('two / two', false);
checkContainsStr('(-two) * (-two)', false);
checkContainsStr('two + exp(two, two)', false);
checkContainsStr('exp(two, two) + two', true);
checkContainsStr('two + a[two]', false);
checkContainsStr('a[two] + two', true);
// Check parens
checkContainsStr('two + two + two', false);
assert.throws(function() { checkContainsStr('two + two + two', true); });
checkContainsStr('two + two - two', false);
checkContainsStr('two * (two * two)', true);
checkContainsStr('(s + s).length', true);
checkContainsStr('s + s.length', false);
}
test();