| 1 |
function dec(target, name, descriptor) { |
| 2 |
expect(target).toBeTruthy(); |
| 3 |
expect(typeof name).toBe("string"); |
| 4 |
expect(typeof descriptor).toBe("object"); |
| 5 |
|
| 6 |
target.decoratedProps = (target.decoratedProps || []).concat([name]); |
| 7 |
|
| 8 |
let initializer = descriptor.initializer; |
| 9 |
Object.assign(descriptor, { |
| 10 |
enumerable: name.indexOf("enum") !== -1, |
| 11 |
configurable: name.indexOf("conf") !== -1, |
| 12 |
writable: name.indexOf("write") !== -1, |
| 13 |
initializer: function(...args){ |
| 14 |
return '__' + initializer.apply(this, args) + '__'; |
| 15 |
}, |
| 16 |
}); |
| 17 |
} |
| 18 |
|
| 19 |
class Example { |
| 20 |
@dec |
| 21 |
static enumconfwrite = 1; |
| 22 |
|
| 23 |
@dec |
| 24 |
static enumconf = 2; |
| 25 |
|
| 26 |
@dec |
| 27 |
static enumwrite = 3; |
| 28 |
|
| 29 |
@dec |
| 30 |
static enum = 4; |
| 31 |
|
| 32 |
@dec |
| 33 |
static confwrite = 5; |
| 34 |
|
| 35 |
@dec |
| 36 |
static conf = 6; |
| 37 |
|
| 38 |
@dec |
| 39 |
static write = 7; |
| 40 |
|
| 41 |
@dec |
| 42 |
static _ = 8; |
| 43 |
} |
| 44 |
|
| 45 |
const inst = new Example(); |
| 46 |
|
| 47 |
expect(Example).toHaveProperty("decoratedProps"); |
| 48 |
expect(Example.decoratedProps).toEqual([ |
| 49 |
"enumconfwrite", |
| 50 |
"enumconf", |
| 51 |
"enumwrite", |
| 52 |
"enum", |
| 53 |
"confwrite", |
| 54 |
"conf", |
| 55 |
"write", |
| 56 |
"_", |
| 57 |
]); |
| 58 |
|
| 59 |
const descs = Object.getOwnPropertyDescriptors(Example); |
| 60 |
|
| 61 |
expect(descs.enumconfwrite.enumerable).toBeTruthy(); |
| 62 |
expect(descs.enumconfwrite.writable).toBeTruthy(); |
| 63 |
expect(descs.enumconfwrite.configurable).toBeTruthy(); |
| 64 |
expect(Example.enumconfwrite).toBe("__1__"); |
| 65 |
|
| 66 |
expect(descs.enumconf.enumerable).toBeTruthy(); |
| 67 |
expect(descs.enumconf.writable).toBe(false); |
| 68 |
expect(descs.enumconf.configurable).toBeTruthy(); |
| 69 |
expect(Example.enumconf).toBe("__2__"); |
| 70 |
|
| 71 |
expect(descs.enumwrite.enumerable).toBeTruthy(); |
| 72 |
expect(descs.enumwrite.writable).toBeTruthy(); |
| 73 |
expect(descs.enumwrite.configurable).toBe(false); |
| 74 |
expect(Example.enumwrite).toBe("__3__"); |
| 75 |
|
| 76 |
expect(descs.enum.enumerable).toBeTruthy(); |
| 77 |
expect(descs.enum.writable).toBe(false); |
| 78 |
expect(descs.enum.configurable).toBe(false); |
| 79 |
expect(Example.enum).toBe("__4__"); |
| 80 |
|
| 81 |
expect(descs.confwrite.enumerable).toBe(false); |
| 82 |
expect(descs.confwrite.writable).toBeTruthy(); |
| 83 |
expect(descs.confwrite.configurable).toBeTruthy(); |
| 84 |
expect(Example.confwrite).toBe("__5__"); |
| 85 |
|
| 86 |
expect(descs.conf.enumerable).toBe(false); |
| 87 |
expect(descs.conf.writable).toBe(false); |
| 88 |
expect(descs.conf.configurable).toBeTruthy(); |
| 89 |
expect(Example.conf).toBe("__6__"); |
| 90 |
|
| 91 |
expect(descs.write.enumerable).toBe(false); |
| 92 |
expect(descs.write.writable).toBeTruthy(); |
| 93 |
expect(descs.write.configurable).toBe(false); |
| 94 |
expect(Example.write).toBe("__7__"); |
| 95 |
|
| 96 |
expect(descs._.enumerable).toBe(false); |
| 97 |
expect(descs._.writable).toBe(false); |
| 98 |
expect(descs._.configurable).toBe(false); |
| 99 |
expect(Example._).toBe("__8__"); |