summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/class/methodOverwrites.js
blob: c1c460a28c6d78aa5152fc76671d0c86f8b8656d (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
// Ensure that we can overwrite methods when more tha one is present.
{
    var result = 0;
    // Regardless of order, the constructor is overridden by any CPN, because it's
    // processed seperately.
    class a { ["constructor"]() { result += 1; }; constructor() { result += 2; } }
    var aInst = new a();
    assertEq(result, 2);
    aInst.constructor();
    assertEq(result, 3);

    class b { constructor() { result += 2; } ["constructor"]() { result += 1; }; }
    var bInst = new b();
    assertEq(result, 5);
    bInst.constructor();
    assertEq(result, 6);

    class c { constructor() { } method() { result += 1 } get method() { result += 2 } }
    new c().method;
    assertEq(result, 8);

    class d { constructor() { } get method() { result += 1 } method() { result += 2 } }
    new d().method();
    assertEq(result, 10);

    // getters and setter should not overwrite each other, but merge cleanly.
    class e { constructor() { } get method() { result += 1 } set method(x) { } }
    new e().method;
    assertEq(result, 11);

    class f { constructor() { }
            set method(x) { throw "NO"; }
            method() { throw "NO" }
            get method() { return new Function("result += 1"); }
            }
    new f().method();
    assertEq(result, 12);
}

// Try again with expressions.
{
    var result = 0;
    // Regardless of order, the constructor is overridden by any CPN, because it's
    // processed seperately.
    let a = class { ["constructor"]() { result += 1; }; constructor() { result += 2; } };
    var aInst = new a();
    assertEq(result, 2);
    aInst.constructor();
    assertEq(result, 3);

    let b = class { constructor() { result += 2; } ["constructor"]() { result += 1; }; };
    var bInst = new b();
    assertEq(result, 5);
    bInst.constructor();
    assertEq(result, 6);

    let c = class { constructor() { } method() { result += 1 } get method() { result += 2 } };
    new c().method;
    assertEq(result, 8);

    let d = class { constructor() { } get method() { result += 1 } method() { result += 2 } };
    new d().method();
    assertEq(result, 10);

    // getters and setter should not overwrite each other, but merge cleanly.
    let e = class { constructor() { } get method() { result += 1 } set method(x) { } };
    new e().method;
    assertEq(result, 11);

    let f = class { constructor() { }
                    set method(x) { throw "NO"; }
                    method() { throw "NO" }
                    get method() { return new Function("result += 1"); }
                  };
    new f().method();
    assertEq(result, 12);
}

if (typeof reportCompare === "function")
    reportCompare(0, 0, "OK");