-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_hierarchy.test.js
155 lines (137 loc) · 5.15 KB
/
class_hierarchy.test.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { test } from 'zora'
// @ts-ignore
import { getMethodsOfClass, getMethodsOfInstance, makeFactory } from '@toolbuilder/make-factory'
class A {
method1 () {}
method2 () {}
static static1 () {}
static static2 () {}
}
// Test validation data for class A
const methodsOfA = {
instanceMethods: [
{ constructor: A, methodName: 'method1' },
{ constructor: A, methodName: 'method2' }
],
staticMethods: [
{ constructor: A, methodName: 'static1' },
{ constructor: A, methodName: 'static2' }
]
}
class B extends A {
method1 () { super.method1() }
method3 () {}
static static2 () {}
static static3 () {}
}
// Test validation data for class B
const methodsOfB = {
instanceMethods: [
{ constructor: B, methodName: 'method1' },
{ constructor: B, methodName: 'method3' }
],
staticMethods: [
{ constructor: B, methodName: 'static2' },
{ constructor: B, methodName: 'static3' }
]
}
class C extends B {}
class D extends B {
constructor (a, b) {
super()
this.attribute = 'D.constructor'
}
method1 () { return 'D.method1' }
method4 () { return 'D.method4' }
static static3 () { return 'D.static3' }
static static4 () { return 'D.static4' }
}
// Test validation data for class D
const methodsOfD = {
instanceMethods: [
{ constructor: D, methodName: 'method1' },
{ constructor: D, methodName: 'method4' }
],
staticMethods: [
{ constructor: D, methodName: 'static3' },
{ constructor: D, methodName: 'static4' }
]
}
// Combine methodsOf* instances together to make full set of methods for a class.
const combinedData = (...args) => {
const combined = { instanceMethods: [], staticMethods: [] }
for (const data of args) {
for (const instanceData of data.instanceMethods) {
combined.instanceMethods.push(instanceData)
}
for (const staticData of data.staticMethods) {
combined.staticMethods.push(staticData)
}
}
return combined
}
// Run test for specific combination of data
const validate = (test, constructor, stopAt, ...data) => {
const name = constructor.name
const actual = getMethodsOfClass(constructor, stopAt)
const expected = combinedData(...data)
// handy that javascript now iterates Objects in a guaranteed order
test.deepEqual(actual, expected, `methods reported correctly for ${name}, stopping at ${stopAt.name}`)
}
test('getMethodsOfClass', assert => {
validate(assert, Object, Object, { instanceMethods: [], staticMethods: [] })
validate(assert, A, Object, methodsOfA)
validate(assert, B, Object, methodsOfB, methodsOfA)
validate(assert, B, A, methodsOfB)
validate(assert, C, Object, methodsOfB, methodsOfA)
validate(assert, C, A, methodsOfB)
validate(assert, D, Object, methodsOfD, methodsOfB, methodsOfA)
})
test('getMethodsOfInstance', assert => {
const actual = getMethodsOfInstance(new D(), A)
const expected = getMethodsOfClass(D, A) // since we've just tested getMethodsOfClass
assert.deepEqual(actual, expected, 'getMethodsOfInstance agrees with getMethodsOfClass')
})
test('makeFactory', assert => {
const StaticD = makeFactory(D)
const actualMethodData = getMethodsOfClass(StaticD)
const expectedMethodData = {
instanceMethods: [],
staticMethods: [
{ constructor: StaticD, methodName: 'static3' },
{ constructor: StaticD, methodName: 'static4' },
{ constructor: StaticD, methodName: 'static2' },
{ constructor: StaticD, methodName: 'static1' }
]
}
assert.deepEqual(actualMethodData, expectedMethodData, 'factory class has correct methods')
assert.deepEqual(StaticD().attribute, 'D.constructor', 'factory class as function creates correct instance')
assert.deepEqual(StaticD.static3(), 'D.static3', 'correct static method is called - not super class method')
assert.deepEqual(StaticD.static4(), 'D.static4', 'correct static method is called - not super class method')
assert.truthy(StaticD() instanceof D, 'function call returns instance')
})
test('makeFactory with stopAt', assert => {
const stopAt = A
const StaticD = makeFactory(D, stopAt)
const actualMethodData = getMethodsOfClass(StaticD)
const expectedMethodData = {
instanceMethods: [],
staticMethods: [
{ constructor: StaticD, methodName: 'static3' },
{ constructor: StaticD, methodName: 'static4' },
{ constructor: StaticD, methodName: 'static2' }
]
}
assert.deepEqual(actualMethodData, expectedMethodData, 'factory class has correct methods')
assert.deepEqual(StaticD().attribute, 'D.constructor', 'factory class as function creates correct instance')
assert.deepEqual(StaticD.static3(), 'D.static3', 'correct static method is called - not super class method')
assert.deepEqual(StaticD.static4(), 'D.static4', 'correct static method is called - not super class method')
assert.truthy(StaticD() instanceof D, 'function call returns instance')
})
test('makeFactory with Object', assert => {
const O = makeFactory(Object)
const actualMethodData = getMethodsOfClass(O)
const expectedMethodData = { instanceMethods: [], staticMethods: [] }
assert.deepEqual(actualMethodData, expectedMethodData, 'factory class has correct methods')
assert.truthy(O() instanceof Object, 'function call returns instance')
})