File: property.js

package info (click to toggle)
node-hashish 0.0.4%2Bgit34dfe43-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 132 kB
  • sloc: javascript: 518; makefile: 2
file content (75 lines) | stat: -rw-r--r-- 2,056 bytes parent folder | download
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
var Hash = require('hashish');
var assert = require('assert');
var vm = require('vm');
var fs = require('fs');

var src = fs.readFileSync(__dirname + '/../index.js', 'utf8');

exports.defineGetter = function () {
    var context = {
        module : { exports : {} },
        Object : {
            keys : Object.keys,
            defineProperty : undefined,
        },
        require : require,
    };
    context.exports = context.module.exports;
    
    vm.runInNewContext('(function () {' + src + '})()', context);
    var Hash_ = context.module.exports;
    
    var times = 0;
    let tmp = { __defineGetter__: function() {
        times ++;
        return Object.__defineGetter__.apply(this, arguments);
    }};
    let tmp2 = new Object();
    Object.setPrototypeOf(tmp2, tmp);
    Object.setPrototypeOf(Hash_, tmp2);
    
    assert.equal(vm.runInNewContext('Object.defineProperty', context), null);
    
    assert.deepEqual(
        Hash_({ a : 1, b : 2, c : 3 }).values,
        [ 1, 2, 3 ]
    );
    
    assert.ok(times > 5);
};

exports.defineProperty = function () {
    var times = 0;
    var context = {
        module : { exports : {} },
        Object : {
            keys : Object.keys,
            defineProperty : function (prop) {
                times ++;
                if (prop.get) throw new TypeError('engine does not support')
                assert.fail('should have asserted by now');
            },
        },
        require : require
    };
    context.exports = context.module.exports;
    
    vm.runInNewContext('(function () {' + src + '})()', context);
    var Hash_ = context.module.exports;
    
    let tmp = { __defineGetter__: function() {
        assert.fail('getter called when a perfectly good'
            + ' defineProperty was available'
        );
    }};
    let tmp2 = new Object();
    Object.setPrototypeOf(tmp2, tmp);
    Object.setPrototypeOf(Hash_, tmp2);
    
    assert.deepEqual(
        Hash_({ a : 1, b : 2, c : 3 }).values,
        [ 1, 2, 3 ]
    );
    
    assert.equal(times, 1);
};