-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex3.test.js
More file actions
74 lines (62 loc) · 1.53 KB
/
ex3.test.js
File metadata and controls
74 lines (62 loc) · 1.53 KB
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
'use strict';
const { safeSet } = require('./ex3');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
function assertThrows(fn, messagePart) {
let threw = false;
try {
fn();
} catch (err) {
threw = true;
if (messagePart) {
assert(
String(err && err.message).includes(messagePart),
`Expected thrown error to include "${messagePart}"`
);
}
}
assert(threw, 'Expected function to throw');
}
{
const obj = {};
Object.defineProperty(obj, 'fixed', {
value: 1,
writable: false,
configurable: false,
enumerable: true,
});
let threw = false;
let result;
try {
result = safeSet(obj, 'fixed', 2);
} catch (err) {
threw = true;
}
assert(threw === false, 'Expected non-writable assignment to return false, not throw');
assert(result === false, 'Expected Reflect-style false on non-writable property');
assert(obj.fixed === 1, 'Expected original non-writable value to remain');
}
{
const obj = {
set boom(v) {
throw new Error(`setter exploded:${v}`);
},
};
assertThrows(
() => safeSet(obj, 'boom', 9),
'setter exploded:9'
);
}
{
const proto = {
set value(v) {
this._stored = v * 2;
},
};
const obj = Object.create(proto);
const ok = safeSet(obj, 'value', 5);
assert(ok === true, 'Expected inherited setter assignment to succeed');
assert(obj._stored === 10, 'Expected inherited setter to run with receiver as object');
}
console.log('ex3 tests passed');