@@ -3,14 +3,13 @@ import { Test } from 'nodeunit';
3
3
import { App as Root , Aws , Construct , ConstructNode , ConstructOrder , IConstruct , Lazy , ValidationError } from '../lib' ;
4
4
5
5
// tslint:disable:variable-name
6
- // tslint:disable:max-line-length
7
6
8
7
export = {
9
8
'the "Root" construct is a special construct which can be used as the root of the tree' ( test : Test ) {
10
9
const root = new Root ( ) ;
11
10
test . equal ( root . node . id , '' , 'if not specified, name of a root construct is an empty string' ) ;
12
11
test . ok ( ! root . node . scope , 'no parent' ) ;
13
- test . equal ( root . node . children . length , 0 , 'a construct is created without children' ) ; // no children
12
+ test . equal ( root . node . children . length , 1 ) ;
14
13
test . done ( ) ;
15
14
} ,
16
15
@@ -100,7 +99,7 @@ export = {
100
99
const child = new Construct ( root , 'Child1' ) ;
101
100
new Construct ( root , 'Child2' ) ;
102
101
test . equal ( child . node . children . length , 0 , 'no children' ) ;
103
- test . equal ( root . node . children . length , 2 , 'two children are expected' ) ;
102
+ test . equal ( root . node . children . length , 3 , 'three children are expected' ) ;
104
103
test . done ( ) ;
105
104
} ,
106
105
@@ -126,9 +125,9 @@ export = {
126
125
const t = createTree ( ) ;
127
126
128
127
test . equal ( t . root . toString ( ) , '<root>' ) ;
129
- test . equal ( t . child1_1_1 . toString ( ) , 'Child1/Child11/Child111' ) ;
130
- test . equal ( t . child2 . toString ( ) , 'Child2' ) ;
131
- test . equal ( toTreeString ( t . root ) , 'App\n Construct [Child1]\n Construct [Child11]\n Construct [Child111]\n Construct [Child12]\n Construct [Child2]\n Construct [Child21]\n' ) ;
128
+ test . equal ( t . child1_1_1 . toString ( ) , 'HighChild/ Child1/Child11/Child111' ) ;
129
+ test . equal ( t . child2 . toString ( ) , 'HighChild/ Child2' ) ;
130
+ test . equal ( toTreeString ( t . root ) , 'App\n TreeMetadata [Tree]\n Construct [HighChild]\n Construct [ Child1]\n Construct [Child11]\n Construct [Child111]\n Construct [Child12]\n Construct [Child2]\n Construct [Child21]\n' ) ;
132
131
test . done ( ) ;
133
132
} ,
134
133
@@ -139,28 +138,30 @@ export = {
139
138
} ;
140
139
141
140
const t = createTree ( context ) ;
142
- test . equal ( t . root . node . tryGetContext ( 'ctx1' ) , 12 ) ;
141
+ test . equal ( t . child1_2 . node . tryGetContext ( 'ctx1' ) , 12 ) ;
143
142
test . equal ( t . child1_1_1 . node . tryGetContext ( 'ctx2' ) , 'hello' ) ;
144
143
test . done ( ) ;
145
144
} ,
146
145
146
+ // tslint:disable-next-line:max-line-length
147
147
'construct.setContext(k,v) sets context at some level and construct.getContext(key) will return the lowermost value defined in the stack' ( test : Test ) {
148
148
const root = new Root ( ) ;
149
- root . node . setContext ( 'c1' , 'root' ) ;
150
- root . node . setContext ( 'c2' , 'root' ) ;
149
+ const highChild = new Construct ( root , 'highChild' ) ;
150
+ highChild . node . setContext ( 'c1' , 'root' ) ;
151
+ highChild . node . setContext ( 'c2' , 'root' ) ;
151
152
152
- const child1 = new Construct ( root , 'child1' ) ;
153
+ const child1 = new Construct ( highChild , 'child1' ) ;
153
154
child1 . node . setContext ( 'c2' , 'child1' ) ;
154
155
child1 . node . setContext ( 'c3' , 'child1' ) ;
155
156
156
- const child2 = new Construct ( root , 'child2' ) ;
157
+ const child2 = new Construct ( highChild , 'child2' ) ;
157
158
const child3 = new Construct ( child1 , 'child1child1' ) ;
158
159
child3 . node . setContext ( 'c1' , 'child3' ) ;
159
160
child3 . node . setContext ( 'c4' , 'child3' ) ;
160
161
161
- test . equal ( root . node . tryGetContext ( 'c1' ) , 'root' ) ;
162
- test . equal ( root . node . tryGetContext ( 'c2' ) , 'root' ) ;
163
- test . equal ( root . node . tryGetContext ( 'c3' ) , undefined ) ;
162
+ test . equal ( highChild . node . tryGetContext ( 'c1' ) , 'root' ) ;
163
+ test . equal ( highChild . node . tryGetContext ( 'c2' ) , 'root' ) ;
164
+ test . equal ( highChild . node . tryGetContext ( 'c3' ) , undefined ) ;
164
165
165
166
test . equal ( child1 . node . tryGetContext ( 'c1' ) , 'root' ) ;
166
167
test . equal ( child1 . node . tryGetContext ( 'c2' ) , 'child1' ) ;
@@ -195,15 +196,15 @@ export = {
195
196
'construct.pathParts returns an array of strings of all names from root to node' ( test : Test ) {
196
197
const tree = createTree ( ) ;
197
198
test . deepEqual ( tree . root . node . path , '' ) ;
198
- test . deepEqual ( tree . child1_1_1 . node . path , 'Child1/Child11/Child111' ) ;
199
- test . deepEqual ( tree . child2 . node . path , 'Child2' ) ;
199
+ test . deepEqual ( tree . child1_1_1 . node . path , 'HighChild/ Child1/Child11/Child111' ) ;
200
+ test . deepEqual ( tree . child2 . node . path , 'HighChild/ Child2' ) ;
200
201
test . done ( ) ;
201
202
} ,
202
203
203
204
'if a root construct has a name, it should be included in the path' ( test : Test ) {
204
205
const tree = createTree ( { } ) ;
205
206
test . deepEqual ( tree . root . node . path , '' ) ;
206
- test . deepEqual ( tree . child1_1_1 . node . path , 'Child1/Child11/Child111' ) ;
207
+ test . deepEqual ( tree . child1_1_1 . node . path , 'HighChild/ Child1/Child11/Child111' ) ;
207
208
test . done ( ) ;
208
209
} ,
209
210
@@ -302,7 +303,7 @@ export = {
302
303
new MyBeautifulConstruct ( root , 'mbc2' ) ;
303
304
new MyBeautifulConstruct ( root , 'mbc3' ) ;
304
305
new MyBeautifulConstruct ( root , 'mbc4' ) ;
305
- test . equal ( root . node . children . length , 4 ) ;
306
+ test . ok ( root . node . children . length >= 4 ) ;
306
307
test . done ( ) ;
307
308
} ,
308
309
@@ -416,7 +417,7 @@ export = {
416
417
417
418
'ancestors returns a list of parents up to root' ( test : Test ) {
418
419
const { child1_1_1 } = createTree ( ) ;
419
- test . deepEqual ( child1_1_1 . node . scopes . map ( x => x . node . id ) , [ '' , 'Child1' , 'Child11' , 'Child111' ] ) ;
420
+ test . deepEqual ( child1_1_1 . node . scopes . map ( x => x . node . id ) , [ '' , 'HighChild' , ' Child1', 'Child11' , 'Child111' ] ) ;
420
421
test . done ( ) ;
421
422
} ,
422
423
@@ -481,12 +482,13 @@ export = {
481
482
482
483
function createTree ( context ?: any ) {
483
484
const root = new Root ( ) ;
485
+ const highChild = new Construct ( root , 'HighChild' ) ;
484
486
if ( context ) {
485
- Object . keys ( context ) . forEach ( key => root . node . setContext ( key , context [ key ] ) ) ;
487
+ Object . keys ( context ) . forEach ( key => highChild . node . setContext ( key , context [ key ] ) ) ;
486
488
}
487
489
488
- const child1 = new Construct ( root , 'Child1' ) ;
489
- const child2 = new Construct ( root , 'Child2' ) ;
490
+ const child1 = new Construct ( highChild , 'Child1' ) ;
491
+ const child2 = new Construct ( highChild , 'Child2' ) ;
490
492
const child1_1 = new Construct ( child1 , 'Child11' ) ;
491
493
const child1_2 = new Construct ( child1 , 'Child12' ) ;
492
494
const child1_1_1 = new Construct ( child1_1 , 'Child111' ) ;
0 commit comments