| 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 |
const inst = { |
| 20 |
@dec |
| 21 |
enumconfwrite: 1, |
| 22 |
|
| 23 |
@dec |
| 24 |
enumconf: 2, |
| 25 |
|
| 26 |
@dec |
| 27 |
enumwrite: 3, |
| 28 |
|
| 29 |
@dec |
| 30 |
enum: 4, |
| 31 |
|
| 32 |
@dec |
| 33 |
confwrite: 5, |
| 34 |
|
| 35 |
@dec |
| 36 |
conf: 6, |
| 37 |
|
| 38 |
@dec |
| 39 |
write: 7, |
| 40 |
|
| 41 |
@dec |
| 42 |
_: 8, |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
expect(inst).toHaveProperty("decoratedProps"); |
| 47 |
expect(inst.decoratedProps).toEqual([ |
| 48 |
"enumconfwrite", |
| 49 |
"enumconf", |
| 50 |
"enumwrite", |
| 51 |
"enum", |
| 52 |
"confwrite", |
| 53 |
"conf", |
| 54 |
"write", |
| 55 |
"_", |
| 56 |
]); |
| 57 |
|
| 58 |
const descs = Object.getOwnPropertyDescriptors(inst); |
| 59 |
|
| 60 |
expect(descs.enumconfwrite.enumerable).toBeTruthy(); |
| 61 |
expect(descs.enumconfwrite.writable).toBeTruthy(); |
| 62 |
expect(descs.enumconfwrite.configurable).toBeTruthy(); |
| 63 |
expect(inst.enumconfwrite).toBe("__1__"); |
| 64 |
|
| 65 |
expect(descs.enumconf.enumerable).toBeTruthy(); |
| 66 |
expect(descs.enumconf.writable).toBe(false); |
| 67 |
expect(descs.enumconf.configurable).toBeTruthy(); |
| 68 |
expect(inst.enumconf).toBe("__2__"); |
| 69 |
|
| 70 |
expect(descs.enumwrite.enumerable).toBeTruthy(); |
| 71 |
expect(descs.enumwrite.writable).toBeTruthy(); |
| 72 |
expect(descs.enumwrite.configurable).toBe(false); |
| 73 |
expect(inst.enumwrite).toBe("__3__"); |
| 74 |
|
| 75 |
expect(descs.enum.enumerable).toBeTruthy(); |
| 76 |
expect(descs.enum.writable).toBe(false); |
| 77 |
expect(descs.enum.configurable).toBe(false); |
| 78 |
expect(inst.enum).toBe("__4__"); |
| 79 |
|
| 80 |
expect(descs.confwrite.enumerable).toBe(false); |
| 81 |
expect(descs.confwrite.writable).toBeTruthy(); |
| 82 |
expect(descs.confwrite.configurable).toBeTruthy(); |
| 83 |
expect(inst.confwrite).toBe("__5__"); |
| 84 |
|
| 85 |
expect(descs.conf.enumerable).toBe(false); |
| 86 |
expect(descs.conf.writable).toBe(false); |
| 87 |
expect(descs.conf.configurable).toBeTruthy(); |
| 88 |
expect(inst.conf).toBe("__6__"); |
| 89 |
|
| 90 |
expect(descs.write.enumerable).toBe(false); |
| 91 |
expect(descs.write.writable).toBeTruthy(); |
| 92 |
expect(descs.write.configurable).toBe(false); |
| 93 |
expect(inst.write).toBe("__7__"); |
| 94 |
|
| 95 |
expect(descs._.enumerable).toBe(false); |
| 96 |
expect(descs._.writable).toBe(false); |
| 97 |
expect(descs._.configurable).toBe(false); |
| 98 |
expect(inst._).toBe("__8__"); |