summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/object/values-entries-lazy-props.js
blob: 0631c048afbed7687604eed1fa33063e9a354558 (plain)
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
// Return new objects for each test case.
function makeTestCases() {
    // Call the resolve hook for arguments/string objects.
    const resolveIndex = object => 0 in object;

    // Calls the resolve hook for functions.
    const resolveFunction = object => "length" in object;

    const expected = array => ({
        keys: Object.keys(array),
        values: Object.values(array),
        entries: Object.entries(array),
    });

    return [
        // Mapped arguments objects.
        {
            object: function(){ return arguments; }(),
            resolve: resolveIndex,
            ...expected([]),
        },
        {
            object: function(x){ return arguments; }(1),
            resolve: resolveIndex,
            ...expected([1]),
        },
        {
            object: function(x, y, z){ return arguments; }(1, 2, 3),
            resolve: resolveIndex,
            ...expected([1, 2, 3]),
        },

        // Unmapped arguments objects.
        {
            object: function(){ "use strict"; return arguments; }(),
            resolve: resolveIndex,
            ...expected([]),
        },
        {
            object: function(x){ "use strict"; return arguments; }(1),
            resolve: resolveIndex,
            ...expected([1]),
        },
        {
            object: function(x, y, z){ "use strict"; return arguments; }(1, 2, 3),
            resolve: resolveIndex,
            ...expected([1, 2, 3]),
        },

        // String objects.
        { object: new String(""), resolve: resolveIndex, ...expected([]), },
        { object: new String("a"), resolve: resolveIndex, ...expected(["a"]), },
        { object: new String("abc"), resolve: resolveIndex, ...expected(["a","b","c"]), },

        // Function objects.
        { object: function(){}, resolve: resolveFunction, ...expected([]), },
    ];
}

var assertWith = {
    keys: assertEqArray,
    values: assertEqArray,
    entries(actual, expected) {
        assertEq(actual.length, expected.length);
        for (let i = 0; i < expected.length; ++i)
            assertEqArray(actual[i], expected[i]);
    }
};

// Test Object.keys/values/entries on objects with enumerate/resolve hooks.

for (let method of ["keys", "values", "entries"]) {
    let assert = assertWith[method];

    // Call each method twice to test the case when
    // - the enumerate hook wasn't yet called,
    // - the enumerate hook was already called.
    for (let {object, [method]: expected} of makeTestCases()) {
        assert(Object[method](object), expected);
        assert(Object[method](object), expected);
    }

    // Test the case when enumerate wasn't yet called, but a property was already resolved.
    for (let {object, resolve, [method]: expected} of makeTestCases()) {
        resolve(object); // Call the resolve hook.

        assert(Object[method](object), expected);
        assert(Object[method](object), expected);
    }
}

if (typeof reportCompare === "function")
    reportCompare(true, true);