@@ -11,8 +11,31 @@ class TestSource extends SourceContainer<
11
11
TestSource , // Self
12
12
TestNode // Children
13
13
> {
14
- newChild ( name : string ) { return new TestNode ( name , this ) ; }
14
+ protected newChild ( name : string ) { return new TestNode ( name , this ) ; }
15
15
lookup ( ) : undefined { return undefined ; }
16
+ newChildNode : SourceContainer < TestSource , TestNode > [ 'newChild' ] =
17
+ ( name : string ) => this . newChild ( name )
18
+
19
+ getChildNode : SourceContainer < TestSource , TestNode > [ 'getChild' ] =
20
+ ( key : string ) => this . getChild ( key )
21
+
22
+ resolveChildNode : SourceContainer < TestSource , TestNode > [ 'resolveChild' ] =
23
+ ( key : string ) => this . resolveChild ( key )
24
+
25
+ setChildNode : SourceContainer < TestSource , TestNode > [ 'setChild' ] =
26
+ ( key : string , value : TestNode ) => this . setChild ( key , value )
27
+
28
+ ensureChildNode : SourceContainer < TestSource , TestNode > [ 'ensureChild' ] =
29
+ ( name : string , key ?: string ) => this . ensureChild ( name , key )
30
+
31
+ childNodes : SourceContainer < TestSource , TestNode > [ 'children' ] =
32
+ ( ) => this . children ( )
33
+
34
+ childNodeHash : SourceContainer < TestSource , TestNode > [ 'childrenHash' ] =
35
+ ( ) => this . childrenHash ( )
36
+
37
+ childNodeMap : SourceContainer < TestSource , TestNode > [ 'childrenMap' ] =
38
+ ( ) => this . childrenMap ( )
16
39
}
17
40
18
41
class TestNode extends Container <
@@ -23,6 +46,12 @@ class TestNode extends Container<
23
46
> {
24
47
newChild ( name : string ) { return new TestSink ( name , this ) ; }
25
48
lookup ( ) : undefined { return undefined ; }
49
+ ensureSink : Container < TestNode , TestSource , TestSource , TestSink > [ 'ensureChild' ] =
50
+ ( name : string , key ?: string ) => this . ensureChild ( name , key )
51
+ getSink : Container < TestNode , TestSource , TestSource , TestSink > [ 'getChild' ] =
52
+ ( name : string ) => this . getChild ( name )
53
+ resolveSink : Container < TestNode , TestSource , TestSource , TestSink > [ 'resolveChild' ] =
54
+ ( name : string ) => this . resolveChild ( name )
26
55
}
27
56
28
57
class TestSink extends SinkContainer <
@@ -44,58 +73,58 @@ export class BlockTreeTests {
44
73
45
74
@test "newChild creates new child, does not set it" ( ) { // Note: this is why `newChild` is protected in Blocks
46
75
let source = new TestSource ( "my-source" ) ;
47
- let child = source . newChild ( "child-node" ) ;
48
- assert . equal ( source . getChild ( "child-node" ) , null ) ;
49
- assert . equal ( source . resolveChild ( "child-node" ) , null ) ;
76
+ let child = source . newChildNode ( "child-node" ) ;
77
+ assert . equal ( source . getChildNode ( "child-node" ) , null ) ;
78
+ assert . equal ( source . resolveChildNode ( "child-node" ) , null ) ;
50
79
assert . equal ( child . parent , source ) ;
51
80
assert . equal ( child . base , undefined ) ;
52
81
assert . equal ( child . root , source ) ;
53
82
}
54
83
55
84
@test "setChild adds new child to parent" ( ) {
56
85
let source = new TestSource ( "my-source" ) ;
57
- let child = source . newChild ( "child-node" ) ;
58
- source . setChild ( "child-key" , child ) ;
59
- assert . equal ( source . getChild ( "child-key" ) , child ) ;
60
- assert . equal ( source . resolveChild ( "child-key" ) , child ) ;
86
+ let child = source . newChildNode ( "child-node" ) ;
87
+ source . setChildNode ( "child-key" , child ) ;
88
+ assert . equal ( source . getChildNode ( "child-key" ) , child ) ;
89
+ assert . equal ( source . resolveChildNode ( "child-key" ) , child ) ;
61
90
}
62
91
63
92
@test "ensureChild creates and adds new child to parent" ( ) {
64
93
let source = new TestSource ( "my-source" ) ;
65
- let child = source . ensureChild ( "child-node" ) ;
66
- assert . equal ( source . getChild ( "child-node" ) , child ) ;
67
- assert . equal ( source . resolveChild ( "child-node" ) , child ) ;
94
+ let child = source . ensureChildNode ( "child-node" ) ;
95
+ assert . equal ( source . getChildNode ( "child-node" ) , child ) ;
96
+ assert . equal ( source . resolveChildNode ( "child-node" ) , child ) ;
68
97
}
69
98
70
99
@test "ensureChild accepts optional key" ( ) {
71
100
let source = new TestSource ( "my-source" ) ;
72
- let child = source . ensureChild ( "child-node" , "child-key" ) ;
73
- assert . equal ( source . getChild ( "child-key" ) , child ) ;
74
- assert . equal ( source . resolveChild ( "child-key" ) , child ) ;
101
+ let child = source . ensureChildNode ( "child-node" , "child-key" ) ;
102
+ assert . equal ( source . getChildNode ( "child-key" ) , child ) ;
103
+ assert . equal ( source . resolveChildNode ( "child-key" ) , child ) ;
75
104
}
76
105
77
106
@test "ensureChild will not overwrite existing nodes" ( ) {
78
107
let source = new TestSource ( "my-source" ) ;
79
- let child1 = source . ensureChild ( "child-node" ) ;
80
- let child2 = source . ensureChild ( "child-node" ) ;
108
+ let child1 = source . ensureChildNode ( "child-node" ) ;
109
+ let child2 = source . ensureChildNode ( "child-node" ) ;
81
110
assert . equal ( child1 , child2 ) ;
82
111
}
83
112
84
113
@test "children accessor methods work as expected" ( ) {
85
114
let source = new TestSource ( "my-source" ) ;
86
- let child1 = source . ensureChild ( "child1" ) ;
87
- let child2 = source . ensureChild ( "child2" ) ;
88
- let child3 = source . ensureChild ( "child3" , "custom-key" ) ;
115
+ let child1 = source . ensureChildNode ( "child1" ) ;
116
+ let child2 = source . ensureChildNode ( "child2" ) ;
117
+ let child3 = source . ensureChildNode ( "child3" , "custom-key" ) ;
89
118
90
- assert . deepEqual ( source . children ( ) , [ child1 , child2 , child3 ] ) ;
91
- assert . deepEqual ( source . childrenHash ( ) , { child1, child2, "custom-key" : child3 } ) ;
92
- assert . deepEqual ( source . childrenMap ( ) , new Map ( [ [ "child1" , child1 ] , [ "child2" , child2 ] , [ "custom-key" , child3 ] ] ) ) ;
119
+ assert . deepEqual ( source . childNodes ( ) , [ child1 , child2 , child3 ] ) ;
120
+ assert . deepEqual ( source . childNodeHash ( ) , { child1, child2, "custom-key" : child3 } ) ;
121
+ assert . deepEqual ( source . childNodeMap ( ) , new Map ( [ [ "child1" , child1 ] , [ "child2" , child2 ] , [ "custom-key" , child3 ] ] ) ) ;
93
122
}
94
123
95
124
@test "grandchildren have tree properties set as expected" ( ) {
96
125
let source = new TestSource ( "my-source" ) ;
97
- let child = source . ensureChild ( "child" ) ;
98
- let grandchild = child . ensureChild ( "grandchild" ) ;
126
+ let child = source . ensureChildNode ( "child" ) ;
127
+ let grandchild = child . ensureSink ( "grandchild" ) ;
99
128
assert . equal ( grandchild . parent , child ) ;
100
129
assert . equal ( grandchild . base , undefined ) ;
101
130
assert . equal ( grandchild . root , source ) ;
@@ -113,30 +142,30 @@ export class BlockTreeTests {
113
142
@test "setBase creates inheritance tree for children" ( ) {
114
143
let base = new TestSource ( "my-base" ) ;
115
144
let source = new TestSource ( "my-source" ) ;
116
- let baseChild = base . ensureChild ( "child" ) ;
117
- let child = source . ensureChild ( "child" ) ;
118
- let baseUnique = base . ensureChild ( "base-only" ) ;
145
+ let baseChild = base . ensureChildNode ( "child" ) ;
146
+ let child = source . ensureChildNode ( "child" ) ;
147
+ let baseUnique = base . ensureChildNode ( "base-only" ) ;
119
148
source . setBase ( base ) ;
120
149
121
150
assert . equal ( child . base , baseChild ) ;
122
- assert . equal ( source . getChild ( "base-only" ) , null ) ;
123
- assert . equal ( source . resolveChild ( "base-only" ) , baseUnique ) ;
151
+ assert . equal ( source . getChildNode ( "base-only" ) , null ) ;
152
+ assert . equal ( source . resolveChildNode ( "base-only" ) , baseUnique ) ;
124
153
assert . deepEqual ( child . resolveInheritance ( ) , [ baseChild ] ) ;
125
154
}
126
155
127
156
@test "setBase creates inheritance tree for grandchildren" ( ) {
128
157
let base = new TestSource ( "my-base" ) ;
129
158
let source = new TestSource ( "my-source" ) ;
130
- let baseChild = base . ensureChild ( "child" ) ;
131
- let baseGrandchild = baseChild . ensureChild ( "grandchild" ) ;
132
- let baseChildUnique = baseChild . ensureChild ( "base-only" ) ;
133
- let child = source . ensureChild ( "child" ) ;
134
- let grandchild = child . ensureChild ( "grandchild" ) ;
159
+ let baseChild = base . ensureChildNode ( "child" ) ;
160
+ let baseGrandchild = baseChild . ensureSink ( "grandchild" ) ;
161
+ let baseChildUnique = baseChild . ensureSink ( "base-only" ) ;
162
+ let child = source . ensureChildNode ( "child" ) ;
163
+ let grandchild = child . ensureSink ( "grandchild" ) ;
135
164
source . setBase ( base ) ;
136
165
137
166
assert . equal ( grandchild . base , baseGrandchild ) ;
138
- assert . equal ( source . getChild ( "child" ) ! . getChild ( "base-only" ) , null ) ;
139
- assert . equal ( source . getChild ( "child" ) ! . resolveChild ( "base-only" ) , baseChildUnique ) ;
167
+ assert . equal ( source . getChildNode ( "child" ) ! . getSink ( "base-only" ) , null ) ;
168
+ assert . equal ( source . getChildNode ( "child" ) ! . resolveSink ( "base-only" ) , baseChildUnique ) ;
140
169
assert . deepEqual ( grandchild . resolveInheritance ( ) , [ baseGrandchild ] ) ;
141
170
}
142
171
}
0 commit comments