forked from liammclennan/JavaScript-Koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBONUS_2_about_reflection.js
More file actions
89 lines (81 loc) · 2.42 KB
/
BONUS_2_about_reflection.js
File metadata and controls
89 lines (81 loc) · 2.42 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
describe("About Reflection (topics/BONUS_2_about_reflection.js)", function () {
class A {
constructor() {
this.aprop = "A";
}
}
class B extends A {
constructor() {
super();
this.bprop = "B";
}
}
it("typeof", function () {
expect(__).toEqual(typeof {}, "what is the type of an empty object?");
expect(__).toEqual(typeof "apple", "what is the type of a string?");
expect(__).toEqual(typeof -5, "what is the type of -5?");
expect(__).toEqual(typeof false, "what is the type of false?");
});
it("property enumeration", function () {
let keys = [];
let values = [];
const person = { name: "Amory Blaine", age: 102, unemployed: true };
for (let propertyName in person) {
keys.push(propertyName);
values.push(person[propertyName]);
}
expect(keys).toEqual(
["__", "__", "__"],
"what are the property names of the object?"
);
expect(values).toEqual(
["__", __, __],
"what are the property values of the object?"
);
});
it("hasOwnProperty", function () {
let b = new B();
let propertyName;
let keys = [];
for (propertyName in b) {
keys.push(propertyName);
}
expect(__).toEqual(keys.length, "how many elements are in the keys array?");
expect([__, __]).toEqual(keys, "what are the properties of the array?");
// hasOwnProperty returns true if the parameter is a property directly on the object,
// but not if it is a property accessible via the prototype chain.
let ownKeys = [];
for (propertyName in b) {
if (b.hasOwnProperty(propertyName)) {
ownKeys.push(propertyName);
}
}
expect(__).toEqual(
ownKeys.length,
"how many elements are in the ownKeys array?"
);
expect([__]).toEqual(ownKeys, "what are the own properties of the array?");
});
it("constructor property", function () {
const a = new A();
const b = new B();
expect(__).toEqual(
typeof a.constructor,
"what is the type of a's constructor?"
);
expect(__).toEqual(
a.constructor.name,
"what is the name of a's constructor?"
);
expect(__).toEqual(
b.constructor.name,
"what is the name of b's constructor?"
);
});
it("eval", function () {
// eval executes a string
let result = "";
eval("result = 'apple' + ' ' + 'pie'");
expect(__).toBe(result, "what is the value of result?");
});
});