fei.wang
2025-08-13 ae7b22322555448d95fd56f505bafa325c167a26
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
'use strict';
 
var test = require('tape');
var isDescriptor = require('../');
var noop = function () {};
 
test('isDescriptor', function (t) {
    t.test('is false when not an object:', function (st) {
        st.notOk(isDescriptor('a'));
        st.notOk(isDescriptor(null));
        st.notOk(isDescriptor([]));
 
        st.end();
    });
 
    t.test('returns true if the property exists', function (st) {
        var obj = { foo: null };
 
        Object.defineProperty(obj, 'bar', {
            value: 'xyz'
        });
 
        Object.defineProperty(obj, 'baz', {
            get: function () {
                return 'aaa';
            }
        });
 
        st.ok(isDescriptor(obj, 'foo'));
        st.ok(isDescriptor(obj, 'bar'));
        st.ok(isDescriptor(obj, 'baz'));
 
        st.end();
    });
 
    t.test('data descriptor:', function (st) {
        st.test('is false when the object has invalid properties:', function (s2t) {
            s2t.notOk(isDescriptor({ value: 'foo', get: noop }));
            s2t.notOk(isDescriptor({ get: noop, value: noop }));
 
            s2t.end();
        });
 
        st.test('is not false when the object has unrecognize properties:', function (s2t) {
            s2t.ok(isDescriptor({ value: 'foo', bar: 'baz' }));
            s2t.ok(isDescriptor({ value: 'foo', bar: 'baz' }));
 
            s2t.end();
        });
 
        st.test('is true when the object has valid properties:', function (s2t) {
            s2t.ok(isDescriptor({ value: 'foo' }));
            s2t.ok(isDescriptor({ value: noop }));
 
            s2t.end();
        });
 
        st.test('is false when a value is not the correct type:', function (s2t) {
            s2t.notOk(isDescriptor({ value: 'foo', enumerable: 'foo' }));
            s2t.notOk(isDescriptor({ value: 'foo', configurable: 'foo' }));
            s2t.notOk(isDescriptor({ value: 'foo', writable: 'foo' }));
 
            s2t.end();
        });
 
        st.end();
    });
 
    t.test('accessor descriptor:', function (st) {
        st.test('should be false when the object has invalid properties:', function (s2t) {
            s2t.ok(!isDescriptor({ get: noop, writable: true }));
            s2t.ok(!isDescriptor({ get: noop, value: true }));
 
            s2t.end();
        });
 
        st.test('is not false when the object has unrecognize properties:', function (s2t) {
            s2t.ok(isDescriptor({ get: noop, set: noop, bar: 'baz' }));
 
            s2t.end();
        });
 
        st.test('is false when an accessor is not a function:', function (s2t) {
            s2t.notOk(isDescriptor({ get: noop, set: 'baz' }));
            s2t.notOk(isDescriptor({ get: 'foo', set: noop }));
            s2t.notOk(isDescriptor({ get: 'foo', bar: 'baz' }));
            s2t.notOk(isDescriptor({ get: 'foo', set: 'baz' }));
 
            s2t.end();
        });
 
        st.test('is false when "get" or "set" is not a function', function (s2t) {
            s2t.notOk(isDescriptor({ set: 'foo' }));
            s2t.notOk(isDescriptor({ get: 'foo' }));
 
            s2t.end();
        });
 
        st.test('is true when the object has valid properties:', function (s2t) {
            s2t.ok(isDescriptor({ get: noop, set: noop }));
            s2t.ok(isDescriptor({ get: noop }));
 
            s2t.end();
        });
 
        st.test('is false when a value is not the correct type:', function (s2t) {
            s2t.notOk(isDescriptor({ get: noop, set: noop, enumerable: 'foo' }));
            s2t.notOk(isDescriptor({ set: noop, configurable: 'foo' }));
            s2t.notOk(isDescriptor({ get: noop, configurable: 'foo' }));
 
            s2t.end();
        });
 
        st.end();
    });
});