-
Notifications
You must be signed in to change notification settings - Fork 30
/
extension.js
262 lines (165 loc) · 5.88 KB
/
extension.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import expect from '../expect.js';
import {
createModelBuilder
} from '../helper.js';
describe('extension', function() {
var createModel = createModelBuilder('test/fixtures/model/');
describe('types', function() {
describe('built-in shadowing', function() {
// given
var model = createModel([ 'shadow' ]);
it('should shadow <Element>', function() {
// when
var element = model.create('s:Element');
// then
expect(element).to.exist;
expect(element.$instanceOf('s:Element')).to.be.true;
});
it('should shadow <Element> in inheritance hierarchy', function() {
// when
var element = model.create('s:NamedElement');
// then
expect(element).to.exist;
expect(element.$instanceOf('s:Element')).to.be.true;
expect(element.$instanceOf('s:NamedElement')).to.be.true;
});
it('should provide <Element> built-in type', function() {
// when
var element = model.create('s:ExtendsBuiltinElement');
// then
expect(element).to.exist;
expect(element.$instanceOf('Element')).to.be.true;
expect(element.$instanceOf('s:ExtendsBuiltinElement')).to.be.true;
});
});
});
describe('extension', function() {
var model = createModel([ 'extension/base', 'extension/custom' ]);
describe('trait', function() {
it('should not provide meta-data', function() {
expect(() => {
model.getType('c:CustomRoot');
}).to.throw(/cannot create <c:CustomRoot> extending <b:Root>/);
});
describe('descriptor', function() {
it('should indicate non-inherited', function() {
// given
var ComplexType = model.getType('b:Root');
// when
var descriptor = model.getElementDescriptor(ComplexType),
customAttrDescriptor = descriptor.propertiesByName['customAttr'],
customBaseAttrDescriptor = descriptor.propertiesByName['customBaseAttr'],
ownAttrDescriptor = descriptor.propertiesByName['ownAttr'];
// then
expect(customAttrDescriptor.inherited).to.be.false;
expect(customBaseAttrDescriptor.inherited).to.be.false;
expect(ownAttrDescriptor.inherited).to.be.true;
});
});
it('should plug-in into type hierarchy', function() {
var root = model.create('b:Root');
// then
expect(root.$instanceOf('c:CustomRoot')).to.be.true;
});
it('should add custom attribute', function() {
// when
var root = model.create('b:Root', {
customAttr: -1
});
// then
expect(root.customAttr).to.eql(-1);
});
it('should refine property', function() {
// given
var Type = model.getType('b:Root');
// when
var genericProperty = Type.$descriptor.propertiesByName['generic'];
// then
expect(genericProperty.type).to.eql('c:CustomGeneric');
});
it('should use refined property', function() {
var customGeneric = model.create('c:CustomGeneric', { count: 100 });
// when
var root = model.create('b:Root', {
generic: customGeneric
});
// then
expect(root.generic).to.eql(customGeneric);
});
});
describe('types', function() {
it('should provide custom types', function() {
var property = model.create('c:Property');
// then
expect(property.$instanceOf('c:Property')).to.be.true;
});
});
describe('generic', function() {
it('should extend Element', function() {
// when
var customGeneric = model.create('c:CustomGeneric', { count: 100 });
// then
expect(customGeneric.$instanceOf('Element')).to.be.true;
});
it('should be part of generic collection', function() {
var customProperty = model.create('c:Property', { key: 'foo', value: 'bar' });
// when
var root = model.create('b:Root', {
genericCollection: [ customProperty ]
});
// then
expect(root.genericCollection).to.eql([ customProperty ]);
});
});
});
describe('property replacement', function() {
var model = createModel([ 'replaces/base' ]);
it('should replace in descriptor', function() {
// given
var Extension = model.getType('b:Extension');
// when
var descriptor = model.getElementDescriptor(Extension),
propertyNames = descriptor.properties.map(function(p) {
return p.name;
});
// then
expect(propertyNames).to.eql([
'name',
'value',
'id'
]);
expect(descriptor.propertiesByName['b:id'].type).to.eql('Integer');
expect(descriptor.propertiesByName['id'].type).to.eql('Integer');
});
});
describe('property redefinition', function() {
var model = createModel([ 'redefines/base' ]);
it('should redefine in descriptor', function() {
// given
var Extension = model.getType('b:Extension');
// when
var descriptor = model.getElementDescriptor(Extension),
propertyNames = descriptor.properties.map(function(p) {
return p.name;
});
// then
expect(propertyNames).to.eql([
'id',
'name',
'value'
]);
expect(descriptor.propertiesByName['b:id'].type).to.eql('Integer');
expect(descriptor.propertiesByName['id'].type).to.eql('Integer');
});
});
describe('extension - self extend', function() {
it('should self-extend', async function() {
// when
var model = createModel([ 'self-extend' ]);
var element = model.create('se:Rect');
// then
expect(element.$instanceOf('se:ExtendedRect')).to.be.true;
expect(element.$instanceOf('se:OtherExtendedRect')).to.be.true;
});
});
});